From b3f4499793fa7b21f6c5d1e099d6ed170ecbe47a Mon Sep 17 00:00:00 2001 From: 董国庆 <364620639@qq.com> Date: 星期四, 15 五月 2025 16:38:45 +0800 Subject: [PATCH] 实验方案管理 --- laboratory/src/views/dataManagement/schemeManagement/addPlan.vue | 609 ++++++++++++++++++++++++------------------------------- 1 files changed, 267 insertions(+), 342 deletions(-) diff --git a/laboratory/src/views/dataManagement/schemeManagement/addPlan.vue b/laboratory/src/views/dataManagement/schemeManagement/addPlan.vue index 969c483..ef3538f 100644 --- a/laboratory/src/views/dataManagement/schemeManagement/addPlan.vue +++ b/laboratory/src/views/dataManagement/schemeManagement/addPlan.vue @@ -70,12 +70,12 @@ </div> <div class="content-box"> <div class="content-box-left"> - <div>项目课题方案名称:{{ groupTableData[0].projectName }}</div> - <div>实验编号:{{ groupTableData[0].experimentCode }}</div> + <div>项目课题方案名称:{{ groupTableData && groupTableData.length > 0 ? groupTableData[0].projectName : '' }}</div> + <div>实验编号:{{ groupTableData && groupTableData.length > 0 ? groupTableData[0].experimentCode : '' }}</div> </div> <div class="content-box-right"> - <div>项目课题方案编号: {{ groupTableData[0].projectCode }}</div> - <div>实验名称: {{ groupTableData[0].experimentName }}</div> + <div>项目课题方案编号: {{ groupTableData && groupTableData.length > 0 ? groupTableData[0].projectCode : '' }}</div> + <div>实验名称: {{ groupTableData && groupTableData.length > 0 ? groupTableData[0].experimentName : '' }}</div> </div> </div> </div> @@ -250,6 +250,19 @@ editId: null, // 编辑的ID viewMaterialData: [], // 查看模式的材料数据 viewEquipmentData: [], // 查看模式的设备数据 + // 状态映射表 + statusTypeMap: { + "-1": "info", + "1": "warning", + "2": "success", + "3": "info" + }, + statusTextMap: { + "-1": "草稿箱", + "1": "待确认", + "2": "已确认", + "3": "已封存" + } }; }, async created() { @@ -261,182 +274,39 @@ } }, methods: { + // ===== 人员相关方法 ===== addMember() { this.$refs.selectMember.open(this.participantsData, []); }, - handleMaterialSubmit(data) { - // 处理实验材料数据 - this.form.experimentMaterial = data; + handleMemberSubmit(selectedMembers) { + this.selectedParticipants = selectedMembers; + this.$refs.selectMember.close(); }, - handleEquipmentSubmit(data) { - // 处理实验设备数据 - this.form.experimentDevice = data; + handleEditMember() { + this.$refs.selectMember.open(this.participantsData, this.selectedParticipants); }, - handleSave() { - // 先获取所有动态组件的数据 - this.$refs.materialComponent.submit(); - this.$refs.equipmentComponent.submit(); - - // 获取所有步骤内容 - 添加错误检查 - this.stepList.forEach((step, index) => { - const stepContentRef = this.$refs['stepContent' + index]; - console.log('stepContentRef', stepContentRef) - const editor = Array.isArray(stepContentRef) ? stepContentRef[0] : stepContentRef; - if (editor && typeof editor.submit === 'function') { - editor.submit(); - } - }); - console.log('材料数据', this.form.experimentMaterial) - console.log('设备数据', this.form.experimentDevice) - console.log('步骤数据', this.form.stepList) - - // 然后进行表单校验 - this.$refs.form.validate((valid) => { - if (valid && this.validateContent()) { - // 构建提交数据 - const formData = { - ...this.form, - // 实验日期,使用 moment 格式化为指定格式 - experimentDate: moment(this.form.experimentDate).format('YYYY-MM-DD HH:mm:ss'), - // 实验调度ID,从选中的调度数据中获取 - dispatchId: this.groupTableData[0]?.id, - // 实验设备,转换为JSON字符串 - experimentDevice: this.form.experimentDevice, - // 实验材料,转换为JSON字符串 - experimentMaterial: this.form.experimentMaterial, - // 实验目的,从富文本编辑器获取内容 - experimentObjective: this.$refs.purposeEditor.getContent(), - // 工艺参数及路线,从富文本编辑器获取内容 - experimentParamRoute: this.$refs.processEditor.getContent(), - // 实验方案人员,包含用户ID、昵称和角色类型 - experimentSchemePersons: this.selectedParticipants.map(person => ({ - userId: person.userId, // 用户ID - nickName: person.nickName, // 用户昵称 - roleType: person.roleType, // 角色类型 - commitTime: moment().format('YYYY-MM-DD HH:mm:ss'), - })), - // 实验步骤记录,转换为JSON字符串,包含步骤名称和内容 - experimentStepRecord: this.stepList.map(step => ({ - stepName: step.stepName, // 步骤名称 - content: step.content // 步骤内容 - })), - // 状态:1=已发送 - status: 1, - // 提交时间,使用 moment 格式化为指定格式 - commitTime: moment().format('YYYY-MM-DD HH:mm:ss'), - }; - console.log('formData 提交数据', formData) - - // 调用添加接口 - add(formData).then(res => { - if (res.code === 200) { - this.$message.success('保存成功'); - this.$router.go(-1) - // 可以在这里添加跳转逻辑 - } else { - this.$message.error(res.msg || '保存失败'); - } - }).catch(err => { - this.$message.error('保存失败'); - console.error('保存失败:', err); - }); - } else { - // 获取第一个错误字段 - const firstError = this.$refs.form.fields.find(field => field.validateState === 'error'); - if (firstError) { - // 滚动到错误字段 - this.$nextTick(() => { - firstError.$el.scrollIntoView({ behavior: 'smooth', block: 'center' }); - }); - } - } - }); - }, - handleSaveDraft() { - // 先获取所有动态组件的数据 - this.$refs.materialComponent.submit(); - this.$refs.equipmentComponent.submit(); - - // 获取所有步骤内容 - 添加错误检查 - this.stepList.forEach((step, index) => { - const stepContentRef = this.$refs['stepContent' + index]; - if (stepContentRef && typeof stepContentRef.submit === 'function') { - stepContentRef.submit(); - } - }); - - // 然后进行表单校验 - this.$refs.form.validate((valid) => { - if (valid && this.validateContent()) { - // 构建草稿数据 - const formData = { - ...this.form, - // 实验日期,使用 moment 格式化为指定格式 - experimentDate: moment(this.form.experimentDate).format('YYYY-MM-DD HH:mm:ss'), - // 实验调度ID,从选中的调度数据中获取 - dispatchId: this.groupTableData[0]?.id, - // 实验设备,转换为JSON字符串 - experimentDevice: JSON.stringify(this.form.experimentDevice), - // 实验材料,转换为JSON字符串 - experimentMaterial: JSON.stringify(this.form.experimentMaterial), - // 实验目的,从富文本编辑器获取内容 - experimentObjective: this.$refs.purposeEditor.getContent(), - // 工艺参数及路线,从富文本编辑器获取内容 - experimentParamRoute: this.$refs.processEditor.getContent(), - // 实验方案人员,包含用户ID、昵称和角色类型 - experimentSchemePersons: this.selectedParticipants.map(person => ({ - userId: person.userId, // 用户ID - nickName: person.nickName, // 用户昵称 - roleType: person.roleType // 角色类型 - })), - // 实验步骤记录,转换为JSON字符串,包含步骤名称和内容 - experimentStepRecord: JSON.stringify(this.stepList.map(step => ({ - stepName: step.stepName, // 步骤名称 - content: step.content // 步骤内容 - }))), - // 状态:-1=草稿箱 - status: -1, - // 提交时间,使用 moment 格式化为指定格式 - commitTime: moment().format('YYYY-MM-DD HH:mm:ss'), - }; - - // 调用添加接口 - add(formData).then(res => { - if (res.code === 200) { - this.$message.success('草稿保存成功'); - // 可以在这里添加跳转逻辑 - } else { - this.$message.error(res.msg || '草稿保存失败'); - } - }).catch(err => { - this.$message.error('草稿保存失败'); - console.error('草稿保存失败:', err); - }); - } else { - // 获取第一个错误字段 - const firstError = this.$refs.form.fields.find(field => field.validateState === 'error'); - if (firstError) { - // 滚动到错误字段 - this.$nextTick(() => { - firstError.$el.scrollIntoView({ behavior: 'smooth', block: 'center' }); - }); - } - } - }); - }, + + // ===== 步骤相关方法 ===== handleAddStep() { this.$refs.addStepDialog.open(); }, handleStepSubmit(stepData) { if (this.editingStepIndex > -1) { + // 编辑现有步骤 this.stepList[this.editingStepIndex].stepName = stepData.stepName; this.editingStepIndex = -1; } else { + // 添加新步骤 this.stepList.push({ stepName: stepData.stepName, content: null, }); } + }, + handleEditStep(index) { + this.editingStepIndex = index; + this.$refs.addStepDialog.open(true); + this.$refs.addStepDialog.setStepName(this.stepList[index].stepName); }, handleDeleteStep(index) { this.$confirm("确认删除该步骤吗?删除后步骤内容将无法恢复", "警告", { @@ -451,14 +321,108 @@ }) .catch(() => { }); }, - handleEditStep(index) { - this.editingStepIndex = index; - this.$refs.addStepDialog.open(true); - this.$refs.addStepDialog.setStepName(this.stepList[index].stepName); - }, handleStepContentSubmit(index, content) { - console.log('步骤内容', content) this.stepList[index].content = content; + }, + + // ===== 材料设备相关方法 ===== + handleMaterialSubmit(data) { + this.form.experimentMaterial = data; + }, + handleEquipmentSubmit(data) { + this.form.experimentDevice = data; + }, + + // ===== 保存提交相关方法 ===== + handleSave() { + this.submitData(1); + }, + handleSaveDraft() { + this.submitData(-1); + }, + submitData(status) { + // 先获取所有动态组件的数据 + this.$refs.materialComponent.submit(); + this.$refs.equipmentComponent.submit(); + + // 获取所有步骤内容 + const stepContentRefs = Object.keys(this.$refs) + .filter(key => key.startsWith('stepContent')) + .map(key => this.$refs[key]); + + stepContentRefs.forEach((ref) => { + const editor = Array.isArray(ref) ? ref[0] : ref; + if (editor && typeof editor.submit === 'function') { + editor.submit(); + } + }); + + // 进行表单校验 + this.$refs.form.validate((valid) => { + if (valid && this.validateContent()) { + // 获取富文本编辑器内容 + const experimentObjective = this.$refs.purposeEditor.getContent(); + const experimentParamRoute = this.$refs.processEditor.getContent(); + + // 构建实验步骤记录数据 + const experimentStepRecord = this.stepList.map(step => ({ + stepName: step.stepName, + content: step.content + })); + + // 构建提交数据 + const formData = { + ...this.form, + experimentDate: moment(this.form.experimentDate).format('YYYY-MM-DD HH:mm:ss'), + dispatchId: this.groupTableData && this.groupTableData.length > 0 ? this.groupTableData[0]?.id : '', + experimentObjective, + experimentParamRoute, + experimentSchemePersons: this.selectedParticipants.map(person => ({ + userId: person.userId, + nickName: person.nickName, + roleType: person.roleType, + commitTime: moment().format('YYYY-MM-DD HH:mm:ss'), + })), + status, + commitTime: moment().format('YYYY-MM-DD HH:mm:ss'), + }; + + // 统一转换JSON字符串 + formData.experimentStepRecord = JSON.stringify(experimentStepRecord); + formData.experimentDevice = JSON.stringify(this.form.experimentDevice); + formData.experimentMaterial = JSON.stringify(this.form.experimentMaterial); + + // 编辑模式下添加id参数 + if (this.isEdit && this.editId) { + formData.id = this.editId; + } + + // 根据是否为编辑模式调用不同接口 + const apiCall = this.isEdit ? update(formData) : add(formData); + + apiCall.then(res => { + if (res.code === 200) { + this.$message.success(status === 1 ? '保存成功' : '草稿保存成功'); + if (status === 1) { + this.$router.go(-1); + } + } else { + this.$message.error(res.msg || (status === 1 ? '保存失败' : '草稿保存失败')); + } + }).catch(err => { + this.$message.error(status === 1 ? '保存失败' : '草稿保存失败'); + console.error(status === 1 ? '保存失败:' : '草稿保存失败:', err); + }); + } else { + // 获取第一个错误字段并滚动到该位置 + const firstError = this.$refs.form.fields.find(field => field.validateState === 'error'); + if (firstError) { + this.$nextTick(() => { + firstError.$el.scrollIntoView({ behavior: 'smooth', block: 'center' }); + }); + } + } + }); }, getAllEditorContent() { return { @@ -467,37 +431,37 @@ }; }, validateContent() { - // // 校验实验调度 - // if (!this.groupTableData || this.groupTableData.length === 0) { - // this.$message.error("请选择实验调度"); - // return false; - // } + // 校验实验调度 + if (!this.groupTableData || this.groupTableData.length === 0) { + this.$message.error("请选择实验调度"); + return false; + } - // // 校验实验日期 - // if (!this.form.experimentDate) { - // this.$message.error("请填写实验日期"); - // return false; - // } + // 校验实验日期 + if (!this.form.experimentDate) { + this.$message.error("请填写实验日期"); + return false; + } - // // 校验参与人员 - // if (!this.selectedParticipants || this.selectedParticipants.length === 0) { - // this.$message.error("请选择参与人员"); - // return false; - // } + // 校验参与人员 + if (!this.selectedParticipants || this.selectedParticipants.length === 0) { + this.$message.error("请选择参与人员"); + return false; + } - // // 校验实验目的 - // const purpose = this.$refs.purposeEditor.getContent(); - // if (!purpose || purpose === '<p></p>' || purpose.trim() === '<p></p>') { - // this.$message.error("请填写实验目的"); - // return false; - // } + // 校验实验目的 + const purpose = this.$refs.purposeEditor.getContent(); + if (!purpose || purpose === '<p></p>' || purpose.trim() === '<p></p>') { + this.$message.error("请填写实验目的"); + return false; + } - // // 校验工艺参数及路线 - // const process = this.$refs.processEditor.getContent(); - // if (!process || process === '<p></p>' || process.trim() === '<p></p>') { - // this.$message.error("请填写工艺参数及路线"); - // return false; - // } + // 校验工艺参数及路线 + const process = this.$refs.processEditor.getContent(); + if (!process || process === '<p></p>' || process.trim() === '<p></p>') { + this.$message.error("请填写工艺参数及路线"); + return false; + } // 校验实验材料 if (!this.form.experimentMaterial) { @@ -518,11 +482,10 @@ } // 校验每个步骤是否都有内容 - for (let i = 0; i < this.stepList.length; i++) { - if (!this.stepList[i].content) { - this.$message.error(`请完善第${i + 1}个步骤的内容`); - return false; - } + const invalidStep = this.stepList.findIndex(step => !step.content); + if (invalidStep !== -1) { + this.$message.error(`请完善第${invalidStep + 1}个步骤的内容`); + return false; } return true; @@ -531,26 +494,14 @@ this.$router.push("/dataManagement/scheme-management/stop-experiment"); }, getStatusType(status) { - const statusMap = { - "-1": "info", - "1": "warning", - "2": "success", - "3": "info" - }; - return statusMap[status] || "info"; + return this.statusTypeMap[status] || "info"; }, getStatusText(status) { - const statusMap = { - "-1": "草稿箱", - "1": "待确认", - "2": "已确认", - "3": "已封存" - }; - return statusMap[status] || "未知"; + return this.statusTextMap[status] || "未知"; }, handleSchedulingSubmit(data) { - this.groupTableData = data; - if (data && data.length > 0) { + this.groupTableData = data || []; + if (data && data.length > 0 && data[0].id) { getGroupByDispatchId({ dispatchId: data[0].id }).then(res => { if (res) { this.groupData = res || []; @@ -577,141 +528,115 @@ handleSchedulingClose() { this.showScheduling = false; }, - handleMemberSubmit(selectedMembers) { - this.selectedParticipants = selectedMembers; - this.$refs.selectMember.close(); - }, - handleEditMember() { - this.$refs.selectMember.open(this.participantsData, this.selectedParticipants); - }, - // 加载编辑数据 + // ===== 数据加载方法 ===== async loadEditData() { try { const res = await getDetail({ id: this.editId }); - console.log('编辑数据', res); - if (res) { - const data = res; - - // 填充基本表单数据 - this.form.experimentDate = data.experimentDate; - - // 填充实验调度信息 - if (data.experimentDispatch && data.experimentDispatch.id) { - this.form.dispatchId = data.experimentDispatch.id; - - // 获取组别信息 - try { - const groupRes = await getGroupByDispatchId({ dispatchId: data.experimentDispatch.id }); - if (groupRes) { - this.groupData = groupRes || []; - } - } catch (err) { - console.error('获取组别列表失败:', err); - } - - // 构建调度表格数据 - this.groupTableData = [{ ...data.experimentDispatch }]; - } - - // 填充参与人员 - if (data.experimentSchemePersons) { - this.selectedParticipants = Array.isArray(data.experimentSchemePersons) - ? data.experimentSchemePersons - : JSON.parse(data.experimentSchemePersons || '[]'); - } - - // 填充富文本编辑器内容 - this.editorContents.purpose = data.experimentObjective || ''; - this.editorContents.process = data.experimentParamRoute || ''; - - // 填充实验材料和设备(编辑模式下转换为查看格式) - if (data.experimentMaterial) { - try { - const materialData = typeof data.experimentMaterial === 'string' - ? JSON.parse(data.experimentMaterial) - : data.experimentMaterial; - this.form.experimentMaterial = materialData; - - } catch (err) { - console.error('解析实验材料数据失败:', err); - this.viewMaterialData = []; - } - } - - if (data.experimentDevice) { - try { - const deviceData = typeof data.experimentDevice === 'string' - ? JSON.parse(data.experimentDevice) - : data.experimentDevice; - this.form.experimentDevice = deviceData; - - } catch (err) { - console.error('解析实验设备数据失败:', err); - this.viewEquipmentData = []; - } - } - - // 填充实验步骤(编辑模式下步骤内容也要转换为查看格式) - if (data.experimentStepRecord) { - try { - const stepsData = typeof data.experimentStepRecord === 'string' - ? JSON.parse(data.experimentStepRecord) - : data.experimentStepRecord; - - this.stepList = (stepsData || []).map(step => ({ - stepName: step.stepName, - // 编辑模式下步骤内容转换为查看格式 - content: step.content - })); - } catch (err) { - console.error('解析实验步骤数据失败:', err); - this.stepList = []; - } - } - - // 等待组件渲染完成后设置编辑器内容 - this.$nextTick(() => { - // 设置富文本编辑器内容 - if (this.$refs.purposeEditor) { - this.$refs.purposeEditor.setContent(this.editorContents.purpose); - } - if (this.$refs.processEditor) { - this.$refs.processEditor.setContent(this.editorContents.process); - } - - // 新增模式下设置动态组件的初始数据 - if (!this.isEdit) { - if (this.$refs.materialComponent && this.form.experimentMaterial) { - this.$refs.materialComponent.setInitialData(this.form.experimentMaterial); - } - if (this.$refs.equipmentComponent && this.form.experimentDevice) { - this.$refs.equipmentComponent.setInitialData(this.form.experimentDevice); - } - - // 设置步骤内容的初始数据 - this.stepList.forEach((step, index) => { - const stepContentRef = this.$refs['stepContent' + index]; - if (stepContentRef && step.content) { - const editor = Array.isArray(stepContentRef) ? stepContentRef[0] : stepContentRef; - if (editor && typeof editor.setInitialData === 'function') { - editor.setInitialData(step.content); - } - } - }); - } - }); - - } else { - this.$message.error(res.msg || '获取详情失败'); - // this.$router.go(-1); + if (!res) { + this.$message.error('获取详情失败'); + return; } + + console.log('编辑数据', res); + const data = res; + + // 填充基本表单数据 + this.form.experimentDate = data.experimentDate; + + // 填充实验调度信息 + if (data.experimentDispatch?.id) { + this.form.dispatchId = data.experimentDispatch.id; + this.groupTableData = [{ ...data.experimentDispatch }]; + + // 获取组别信息 + try { + const groupRes = await getGroupByDispatchId({ dispatchId: data.experimentDispatch.id }); + this.groupData = groupRes || []; + } catch (err) { + console.error('获取组别列表失败:', err); + } + } + + // 填充参与人员 + this.selectedParticipants = Array.isArray(data.experimentSchemePersons) + ? data.experimentSchemePersons + : JSON.parse(data.experimentSchemePersons || '[]'); + + // 填充富文本编辑器内容 + this.editorContents.purpose = data.experimentObjective || ''; + this.editorContents.process = data.experimentParamRoute || ''; + + // 填充实验材料和设备 + try { + this.form.experimentMaterial = typeof data.experimentMaterial === 'string' + ? JSON.parse(data.experimentMaterial) + : data.experimentMaterial; + } catch (err) { + console.error('解析实验材料数据失败:', err); + this.form.experimentMaterial = []; + } + + try { + this.form.experimentDevice = typeof data.experimentDevice === 'string' + ? JSON.parse(data.experimentDevice) + : data.experimentDevice; + } catch (err) { + console.error('解析实验设备数据失败:', err); + this.form.experimentDevice = []; + } + + // 填充实验步骤 + try { + const stepsData = typeof data.experimentStepRecord === 'string' + ? JSON.parse(data.experimentStepRecord) + : data.experimentStepRecord; + + this.stepList = (stepsData || []).map(step => ({ + stepName: step.stepName, + content: step.content + })); + } catch (err) { + console.error('解析实验步骤数据失败:', err); + this.stepList = []; + } + + // 等待组件渲染完成后设置编辑器内容 + this.$nextTick(() => { + // 设置富文本编辑器内容 + if (this.$refs.purposeEditor) { + this.$refs.purposeEditor.setContent(this.editorContents.purpose); + } + if (this.$refs.processEditor) { + this.$refs.processEditor.setContent(this.editorContents.process); + } + + // 设置动态组件的初始数据 + if (!this.isEdit) { + if (this.$refs.materialComponent && this.form.experimentMaterial) { + this.$refs.materialComponent.setInitialData(this.form.experimentMaterial); + } + if (this.$refs.equipmentComponent && this.form.experimentDevice) { + this.$refs.equipmentComponent.setInitialData(this.form.experimentDevice); + } + + // 设置步骤内容的初始数据 + this.stepList.forEach((step, index) => { + const stepContentRef = this.$refs['stepContent' + index]; + if (stepContentRef && step.content) { + const editor = Array.isArray(stepContentRef) ? stepContentRef[0] : stepContentRef; + if (editor?.setInitialData) { + editor.setInitialData(step.content); + } + } + }); + } + }); + } catch (error) { this.$message.error('获取详情失败'); console.error('获取详情失败:', error); - // this.$router.go(-1); } }, - // 转换数据格式为ViewDynamicComponent需要的格式 convertToViewFormat(data) { if (!data || !Array.isArray(data)) return []; -- Gitblit v1.7.1