Commit 7b7e5776 by chenwq1

fix: 抽离公共方法

1 parent bd73d1ec
Pipeline #91 for 7b7e5776 failed in 0 seconds
<script setup lang="ts">
import type { CustomizeFormValidateResult } from "@/types/views/car-detail";
import { ref, watch } from "vue";
import selectDatePicker from "@/components/customizeForm/selectDatePicker.vue";
......@@ -126,7 +128,7 @@ function setSubmitData() {
};
}
async function validate() {
async function validate(): Promise<CustomizeFormValidateResult> {
const nodesData = setSubmitData();
console.log('validate', nodesData);
let success = true;
......@@ -144,7 +146,8 @@ async function validate() {
};
}
function saveData() {
/** 与 `validate` 相同返回结构,不校验必填,用于静默保存 / 上一步等场景 */
async function saveData(): Promise<CustomizeFormValidateResult> {
const nodesData = setSubmitData();
console.log('saveData', nodesData);
return {
......
......@@ -146,17 +146,13 @@
</template>
<script setup lang="ts">
import type { BigModule, CustomizeFormValidateResult } from "@/types/views/car-detail";
import type { InspectionTemplateDefaultData, LeafNodeRecord } from "@api";
import { computed, onMounted, ref } from "vue";
import { useRoute, useRouter } from "vue-router";
import customizeForm from "@/components/customizeForm/Index.vue";
import type {
BigModule,
CustomizeFormValidateResult,
InspectSubContentExpose,
} from "@/types/views/car-detail";
import {
applyResolvedInitialValuesToLeafNodes,
completeInspection,
......@@ -184,8 +180,7 @@ const activeBigKey = ref<number | string>("");
const showModulePicker = ref(false);
const loading = ref(false);
const submitting = ref(false);
/** 二级项检测内容区(叶子节点表单),子组件 defineExpose */
const inspectSubContentRef = ref<InspectSubContentExpose | null>(null);
const customizeFormRef = ref<InstanceType<typeof customizeForm> | null>(null);
/** 检测评分表单 */
const inspectionScoreFormRef = ref<InstanceType<typeof ScoreForm> | null>(null);
......@@ -316,34 +311,6 @@ function onOpenModulePicker() {
showModulePicker.value = true;
}
/** 切换大项 / 二级项前静默落库当前叶子数据 */
async function persistCurrentSubTabSilently() {
if (activeBigKey.value === SCORE_BIG_KEY) return;
try {
const leafNodes = (await inspectSubContentRef.value?.collectData(false)) ?? [];
await saveInspectionRecord(buildInspectionSavePayload(leafNodes, false));
} catch {
// 静默失败,不阻断切换
}
}
async function onSelectBigModule(selectedBigModule: BigModule) {
showModulePicker.value = false;
if (selectedBigModule.key === activeBigKey.value) return;
await persistCurrentSubTabSilently();
activeBigKey.value = selectedBigModule.key;
activeSubKey.value = "";
if (selectedBigModule.key !== SCORE_BIG_KEY) {
loadSubTabsForBigModule(selectedBigModule.key);
}
}
async function onSelectSubTab(subTabKey: number | string) {
if (subTabKey === activeSubKey.value) return;
await persistCurrentSubTabSilently();
activeSubKey.value = subTabKey;
}
/** 构造 `saveInspectionRecord` 请求体 */
function buildInspectionSavePayload(leafNodes: LeafNodeRecord[], nextFlag: boolean) {
return {
......@@ -382,9 +349,6 @@ function advanceInspectionStep() {
}
}
const customizeFormRef = ref();
function leafRecordFromValidateData(raw: CustomizeFormValidateResult["data"]): LeafNodeRecord | null {
if (!raw) return null;
return {
......@@ -403,6 +367,42 @@ function leafRecordFromValidateData(raw: CustomizeFormValidateResult["data"]): L
};
}
/** `saveData` → 转叶子记录 → `saveInspectionRecord`;无有效数据时不请求接口 */
async function saveCurrentLeafInspectionRecord(nextFlag: boolean): Promise<boolean> {
const saved = (await customizeFormRef.value?.saveData?.()) as CustomizeFormValidateResult | undefined;
const record = saved?.data ? leafRecordFromValidateData(saved.data) : null;
if (!record) return false;
await saveInspectionRecord(buildInspectionSavePayload([record], nextFlag));
return true;
}
/** 切换大项 / 二级项前静默落库当前叶子数据 */
async function persistCurrentSubTabSilently() {
if (activeBigKey.value === SCORE_BIG_KEY) return;
try {
await saveCurrentLeafInspectionRecord(false);
} catch {
// 静默失败,不阻断切换
}
}
async function onSelectBigModule(selectedBigModule: BigModule) {
showModulePicker.value = false;
if (selectedBigModule.key === activeBigKey.value) return;
await persistCurrentSubTabSilently();
activeBigKey.value = selectedBigModule.key;
activeSubKey.value = "";
if (selectedBigModule.key !== SCORE_BIG_KEY) {
loadSubTabsForBigModule(selectedBigModule.key);
}
}
async function onSelectSubTab(subTabKey: number | string) {
if (subTabKey === activeSubKey.value) return;
await persistCurrentSubTabSilently();
activeSubKey.value = subTabKey;
}
/** 下一步:校验当前叶子 → 保存 → 前进 */
async function onNext() {
if (submitting.value) return;
......@@ -413,12 +413,11 @@ async function onNext() {
showToast(res?.message ?? "校验失败");
return;
}
const record = leafRecordFromValidateData(res.data);
if (!record) {
const savedOk = await saveCurrentLeafInspectionRecord(true);
if (!savedOk) {
showToast("未获取到表单数据");
return;
}
await saveInspectionRecord(buildInspectionSavePayload([record], true));
advanceInspectionStep();
} catch (error) {
if (!isRequestFailureAlreadyHandled(error)) {
......@@ -450,8 +449,7 @@ async function onPrev() {
return;
}
const leafNodes = (await inspectSubContentRef.value?.collectData(false)) ?? [];
await saveInspectionRecord(buildInspectionSavePayload(leafNodes, false));
await saveCurrentLeafInspectionRecord(false);
const subTabsOfActive = activeBigModule.value?.subTabs ?? [];
const activeSubIndex = subTabsOfActive.findIndex((subTab) => subTab.key === activeSubKey.value);
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!