Procházet zdrojové kódy

refactor(订单地址): 将回调方法改为异步处理并添加错误捕获

yz před 1 měsícem
rodič
revize
0ae460e0d9
1 změnil soubory, kde provedl 63 přidání a 38 odebrání
  1. 63 38
      src/views/order/address/index.vue

+ 63 - 38
src/views/order/address/index.vue

@@ -52,6 +52,12 @@
 import { getList, add, update, remove, getDetail } from '@/api/order/address'
 import { mapGetters } from 'vuex'
 
+/**
+ * @typedef {import('@/api/order/address').CustomerAddressItem} CustomerAddressItem
+ * @typedef {import('@/api/order/address').CustomerAddressForm} CustomerAddressForm
+ * @typedef {import('@/api/order/address').CustomerAddressQueryParams} CustomerAddressQueryParams
+ */
+
 export default {
   name: 'OrderAddress',
   data() {
@@ -327,13 +333,16 @@ export default {
      * 新增前的回调
      * @param {Function} done - 完成回调
      * @param {string} type - 操作类型
-     * @returns {void}
+     * @returns {Promise<void>}
      */
-    beforeOpen(done, type) {
+    async beforeOpen(done, type) {
       if (['edit', 'view'].includes(type)) {
-        getDetail(this.form.id).then(res => {
+        try {
+          const res = await getDetail(this.form.id)
           this.form = res.data.data
-        })
+        } catch (error) {
+          window.console.log(error)
+        }
       }
       done()
     },
@@ -342,17 +351,21 @@ export default {
      * 获取数据
      * @param {Object} page - 分页信息
      * @param {CustomerAddressQueryParams} params - 查询参数
-     * @returns {void}
+     * @returns {Promise<void>}
      */
-    onLoad(page, params = {}) {
+    async onLoad(page, params = {}) {
       this.loading = true
-      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
+      try {
+        const res = await getList(page.currentPage, page.pageSize, Object.assign(params, this.query))
         const data = res.data.data
         this.page.total = data.total
         this.data = data.records
         this.loading = false
         this.selectionClear()
-      })
+      } catch (error) {
+        this.loading = false
+        window.console.log(error)
+      }
     },
     
     /**
@@ -360,20 +373,21 @@ export default {
      * @param {CustomerAddressForm} row - 表单数据
      * @param {Function} done - 完成回调
      * @param {Function} loading - 加载状态回调
-     * @returns {void}
+     * @returns {Promise<void>}
      */
-    rowSave(row, done, loading) {
-      add(row).then(() => {
+    async rowSave(row, done, loading) {
+      try {
+        await add(row)
         done()
         this.onLoad(this.page)
         this.$message({
           type: 'success',
           message: '操作成功!'
         })
-      }, error => {
+      } catch (error) {
         loading()
         window.console.log(error)
-      })
+      }
     },
     
     /**
@@ -382,67 +396,78 @@ export default {
      * @param {number} index - 行索引
      * @param {Function} done - 完成回调
      * @param {Function} loading - 加载状态回调
-     * @returns {void}
+     * @returns {Promise<void>}
      */
-    rowUpdate(row, index, done, loading) {
-      update(row).then(() => {
+    async rowUpdate(row, index, done, loading) {
+      try {
+        await update(row)
         done()
         this.onLoad(this.page)
         this.$message({
           type: 'success',
           message: '操作成功!'
         })
-      }, error => {
+      } catch (error) {
         loading()
         window.console.log(error)
-      })
+      }
     },
     
     /**
      * 删除
      * @param {CustomerAddressItem} row - 行数据
      * @param {number} index - 行索引
-     * @returns {void}
+     * @returns {Promise<void>}
      */
-    rowDel(row, index) {
-      this.$confirm('确定将选择数据删除?', {
-        confirmButtonText: '确定',
-        cancelButtonText: '取消',
-        type: 'warning'
-      }).then(() => {
-        return remove(row.id)
-      }).then(() => {
+    async rowDel(row, index) {
+      try {
+        await this.$confirm('确定将选择数据删除?', {
+          confirmButtonText: '确定',
+          cancelButtonText: '取消',
+          type: 'warning'
+        })
+        await remove(row.id)
         this.onLoad(this.page)
         this.$message({
           type: 'success',
           message: '操作成功!'
         })
-      })
+      } catch (error) {
+        // 用户取消删除或删除失败
+        if (error !== 'cancel') {
+          window.console.log(error)
+        }
+      }
     },
     
     /**
      * 批量删除
-     * @returns {void}
+     * @returns {Promise<void>}
      */
-    handleDelete() {
+    async handleDelete() {
       if (this.selectionList.length === 0) {
         this.$message.warning('请选择至少一条数据')
         return
       }
-      this.$confirm('确定将选择数据删除?', {
-        confirmButtonText: '确定',
-        cancelButtonText: '取消',
-        type: 'warning'
-      }).then(() => {
-        return remove(this.ids)
-      }).then(() => {
+      try {
+        await this.$confirm('确定将选择数据删除?', {
+          confirmButtonText: '确定',
+          cancelButtonText: '取消',
+          type: 'warning'
+        })
+        await remove(this.ids)
         this.onLoad(this.page)
         this.$message({
           type: 'success',
           message: '操作成功!'
         })
         this.$refs.crud.toggleSelection()
-      })
+      } catch (error) {
+        // 用户取消删除或删除失败
+        if (error !== 'cancel') {
+          window.console.log(error)
+        }
+      }
     },
     
     /**