Commit c54f3162 by chenwq1

refactor: 优化表单验证与保存逻辑

- 移除 `validate` 和 `saveData` 函数中的调试日志。
- 更新 `CustomizeFormValidateResult` 接口,简化数据结构,新增 `CustomizeFormValidateNode` 接口以更好地描述验证节点。
- 修改 `leafRecordFromValidateData` 函数为 `leafRecordFromValidateNode`,以适应新的数据结构。
1 parent d61218e2
Pipeline #98 for c54f3162 failed in 0 seconds
...@@ -95,7 +95,6 @@ function setSubmitData() { ...@@ -95,7 +95,6 @@ function setSubmitData() {
async function validate(): Promise<CustomizeFormValidateResult> { async function validate(): Promise<CustomizeFormValidateResult> {
const nodesData = setSubmitData(); const nodesData = setSubmitData();
console.log('validate', nodesData);
let success = true; let success = true;
let message = ''; let message = '';
columnsList.value.forEach((item: any) => { columnsList.value.forEach((item: any) => {
...@@ -114,7 +113,6 @@ async function validate(): Promise<CustomizeFormValidateResult> { ...@@ -114,7 +113,6 @@ async function validate(): Promise<CustomizeFormValidateResult> {
/** 与 `validate` 相同返回结构,不校验必填,用于静默保存 / 上一步等场景 */ /** 与 `validate` 相同返回结构,不校验必填,用于静默保存 / 上一步等场景 */
async function saveData(): Promise<CustomizeFormValidateResult> { async function saveData(): Promise<CustomizeFormValidateResult> {
const nodesData = setSubmitData(); const nodesData = setSubmitData();
console.log('saveData', nodesData);
return { return {
data: nodesData, data: nodesData,
message: "操作成功", message: "操作成功",
......
...@@ -42,15 +42,18 @@ export interface CustomizeFormValidateFieldItem { ...@@ -42,15 +42,18 @@ export interface CustomizeFormValidateFieldItem {
valueText?: string; valueText?: string;
} }
/** 中间表单 validate / saveData 返回体 */ /** 中间表单 validate / saveData 下单个叶子节点(与 `setSubmitData` 一项一致) */
export interface CustomizeFormValidateResult { export interface CustomizeFormValidateNode {
success: boolean;
message?: string;
data?: {
data: CustomizeFormValidateFieldItem[];
nodeId: number | string; nodeId: number | string;
nodeName: string; nodeName: string;
remark: string;
resultStatus: number; resultStatus: number;
}; remark: string;
data: CustomizeFormValidateFieldItem[];
}
/** 中间表单 validate / saveData 返回体;`data` 为当前二级下多列叶子节点数组 */
export interface CustomizeFormValidateResult {
success: boolean;
message?: string;
data?: CustomizeFormValidateNode[];
} }
...@@ -155,7 +155,11 @@ ...@@ -155,7 +155,11 @@
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import type { BigModule, CustomizeFormValidateResult } from "@/types/views/car-detail"; import type {
BigModule,
CustomizeFormValidateNode,
CustomizeFormValidateResult,
} from "@/types/views/car-detail";
import type { import type {
CompleteInspectionResultData, CompleteInspectionResultData,
InspectionNode, InspectionNode,
...@@ -466,8 +470,7 @@ function advanceInspectionStep() { ...@@ -466,8 +470,7 @@ function advanceInspectionStep() {
} }
} }
function leafRecordFromValidateData(raw: CustomizeFormValidateResult["data"]): LeafNodeRecord | null { function leafRecordFromValidateNode(raw: CustomizeFormValidateNode): LeafNodeRecord {
if (!raw) return null;
return { return {
data: (raw.data ?? []).map((item) => ({ data: (raw.data ?? []).map((item) => ({
interactionType: item.interactionType ?? "", interactionType: item.interactionType ?? "",
...@@ -486,9 +489,10 @@ function leafRecordFromValidateData(raw: CustomizeFormValidateResult["data"]): L ...@@ -486,9 +489,10 @@ function leafRecordFromValidateData(raw: CustomizeFormValidateResult["data"]): L
/** `saveData` → 转叶子记录 → `saveInspectionRecord`;无有效数据时不请求接口 */ /** `saveData` → 转叶子记录 → `saveInspectionRecord`;无有效数据时不请求接口 */
async function saveCurrentLeafInspectionRecord(nextFlag: boolean): Promise<boolean> { async function saveCurrentLeafInspectionRecord(nextFlag: boolean): Promise<boolean> {
const saved = (await customizeFormRef.value?.saveData?.()) as CustomizeFormValidateResult | undefined; const saved = (await customizeFormRef.value?.saveData?.()) as CustomizeFormValidateResult | undefined;
const record = saved?.data ? leafRecordFromValidateData(saved.data) : null; const rows = saved?.data;
if (!record) return false; if (!rows?.length) return false;
await saveInspectionRecord(buildInspectionSavePayload([record], nextFlag)); const leafNodes = rows.map((row) => leafRecordFromValidateNode(row));
await saveInspectionRecord(buildInspectionSavePayload(leafNodes, nextFlag));
return true; return true;
} }
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!