Dashboard.vue 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  1. <template>
  2. <!-- 顶部栏 -->
  3. <div
  4. style="
  5. height: 64px;
  6. display: flex;
  7. align-items: center;
  8. padding: 0 16px;
  9. justify-content: space-between;
  10. background: #fff;
  11. border-bottom: 1px solid #f0f0f0;
  12. "
  13. >
  14. <div style="display: flex; align-items: center; gap: 12px">
  15. <div style="font-weight: 700; font-size: 18px">AI Agent</div>
  16. <div style="color: #888; font-size: 13px">概述</div>
  17. </div>
  18. <div style="display: flex; align-items: center; gap: 12px">
  19. <el-dropdown
  20. style="background: #ff6b6b"
  21. split-button
  22. type="primary"
  23. @click="handleMenuClick(buttonConfig.text)"
  24. >
  25. {{ buttonConfig.text }}
  26. <template #dropdown>
  27. <el-dropdown-menu>
  28. <el-dropdown-item
  29. v-for="item in buttonConfig.items"
  30. :key="item"
  31. @click="handleMenuClick(item)"
  32. >
  33. {{ item }}
  34. </el-dropdown-item>
  35. </el-dropdown-menu>
  36. </template>
  37. </el-dropdown>
  38. </div>
  39. </div>
  40. <div style="padding: 16px">
  41. <el-card shadow="never" style="padding: 18px">
  42. <el-row :gutter="16" justify="start">
  43. <el-col :span="4" v-for="(card, idx) in cards" :key="idx">
  44. <div
  45. style="
  46. background: linear-gradient(135deg, #fff 0%, #fafafa 100%);
  47. padding: 16px;
  48. border-radius: 8px;
  49. box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  50. border: 1px solid #f0f0f0;
  51. "
  52. >
  53. <div style="font-size: 13px; color: #888">{{ card.title }}</div>
  54. <div style="font-size: 20px; margin-top: 6px; font-weight: 600">{{ card.value }}</div>
  55. </div>
  56. </el-col>
  57. </el-row>
  58. </el-card>
  59. <el-card style="margin-top: 16px">
  60. <el-tabs v-model="dashboardStore.activeTab">
  61. <el-tab-pane label="工作流程" name="flows"></el-tab-pane>
  62. <el-tab-pane label="证书" name="certs"></el-tab-pane>
  63. <el-tab-pane label="执行" name="execs"></el-tab-pane>
  64. <el-tab-pane label="变量" name="vars"></el-tab-pane>
  65. <el-tab-pane label="数据表" name="tables"></el-tab-pane>
  66. </el-tabs>
  67. <!-- 工作流程和证书 Tab 的搜索和排序 -->
  68. <div
  69. v-if="activeTab === 'flows' || activeTab === 'certs'"
  70. style="display: flex; justify-content: space-between; align-items: center; margin-top: 12px"
  71. >
  72. <div style="display: flex; gap: 8px; align-items: center">
  73. <el-input
  74. placeholder="搜索"
  75. :prefix-icon="Search"
  76. v-model="filter"
  77. clearable
  78. style="width: 260px"
  79. />
  80. <el-select v-model="sort" placeholder="排序" style="width: 180px">
  81. <el-option label="按更新时间排序" value="updated" />
  82. <el-option label="按创建时间排序" value="created" />
  83. <el-option label="按名称升序" value="name" />
  84. <el-option label="按名称降序" value="name-desc" />
  85. </el-select>
  86. </div>
  87. <div>
  88. <el-button text>筛选</el-button>
  89. </div>
  90. </div>
  91. <!-- 执行 Tab 的搜索和筛选 -->
  92. <div
  93. v-else-if="activeTab === 'execs'"
  94. style="display: flex; justify-content: space-between; align-items: center; margin-top: 12px"
  95. >
  96. <div style="display: flex; gap: 8px; align-items: center">
  97. <el-input
  98. placeholder="搜索"
  99. :prefix-icon="Search"
  100. v-model="filter"
  101. clearable
  102. style="width: 260px"
  103. />
  104. <el-select v-model="sort" placeholder="排序" style="width: 180px">
  105. <el-option label="按名称升序" value="name" />
  106. <el-option label="按名称降序" value="name-desc" />
  107. </el-select>
  108. </div>
  109. <div>
  110. <el-button type="text">筛选</el-button>
  111. </div>
  112. </div>
  113. <!-- 变量 Tab 的搜索和添加按钮 -->
  114. <div
  115. v-else-if="activeTab === 'vars'"
  116. style="display: flex; justify-content: space-between; align-items: center; margin-top: 12px"
  117. >
  118. <div style="display: flex; gap: 8px; align-items: center">
  119. <el-input
  120. placeholder="搜索变量......"
  121. :prefix-icon="Search"
  122. v-model="varFilter"
  123. clearable
  124. style="width: 260px"
  125. />
  126. <el-select v-model="varSort" placeholder="按名称排序" style="width: 200px">
  127. <el-option label="按名称排序" value="name" />
  128. </el-select>
  129. </div>
  130. <div style="display: flex; gap: 8px">
  131. <el-button type="text">筛选</el-button>
  132. </div>
  133. </div>
  134. <div style="margin-top: 12px">
  135. <!-- 工作流程 Tab -->
  136. <template v-if="activeTab === 'flows'">
  137. <el-empty v-if="getTabData.length === 0" description="无工作流程" />
  138. <div v-else>
  139. <div v-for="item in pagedData" :key="item.id" style="margin-bottom: 12px">
  140. <el-card>
  141. <div style="display: flex; justify-content: space-between; align-items: center">
  142. <div>
  143. <div style="font-weight: 700" class="cursor-pointer" @click="toEditor">
  144. {{ item.title }}
  145. </div>
  146. <div style="color: #999; font-size: 12px">上次更新时间: {{ item.created }}</div>
  147. </div>
  148. <div style="display: flex; gap: 8px; align-items: center">
  149. <el-tag size="small" type="info">流程标签</el-tag>
  150. <el-dropdown>
  151. <span class="el-dropdown-link cursor-pointer" @click.stop>•••</span>
  152. <template #dropdown>
  153. <el-dropdown-menu>
  154. <el-dropdown-item @click="toEditor">打开</el-dropdown-item>
  155. <el-dropdown-item>分享</el-dropdown-item>
  156. <el-dropdown-item>复制</el-dropdown-item>
  157. <el-dropdown-item>移动</el-dropdown-item>
  158. <el-dropdown-item>档案</el-dropdown-item>
  159. </el-dropdown-menu>
  160. </template>
  161. </el-dropdown>
  162. </div>
  163. </div>
  164. </el-card>
  165. </div>
  166. </div>
  167. </template>
  168. <!-- 执行 Tab - 表格形式 -->
  169. <template v-else-if="activeTab === 'execs'">
  170. <div
  171. v-if="getTabData.length === 0"
  172. style="text-align: center; padding: 40px; color: #999"
  173. >
  174. 没有正在进行的执行。
  175. </div>
  176. <el-table v-else :data="getTabData" style="width: 100%; margin-top: 12px" stripe border>
  177. <el-table-column type="selection" width="50" />
  178. <el-table-column prop="workflow" label="工作流程">
  179. <template #default="scope">
  180. <el-button type="text" @click="$router.push(`/workflow/${scope.row.id}`)">{{
  181. scope.row.workflow
  182. }}</el-button>
  183. </template>
  184. </el-table-column>
  185. <el-table-column prop="status" label="地位" width="100">
  186. <template #default="scope">
  187. <div style="display: flex; align-items: center; gap: 6px">
  188. <el-icon style="color: #67c23a" v-if="scope.row.status === '成功'">
  189. <svg
  190. xmlns="http://www.w3.org/2000/svg"
  191. viewBox="0 0 24 24"
  192. fill="currentColor"
  193. width="1em"
  194. height="1em"
  195. >
  196. <path d="M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z" />
  197. </svg>
  198. </el-icon>
  199. {{ scope.row.status }}
  200. </div>
  201. </template>
  202. </el-table-column>
  203. <el-table-column prop="startTime" label="开始" />
  204. <el-table-column prop="duration" label="运行时间" width="100" />
  205. <el-table-column prop="executionId" label="执行 ID" width="80" />
  206. <el-table-column label="手动执行" width="100" align="center">
  207. <template #default>
  208. <el-button style="background: #ccc; color: #666; border: none" size="small"
  209. >手动执行</el-button
  210. >
  211. </template>
  212. </el-table-column>
  213. <el-table-column label="操作" width="80" align="center">
  214. <template #default>
  215. <el-dropdown>
  216. <span class="el-dropdown-link">⋯</span>
  217. <template #dropdown>
  218. <el-dropdown-menu>
  219. <el-dropdown-item>删除</el-dropdown-item>
  220. </el-dropdown-menu>
  221. </template>
  222. </el-dropdown>
  223. </template>
  224. </el-table-column>
  225. </el-table>
  226. </template>
  227. <!-- 变量 Tab - 表格或空状态 -->
  228. <template v-else-if="activeTab === 'vars'">
  229. <div
  230. v-if="filteredVariables.length === 0"
  231. style="
  232. text-align: center;
  233. padding: 60px 20px;
  234. border: 2px dashed #ddd;
  235. border-radius: 6px;
  236. background: #fafafa;
  237. color: #666;
  238. "
  239. >
  240. <div style="font-size: 48px; margin-bottom: 12px">👋</div>
  241. <div style="font-size: 16px; margin-bottom: 8px">迄今,我还没设置一个变量</div>
  242. <div style="font-size: 13px; color: #999; margin-bottom: 16px">
  243. 变量可用于存储可在多个工作流程中轻松引用的数据。
  244. </div>
  245. <el-button @click="dashboardStore.openVarDialog">添加第一个变量</el-button>
  246. </div>
  247. <el-table v-else :data="filteredVariables" style="width: 100%; margin-top: 12px">
  248. <el-table-column prop="key" label="key" />
  249. <el-table-column prop="value" label="值" />
  250. <el-table-column prop="usage" label="使用语法" width="150">
  251. <template #default="scope">
  252. <el-tag type="info">{{ scope.row.usage }}</el-tag>
  253. </template>
  254. </el-table-column>
  255. <el-table-column prop="scope" label="范围" width="100">
  256. <template #default="scope">
  257. <el-tag size="small">{{ scope.row.scope === 'scope' ? '全局' : '' }}</el-tag>
  258. </template>
  259. </el-table-column>
  260. <el-table-column label="操作" width="200" align="right">
  261. <template #default="scope">
  262. <el-button text size="small" @click="editVariable(scope.row)">编辑</el-button>
  263. <el-button text size="small" type="danger" @click="deleteVariable(scope.row.id)"
  264. >删除</el-button
  265. >
  266. </template>
  267. </el-table-column>
  268. </el-table>
  269. <div v-if="filteredVariables.length > 0" style="margin-top: 12px; text-align: right">
  270. <span style="color: #999; font-size: 12px">页面大小: </span>
  271. <el-select v-model="varPageSize" size="small" style="width: 60px">
  272. <el-option label="25" value="25" />
  273. <el-option label="50" value="50" />
  274. <el-option label="100" value="100" />
  275. </el-select>
  276. </div>
  277. </template>
  278. <!-- 其他 Tab -->
  279. <template v-else-if="activeTab === 'tables'">
  280. <div
  281. v-if="tables.length === 0"
  282. style="
  283. text-align: center;
  284. padding: 60px 20px;
  285. border: 2px dashed #ddd;
  286. border-radius: 6px;
  287. background: #fafafa;
  288. "
  289. >
  290. <div style="font-size: 16px; margin-bottom: 8px; color: #333">
  291. 您目前还没有任何数据表。
  292. </div>
  293. <div style="font-size: 13px; color: #999; margin-bottom: 20px">
  294. 使用数据表来保存执行结果、在工作流之间共享数据以及跨进程体估指标。
  295. </div>
  296. <el-button @click="dashboardStore.openTableDialog">创建数据表</el-button>
  297. </div>
  298. <div v-else>
  299. <div
  300. v-for="item in tables"
  301. :key="item.id"
  302. style="
  303. margin-bottom: 12px;
  304. padding: 12px;
  305. border: 1px solid #f0f0f0;
  306. border-radius: 6px;
  307. background: #fff;
  308. "
  309. >
  310. <div style="display: flex; justify-content: space-between; align-items: center">
  311. <div style="display: flex; gap: 12px; align-items: center; flex: 1">
  312. <div style="font-size: 20px">📋</div>
  313. <div style="flex: 1">
  314. <div style="font-weight: 600; margin-bottom: 4px; font-size: 14px">
  315. {{ item.name }}
  316. </div>
  317. <div style="color: #999; font-size: 12px; display: flex; gap: 12px">
  318. <span>{{ item.size || '0MB' }}</span>
  319. <span>{{ item.rows || '0' }}行</span>
  320. <span style="color: #ff6b6b">操作更新</span>
  321. <span style="color: #ff6b6b">操作创建</span>
  322. </div>
  323. </div>
  324. </div>
  325. <div>
  326. <el-dropdown>
  327. <span class="el-dropdown-link">•••</span>
  328. <template #dropdown>
  329. <el-dropdown-menu>
  330. <el-dropdown-item>下载 CSV 文件</el-dropdown-item>
  331. <el-dropdown-item>删除</el-dropdown-item>
  332. </el-dropdown-menu>
  333. </template>
  334. </el-dropdown>
  335. </div>
  336. </div>
  337. </div>
  338. <!-- 数据表分页器 -->
  339. <div
  340. style="
  341. display: flex;
  342. justify-content: flex-end;
  343. align-items: center;
  344. margin-top: 16px;
  345. gap: 12px;
  346. "
  347. >
  348. <span style="color: #666; font-size: 13px">总计 {{ tables.length }}</span>
  349. <el-input v-model="tablePageInput" style="width: 50px" placeholder="1" />
  350. <el-select v-model="tablePageSize" size="small" style="width: 100px">
  351. <el-option label="50/页" value="50" />
  352. <el-option label="100/页" value="100" />
  353. <el-option label="200/页" value="200" />
  354. </el-select>
  355. </div>
  356. </div>
  357. </template>
  358. <!-- 其他 Tab -->
  359. <template v-else>
  360. <el-empty v-if="getTabData.length === 0" :description="`无${getTabLabel}`" />
  361. <div v-else>
  362. <div v-for="item in getTabData" :key="item.id" style="margin-bottom: 12px">
  363. <el-card>
  364. <div style="display: flex; justify-content: space-between; align-items: center">
  365. <div style="display: flex; gap: 12px; align-items: flex-start; flex: 1">
  366. <div style="font-size: 24px">{{ item.icon || '📋' }}</div>
  367. <div style="flex: 1">
  368. <div style="font-weight: 700; margin-bottom: 4px">{{ item.title }}</div>
  369. <div style="color: #999; font-size: 12px">{{ item.description }}</div>
  370. </div>
  371. </div>
  372. <div style="display: flex; gap: 8px; align-items: center">
  373. <!-- <el-tag size="small" type="info">个人的</el-tag> -->
  374. <el-dropdown>
  375. <span class="el-dropdown-link">•••</span>
  376. <template #dropdown>
  377. <el-dropdown-menu>
  378. <el-dropdown-item>打开</el-dropdown-item>
  379. <el-dropdown-item>删除</el-dropdown-item>
  380. <el-dropdown-item>变更所有者</el-dropdown-item>
  381. </el-dropdown-menu>
  382. </template>
  383. </el-dropdown>
  384. </div>
  385. </div>
  386. </el-card>
  387. </div>
  388. </div>
  389. </template>
  390. </div>
  391. <!-- 分页器仅在工作流程 Tab 显示 -->
  392. <div
  393. v-if="activeTab === 'flows'"
  394. style="
  395. display: flex;
  396. justify-content: flex-end;
  397. align-items: center;
  398. margin-top: 16px;
  399. gap: 12px;
  400. "
  401. >
  402. <el-pagination
  403. background
  404. :page-size="pageSize"
  405. :current-page="currentPage"
  406. :page-sizes="[10, 20, 50]"
  407. @update:current-page="currentPage = $event"
  408. @update:page-size="pageSize = $event"
  409. :total="getTabData.length"
  410. layout="total, prev, pager, next, sizes"
  411. />
  412. </div>
  413. </el-card>
  414. <!-- 新变量对话框 -->
  415. <el-dialog
  416. v-model="dashboardStore.showVarDialog"
  417. :title="varDialogTitle"
  418. width="500px"
  419. @close="resetVarForm"
  420. >
  421. <el-form ref="varFormRef" :model="varForm" :rules="varFormRules" label-position="top">
  422. <el-form-item label="Key" prop="key">
  423. <el-input v-model="varForm.key" placeholder="请输入key" />
  424. </el-form-item>
  425. <el-form-item label="值" prop="value">
  426. <el-input v-model="varForm.value" type="textarea" placeholder="请输入一个值" rows="4" />
  427. </el-form-item>
  428. <el-form-item label="范围" prop="scope">
  429. <el-select v-model="varForm.scope" placeholder="选择">
  430. <el-option label="全局" value="scope" />
  431. </el-select>
  432. </el-form-item>
  433. </el-form>
  434. <template #footer>
  435. <div style="text-align: right">
  436. <el-button @click="dashboardStore.closeVarDialog">取消</el-button>
  437. <el-button type="primary" @click="submitVariable">提交</el-button>
  438. </div>
  439. </template>
  440. </el-dialog>
  441. <!-- 创建新数据表对话框 -->
  442. <el-dialog
  443. v-model="dashboardStore.showTableDialog"
  444. title="创建新数据表"
  445. width="500px"
  446. @close="resetTableForm"
  447. >
  448. <el-form ref="tableFormRef" :model="tableForm" :rules="tableFormRules" label-position="top">
  449. <el-form-item label="数据表名称" prop="name">
  450. <el-input v-model="tableForm.name" placeholder="输入数据表名称" />
  451. </el-form-item>
  452. <el-form-item label="创建方式" prop="method">
  453. <el-radio-group v-model="tableForm.method">
  454. <el-radio value="from-scratch">从零开始</el-radio>
  455. <el-radio value="import-csv">导入 CSV 文件</el-radio>
  456. </el-radio-group>
  457. </el-form-item>
  458. </el-form>
  459. <template #footer>
  460. <div style="text-align: right">
  461. <el-button @click="dashboardStore.closeTableDialog">取消</el-button>
  462. <el-button type="primary" @click="submitTable">创建</el-button>
  463. </div>
  464. </template>
  465. </el-dialog>
  466. </div>
  467. </template>
  468. <script setup lang="ts">
  469. import { ref, computed } from 'vue'
  470. import { useRouter } from 'vue-router'
  471. import { ElMessage, ElMessageBox } from 'element-plus'
  472. import { Search } from '@element-plus/icons-vue'
  473. import { useDashboardStore } from '@/stores/dashboard'
  474. import { v4 } from 'uuid'
  475. const $router = useRouter()
  476. const dashboardStore = useDashboardStore()
  477. // 根据当前 tab 确定按钮文字和下拉菜单
  478. const buttonConfig = computed(() => {
  479. const tab = dashboardStore.activeTab
  480. const configs: Record<string, { text: string; items: string[] }> = {
  481. flows: { text: '创建工作流程', items: ['创建凭证', '创建变量', '创建数据表'] },
  482. certs: { text: '创建凭证', items: ['创建工作流程', '创建变量', '创建数据表'] },
  483. execs: { text: '创建工作流程', items: ['创建凭证', '创建变量', '创建数据表'] },
  484. vars: { text: '创建变量', items: ['创建工作流程', '创建凭证', '创建数据表'] },
  485. tables: { text: '创建数据表', items: ['创建工作流程', '创建凭证', '创建变量'] }
  486. }
  487. return configs[tab] || { text: '创建工作流程', items: ['创建凭证', '创建变量', '创建数据表'] }
  488. })
  489. const handleMenuClick = (text: string) => {
  490. const actionMap: Record<string, () => void> = {
  491. 创建工作流程: () => {
  492. const id = v4()
  493. $router.push(`/workflow/${id}`)
  494. },
  495. 创建凭证: () => console.log('创建凭证'),
  496. 创建变量: () => {
  497. dashboardStore.setActiveTab('vars')
  498. dashboardStore.openVarDialog()
  499. },
  500. 创建数据表: () => {
  501. dashboardStore.setActiveTab('tables')
  502. dashboardStore.openTableDialog()
  503. }
  504. }
  505. const action = actionMap[text]
  506. if (action) action()
  507. }
  508. const cards = [
  509. { title: '生产执行', value: 0 },
  510. { title: '执行失败', value: 0 },
  511. { title: '故障率', value: '0%' },
  512. { title: '节省时间', value: '0%' },
  513. { title: '运行时间(平均)', value: '0s' }
  514. ]
  515. const projectMap = JSON.parse(localStorage.getItem(`workflow-map`) || '{}')
  516. const workflows = ref([
  517. ...Object.entries(projectMap)
  518. .map(([_id, workflow]) => workflow)
  519. .map((item: any) => ({
  520. ...item,
  521. id: item.id,
  522. title: item.name
  523. })),
  524. { id: 1, title: '与新同事人才交流', created: '1 月 23 日', description: '', icon: '' },
  525. {
  526. id: 2,
  527. title: 'RAG 同步机器人,基于 Supabase',
  528. created: '1 月 23 日',
  529. description: '',
  530. icon: ''
  531. },
  532. {
  533. id: 3,
  534. title: '利用 Gemini AI, OCR 和 Google Sheets',
  535. created: '1 月 23 日',
  536. description: '',
  537. icon: ''
  538. }
  539. ])
  540. const certificates = ref([
  541. {
  542. id: 1,
  543. title: 'n8n 免费 OpenAI API 额度',
  544. description: 'OpenAI 创建更新了 3 天前 | 创建时间:1 月 23 日',
  545. created: '1 月 23 日',
  546. icon: '🤖'
  547. },
  548. {
  549. id: 2,
  550. title: 'Google Sheets 帐户',
  551. description: 'Google Sheets OAuth2 API | 上次更新时间:3 天前 | 创建时间:1 月 23 日',
  552. created: '1 月 23 日',
  553. icon: '📊'
  554. }
  555. ])
  556. const executions = ref([
  557. {
  558. id: 1,
  559. workflow: '我的工作流程',
  560. status: '成功',
  561. startTime: '1 月 26 10:39:03',
  562. duration: '35 秒',
  563. executionId: '1'
  564. },
  565. {
  566. id: 2,
  567. workflow: '工作流程 2',
  568. status: '成功',
  569. startTime: '1 月 26 10:30:00',
  570. duration: '2 分钟',
  571. executionId: '2'
  572. }
  573. ])
  574. const variables = ref<{ id: number; key: string; value: string; usage: string; scope: string }[]>(
  575. []
  576. )
  577. const tables = ref<
  578. { id: number; name: string; description: string; method: string; size: string; rows: string }[]
  579. >([])
  580. // 数据表相关
  581. const tablePageInput = ref('')
  582. const tablePageSize = ref('50')
  583. const tableForm = ref({
  584. name: '',
  585. method: 'from-scratch'
  586. })
  587. const resetTableForm = () => {
  588. tableForm.value = {
  589. name: '',
  590. method: 'from-scratch'
  591. }
  592. }
  593. const tableFormRef = ref()
  594. const tableFormRules = {
  595. name: [
  596. { required: true, message: '请输入数据表名称', trigger: 'blur' },
  597. { min: 1, max: 100, message: '数据表名称长度为 1-100 个字符', trigger: 'blur' }
  598. ],
  599. method: [{ required: true, message: '请选择创建方式', trigger: 'change' }]
  600. }
  601. const submitTable = async () => {
  602. try {
  603. await tableFormRef.value?.validate()
  604. const newTable = {
  605. id: Math.max(...tables.value.map((t: any) => t.id), 0) + 1,
  606. name: tableForm.value.name,
  607. description: tableForm.value.method === 'from-scratch' ? '从零开始创建' : '从 CSV 导入',
  608. method: tableForm.value.method,
  609. size: '0MB',
  610. rows: '0'
  611. }
  612. tables.value.push(newTable)
  613. dashboardStore.closeTableDialog()
  614. resetTableForm()
  615. ElMessage.success('数据表已创建')
  616. } catch (error) {
  617. // 验证失败,form 会自动显示错误信息
  618. }
  619. }
  620. const filter = ref('')
  621. const sort = ref('name')
  622. const pageSize = ref(10)
  623. // 使用 computed 来包装 dashboardStore.activeTab,以便简化模板中的引用
  624. const activeTab = computed(() => dashboardStore.activeTab)
  625. // 变量表单相关
  626. const varFilter = ref('')
  627. const varSort = ref('name')
  628. const varPageSize = ref('25')
  629. const varForm = ref({
  630. id: undefined as number | undefined,
  631. key: '',
  632. value: '',
  633. scope: 'scope'
  634. })
  635. const resetVarForm = () => {
  636. varForm.value = {
  637. id: undefined,
  638. key: '',
  639. value: '',
  640. scope: 'scope'
  641. }
  642. }
  643. const varFormRef = ref()
  644. const varFormRules = {
  645. key: [
  646. { required: true, message: '请输入Key', trigger: 'blur' },
  647. { min: 1, max: 100, message: 'Key 长度为 1-100 个字符', trigger: 'blur' }
  648. ],
  649. value: [],
  650. scope: [{ required: true, message: '请选择范围', trigger: 'change' }]
  651. }
  652. const submitVariable = async () => {
  653. try {
  654. await varFormRef.value?.validate()
  655. // 编辑已有变量
  656. if (varForm.value.id) {
  657. const idx = variables.value.findIndex((v: any) => v.id === varForm.value.id)
  658. if (idx !== -1) {
  659. variables.value[idx] = {
  660. id: varForm.value.id,
  661. key: varForm.value.key,
  662. value: varForm.value.value,
  663. usage: `$vars.${varForm.value.key}`,
  664. scope: varForm.value.scope
  665. }
  666. dashboardStore.closeVarDialog()
  667. resetVarForm()
  668. ElMessage.success('变量已更新')
  669. return
  670. }
  671. }
  672. // 新增变量
  673. const newVar = {
  674. id: Math.max(...variables.value.map((v: any) => v.id), 0) + 1,
  675. key: varForm.value.key,
  676. value: varForm.value.value,
  677. usage: `$vars.${varForm.value.key}`,
  678. scope: varForm.value.scope
  679. }
  680. variables.value.push(newVar)
  681. dashboardStore.closeVarDialog()
  682. resetVarForm()
  683. ElMessage.success('变量已添加')
  684. } catch (error) {
  685. // 验证失败,form 会自动显示错误信息
  686. }
  687. }
  688. const varDialogTitle = computed(() => (varForm.value.id ? '编辑变量' : '新增'))
  689. const editVariable = (variable: any) => {
  690. varForm.value = { ...variable, scope: variable.scope }
  691. dashboardStore.openVarDialog()
  692. }
  693. const deleteVariable = (id: number) => {
  694. ElMessageBox.confirm('确定删除该变量吗?', '提示', {
  695. confirmButtonText: '确定',
  696. cancelButtonText: '取消',
  697. type: 'warning'
  698. })
  699. .then(() => {
  700. variables.value = variables.value.filter((v: any) => v.id !== id)
  701. ElMessage.success('变量已删除')
  702. })
  703. .catch(() => {})
  704. }
  705. const pageMap = ref({
  706. flows: 1,
  707. certs: 1,
  708. execs: 1,
  709. vars: 1,
  710. tables: 1
  711. })
  712. const currentPage = computed({
  713. get: () => pageMap.value[activeTab.value as keyof typeof pageMap.value] || 1,
  714. set: (val: number) => {
  715. pageMap.value[activeTab.value as keyof typeof pageMap.value] = val
  716. }
  717. })
  718. const getTabLabel = computed(() => {
  719. const labels: Record<string, string> = {
  720. flows: '工作流程',
  721. certs: '证书',
  722. execs: '执行',
  723. vars: '变量',
  724. tables: '数据表'
  725. }
  726. return labels[activeTab.value] || '项目'
  727. })
  728. const getTabData = computed(() => {
  729. const tabs: Record<string, any> = {
  730. flows: workflows.value,
  731. certs: certificates.value,
  732. execs: executions.value,
  733. vars: variables.value,
  734. tables: tables.value
  735. }
  736. const data = tabs[activeTab.value] || []
  737. const q = filter.value.trim().toLowerCase()
  738. if (!q) return data
  739. return data.filter((item: any) => item.title.toLowerCase().includes(q))
  740. })
  741. const pagedData = computed(() => {
  742. const start = (currentPage.value - 1) * pageSize.value
  743. return getTabData.value.slice(start, start + pageSize.value)
  744. })
  745. const filteredVariables = computed(() => {
  746. let result = variables.value
  747. const q = varFilter.value.trim().toLowerCase()
  748. if (q) {
  749. result = result.filter(
  750. (v: any) => v.key.toLowerCase().includes(q) || v.value.toLowerCase().includes(q)
  751. )
  752. }
  753. // 排序
  754. if (varSort.value === 'name') {
  755. result.sort((a: any, b: any) => a.key.localeCompare(b.key))
  756. } else if (varSort.value === 'name-desc') {
  757. result.sort((a: any, b: any) => b.key.localeCompare(a.key))
  758. }
  759. return result
  760. })
  761. const toEditor = () => {
  762. $router.push('/workflow/1')
  763. }
  764. </script>
  765. <style lang="less" scoped></style>