doubleSystemManagementDetail.vue 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. <template>
  2. <main class="safety-platform-container__main">
  3. <BasicForm
  4. ref="basicFormRef"
  5. :formData="ruleFormData"
  6. :formRules="isViewMode ? undefined : formRules"
  7. :formConfig="computedFormConfig"
  8. >
  9. <template #fileFormat>
  10. <el-radio-group v-model="ruleFormData.fileFormat" :disabled="isViewMode">
  11. <el-radio value="PDF">PDF</el-radio>
  12. <el-radio value="WORD">WORD</el-radio>
  13. </el-radio-group>
  14. </template>
  15. <template #fileUrl>
  16. <UploadFiles
  17. label="上传文件"
  18. :maxCount="1"
  19. :file-list="ruleFormData.fileUrlList"
  20. :disabled="isViewMode"
  21. :allow-all-file-types="true"
  22. @uploadSuccess="handleUploadSuccess"
  23. />
  24. </template>
  25. <template #content>
  26. <div class="editor-container">
  27. <Toolbar
  28. style="border-bottom: 1px solid #dcdfe6"
  29. :editor="editorRef"
  30. />
  31. <Editor
  32. style="height: 400px; overflow-y: auto"
  33. v-model="ruleFormData.content"
  34. mode="default"
  35. :defaultConfig="editorConfig"
  36. @on-created="handleEditorCreated"
  37. @on-change="handleEditorChange"
  38. />
  39. </div>
  40. </template>
  41. <template #status>
  42. <el-radio-group v-model="ruleFormData.status" :disabled="isViewMode">
  43. <el-radio :value="1">启用</el-radio>
  44. <el-radio :value="0">禁用</el-radio>
  45. </el-radio-group>
  46. </template>
  47. </BasicForm>
  48. </main>
  49. <footer class="safety-platform-container__footer">
  50. <el-button @click="router.back()">返回</el-button>
  51. <el-button v-if="!isViewMode" type="primary" @click="handleSubmit">
  52. {{ isCreateMode ? '提交' : '保存' }}
  53. </el-button>
  54. </footer>
  55. </template>
  56. <script setup lang="ts">
  57. import { computed, onMounted, ref, shallowRef, onBeforeUnmount } from 'vue';
  58. import { useRoute, useRouter } from 'vue-router';
  59. import { ElMessage } from 'element-plus';
  60. import BasicForm from '@/components/BasicForm.vue';
  61. import UploadFiles from '@/components/UploadFiles/UploadFiles.vue';
  62. import { Editor, Toolbar } from '@wangeditor/editor-for-vue';
  63. import '@wangeditor/editor/dist/css/style.css';
  64. import { useFormConfigHook } from '@/hooks/useFormConfigHook';
  65. import { DUAL_SYSTEM_FORM_CONFIG, DUAL_SYSTEM_FORM_DATA, DUAL_SYSTEM_FORM_RULES } from '../configs/form';
  66. import {
  67. queryDualSystemById,
  68. saveDualSystem,
  69. updateDualSystem,
  70. type ProductionSafetyFile,
  71. } from '@/api/production-safety-system';
  72. import type { FileItem } from '@/components/UploadFiles/types';
  73. import { formatAttachmentList } from '@/components/UploadFiles/utils';
  74. const router = useRouter();
  75. const route = useRoute();
  76. const operate = computed(() => (route.query.operate as string) || 'dual-system-create');
  77. const currentId = computed(() => Number(route.query.id));
  78. const isCreateMode = computed(() => operate.value === 'dual-system-create');
  79. const isEditMode = computed(() => operate.value === 'dual-system-edit');
  80. const isViewMode = computed(() => operate.value === 'dual-system-view');
  81. const { ruleFormData, formRules, ruleFormConfig, cloneRuleFormData, beforeRouteLeave } =
  82. useFormConfigHook(DUAL_SYSTEM_FORM_CONFIG, DUAL_SYSTEM_FORM_DATA, DUAL_SYSTEM_FORM_RULES);
  83. // 查看模式下,所有字段设为只读
  84. const viewFormConfig = ref(
  85. DUAL_SYSTEM_FORM_CONFIG.map((item) => ({
  86. ...item,
  87. componentProps: {
  88. ...item.componentProps,
  89. disabled: true,
  90. },
  91. })),
  92. );
  93. const computedFormConfig = computed(() => {
  94. if (isViewMode.value) {
  95. return viewFormConfig.value;
  96. }
  97. return ruleFormConfig.value;
  98. });
  99. const basicFormRef = ref<InstanceType<typeof BasicForm>>();
  100. // 富文本编辑器
  101. const editorRef = shallowRef();
  102. const editorConfig = computed(() => ({
  103. placeholder: '请输入文档内容',
  104. MENU_CONF: {},
  105. }));
  106. const handleEditorCreated = (editor: any) => {
  107. editorRef.value = editor;
  108. };
  109. const handleEditorChange = () => {
  110. // 编辑器内容变化时的处理
  111. };
  112. // 文件上传
  113. const handleUploadSuccess = (files: FileItem[]) => {
  114. ruleFormData.fileUrlList = files;
  115. };
  116. // 将逗号分隔的URL字符串转换为FileItem数组
  117. const convertFileUrlToFileItems = (fileUrl: string): FileItem[] => {
  118. if (!fileUrl || !fileUrl.trim()) {
  119. return [];
  120. }
  121. // 按逗号分割URL
  122. const urls = fileUrl.split(',').map(url => url.trim()).filter(url => url);
  123. return urls.map((url, index) => {
  124. // 从URL中提取文件名
  125. const urlParts = url.split('/');
  126. const fileName = urlParts[urlParts.length - 1] || `附件${index + 1}`;
  127. // 根据文件扩展名判断文件类型
  128. const extension = fileName.split('.').pop()?.toLowerCase() || '';
  129. let fileType = 'pdf';
  130. if (extension === 'doc' || extension === 'docx') {
  131. fileType = 'word';
  132. } else if (extension === 'xls' || extension === 'xlsx') {
  133. fileType = 'excel';
  134. } else if (extension === 'ppt' || extension === 'pptx') {
  135. fileType = 'ppt';
  136. }
  137. return {
  138. fileId: Date.now() + index,
  139. fileName,
  140. fileType,
  141. fileSize: '0',
  142. fileUrl: url,
  143. };
  144. });
  145. };
  146. const handleValidate = async () => {
  147. if (!basicFormRef.value) return;
  148. const res = await basicFormRef.value.validateForm();
  149. return res;
  150. };
  151. const getDetail = async () => {
  152. if (!currentId.value) return;
  153. try {
  154. const res = await queryDualSystemById(currentId.value);
  155. if (res) {
  156. // 映射接口字段到表单字段
  157. ruleFormData.fileName = res.fileName || '';
  158. ruleFormData.classifyName = res.classifyName || '';
  159. ruleFormData.fileCode = res.fileCode || '';
  160. ruleFormData.fileVersion = res.fileVersion || '';
  161. ruleFormData.fileFormat = res.fileFormat || '';
  162. ruleFormData.releaseDate = res.releaseDate || '';
  163. ruleFormData.fileUrl = res.fileUrl || '';
  164. ruleFormData.content = res.content || '';
  165. ruleFormData.status = res.status ?? 1;
  166. // 如果有文件URL,转换为FileItem格式
  167. ruleFormData.fileUrlList = convertFileUrlToFileItems(res.fileUrl || '');
  168. }
  169. cloneRuleFormData();
  170. } catch (e) {
  171. console.error('获取双体系建设详情失败:', e);
  172. ElMessage.error('获取详情失败');
  173. }
  174. };
  175. const handleSubmit = async () => {
  176. const res = await handleValidate();
  177. if (!res) return;
  178. // 验证文件上传(必填)
  179. if (!ruleFormData.fileUrlList || ruleFormData.fileUrlList.length === 0) {
  180. ElMessage.warning('请上传文件');
  181. return;
  182. }
  183. try {
  184. // 处理文件上传:先上传文件获取 URL,然后提取 fileUrl
  185. let fileUrl = '';
  186. if (ruleFormData.fileUrlList && ruleFormData.fileUrlList.length > 0) {
  187. // 分离已有URL的文件和新上传的文件
  188. const existingFiles: string[] = [];
  189. const newFiles: FileItem[] = [];
  190. ruleFormData.fileUrlList.forEach((file: FileItem) => {
  191. // 如果文件已经有 fileUrl 且没有 file 对象,说明是已有文件
  192. if (file.fileUrl && !file.file) {
  193. existingFiles.push(file.fileUrl);
  194. } else {
  195. // 否则是需要上传的新文件
  196. newFiles.push(file);
  197. }
  198. });
  199. // 上传新文件
  200. let uploadedUrls: string[] = [];
  201. if (newFiles.length > 0) {
  202. const uploadedFiles = await formatAttachmentList(newFiles);
  203. uploadedUrls = uploadedFiles
  204. .map((file: any) => file.fileUrl || file.url || '')
  205. .filter((url: string) => url);
  206. }
  207. // 合并已有URL和新上传的URL,取第一个作为fileUrl
  208. const allUrls = [...existingFiles, ...uploadedUrls].filter((url: string) => url);
  209. fileUrl = allUrls.length > 0 ? allUrls[0] : '';
  210. }
  211. const basePayload: ProductionSafetyFile = {
  212. fileName: ruleFormData.fileName,
  213. classifyName: ruleFormData.classifyName,
  214. fileCode: ruleFormData.fileCode,
  215. fileVersion: ruleFormData.fileVersion,
  216. fileFormat: ruleFormData.fileFormat,
  217. releaseDate: ruleFormData.releaseDate,
  218. fileUrl: fileUrl || undefined,
  219. content: ruleFormData.content || undefined,
  220. status: ruleFormData.status ?? 1,
  221. };
  222. if (isCreateMode.value) {
  223. await saveDualSystem(basePayload);
  224. ElMessage.success('创建成功');
  225. } else if (isEditMode.value && currentId.value) {
  226. await updateDualSystem({
  227. id: currentId.value,
  228. ...basePayload,
  229. });
  230. ElMessage.success('保存成功');
  231. }
  232. router.back();
  233. } catch (e) {
  234. console.error('保存双体系建设失败:', e);
  235. ElMessage.error('保存失败,请重试');
  236. }
  237. };
  238. onMounted(() => {
  239. cloneRuleFormData();
  240. beforeRouteLeave();
  241. if (isEditMode.value || isViewMode.value) {
  242. getDetail();
  243. }
  244. });
  245. onBeforeUnmount(() => {
  246. const editor = editorRef.value;
  247. if (editor == null) return;
  248. editor.destroy();
  249. });
  250. </script>
  251. <style scoped lang="scss">
  252. @use '@/styles/page-details-layout.scss' as *;
  253. .editor-container {
  254. width: 100%;
  255. border: 1px solid #dcdfe6;
  256. border-radius: 4px;
  257. overflow: hidden;
  258. }
  259. .content-display {
  260. min-height: 200px;
  261. padding: 12px;
  262. border: 1px solid #dcdfe6;
  263. border-radius: 4px;
  264. background-color: #f5f7fa;
  265. }
  266. .file-display {
  267. .file-link {
  268. color: #409eff;
  269. text-decoration: none;
  270. &:hover {
  271. text-decoration: underline;
  272. }
  273. }
  274. }
  275. .no-file {
  276. color: rgba(0, 0, 0, 0.65);
  277. }
  278. </style>