CompleteDialog.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <template>
  2. <el-dialog
  3. title="完成任务"
  4. :visible="visible"
  5. width="600px"
  6. @close="handleClose"
  7. @update:visible="$emit('update:visible', $event)">
  8. <el-form ref="form" label-position="right" label-width="140px" :model="form" :rules="rules" size="medium">
  9. <el-form-item label="实际工作量 (小时)" prop="actualWorkHour">
  10. <div class="actual-hour-row">
  11. <el-input-number
  12. v-model="form.actualWorkHour"
  13. controls-position="right"
  14. :min="0"
  15. :precision="1"
  16. :step="0.5" />
  17. <el-button size="mini" @click="addActualWorkHour(0.5)">+0.5</el-button>
  18. <el-button size="mini" @click="addActualWorkHour(2)">+2</el-button>
  19. </div>
  20. </el-form-item>
  21. <el-form-item label="完成时间" prop="completionDate">
  22. <el-date-picker
  23. v-model="form.completionDate"
  24. placeholder="选择完成日期"
  25. style="width: 100%"
  26. type="date"
  27. value-format="yyyy-MM-dd" />
  28. </el-form-item>
  29. <el-form-item label="备注" prop="remark">
  30. <el-input v-model="form.remark" placeholder="请输入完成备注..." :rows="4" type="textarea" />
  31. </el-form-item>
  32. <el-form-item label="附件上传" prop="attachments">
  33. <el-upload
  34. ref="uploadRef"
  35. action=""
  36. :auto-upload="false"
  37. :before-upload="beforeUpload"
  38. class="upload-demo"
  39. :file-list="fileList"
  40. :limit="5"
  41. :multiple="true"
  42. :on-change="handleFileChange"
  43. :on-remove="handleFileRemove">
  44. <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
  45. <el-button slot="tip" size="mini">最多5个文件,每个不超过20MB</el-button>
  46. </el-upload>
  47. </el-form-item>
  48. </el-form>
  49. <div slot="footer" class="dialog-footer">
  50. <el-button @click="handleClose">取消</el-button>
  51. <el-button :loading="submitLoading" type="primary" @click="handleSubmit">提交</el-button>
  52. </div>
  53. </el-dialog>
  54. </template>
  55. <script>
  56. import attachmentUploadMixin from '@/mixins/attachmentUpload'
  57. import opsEventTaskApi from '@/api/devops/opsEventTask'
  58. export default {
  59. name: 'CompleteDialog',
  60. mixins: [attachmentUploadMixin],
  61. props: {
  62. visible: { type: Boolean, default: false },
  63. taskId: { type: Number, default: null },
  64. taskData: { type: Object, default: null },
  65. testResult: { type: String, default: '' },
  66. },
  67. data() {
  68. return {
  69. form: {
  70. actualWorkHour: null,
  71. remark: '',
  72. completionDate: '',
  73. attachments: [],
  74. },
  75. rules: {
  76. actualWorkHour: [{ required: true, message: '请输入实际工作量', trigger: 'blur' }],
  77. },
  78. submitLoading: false,
  79. }
  80. },
  81. watch: {
  82. async visible(val) {
  83. if (!val || !this.taskId) return
  84. // set default completion date to today
  85. if (!this.form.completionDate) {
  86. const today = new Date()
  87. const y = today.getFullYear()
  88. const m = String(today.getMonth() + 1).padStart(2, '0')
  89. const d = String(today.getDate()).padStart(2, '0')
  90. this.form.completionDate = `${y}-${m}-${d}`
  91. }
  92. // pre-fill actualWorkHour from registered work hours
  93. try {
  94. const res = await opsEventTaskApi.getWorkHourList(this.taskId)
  95. const workHourList = res.data?.list || res.data || []
  96. const totalRegisteredHours = workHourList.reduce((sum, row) => sum + (Number(row.actualHour) || 0), 0)
  97. if (totalRegisteredHours > 0) {
  98. this.form.actualWorkHour = Math.round(totalRegisteredHours * 10) / 10
  99. return
  100. }
  101. } catch (e) {
  102. // fallback to taskData if query fails
  103. }
  104. // fallback: use taskData.actualWorkHour
  105. if (this.taskData && this.taskData.actualWorkHour != null) {
  106. this.form.actualWorkHour = this.taskData.actualWorkHour
  107. }
  108. },
  109. },
  110. methods: {
  111. addActualWorkHour(hours) {
  112. const current = Number(this.form.actualWorkHour) || 0
  113. this.form.actualWorkHour = Math.round((current + hours) * 10) / 10
  114. },
  115. formatRemark(remark) {
  116. if (!remark) return remark
  117. // 将内容按句子或逻辑段落分段,增加换行符以便阅读
  118. return remark
  119. .replace(/([。!?])/g, '$1\n') // 在句号、感叹号、问号后添加换行
  120. .replace(/(\d+\.[\s]*)/g, '\n$1') // 在数字编号前添加换行
  121. .replace(/(\n{2,})/g, '\n') // 去除多余连续换行
  122. .trim()
  123. },
  124. handleSubmit() {
  125. this.$refs.form.validate(async (valid) => {
  126. if (!valid) return
  127. if (!this.taskId) {
  128. this.$message.error('任务ID不能为空')
  129. return
  130. }
  131. this.submitLoading = true
  132. try {
  133. // 上传附件,后端 complete 接口会自动创建过程记录
  134. const attachments = await this.uploadAttachments()
  135. // 完成任务(后端会自动创建过程记录并将附件信息记录到任务附件中)
  136. const testResult = this.testResult || (this.taskData && this.taskData.testResult) || ''
  137. const payload = {
  138. id: this.taskId,
  139. actualWorkHour: this.form.actualWorkHour,
  140. remark: this.formatRemark(this.form.remark),
  141. attachments,
  142. testResult,
  143. completeDate: this.form.completionDate,
  144. }
  145. await opsEventTaskApi.complete(payload)
  146. this.$emit('refresh')
  147. this.$emit('success')
  148. this.$message.success('任务完成已提交')
  149. this.handleClose()
  150. } catch (err) {
  151. this.$message.error('提交失败: ' + (err.message || err))
  152. } finally {
  153. this.submitLoading = false
  154. }
  155. })
  156. },
  157. handleClose() {
  158. // reset form and close
  159. this.form.actualWorkHour = null
  160. this.form.remark = ''
  161. this.form.completionDate = ''
  162. this.resetAttachmentFiles()
  163. this.$emit('update:visible', false)
  164. },
  165. },
  166. }
  167. </script>
  168. <style scoped>
  169. .actual-hour-row {
  170. display: flex;
  171. gap: 4px;
  172. align-items: center;
  173. .el-button--mini {
  174. flex-shrink: 0;
  175. }
  176. }
  177. .upload-demo {
  178. display: block;
  179. }
  180. </style>