|
|
|
@ -11,8 +11,9 @@ |
|
|
|
v-model="step.type" @change="onTypeChange(index)" /> |
|
|
|
|
|
|
|
<!-- 根据步骤类型显示对应的表单 --> |
|
|
|
<component :is="getStepComponent(step.type)" :step-data="step.formData" |
|
|
|
@update="onFormUpdate(index, $event)"> |
|
|
|
<!-- 根据步骤类型显示对应的表单 --> |
|
|
|
<component :is="getStepComponent(step.type)" :formData="step.formData" |
|
|
|
@update="onFormUpdate(index, $event)" :ref="'stepCompRef_' + index"> |
|
|
|
</component> |
|
|
|
<div class="step-header-item"> |
|
|
|
<el-button type="text" @click="removeStep(index)" icon="el-icon-delete" |
|
|
|
@ -25,12 +26,10 @@ |
|
|
|
</template> |
|
|
|
|
|
|
|
<script> |
|
|
|
import ContainerStep from './StepComponents/ContainerStep.vue' |
|
|
|
import HandleFormItem from './HandleFormItem.vue'; |
|
|
|
import Czdd from './StepComponents/ry/czdd.vue';//溶液-操作地点 |
|
|
|
|
|
|
|
const stepTypes = [ |
|
|
|
{ label: '容器选择', value: 'container' }, |
|
|
|
{ label: '操作地点', value: 'czdd' }, |
|
|
|
]; |
|
|
|
|
|
|
|
@ -57,14 +56,12 @@ export default { |
|
|
|
}, |
|
|
|
components: { |
|
|
|
HandleFormItem, |
|
|
|
ContainerStep, |
|
|
|
Czdd, |
|
|
|
}, |
|
|
|
computed: { |
|
|
|
stepComponentMap() { |
|
|
|
if (!this.componentMap) { |
|
|
|
this.componentMap = { |
|
|
|
'container': 'ContainerStep', |
|
|
|
'czdd': 'Czdd', |
|
|
|
} |
|
|
|
} |
|
|
|
@ -86,24 +83,19 @@ export default { |
|
|
|
// } |
|
|
|
}, |
|
|
|
watch: { |
|
|
|
steps: { |
|
|
|
handler(newVal) { |
|
|
|
this.$emit('input', newVal.map(step => ({ |
|
|
|
type: step.type, |
|
|
|
formData: step.formData |
|
|
|
}))) |
|
|
|
}, |
|
|
|
deep: true |
|
|
|
}, |
|
|
|
'formData': { |
|
|
|
// steps: { |
|
|
|
// handler(newVal) { |
|
|
|
// this.$emit('input', newVal.map(step => ({ |
|
|
|
// type: step.type, |
|
|
|
// formData: step.formData |
|
|
|
// }))) |
|
|
|
// }, |
|
|
|
// deep: true |
|
|
|
// }, |
|
|
|
formData: { |
|
|
|
handler(newVal) { |
|
|
|
console.log('stepValue', newVal) |
|
|
|
if (!newVal || newVal.length === 0) return |
|
|
|
this.steps = newVal.map((step) => ({ |
|
|
|
id: this.stepId++, |
|
|
|
type: step.type || '', |
|
|
|
formData: step |
|
|
|
})) |
|
|
|
this.steps = newVal; |
|
|
|
}, |
|
|
|
deep: true, |
|
|
|
immediate: true |
|
|
|
@ -149,8 +141,6 @@ export default { |
|
|
|
}, |
|
|
|
|
|
|
|
onFormUpdate(stepIndex, formData) { |
|
|
|
console.log('stepIndex', stepIndex) |
|
|
|
console.log('formData', formData) |
|
|
|
this.steps[stepIndex].formData = formData |
|
|
|
}, |
|
|
|
|
|
|
|
@ -161,15 +151,7 @@ export default { |
|
|
|
|
|
|
|
// 公共方法:获取所有步骤数据 |
|
|
|
getFormData() { |
|
|
|
return new Promise((resolve, reject) => { |
|
|
|
// 校验所有步骤是否填写完整 |
|
|
|
const validation = this.validateSteps() |
|
|
|
if (!validation.isValid) { |
|
|
|
// this.$message.error(validation.errors[0]) |
|
|
|
reject(validation.errors[0]) |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
return new Promise(async (resolve, reject) => { |
|
|
|
// 检查是否有步骤数据 |
|
|
|
if (this.steps.length === 0) { |
|
|
|
// this.$message.error(this.$t('template.common.addStepError')) |
|
|
|
@ -177,20 +159,42 @@ export default { |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
const stepData = this.steps.map(step => ({ |
|
|
|
type: step.type, |
|
|
|
...step.formData |
|
|
|
})) |
|
|
|
resolve({ stepData }) |
|
|
|
try { |
|
|
|
const stepData = await Promise.all( |
|
|
|
this.steps.map(async (step, index) => { |
|
|
|
const stepComponentRef = this.$refs[`stepCompRef_${index}`]; |
|
|
|
if (stepComponentRef && stepComponentRef.length > 0) { |
|
|
|
try { |
|
|
|
const stepFormData = await stepComponentRef[0].getFormData(); |
|
|
|
return { type: step.type, formData: stepFormData } |
|
|
|
} catch (error) { |
|
|
|
// 如果某个步骤的getFormData方法失败,抛出错误 |
|
|
|
throw error; |
|
|
|
} |
|
|
|
} else { |
|
|
|
// 如果没有找到组件引用,返回原始数据 |
|
|
|
return { type: step.type, formData: step.formData } |
|
|
|
} |
|
|
|
}) |
|
|
|
); |
|
|
|
resolve({ stepData }); |
|
|
|
} catch (error) { |
|
|
|
reject(error); |
|
|
|
} |
|
|
|
}) |
|
|
|
}, |
|
|
|
|
|
|
|
// 直接获取表单数据,不做校验 |
|
|
|
getFilledFormData() { |
|
|
|
const stepData = this.steps.map(step => ({ |
|
|
|
type: step.type, |
|
|
|
...step.formData |
|
|
|
})) |
|
|
|
const stepData = this.steps.map((step, index) => { |
|
|
|
const stepComponentRef = this.$refs[`stepCompRef_${index}`]; |
|
|
|
if(stepComponentRef && stepComponentRef.length > 0){ |
|
|
|
const stepFormData = this.$refs[`stepCompRef_${index}`][0]?.getFilledFormData(); |
|
|
|
return { type: step.type, formData: stepFormData } |
|
|
|
}else{ |
|
|
|
return { type: step.type, formData: step.formData } |
|
|
|
} |
|
|
|
}) |
|
|
|
return { stepData } |
|
|
|
}, |
|
|
|
|
|
|
|
@ -227,14 +231,28 @@ export default { |
|
|
|
}, |
|
|
|
|
|
|
|
// 公共方法:验证所有步骤 |
|
|
|
validateSteps() { |
|
|
|
async validateSteps() { |
|
|
|
const errors = [] |
|
|
|
this.steps.forEach((step, index) => { |
|
|
|
for (let index = 0; index < this.steps.length; index++) { |
|
|
|
const step = this.steps[index]; |
|
|
|
|
|
|
|
if (!step.type) { |
|
|
|
errors.push(`步骤 ${index + 1}: 请选择步骤类型`) |
|
|
|
continue; |
|
|
|
} |
|
|
|
// 可以在这里添加更具体的表单验证逻辑 |
|
|
|
}) |
|
|
|
|
|
|
|
// 获取当前步骤的组件实例 |
|
|
|
const stepComponentRef = this.$refs[`stepCompRef_${index}`]; |
|
|
|
if (stepComponentRef && stepComponentRef.length > 0) { |
|
|
|
try { |
|
|
|
// 调用子组件的getFormData方法进行验证(不抛出错误,只验证) |
|
|
|
await stepComponentRef[0].validateAndMarkRed(); |
|
|
|
} catch (error) { |
|
|
|
// validateAndMarkRed方法不应该抛出错误,但如果有的话捕获它 |
|
|
|
console.error(`步骤 ${index + 1} 验证时出错:`, error); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
return { |
|
|
|
isValid: errors.length === 0, |
|
|
|
errors |
|
|
|
|