Prechádzať zdrojové kódy

refactor(announcement): 将Promise链式调用改为async/await语法

yz 1 mesiac pred
rodič
commit
eac3881b57
1 zmenil súbory, kde vykonal 71 pridanie a 57 odobranie
  1. 71 57
      src/views/announcement/index.vue

+ 71 - 57
src/views/announcement/index.vue

@@ -239,14 +239,15 @@ export default {
     },
     methods: {
         // 加载分类选项
-        loadCategoryOptions() {
-            getCategoryList().then(res => {
+        async loadCategoryOptions() {
+            try {
+                const res = await getCategoryList();
                 this.categoryOptions = res.data.data || [];
                 const categoryColumn = this.option.column.find(col => col.prop === 'categoryId');
                 if (categoryColumn) {
                     categoryColumn.dicData = this.categoryOptions;
                 }
-            }).catch(() => {
+            } catch (error) {
                 // 如果接口不存在,使用模拟数据
                 this.categoryOptions = [
                     { id: 1, categoryName: '系统公告', value: 1, label: '系统公告' },
@@ -258,7 +259,7 @@ export default {
                 if (categoryColumn) {
                     categoryColumn.dicData = this.categoryOptions;
                 }
-            });
+            }
         },
         // 查看详情
         viewDetail(row) {
@@ -266,14 +267,15 @@ export default {
             this.detailVisible = true;
         },
         // 加载经销商选项
-        loadDealerOptions() {
-            getDealerList().then(res => {
+        async loadDealerOptions() {
+            try {
+                const res = await getDealerList();
                 this.dealerOptions = res.data.data || [];
                 const dealerColumn = this.option.column.find(col => col.prop === 'dealerId');
                 if (dealerColumn) {
                     dealerColumn.dicData = this.dealerOptions;
                 }
-            }).catch(() => {
+            } catch (error) {
                 // 如果接口不存在,使用模拟数据
                 this.dealerOptions = [
                     { id: 1, dealerName: '经销商A' },
@@ -284,17 +286,18 @@ export default {
                 if (dealerColumn) {
                     dealerColumn.dicData = this.dealerOptions;
                 }
-            });
+            }
         },
         // 加载品牌选项
-        loadBrandOptions() {
-            getBrandList().then(res => {
+        async loadBrandOptions() {
+            try {
+                const res = await getBrandList();
                 this.brandOptions = res.data.data || [];
                 const brandColumn = this.option.column.find(col => col.prop === 'brandId');
                 if (brandColumn) {
                     brandColumn.dicData = this.brandOptions;
                 }
-            }).catch(() => {
+            } catch (error) {
                 // 如果接口不存在,使用模拟数据
                 this.brandOptions = [
                     { id: 1, brandName: '品牌A' },
@@ -305,50 +308,53 @@ export default {
                 if (brandColumn) {
                     brandColumn.dicData = this.brandOptions;
                 }
-            });
+            }
         },
-        rowSave(row, done, loading) {
-            add(row).then(() => {
+        async rowSave(row, done, loading) {
+            try {
+                await add(row);
                 this.onLoad(this.page);
                 this.$message({
                     type: "success",
                     message: "操作成功!"
                 });
                 done();
-            }, error => {
+            } catch (error) {
                 console.log(error);
                 loading();
-            });
+            }
         },
-        rowUpdate(row, index, done, loading) {
-            update(row).then(() => {
+        async rowUpdate(row, index, done, loading) {
+            try {
+                await update(row);
                 this.onLoad(this.page);
                 this.$message({
                     type: "success",
                     message: "操作成功!"
                 });
                 done();
-            }, error => {
+            } catch (error) {
                 console.log(error);
                 loading();
-            });
+            }
         },
-        rowDel(row) {
-            this.$confirm("确定将选择数据删除?", {
-                confirmButtonText: "确定",
-                cancelButtonText: "取消",
-                type: "warning"
-            })
-                .then(() => {
-                    return remove(row.id);
-                })
-                .then(() => {
-                    this.onLoad(this.page);
-                    this.$message({
-                        type: "success",
-                        message: "操作成功!"
-                    });
+        async rowDel(row) {
+            try {
+                await this.$confirm("确定将选择数据删除?", {
+                    confirmButtonText: "确定",
+                    cancelButtonText: "取消",
+                    type: "warning"
+                });
+                await remove(row.id);
+                this.onLoad(this.page);
+                this.$message({
+                    type: "success",
+                    message: "操作成功!"
                 });
+            } catch (error) {
+                // 用户取消删除或删除失败
+                console.log(error);
+            }
         },
         searchReset() {
             this.query = {};
@@ -367,33 +373,37 @@ export default {
             this.selectionList = [];
             this.$refs.crud.toggleSelection();
         },
-        handleDelete() {
+        async handleDelete() {
             if (this.selectionList.length === 0) {
                 this.$message.warning("请选择至少一条数据");
                 return;
             }
-            this.$confirm("确定将选择数据删除?", {
-                confirmButtonText: "确定",
-                cancelButtonText: "取消",
-                type: "warning"
-            })
-                .then(() => {
-                    return remove(this.ids);
-                })
-                .then(() => {
-                    this.onLoad(this.page);
-                    this.$message({
-                        type: "success",
-                        message: "操作成功!"
-                    });
-                    this.$refs.crud.toggleSelection();
+            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) {
+                // 用户取消删除或删除失败
+                console.log(error);
+            }
         },
-        beforeOpen(done, type) {
+        async beforeOpen(done, type) {
             if (["edit", "view"].includes(type)) {
-                getAnnouncement(this.form.id).then(res => {
+                try {
+                    const res = await getAnnouncement(this.form.id);
                     this.form = res.data.data;
-                });
+                } catch (error) {
+                    console.log(error);
+                }
             }
             done();
         },
@@ -406,7 +416,7 @@ export default {
         refreshChange() {
             this.onLoad(this.page, this.query);
         },
-        onLoad(page, params = {}) {
+        async onLoad(page, params = {}) {
             const { publishTime } = this.query;
             let values = {
                 ...params,
@@ -421,13 +431,17 @@ export default {
                 values.publishTime = null;
             }
             this.loading = true;
-            getList(page.currentPage, page.pageSize, values).then(res => {
+            try {
+                const res = await getList(page.currentPage, page.pageSize, values);
                 const data = res.data.data;
                 this.page.total = data.total;
                 this.data = data.records;
                 this.loading = false;
                 this.selectionClear();
-            });
+            } catch (error) {
+                console.log(error);
+                this.loading = false;
+            }
         }
     }
 };