Browse Source

refactor(announcement): 将分类管理方法改为异步处理并添加错误处理

yz 2 months ago
parent
commit
681103dc5c
1 changed files with 105 additions and 98 deletions
  1. 105 98
      src/views/announcement/category.vue

+ 105 - 98
src/views/announcement/category.vue

@@ -307,7 +307,7 @@ export default {
      * 删除选中的分类
      * @description 批量删除选中的分类
      */
-    handleDelete() {
+    async handleDelete() {
       if (this.selectionList.length === 0) {
         this.$message.warning("请选择至少一条数据");
         return;
@@ -320,58 +320,57 @@ export default {
         return;
       }
 
-      this.$confirm("确定将选择数据删除?", {
-        confirmButtonText: "确定",
-        cancelButtonText: "取消",
-        type: "warning"
-      })
-        .then(() => {
-          return removeCategory(this.ids);
-        })
-        .then(() => {
-          this.onLoad();
-          this.$message({
-            type: "success",
-            message: "操作成功!"
-          });
-          this.$refs.crud.toggleSelection();
-        })
-        .catch(() => {
-          this.$message({
-            type: "info",
-            message: "已取消删除"
-          });
+      try {
+        await this.$confirm("确定将选择数据删除?", {
+          confirmButtonText: "确定",
+          cancelButtonText: "取消",
+          type: "warning"
         });
+        
+        await removeCategory(this.ids);
+        
+        this.onLoad();
+        this.$message({
+          type: "success",
+          message: "操作成功!"
+        });
+        this.$refs.crud.toggleSelection();
+      } catch (error) {
+        this.$message({
+          type: "info",
+          message: "已取消删除"
+        });
+      }
     },
 
     /**
      * 状态变更处理
      * @param {CategoryItem} row - 行数据
      */
-    handleStatusChange(row) {
+    async handleStatusChange(row) {
       const statusText = row.status === 1 ? '启用' : '禁用';
-      this.$confirm(`确定${statusText}该分类吗?`, {
-        confirmButtonText: "确定",
-        cancelButtonText: "取消",
-        type: "warning"
-      })
-        .then(() => {
-          return updateCategoryStatus(row.id, row.status);
-        })
-        .then(() => {
-          this.$message({
-            type: "success",
-            message: `${statusText}成功!`
-          });
-        })
-        .catch(() => {
-          // 恢复原状态
-          row.status = row.status === 1 ? 0 : 1;
-          this.$message({
-            type: "info",
-            message: "已取消操作"
-          });
+      
+      try {
+        await this.$confirm(`确定${statusText}该分类吗?`, {
+          confirmButtonText: "确定",
+          cancelButtonText: "取消",
+          type: "warning"
+        });
+        
+        await updateCategoryStatus(row.id, row.status);
+        
+        this.$message({
+          type: "success",
+          message: `${statusText}成功!`
         });
+      } catch (error) {
+        // 恢复原状态
+        row.status = row.status === 1 ? 0 : 1;
+        this.$message({
+          type: "info",
+          message: "已取消操作"
+        });
+      }
     },
 
     /**
@@ -379,11 +378,14 @@ export default {
      * @param {Function} done - 完成回调
      * @param {string} type - 操作类型 (add/edit/view)
      */
-    beforeOpen(done, type) {
+    async beforeOpen(done, type) {
       if (["edit", "view"].includes(type)) {
-        getCategoryDetail(this.form.id).then(res => {
+        try {
+          const res = await getCategoryDetail(this.form.id);
           this.form = res.data.data;
-        });
+        } catch (error) {
+          console.error('获取分类详情失败:', error);
+        }
       } else if (type === "add") {
         // 新增时设置默认值
         this.form = {
@@ -414,27 +416,29 @@ export default {
      * @param {CategoryItem} row - 行数据
      * @param {number} index - 行索引
      */
-    rowDel(row, index) {
+    async rowDel(row, index) {
       if (row.isSystem === 1) {
         this.$message.error("系统分类不能删除");
         return;
       }
 
-      this.$confirm("确定将选择数据删除?", {
-        confirmButtonText: "确定",
-        cancelButtonText: "取消",
-        type: "warning"
-      })
-        .then(() => {
-          return removeCategory(row.id);
-        })
-        .then(() => {
-          this.onLoad();
-          this.$message({
-            type: "success",
-            message: "操作成功!"
-          });
+      try {
+        await this.$confirm("确定将选择数据删除?", {
+          confirmButtonText: "确定",
+          cancelButtonText: "取消",
+          type: "warning"
         });
+        
+        await removeCategory(row.id);
+        
+        this.onLoad();
+        this.$message({
+          type: "success",
+          message: "操作成功!"
+        });
+      } catch (error) {
+        console.error('删除分类失败:', error);
+      }
     },
 
     /**
@@ -444,19 +448,20 @@ export default {
      * @param {Function} done - 完成回调
      * @param {Function} loading - 加载状态回调
      */
-    rowUpdate(row, index, done, loading) {
-      updateCategory(row)
-        .then(() => {
-          this.onLoad();
-          this.$message({
-            type: "success",
-            message: "操作成功!"
-          });
-          done();
-        })
-        .catch(() => {
-          loading();
+    async rowUpdate(row, index, done, loading) {
+      try {
+        await updateCategory(row);
+        
+        this.onLoad();
+        this.$message({
+          type: "success",
+          message: "操作成功!"
         });
+        done();
+      } catch (error) {
+        console.error('更新分类失败:', error);
+        loading();
+      }
     },
 
     /**
@@ -465,19 +470,20 @@ export default {
      * @param {Function} done - 完成回调
      * @param {Function} loading - 加载状态回调
      */
-    rowSave(row, done, loading) {
-      addCategory(row)
-        .then(() => {
-          this.onLoad();
-          this.$message({
-            type: "success",
-            message: "操作成功!"
-          });
-          done();
-        })
-        .catch(() => {
-          loading();
+    async rowSave(row, done, loading) {
+      try {
+        await addCategory(row);
+        
+        this.onLoad();
+        this.$message({
+          type: "success",
+          message: "操作成功!"
         });
+        done();
+      } catch (error) {
+        console.error('新增分类失败:', error);
+        loading();
+      }
     },
 
     /**
@@ -526,19 +532,20 @@ export default {
      * 加载数据
      * @param {QueryParams} params - 查询参数
      */
-    onLoad(params = {}) {
+    async onLoad(params = {}) {
       this.loading = true;
-      getCategoryList(Object.assign(params, this.query))
-        .then(res => {
-          const data = res.data.data;
-          this.data = Array.isArray(data) ? data : [];
-          this.loading = false;
-          this.selectionClear();
-        })
-        .catch(() => {
-          this.loading = false;
-          this.data = [];
-        });
+      
+      try {
+        const res = await getCategoryList(Object.assign(params, this.query));
+        const data = res.data.data;
+        this.data = Array.isArray(data) ? data : [];
+        this.selectionClear();
+      } catch (error) {
+        console.error('加载分类列表失败:', error);
+        this.data = [];
+      } finally {
+        this.loading = false;
+      }
     }
   },