Browse Source

fix(订单表单): 修复编辑模式切换时未加载详情的问题并优化保存逻辑

yz 1 week ago
parent
commit
213afc12a5
2 changed files with 52 additions and 5 deletions
  1. 14 0
      src/components/order-form/order-form.vue
  2. 38 5
      src/views/order/order/index-avue.vue

+ 14 - 0
src/components/order-form/order-form.vue

@@ -212,6 +212,20 @@ export default {
       immediate: true
     },
 
+    // 监听编辑模式切换:当从新增切到编辑且已拿到ID时,主动加载详情
+    isEdit: {
+      handler(newVal, oldVal) {
+        if (newVal && !oldVal) {
+          this.$nextTick(() => {
+            const id = this.orderId || (this.formData && this.formData.id)
+            if (id) {
+              this.loadOrderDetail(String(id))
+            }
+          })
+        }
+      }
+    },
+
     /**
      * 监听表单数据变化,用于地址回显
      * @description 当表单数据中的客户编码和地址相关字段都有值时,触发地址选择组件的回显

+ 38 - 5
src/views/order/order/index-avue.vue

@@ -422,7 +422,7 @@ export default {
      * @param {OrderItem} orderData - 保存后的订单数据
      * @returns {void}
      */
-    handleFormSaveSuccess(orderData) {
+    async handleFormSaveSuccess(orderData) {
       if (this.isEditMode) {
         // 编辑模式:保持在表单页,不返回列表
         this.orderFormVisible = true
@@ -434,10 +434,43 @@ export default {
         // 新增模式:保存后停留当前页并切换到编辑模式,显示提交按钮
         this.orderFormVisible = true
         this.isEditMode = true
-        // 设置新创建订单的ID,确保子组件处于编辑态并加载详情
-        this.editOrderId = orderData && orderData.id ? String(orderData.id) : this.editOrderId
-        // 刷新列表数据,保持数据最新
-        this.onLoad(this.page)
+        // 尝试从响应数据拿ID;若无则从列表首条获取ID
+        const respId = orderData && orderData.id ? String(orderData.id) : null
+        if (respId) {
+          this.editOrderId = respId
+        } else {
+          try {
+            const latestId = await this.fetchLatestOrderId()
+            if (latestId) {
+              this.editOrderId = String(latestId)
+            }
+          } catch (e) {
+            // 获取最新ID失败,保留当前状态,用户仍可手动返回列表
+            // eslint-disable-next-line no-console
+            console.warn('获取最新订单ID失败:', e)
+          } finally {
+            // 刷新列表数据,保持数据最新
+            this.onLoad(this.page)
+          }
+        }
+      }
+    },
+
+    /**
+     * 获取最新创建的订单ID(列表第一页第一条)
+     * @returns {Promise<string|number|null>}
+     */
+    async fetchLatestOrderId() {
+      try {
+        const res = await getOrderList(1, 1, {})
+        const data = res && res.data && res.data.data
+        const records = data && Array.isArray(data.records) ? data.records : []
+        const first = records[0]
+        return (first && first.id) ? first.id : null
+      } catch (error) {
+        // eslint-disable-next-line no-console
+        console.error('fetchLatestOrderId error:', error)
+        return null
       }
     },