editor.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. <template>
  2. <view class="container">
  3. <!-- 加载状态 -->
  4. <view v-if="loading" class="loading">
  5. <text>加载中...</text>
  6. </view>
  7. <!-- 表单内容 -->
  8. <view v-else class="form-container">
  9. <!-- 基本信息 -->
  10. <view class="section">
  11. <view class="section-title">基本信息</view>
  12. <view class="form-item">
  13. <text class="label">流程名称 <text class="required">*</text></text>
  14. <input v-model="flowName" class="input" placeholder="请输入流程名称" />
  15. </view>
  16. <view class="form-item">
  17. <text class="label">流程描述</text>
  18. <textarea v-model="flowDescription" class="textarea" placeholder="可选,描述流程用途" />
  19. </view>
  20. </view>
  21. <!-- 节点列表 -->
  22. <view class="section">
  23. <view class="section-title">
  24. <text>任务节点</text>
  25. <text class="node-count">{{ nodes.length }} 个节点</text>
  26. </view>
  27. <!-- 节点列表 -->
  28. <view v-for="(node, index) in nodes" :key="getNodeKey(index)" class="node-item">
  29. <view class="node-header">
  30. <view class="node-order">
  31. <text class="order-num">{{ index + 1 }}</text>
  32. </view>
  33. <view class="node-info">
  34. <text class="node-template">{{ getNodeTemplateName(node) }}</text>
  35. <text class="node-id">{{ node.id }}</text>
  36. </view>
  37. <view class="node-actions">
  38. <button class="btn-icon" @click="handleEditNode(node)">✏️</button>
  39. <button class="btn-icon btn-delete" @click="handleDeleteNode(index)">🗑️</button>
  40. <button v-if="index > 0" class="btn-icon" @click="handleMoveNode(index, -1)">↑</button>
  41. <button v-if="index < nodes.length - 1" class="btn-icon" @click="handleMoveNode(index, 1)">↓</button>
  42. </view>
  43. </view>
  44. <view class="node-preview">
  45. <text class="node-title">{{ node.title }}</text>
  46. </view>
  47. </view>
  48. <!-- 添加节点按钮 -->
  49. <view class="add-node-btn" @click="showNodePicker">
  50. <text class="add-icon">+</text>
  51. <text>添加任务节点</text>
  52. </view>
  53. </view>
  54. <!-- 触发条件设置 -->
  55. <view class="section" v-if="nodes.length > 1">
  56. <view class="section-title">触发条件</view>
  57. <view class="condition-hint">
  58. <text>每个节点将在前一个节点完成后自动触发</text>
  59. </view>
  60. </view>
  61. <!-- 保存按钮 -->
  62. <view class="action-bar">
  63. <button class="btn-save" @click="handleSaveDraft">保存草稿</button>
  64. <button v-if="!isDraft" class="btn-publish" @click="handlePublish">发布</button>
  65. </view>
  66. </view>
  67. <!-- 节点选择弹窗 -->
  68. <view v-if="showNodePickerFlag" class="modal-mask" @click="showNodePickerFlag = false">
  69. <view class="modal modal-large" @click.stop>
  70. <view class="modal-header">
  71. <text class="modal-title">选择任务模板</text>
  72. <button class="btn-close" @click="showNodePickerFlag = false">×</button>
  73. </view>
  74. <view class="search-box">
  75. <input v-model="templateSearch" class="search-input" placeholder="搜索模板..." />
  76. </view>
  77. <scroll-view scroll-y class="template-list">
  78. <view
  79. v-for="tpl in filteredTemplates"
  80. :key="tpl.id"
  81. class="template-item"
  82. @click="addNodeFromTemplate(tpl)"
  83. >
  84. <text class="tpl-name">{{ tpl.title }}</text>
  85. <text class="tpl-points">+{{ tpl.points || 2 }}分</text>
  86. </view>
  87. <view v-if="filteredTemplates.length === 0" class="empty">
  88. <text>无匹配模板</text>
  89. </view>
  90. </scroll-view>
  91. </view>
  92. </view>
  93. <!-- 节点编辑弹窗 -->
  94. <view v-if="showNodeEditFlag" class="modal-mask" @click="closeNodeEdit">
  95. <view class="modal modal-large" @click.stop>
  96. <view class="modal-header">
  97. <text class="modal-title">编辑节点</text>
  98. <button class="btn-close" @click="closeNodeEdit">×</button>
  99. </view>
  100. <view class="form-container">
  101. <view class="form-item">
  102. <text class="label">任务标题</text>
  103. <input v-model="editNode.title" class="input" placeholder="输入任务标题" />
  104. </view>
  105. <view class="form-item">
  106. <text class="label">超时时间(分钟)</text>
  107. <input v-model="editNode.timeout_minutes" class="input" type="number" placeholder="0表示不限制" />
  108. </view>
  109. <view class="form-item">
  110. <text class="label">最大循环次数</text>
  111. <input v-model="editNode.max_loops" class="input" type="number" placeholder="0表示不限制" />
  112. </view>
  113. <view class="form-item">
  114. <text class="label">执行人</text>
  115. <picker :range="executorTypes" @change="onExecutorChange">
  116. <view class="picker">{{ executorLabels[editNode.executorType] || '请选择' }}</view>
  117. </picker>
  118. </view>
  119. </view>
  120. <view class="action-bar">
  121. <button class="btn-save" @click="saveNodeEdit">保存</button>
  122. </view>
  123. </view>
  124. </view>
  125. </view>
  126. </template>
  127. <script>
  128. import { getOrchestrationFlowDetail, saveOrchestrationFlow, publishOrchestrationFlow, getNodeTemplateList } from '../../utils/api.js'
  129. let _nodeSeq = 0
  130. export default {
  131. data() {
  132. return {
  133. flowId: null,
  134. flowName: '',
  135. flowDescription: '',
  136. nodes: [],
  137. templates: [],
  138. templateSearch: '',
  139. showNodePickerFlag: false,
  140. showNodeEditFlag: false,
  141. editNode: null,
  142. loading: false,
  143. executorTypes: ['child', 'member'],
  144. executorLabels: { child: '孩子', member: '家庭成员' }
  145. }
  146. },
  147. computed: {
  148. isDraft() {
  149. return this.flowId === null
  150. },
  151. filteredTemplates() {
  152. if (!this.templateSearch) return this.templates
  153. var kw = this.templateSearch.toLowerCase()
  154. return this.templates.filter(function(t) {
  155. return t.title.indexOf(kw) >= 0
  156. })
  157. }
  158. },
  159. onLoad(options) {
  160. var id = options.id ? Number(options.id) : null
  161. if (id) {
  162. this.flowId = id
  163. this.loadFlowDetail(id)
  164. } else {
  165. this.flowId = null
  166. this.initEmptyForm()
  167. }
  168. this.loadTemplates()
  169. },
  170. methods: {
  171. initEmptyForm() {
  172. this.flowName = ''
  173. this.flowDescription = ''
  174. this.nodes = []
  175. _nodeSeq = 0
  176. },
  177. async loadFlowDetail(id) {
  178. this.loading = true
  179. try {
  180. var res = await getOrchestrationFlowDetail(id)
  181. var flow = res.data.flow
  182. if (flow) {
  183. this.flowName = flow.name || ''
  184. this.flowDescription = flow.description || ''
  185. // 解析 configJson
  186. if (flow.configJson) {
  187. var cfg = JSON.parse(flow.configJson)
  188. this.nodes = (cfg.nodes || []).map(function(n) {
  189. return {
  190. id: n.id,
  191. task_template_ref: n.task_template_ref,
  192. title: n.title || '',
  193. executorType: n.executorType || 'child',
  194. timeout_minutes: n.timeout_minutes || 0,
  195. max_loops: n.max_loops || 0
  196. }
  197. })
  198. _nodeSeq = this.nodes.length
  199. }
  200. }
  201. } catch (e) {
  202. console.error('加载流程详情失败', e)
  203. uni.showToast({ title: '加载失败', icon: 'none' })
  204. } finally {
  205. this.loading = false
  206. }
  207. },
  208. async loadTemplates() {
  209. try {
  210. var res = await getNodeTemplateList({ page: 1, size: 100 })
  211. this.templates = res.data.records || res.data || []
  212. } catch (e) {
  213. console.error('加载模板失败', e)
  214. this.templates = []
  215. }
  216. },
  217. getNodeKey: function(index) {
  218. return 'node-' + index
  219. },
  220. getNodeTemplateName(node) {
  221. if (!node.task_template_ref) return '未选择模板'
  222. var tpl = this.templates.find(function(t) { return t.id === Number(node.task_template_ref) })
  223. return tpl ? tpl.title : ('模板#' + node.task_template_ref)
  224. },
  225. showNodePicker() {
  226. this.showNodePickerFlag = true
  227. this.templateSearch = ''
  228. },
  229. addNodeFromTemplate(tpl) {
  230. _nodeSeq += 1
  231. var node = {
  232. id: 'n' + _nodeSeq,
  233. task_template_ref: tpl.id,
  234. title: tpl.title,
  235. executorType: 'child',
  236. timeout_minutes: 0,
  237. max_loops: 0
  238. }
  239. this.nodes.push(node)
  240. this.showNodePickerFlag = false
  241. uni.showToast({ title: '已添加节点', icon: 'success' })
  242. },
  243. handleEditNode(node) {
  244. this.editNode = JSON.parse(JSON.stringify(node))
  245. this.showNodeEditFlag = true
  246. },
  247. closeNodeEdit() {
  248. this.showNodeEditFlag = false
  249. this.editNode = null
  250. },
  251. saveNodeEdit() {
  252. if (!this.editNode) return
  253. var idx = this.nodes.findIndex(function(n) { return n.id === this.editNode.id })
  254. if (idx >= 0) {
  255. this.$set(this.nodes, idx, JSON.parse(JSON.stringify(this.editNode)))
  256. }
  257. this.closeNodeEdit()
  258. },
  259. onExecutorChange(e) {
  260. var idx = e.detail.value
  261. if (this.editNode) {
  262. this.editNode.executorType = this.executorTypes[idx]
  263. }
  264. },
  265. handleDeleteNode(index) {
  266. uni.showModal({
  267. title: '确认删除',
  268. content: '确定要删除这个节点吗?',
  269. success: (res) => {
  270. if (res.confirm) {
  271. this.nodes.splice(index, 1)
  272. }
  273. }
  274. })
  275. },
  276. handleMoveNode(index, direction) {
  277. var newIndex = index + direction
  278. if (newIndex < 0 || newIndex >= this.nodes.length) return
  279. var temp = this.nodes[index]
  280. this.$set(this.nodes, index, this.nodes[newIndex])
  281. this.$set(this.nodes, newIndex, temp)
  282. },
  283. validateBeforeSave() {
  284. var errs = []
  285. if (!this.flowName.trim()) errs.push('请填写流程名称')
  286. if (this.nodes.length === 0) errs.push('请至少添加一个任务节点')
  287. if (this.nodes.some(function(n) { return !n.task_template_ref })) {
  288. errs.push('存在未绑定任务模板的节点')
  289. }
  290. return errs
  291. },
  292. async handleSaveDraft() {
  293. var errs = this.validateBeforeSave()
  294. if (errs.length > 0) {
  295. uni.showToast({ title: errs[0], icon: 'none' })
  296. return
  297. }
  298. uni.showLoading({ title: '保存中...' })
  299. try {
  300. var cfg = {
  301. nodes: this.nodes.map(function(n) {
  302. return {
  303. id: n.id,
  304. task_template_ref: String(n.task_template_ref),
  305. title: n.title,
  306. executorType: n.executorType,
  307. timeout_minutes: n.timeout_minutes,
  308. max_loops: n.max_loops
  309. }
  310. })
  311. }
  312. var res = await saveOrchestrationFlow({
  313. id: this.flowId,
  314. name: this.flowName,
  315. description: this.flowDescription,
  316. configJson: JSON.stringify(cfg)
  317. })
  318. this.flowId = res.data.id || this.flowId
  319. uni.hideLoading()
  320. uni.showToast({ title: '保存成功', icon: 'success' })
  321. setTimeout(function() {
  322. uni.navigateBack()
  323. }, 1000)
  324. } catch (e) {
  325. uni.hideLoading()
  326. uni.showToast({ title: e.message || '保存失败', icon: 'none' })
  327. }
  328. },
  329. async handlePublish() {
  330. var errs = this.validateBeforeSave()
  331. if (errs.length > 0) {
  332. uni.showToast({ title: errs[0], icon: 'none' })
  333. return
  334. }
  335. // 先保存
  336. uni.showLoading({ title: '发布中...' })
  337. try {
  338. var cfg = {
  339. nodes: this.nodes.map(function(n) {
  340. return {
  341. id: n.id,
  342. task_template_ref: String(n.task_template_ref),
  343. title: n.title,
  344. executorType: n.executorType,
  345. timeout_minutes: n.timeout_minutes,
  346. max_loops: n.max_loops
  347. }
  348. })
  349. }
  350. var saveRes = await saveOrchestrationFlow({
  351. id: this.flowId,
  352. name: this.flowName,
  353. description: this.flowDescription,
  354. configJson: JSON.stringify(cfg)
  355. })
  356. this.flowId = saveRes.data.id || this.flowId
  357. // 再发布
  358. await publishOrchestrationFlow(this.flowId)
  359. uni.hideLoading()
  360. uni.showToast({ title: '发布成功', icon: 'success' })
  361. setTimeout(function() {
  362. uni.navigateBack()
  363. }, 1000)
  364. } catch (e) {
  365. uni.hideLoading()
  366. uni.showToast({ title: e.message || '发布失败', icon: 'none' })
  367. }
  368. }
  369. }
  370. }
  371. </script>
  372. <style scoped>
  373. .container {
  374. min-height: 100vh;
  375. background: #F8F8F8;
  376. }
  377. .loading {
  378. text-align: center;
  379. padding: 100rpx;
  380. color: #94A3B8;
  381. }
  382. .form-container {
  383. padding: 30rpx;
  384. }
  385. .section {
  386. background: #fff;
  387. border-radius: 16rpx;
  388. padding: 30rpx;
  389. margin-bottom: 24rpx;
  390. }
  391. .section-title {
  392. font-size: 28rpx;
  393. font-weight: bold;
  394. color: #1E293B;
  395. margin-bottom: 24rpx;
  396. display: flex;
  397. align-items: center;
  398. justify-content: space-between;
  399. }
  400. .node-count {
  401. font-size: 24rpx;
  402. color: #94A3B8;
  403. font-weight: normal;
  404. }
  405. .form-item {
  406. margin-bottom: 24rpx;
  407. }
  408. .label {
  409. display: block;
  410. font-size: 26rpx;
  411. color: #64748B;
  412. margin-bottom: 12rpx;
  413. }
  414. .required {
  415. color: #EF4444;
  416. }
  417. .input {
  418. width: 100%;
  419. height: 80rpx;
  420. border: 2rpx solid #E2E8F0;
  421. border-radius: 12rpx;
  422. padding: 0 20rpx;
  423. font-size: 28rpx;
  424. box-sizing: border-box;
  425. }
  426. .textarea {
  427. width: 100%;
  428. height: 160rpx;
  429. border: 2rpx solid #E2E8F0;
  430. border-radius: 12rpx;
  431. padding: 20rpx;
  432. font-size: 28rpx;
  433. box-sizing: border-box;
  434. }
  435. .picker {
  436. width: 100%;
  437. height: 80rpx;
  438. border: 2rpx solid #E2E8F0;
  439. border-radius: 12rpx;
  440. padding: 0 20rpx;
  441. font-size: 28rpx;
  442. line-height: 80rpx;
  443. }
  444. .node-item {
  445. background: #F8FAFC;
  446. border: 2rpx solid #E2E8F0;
  447. border-radius: 12rpx;
  448. padding: 20rpx;
  449. margin-bottom: 16rpx;
  450. }
  451. .node-header {
  452. display: flex;
  453. align-items: center;
  454. }
  455. .node-order {
  456. width: 48rpx;
  457. height: 48rpx;
  458. background: #F97316;
  459. color: #fff;
  460. border-radius: 50%;
  461. display: flex;
  462. align-items: center;
  463. justify-content: center;
  464. margin-right: 16rpx;
  465. }
  466. .order-num {
  467. font-size: 24rpx;
  468. font-weight: bold;
  469. }
  470. .node-info {
  471. flex: 1;
  472. min-width: 0;
  473. }
  474. .node-template {
  475. display: block;
  476. font-size: 26rpx;
  477. color: #1E293B;
  478. font-weight: 500;
  479. overflow: hidden;
  480. text-overflow: ellipsis;
  481. white-space: nowrap;
  482. }
  483. .node-id {
  484. display: block;
  485. font-size: 20rpx;
  486. color: #94A3B8;
  487. }
  488. .node-actions {
  489. display: flex;
  490. gap: 8rpx;
  491. }
  492. .btn-icon {
  493. width: 48rpx;
  494. height: 48rpx;
  495. border-radius: 8rpx;
  496. background: #fff;
  497. border: 1rpx solid #E2E8F0;
  498. font-size: 24rpx;
  499. display: flex;
  500. align-items: center;
  501. justify-content: center;
  502. padding: 0;
  503. }
  504. .btn-icon::after { border: none; }
  505. .btn-delete { color: #EF4444; }
  506. .node-preview {
  507. margin-top: 12rpx;
  508. padding-left: 64rpx;
  509. }
  510. .node-title {
  511. font-size: 24rpx;
  512. color: #64748B;
  513. }
  514. .add-node-btn {
  515. border: 2rpx dashed #F97316;
  516. border-radius: 12rpx;
  517. padding: 28rpx 0;
  518. text-align: center;
  519. color: #F97316;
  520. }
  521. .add-icon {
  522. font-size: 36rpx;
  523. margin-right: 8rpx;
  524. }
  525. .condition-hint {
  526. font-size: 24rpx;
  527. color: #94A3B8;
  528. padding: 16rpx;
  529. background: #F8FAFC;
  530. border-radius: 8rpx;
  531. }
  532. .action-bar {
  533. padding: 30rpx;
  534. display: flex;
  535. gap: 20rpx;
  536. }
  537. .btn-save, .btn-publish {
  538. flex: 1;
  539. height: 88rpx;
  540. line-height: 88rpx;
  541. border-radius: 44rpx;
  542. font-size: 30rpx;
  543. font-weight: bold;
  544. border: none;
  545. }
  546. .btn-save::after, .btn-publish::after { border: none; }
  547. .btn-save { background: #F1F5F9; color: #64748B; }
  548. .btn-publish { background: linear-gradient(135deg, #F97316 0%, #EA580C 100%); color: #fff; }
  549. .modal-mask {
  550. position: fixed;
  551. top: 0;
  552. left: 0;
  553. right: 0;
  554. bottom: 0;
  555. background: rgba(0, 0, 0, 0.5);
  556. z-index: 1000;
  557. display: flex;
  558. align-items: flex-end;
  559. }
  560. .modal {
  561. width: 100%;
  562. background: #fff;
  563. border-radius: 32rpx 32rpx 0 0;
  564. max-height: 90vh;
  565. overflow: hidden;
  566. display: flex;
  567. flex-direction: column;
  568. }
  569. .modal-large {
  570. max-height: 80vh;
  571. }
  572. .modal-header {
  573. padding: 30rpx;
  574. border-bottom: 1rpx solid #E2E8F0;
  575. display: flex;
  576. align-items: center;
  577. justify-content: space-between;
  578. }
  579. .modal-title {
  580. font-size: 32rpx;
  581. font-weight: bold;
  582. color: #1E293B;
  583. }
  584. .btn-close {
  585. width: 56rpx;
  586. height: 56rpx;
  587. border-radius: 50%;
  588. background: #F1F5F9;
  589. font-size: 32rpx;
  590. line-height: 56rpx;
  591. text-align: center;
  592. padding: 0;
  593. border: none;
  594. }
  595. .btn-close::after { border: none; }
  596. .search-box {
  597. padding: 20rpx 30rpx;
  598. border-bottom: 1rpx solid #E2E8F0;
  599. }
  600. .search-input {
  601. width: 100%;
  602. height: 72rpx;
  603. border: 2rpx solid #E2E8F0;
  604. border-radius: 36rpx;
  605. padding: 0 24rpx;
  606. font-size: 26rpx;
  607. box-sizing: border-box;
  608. }
  609. .template-list {
  610. flex: 1;
  611. overflow-y: auto;
  612. padding: 20rpx 30rpx;
  613. }
  614. .template-item {
  615. padding: 24rpx 0;
  616. border-bottom: 1rpx solid #F1F5F9;
  617. display: flex;
  618. justify-content: space-between;
  619. align-items: center;
  620. }
  621. .tpl-name {
  622. font-size: 28rpx;
  623. color: #1E293B;
  624. }
  625. .tpl-points {
  626. font-size: 24rpx;
  627. color: #F97316;
  628. font-weight: 500;
  629. }
  630. .empty {
  631. text-align: center;
  632. padding: 60rpx;
  633. color: #94A3B8;
  634. }
  635. </style>