editor.vue 18 KB

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