safetyTrainingDetail.vue 11 KB

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