From 2afdb1d23d50d0ae8c8e0e304c315a46c4d8d5f9 Mon Sep 17 00:00:00 2001 From: lb Date: Mon, 3 Apr 2023 11:35:14 +0800 Subject: [PATCH] update +++ --- src/components/DialogJustForm.vue | 103 +++++++++++++----- .../pms/qualityInspectionRecord/config.js | 3 +- 2 files changed, 80 insertions(+), 26 deletions(-) diff --git a/src/components/DialogJustForm.vue b/src/components/DialogJustForm.vue index d070454..712293b 100644 --- a/src/components/DialogJustForm.vue +++ b/src/components/DialogJustForm.vue @@ -252,34 +252,87 @@ export default { }; }, mounted() { + /** 临时保存所有发出的请求 */ + const promiseHistory = {}; /** 处理 injectTo 选项 */ this.configs.form.rows.forEach((row) => { row.forEach((col) => { - if ("injectTo" in col && Array.isArray(col.injectTo)) { - const valueProp = "optionValue" in col ? col.optionValue : "id"; - const unwatch = this.$watch( - () => this.dataForm[col.prop], - (newVal) => { - const chosenObject = this.cachedList[col.prop].find((i) => i[valueProp] === newVal); - if (chosenObject) { - // 如果找到了 - col.injectTo.map((item) => { - console.log("[setting ---> ]:", item[1], "to", `dataForm[${item[0]}]`); - this.$set(this.dataForm, item[0], chosenObject[item[1]]); - this.$forceUpdate(); - }); - } else { - console.log("DialogJustForm mounted(): 没有找到 injectTo 关联的对象"); + if (col.fetchData && typeof col.fetchData === "function" && !col.hasPrev) { + // 获取数据 - 不需要等待前置条件时 + promiseHistory[col.prop] = col.fetchData().then(({ data: res }) => { + if (res.code === 0) { + if ("list" in res.data) { + // 填充 options + this.$set( + col, + "options", + res.data.list.map((i) => ({ + label: col.optionLabel ? i[col.optionLabel] : i.name, + value: col.optionValue ? i[col.optionValue] : i.id, + })) + ); + + // 是否需要缓存数据列表 + if ("injectTo" in col) { + // 缓存 + this.cachedList[col.prop] = res.data.list; + // 设置监听 + const valueProp = "optionValue" in col ? col.optionValue : "id"; + const unwatch = this.$watch( + () => this.dataForm[col.prop], + (val) => { + const chosenObject = this.cachedList[col.prop].find((i) => i[valueProp] === val); + if (chosenObject) { + // 如果找到了 + col.injectTo.map((item) => { + this.$set(this.dataForm, item[0], chosenObject[item[1]]); + this.$forceUpdate(); + }); + } else { + console.log("DialogJustForm mounted(): 没有找到 injectTo 关联的对象"); + } + }, + { + immediate: false, + } + ); + this.watchList.push(unwatch); + } + + if ("autoUpdateProp" in col) { + this.$set(savedDatalist, col.prop, res.data.list); + } + + // end branch + } else if (Array.isArray(res.data)) { + // 是否需要缓存数据列表 + if ("injectTo" in col) { + // 缓存 + this.cachedList[col.prop] = res.data; + } + this.$set( + col, + "options", + res.data.map((i) => ({ + label: col.optionLabel ? i[col.optionLabel] : i.name, + value: col.optionValue ? i[col.optionValue] : i.id, + })) + ); + if ("autoUpdateProp" in col) { + this.$set(savedDatalist, col.prop, res.data); + } } - }, - { - immediate: false, + } else { + col.options.splice(0); } - ); - this.watchList.push(unwatch); + }); } - if ("toggleFetchData" in col) { + if (col.fetchData && typeof col.fetchData === "function" && col.hasPrev) { + // 获取数据 - 需要等待前置条件时 + + // 1.检查前置条件是否已经 fullfilled + // 2.没有则等待,有则 fetchData const unwatch = this.$watch( () => this.dataForm[col.toggleFetchData], (val) => { @@ -318,12 +371,10 @@ export default { if ("autoUpdateProp" in col) { this.$set(savedDatalist, col.prop, res.data); } - } - // col.options = res.data.list; + } } else { col.options.splice(0); - } - // dataForm[col.prop] = col.default ?? null; // not perfect! + } }); }, { @@ -332,6 +383,8 @@ export default { ); this.watchList.push(unwatch); } + + /// old part ==================== }); }); }, diff --git a/src/views/modules/pms/qualityInspectionRecord/config.js b/src/views/modules/pms/qualityInspectionRecord/config.js index 04f8d90..d01e329 100644 --- a/src/views/modules/pms/qualityInspectionRecord/config.js +++ b/src/views/modules/pms/qualityInspectionRecord/config.js @@ -150,7 +150,8 @@ export default function () { prop: "orderId", options: [], optionLabel: 'orderCode', - toggleFetchData: 'carId', // 当 carId 改变的时候,也会 fetchData + // toggleFetchData: 'carId', // 当 carId 改变的时候,也会 fetchData + hasPrev: 'carId', // 当 carId 改变的时候,也会 fetchData // 需要保存当前状态 needCache: true, fetchDataParam: { search: 'carId', get: 'id' }, // 伴随着 toggleFetchData 出现