From 5461b8ebf2f8bf66a9d34351eb8783484304958a Mon Sep 17 00:00:00 2001
From: 648540858 <648540858@qq.com>
Date: 星期一, 22 八月 2022 16:17:28 +0800
Subject: [PATCH] 支持全局异常和统一返回结果,未完待续

---
 src/main/java/com/genersoft/iot/vmp/vmanager/user/RoleController.java |   38 ++++++++++++++------------------------
 1 files changed, 14 insertions(+), 24 deletions(-)

diff --git a/src/main/java/com/genersoft/iot/vmp/vmanager/user/RoleController.java b/src/main/java/com/genersoft/iot/vmp/vmanager/user/RoleController.java
index d4d5a2c..769dca0 100644
--- a/src/main/java/com/genersoft/iot/vmp/vmanager/user/RoleController.java
+++ b/src/main/java/com/genersoft/iot/vmp/vmanager/user/RoleController.java
@@ -1,9 +1,11 @@
 package com.genersoft.iot.vmp.vmanager.user;
 
+import com.genersoft.iot.vmp.conf.exception.ControllerException;
 import com.genersoft.iot.vmp.conf.security.SecurityUtils;
 import com.genersoft.iot.vmp.service.IRoleService;
 import com.genersoft.iot.vmp.storager.dao.dto.Role;
 import com.genersoft.iot.vmp.utils.DateUtil;
+import com.genersoft.iot.vmp.vmanager.bean.ErrorCode;
 import com.genersoft.iot.vmp.vmanager.bean.WVPResult;
 
 import io.swagger.v3.oas.annotations.Operation;
@@ -29,16 +31,13 @@
     @Operation(summary = "娣诲姞瑙掕壊")
     @Parameter(name = "name", description = "瑙掕壊鍚�", required = true)
     @Parameter(name = "authority", description = "鏉冮檺锛堣嚜琛屽畾涔夊唴瀹癸紝鐩墠鏈娇鐢級", required = true)
-    public ResponseEntity<WVPResult<Integer>> add(@RequestParam String name,
+    public void add(@RequestParam String name,
                                                   @RequestParam(required = false) String authority){
-        WVPResult<Integer> result = new WVPResult<>();
         // 鑾峰彇褰撳墠鐧诲綍鐢ㄦ埛id
         int currenRoleId = SecurityUtils.getUserInfo().getRole().getId();
         if (currenRoleId != 1) {
             // 鍙敤瑙掕壊id涓�1鎵嶅彲浠ュ垹闄ゅ拰娣诲姞鐢ㄦ埛
-            result.setCode(-1);
-            result.setMsg("鐢ㄦ埛鏃犳潈闄�");
-            return new ResponseEntity<>(result, HttpStatus.FORBIDDEN);
+            throw new ControllerException(ErrorCode.ERROR403);
         }
 
         Role role = new Role();
@@ -48,42 +47,33 @@
         role.setUpdateTime(DateUtil.getNow());
 
         int addResult = roleService.add(role);
-
-        result.setCode(addResult > 0 ? 0 : -1);
-        result.setMsg(addResult > 0 ? "success" : "fail");
-        result.setData(addResult);
-        return new ResponseEntity<>(result, HttpStatus.OK);
+        if (addResult <= 0) {
+            throw new ControllerException(ErrorCode.ERROR100);
+        }
     }
 
     @DeleteMapping("/delete")
     @Operation(summary = "鍒犻櫎瑙掕壊")
     @Parameter(name = "id", description = "鐢ㄦ埛Id", required = true)
-    public ResponseEntity<WVPResult<String>> delete(@RequestParam Integer id){
+    public void delete(@RequestParam Integer id){
         // 鑾峰彇褰撳墠鐧诲綍鐢ㄦ埛id
         int currenRoleId = SecurityUtils.getUserInfo().getRole().getId();
-        WVPResult<String> result = new WVPResult<>();
         if (currenRoleId != 1) {
             // 鍙敤瑙掕壊id涓�0鎵嶅彲浠ュ垹闄ゅ拰娣诲姞鐢ㄦ埛
-            result.setCode(-1);
-            result.setMsg("鐢ㄦ埛鏃犳潈闄�");
-            return new ResponseEntity<>(result, HttpStatus.FORBIDDEN);
+            throw new ControllerException(ErrorCode.ERROR403);
         }
         int deleteResult = roleService.delete(id);
 
-        result.setCode(deleteResult>0? 0 : -1);
-        result.setMsg(deleteResult>0? "success" : "fail");
-        return new ResponseEntity<>(result, HttpStatus.OK);
+        if (deleteResult <= 0) {
+            throw new ControllerException(ErrorCode.ERROR100);
+        }
     }
 
     @GetMapping("/all")
     @Operation(summary = "鏌ヨ瑙掕壊")
-    public ResponseEntity<WVPResult<List<Role>>> all(){
+    public List<Role> all(){
         // 鑾峰彇褰撳墠鐧诲綍鐢ㄦ埛id
         List<Role> allRoles = roleService.getAll();
-        WVPResult<List<Role>> result = new WVPResult<>();
-        result.setCode(0);
-        result.setMsg("success");
-        result.setData(allRoles);
-        return new ResponseEntity<>(result, HttpStatus.OK);
+        return roleService.getAll();
     }
 }

--
Gitblit v1.8.0