| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324 |
- <template>
- <main class="safety-platform-container__main">
- <BasicForm
- ref="basicFormRef"
- :formData="ruleFormData"
- :formRules="isViewMode ? undefined : formRules"
- :formConfig="computedFormConfig"
- >
- <template #fileFormat>
- <el-radio-group v-model="ruleFormData.fileFormat" :disabled="isViewMode">
- <el-radio value="PDF">PDF</el-radio>
- <el-radio value="WORD">WORD</el-radio>
- </el-radio-group>
- </template>
- <template #fileUrl>
- <UploadFiles
- label="上传文件"
- :maxCount="1"
- :file-list="ruleFormData.fileUrlList"
- :disabled="isViewMode"
- :allow-all-file-types="true"
- @uploadSuccess="handleUploadSuccess"
- />
- </template>
- <template #content>
- <div class="editor-container">
- <Toolbar
- style="border-bottom: 1px solid #dcdfe6"
- :editor="editorRef"
- />
- <Editor
- style="height: 400px; overflow-y: auto"
- v-model="ruleFormData.content"
- mode="default"
- :defaultConfig="editorConfig"
- @on-created="handleEditorCreated"
- @on-change="handleEditorChange"
- />
- </div>
- </template>
- <template #status>
- <el-radio-group v-model="ruleFormData.status" :disabled="isViewMode">
- <el-radio :value="1">启用</el-radio>
- <el-radio :value="0">禁用</el-radio>
- </el-radio-group>
- </template>
- </BasicForm>
- </main>
- <footer class="safety-platform-container__footer">
- <el-button @click="router.back()">返回</el-button>
- <el-button v-if="!isViewMode" type="primary" @click="handleSubmit">
- {{ isCreateMode ? '提交' : '保存' }}
- </el-button>
- </footer>
- </template>
- <script setup lang="ts">
- import { computed, onMounted, ref, shallowRef, onBeforeUnmount } from 'vue';
- import { useRoute, useRouter } from 'vue-router';
- import { ElMessage } from 'element-plus';
- import BasicForm from '@/components/BasicForm.vue';
- import UploadFiles from '@/components/UploadFiles/UploadFiles.vue';
- import { Editor, Toolbar } from '@wangeditor/editor-for-vue';
- import '@wangeditor/editor/dist/css/style.css';
- import { useFormConfigHook } from '@/hooks/useFormConfigHook';
- import {
- SAFETY_STANDARDIZATION_FORM_CONFIG,
- SAFETY_STANDARDIZATION_FORM_DATA,
- SAFETY_STANDARDIZATION_FORM_RULES,
- } from '../configs/form';
- import {
- querySafetyStandardizationById,
- saveSafetyStandardization,
- updateSafetyStandardization,
- type ProductionSafetyFile,
- } from '@/api/production-safety-system';
- import type { FileItem } from '@/components/UploadFiles/types';
- import { formatAttachmentList } from '@/components/UploadFiles/utils';
- const router = useRouter();
- const route = useRoute();
- const operate = computed(() => (route.query.operate as string) || 'safety-standardization-create');
- const currentId = computed(() => Number(route.query.id));
- const isCreateMode = computed(() => operate.value === 'safety-standardization-create');
- const isEditMode = computed(() => operate.value === 'safety-standardization-edit');
- const isViewMode = computed(() => operate.value === 'safety-standardization-view');
- const { ruleFormData, formRules, ruleFormConfig, cloneRuleFormData, beforeRouteLeave } =
- useFormConfigHook(
- SAFETY_STANDARDIZATION_FORM_CONFIG,
- SAFETY_STANDARDIZATION_FORM_DATA,
- SAFETY_STANDARDIZATION_FORM_RULES,
- );
- // 查看模式下,所有字段设为只读
- const viewFormConfig = ref(
- SAFETY_STANDARDIZATION_FORM_CONFIG.map((item) => ({
- ...item,
- componentProps: {
- ...item.componentProps,
- disabled: true,
- },
- })),
- );
- const computedFormConfig = computed(() => {
- if (isViewMode.value) {
- return viewFormConfig.value;
- }
- return ruleFormConfig.value;
- });
- const basicFormRef = ref<InstanceType<typeof BasicForm>>();
-
- // 富文本编辑器
- const editorRef = shallowRef();
- const editorConfig = computed(() => ({
- placeholder: '请输入文档内容',
- MENU_CONF: {},
- }));
- const handleEditorCreated = (editor: any) => {
- editorRef.value = editor;
- };
- const handleEditorChange = () => {
- // 编辑器内容变化时的处理
- };
- // 文件上传
- const handleUploadSuccess = (files: FileItem[]) => {
- ruleFormData.fileUrlList = files;
- };
- // 将逗号分隔的URL字符串转换为FileItem数组
- const convertFileUrlToFileItems = (fileUrl: string): FileItem[] => {
- if (!fileUrl || !fileUrl.trim()) {
- return [];
- }
-
- // 按逗号分割URL
- const urls = fileUrl.split(',').map(url => url.trim()).filter(url => url);
-
- return urls.map((url, index) => {
- // 从URL中提取文件名
- const urlParts = url.split('/');
- const fileName = urlParts[urlParts.length - 1] || `附件${index + 1}`;
-
- // 根据文件扩展名判断文件类型
- const extension = fileName.split('.').pop()?.toLowerCase() || '';
- let fileType = 'pdf';
- if (extension === 'doc' || extension === 'docx') {
- fileType = 'word';
- } else if (extension === 'xls' || extension === 'xlsx') {
- fileType = 'excel';
- } else if (extension === 'ppt' || extension === 'pptx') {
- fileType = 'ppt';
- }
-
- return {
- fileId: Date.now() + index,
- fileName,
- fileType,
- fileSize: '0',
- fileUrl: url,
- };
- });
- };
- const handleValidate = async () => {
- if (!basicFormRef.value) return;
- const res = await basicFormRef.value.validateForm();
- return res;
- };
- const getDetail = async () => {
- if (!currentId.value) return;
- try {
- const res = await querySafetyStandardizationById(currentId.value);
- if (res) {
- // 映射接口字段到表单字段
- ruleFormData.fileName = res.fileName || '';
- ruleFormData.classifyName = res.classifyName || '';
- ruleFormData.fileCode = res.fileCode || '';
- ruleFormData.fileVersion = res.fileVersion || '';
- ruleFormData.fileFormat = res.fileFormat || '';
- ruleFormData.releaseDate = res.releaseDate || '';
- ruleFormData.fileUrl = res.fileUrl || '';
- ruleFormData.content = res.content || '';
- ruleFormData.status = res.status ?? 1;
-
- // 如果有文件URL,转换为FileItem格式
- ruleFormData.fileUrlList = convertFileUrlToFileItems(res.fileUrl || '');
- }
- cloneRuleFormData();
- } catch (e) {
- console.error('获取安全标准化体系建设详情失败:', e);
- ElMessage.error('获取详情失败');
- }
- };
- const handleSubmit = async () => {
- const res = await handleValidate();
- if (!res) return;
-
- // 验证文件上传(必填)
- if (!ruleFormData.fileUrlList || ruleFormData.fileUrlList.length === 0) {
- ElMessage.warning('请上传文件');
- return;
- }
-
- try {
- // 处理文件上传:先上传文件获取 URL,然后提取 fileUrl
- let fileUrl = '';
- if (ruleFormData.fileUrlList && ruleFormData.fileUrlList.length > 0) {
- // 分离已有URL的文件和新上传的文件
- const existingFiles: string[] = [];
- const newFiles: FileItem[] = [];
-
- ruleFormData.fileUrlList.forEach((file: FileItem) => {
- // 如果文件已经有 fileUrl 且没有 file 对象,说明是已有文件
- if (file.fileUrl && !file.file) {
- existingFiles.push(file.fileUrl);
- } else {
- // 否则是需要上传的新文件
- newFiles.push(file);
- }
- });
- // 上传新文件
- let uploadedUrls: string[] = [];
- if (newFiles.length > 0) {
- const uploadedFiles = await formatAttachmentList(newFiles);
- uploadedUrls = uploadedFiles
- .map((file: any) => file.fileUrl || file.url || '')
- .filter((url: string) => url);
- }
- // 合并已有URL和新上传的URL,取第一个作为fileUrl
- const allUrls = [...existingFiles, ...uploadedUrls].filter((url: string) => url);
- fileUrl = allUrls.length > 0 ? allUrls[0] : '';
- }
- const basePayload: ProductionSafetyFile = {
- fileName: ruleFormData.fileName,
- classifyName: ruleFormData.classifyName,
- fileCode: ruleFormData.fileCode,
- fileVersion: ruleFormData.fileVersion,
- fileFormat: ruleFormData.fileFormat,
- releaseDate: ruleFormData.releaseDate,
- fileUrl: fileUrl || undefined,
- content: ruleFormData.content || undefined,
- status: ruleFormData.status ?? 1,
- };
- if (isCreateMode.value) {
- await saveSafetyStandardization(basePayload);
- ElMessage.success('创建成功');
- } else if (isEditMode.value && currentId.value) {
- await updateSafetyStandardization({
- id: currentId.value,
- ...basePayload,
- });
- ElMessage.success('保存成功');
- }
- router.back();
- } catch (e) {
- console.error('保存安全标准化体系建设失败:', e);
- ElMessage.error('保存失败,请重试');
- }
- };
- onMounted(() => {
- cloneRuleFormData();
- beforeRouteLeave();
- if (isEditMode.value || isViewMode.value) {
- getDetail();
- }
- });
- onBeforeUnmount(() => {
- const editor = editorRef.value;
- if (editor == null) return;
- editor.destroy();
- });
- </script>
- <style scoped lang="scss">
- @use '@/styles/page-details-layout.scss' as *;
- .editor-container {
- width: 100%;
- border: 1px solid #dcdfe6;
- border-radius: 4px;
- overflow: hidden;
- }
- .content-display {
- min-height: 200px;
- padding: 12px;
- border: 1px solid #dcdfe6;
- border-radius: 4px;
- background-color: #f5f7fa;
- }
- .file-display {
- .file-link {
- color: #409eff;
- text-decoration: none;
- &:hover {
- text-decoration: underline;
- }
- }
- }
- .no-file {
- color: rgba(0, 0, 0, 0.65);
- }
- </style>
|