editor.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  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="'node-' + 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, pageSize: 100 })
  211. this.templates = res.data.list || []
  212. } catch (e) {
  213. console.error('加载模板失败', e)
  214. this.templates = []
  215. }
  216. },
  217. getNodeTemplateName(node) {
  218. if (!node.task_template_ref) return '未选择模板'
  219. var tpl = this.templates.find(function(t) { return t.id === Number(node.task_template_ref) })
  220. return tpl ? tpl.title : ('模板#' + node.task_template_ref)
  221. },
  222. showNodePicker() {
  223. this.showNodePickerFlag = true
  224. this.templateSearch = ''
  225. },
  226. addNodeFromTemplate(tpl) {
  227. _nodeSeq += 1
  228. var node = {
  229. id: 'n' + _nodeSeq,
  230. task_template_ref: tpl.id,
  231. title: tpl.title,
  232. executorType: 'child',
  233. timeout_minutes: 0,
  234. max_loops: 0
  235. }
  236. this.nodes.push(node)
  237. this.showNodePickerFlag = false
  238. uni.showToast({ title: '已添加节点', icon: 'success' })
  239. },
  240. handleEditNode(node) {
  241. this.editNode = JSON.parse(JSON.stringify(node))
  242. this.showNodeEditFlag = true
  243. },
  244. closeNodeEdit() {
  245. this.showNodeEditFlag = false
  246. this.editNode = null
  247. },
  248. saveNodeEdit() {
  249. if (!this.editNode) return
  250. var idx = this.nodes.findIndex(function(n) { return n.id === this.editNode.id })
  251. if (idx >= 0) {
  252. this.$set(this.nodes, idx, JSON.parse(JSON.stringify(this.editNode)))
  253. }
  254. this.closeNodeEdit()
  255. },
  256. onExecutorChange(e) {
  257. var idx = e.detail.value
  258. if (this.editNode) {
  259. this.editNode.executorType = this.executorTypes[idx]
  260. }
  261. },
  262. handleDeleteNode(index) {
  263. uni.showModal({
  264. title: '确认删除',
  265. content: '确定要删除这个节点吗?',
  266. success: (res) => {
  267. if (res.confirm) {
  268. this.nodes.splice(index, 1)
  269. }
  270. }
  271. })
  272. },
  273. handleMoveNode(index, direction) {
  274. var newIndex = index + direction
  275. if (newIndex < 0 || newIndex >= this.nodes.length) return
  276. var temp = this.nodes[index]
  277. this.$set(this.nodes, index, this.nodes[newIndex])
  278. this.$set(this.nodes, newIndex, temp)
  279. },
  280. validateBeforeSave() {
  281. var errs = []
  282. if (!this.flowName.trim()) errs.push('请填写流程名称')
  283. if (this.nodes.length === 0) errs.push('请至少添加一个任务节点')
  284. if (this.nodes.some(function(n) { return !n.task_template_ref })) {
  285. errs.push('存在未绑定任务模板的节点')
  286. }
  287. return errs
  288. },
  289. async handleSaveDraft() {
  290. var errs = this.validateBeforeSave()
  291. if (errs.length > 0) {
  292. uni.showToast({ title: errs[0], icon: 'none' })
  293. return
  294. }
  295. uni.showLoading({ title: '保存中...' })
  296. try {
  297. var cfg = {
  298. nodes: this.nodes.map(function(n) {
  299. return {
  300. id: n.id,
  301. task_template_ref: String(n.task_template_ref),
  302. title: n.title,
  303. executorType: n.executorType,
  304. timeout_minutes: n.timeout_minutes,
  305. max_loops: n.max_loops
  306. }
  307. })
  308. }
  309. var res = await saveOrchestrationFlow({
  310. id: this.flowId,
  311. name: this.flowName,
  312. description: this.flowDescription,
  313. configJson: JSON.stringify(cfg)
  314. })
  315. this.flowId = res.data.id || this.flowId
  316. uni.hideLoading()
  317. uni.showToast({ title: '保存成功', icon: 'success' })
  318. setTimeout(function() {
  319. uni.navigateBack()
  320. }, 1000)
  321. } catch (e) {
  322. uni.hideLoading()
  323. uni.showToast({ title: e.message || '保存失败', icon: 'none' })
  324. }
  325. },
  326. async handlePublish() {
  327. var errs = this.validateBeforeSave()
  328. if (errs.length > 0) {
  329. uni.showToast({ title: errs[0], icon: 'none' })
  330. return
  331. }
  332. // 先保存
  333. uni.showLoading({ title: '发布中...' })
  334. try {
  335. var cfg = {
  336. nodes: this.nodes.map(function(n) {
  337. return {
  338. id: n.id,
  339. task_template_ref: String(n.task_template_ref),
  340. title: n.title,
  341. executorType: n.executorType,
  342. timeout_minutes: n.timeout_minutes,
  343. max_loops: n.max_loops
  344. }
  345. })
  346. }
  347. var saveRes = await saveOrchestrationFlow({
  348. id: this.flowId,
  349. name: this.flowName,
  350. description: this.flowDescription,
  351. configJson: JSON.stringify(cfg)
  352. })
  353. this.flowId = saveRes.data.id || this.flowId
  354. // 再发布
  355. await publishOrchestrationFlow(this.flowId)
  356. uni.hideLoading()
  357. uni.showToast({ title: '发布成功', icon: 'success' })
  358. setTimeout(function() {
  359. uni.navigateBack()
  360. }, 1000)
  361. } catch (e) {
  362. uni.hideLoading()
  363. uni.showToast({ title: e.message || '发布失败', icon: 'none' })
  364. }
  365. }
  366. }
  367. }
  368. </script>
  369. <style scoped>
  370. .container {
  371. min-height: 100vh;
  372. background: #F8F8F8;
  373. }
  374. .loading {
  375. text-align: center;
  376. padding: 100rpx;
  377. color: #94A3B8;
  378. }
  379. .form-container {
  380. padding: 30rpx;
  381. }
  382. .section {
  383. background: #fff;
  384. border-radius: 16rpx;
  385. padding: 30rpx;
  386. margin-bottom: 24rpx;
  387. }
  388. .section-title {
  389. font-size: 28rpx;
  390. font-weight: bold;
  391. color: #1E293B;
  392. margin-bottom: 24rpx;
  393. display: flex;
  394. align-items: center;
  395. justify-content: space-between;
  396. }
  397. .node-count {
  398. font-size: 24rpx;
  399. color: #94A3B8;
  400. font-weight: normal;
  401. }
  402. .form-item {
  403. margin-bottom: 24rpx;
  404. }
  405. .label {
  406. display: block;
  407. font-size: 26rpx;
  408. color: #64748B;
  409. margin-bottom: 12rpx;
  410. }
  411. .required {
  412. color: #EF4444;
  413. }
  414. .input {
  415. width: 100%;
  416. height: 80rpx;
  417. border: 2rpx solid #E2E8F0;
  418. border-radius: 12rpx;
  419. padding: 0 20rpx;
  420. font-size: 28rpx;
  421. box-sizing: border-box;
  422. }
  423. .textarea {
  424. width: 100%;
  425. height: 160rpx;
  426. border: 2rpx solid #E2E8F0;
  427. border-radius: 12rpx;
  428. padding: 20rpx;
  429. font-size: 28rpx;
  430. box-sizing: border-box;
  431. }
  432. .picker {
  433. width: 100%;
  434. height: 80rpx;
  435. border: 2rpx solid #E2E8F0;
  436. border-radius: 12rpx;
  437. padding: 0 20rpx;
  438. font-size: 28rpx;
  439. line-height: 80rpx;
  440. }
  441. .node-item {
  442. background: #F8FAFC;
  443. border: 2rpx solid #E2E8F0;
  444. border-radius: 12rpx;
  445. padding: 20rpx;
  446. margin-bottom: 16rpx;
  447. }
  448. .node-header {
  449. display: flex;
  450. align-items: center;
  451. }
  452. .node-order {
  453. width: 48rpx;
  454. height: 48rpx;
  455. background: #F97316;
  456. color: #fff;
  457. border-radius: 50%;
  458. display: flex;
  459. align-items: center;
  460. justify-content: center;
  461. margin-right: 16rpx;
  462. }
  463. .order-num {
  464. font-size: 24rpx;
  465. font-weight: bold;
  466. }
  467. .node-info {
  468. flex: 1;
  469. min-width: 0;
  470. }
  471. .node-template {
  472. display: block;
  473. font-size: 26rpx;
  474. color: #1E293B;
  475. font-weight: 500;
  476. overflow: hidden;
  477. text-overflow: ellipsis;
  478. white-space: nowrap;
  479. }
  480. .node-id {
  481. display: block;
  482. font-size: 20rpx;
  483. color: #94A3B8;
  484. }
  485. .node-actions {
  486. display: flex;
  487. gap: 8rpx;
  488. }
  489. .btn-icon {
  490. width: 48rpx;
  491. height: 48rpx;
  492. border-radius: 8rpx;
  493. background: #fff;
  494. border: 1rpx solid #E2E8F0;
  495. font-size: 24rpx;
  496. display: flex;
  497. align-items: center;
  498. justify-content: center;
  499. padding: 0;
  500. }
  501. .btn-icon::after { border: none; }
  502. .btn-delete { color: #EF4444; }
  503. .node-preview {
  504. margin-top: 12rpx;
  505. padding-left: 64rpx;
  506. }
  507. .node-title {
  508. font-size: 24rpx;
  509. color: #64748B;
  510. }
  511. .add-node-btn {
  512. border: 2rpx dashed #F97316;
  513. border-radius: 12rpx;
  514. padding: 28rpx 0;
  515. text-align: center;
  516. color: #F97316;
  517. }
  518. .add-icon {
  519. font-size: 36rpx;
  520. margin-right: 8rpx;
  521. }
  522. .condition-hint {
  523. font-size: 24rpx;
  524. color: #94A3B8;
  525. padding: 16rpx;
  526. background: #F8FAFC;
  527. border-radius: 8rpx;
  528. }
  529. .action-bar {
  530. padding: 30rpx;
  531. display: flex;
  532. gap: 20rpx;
  533. }
  534. .btn-save, .btn-publish {
  535. flex: 1;
  536. height: 88rpx;
  537. line-height: 88rpx;
  538. border-radius: 44rpx;
  539. font-size: 30rpx;
  540. font-weight: bold;
  541. border: none;
  542. }
  543. .btn-save::after, .btn-publish::after { border: none; }
  544. .btn-save { background: #F1F5F9; color: #64748B; }
  545. .btn-publish { background: linear-gradient(135deg, #F97316 0%, #EA580C 100%); color: #fff; }
  546. .modal-mask {
  547. position: fixed;
  548. top: 0;
  549. left: 0;
  550. right: 0;
  551. bottom: 0;
  552. background: rgba(0, 0, 0, 0.5);
  553. z-index: 1000;
  554. display: flex;
  555. align-items: flex-end;
  556. }
  557. .modal {
  558. width: 100%;
  559. background: #fff;
  560. border-radius: 32rpx 32rpx 0 0;
  561. max-height: 90vh;
  562. overflow: hidden;
  563. display: flex;
  564. flex-direction: column;
  565. }
  566. .modal-large {
  567. max-height: 80vh;
  568. }
  569. .modal-header {
  570. padding: 30rpx;
  571. border-bottom: 1rpx solid #E2E8F0;
  572. display: flex;
  573. align-items: center;
  574. justify-content: space-between;
  575. }
  576. .modal-title {
  577. font-size: 32rpx;
  578. font-weight: bold;
  579. color: #1E293B;
  580. }
  581. .btn-close {
  582. width: 56rpx;
  583. height: 56rpx;
  584. border-radius: 50%;
  585. background: #F1F5F9;
  586. font-size: 32rpx;
  587. line-height: 56rpx;
  588. text-align: center;
  589. padding: 0;
  590. border: none;
  591. }
  592. .btn-close::after { border: none; }
  593. .search-box {
  594. padding: 20rpx 30rpx;
  595. border-bottom: 1rpx solid #E2E8F0;
  596. }
  597. .search-input {
  598. width: 100%;
  599. height: 72rpx;
  600. border: 2rpx solid #E2E8F0;
  601. border-radius: 36rpx;
  602. padding: 0 24rpx;
  603. font-size: 26rpx;
  604. box-sizing: border-box;
  605. }
  606. .template-list {
  607. flex: 1;
  608. overflow-y: auto;
  609. padding: 20rpx 30rpx;
  610. }
  611. .template-item {
  612. padding: 24rpx 0;
  613. border-bottom: 1rpx solid #F1F5F9;
  614. display: flex;
  615. justify-content: space-between;
  616. align-items: center;
  617. }
  618. .tpl-name {
  619. font-size: 28rpx;
  620. color: #1E293B;
  621. }
  622. .tpl-points {
  623. font-size: 24rpx;
  624. color: #F97316;
  625. font-weight: 500;
  626. }
  627. .empty {
  628. text-align: center;
  629. padding: 60rpx;
  630. color: #94A3B8;
  631. }
  632. </style>