Browse Source

错误码

liujc 2 years ago
parent
commit
beaec96f6a

+ 18 - 31
.idea/httpRequests/http-requests-log.http

@@ -1,3 +1,21 @@
+GET http://127.0.0.1:9001/open/liveBroadcastRoom/visitorFingerprint?code=0413aiGa1MplkF0RyGGa1sf6S143aiGv
+Connection: Keep-Alive
+User-Agent: Apache-HttpClient/4.5.14 (Java/17.0.6)
+Accept-Encoding: br,deflate,gzip,x-gzip
+
+<> 2023-05-18T145501.200.json
+
+###
+
+GET http://127.0.0.1:9001/open/liveBroadcastRoom/visitorFingerprint?code=0413aiGa1MplkF0RyGGa1sf6S143aiGv
+Connection: Keep-Alive
+User-Agent: Apache-HttpClient/4.5.14 (Java/17.0.6)
+Accept-Encoding: br,deflate,gzip,x-gzip
+
+<> 2023-05-18T145309.200.json
+
+###
+
 GET http://127.0.0.1:9001/open/sysConfig/queryByParamName?paramName=wechat_appid
 Connection: Keep-Alive
 User-Agent: Apache-HttpClient/4.5.14 (Java/17.0.6)
@@ -573,34 +591,3 @@ Accept-Encoding: br,deflate,gzip,x-gzip
 
 ###
 
-POST http://127.0.0.1:8005/open/school/staffSave
-Content-Type: application/json
-Content-Length: 101
-Proxy-Connection: Keep-Alive
-User-Agent: Apache-HttpClient/4.5.14 (Java/17.0.6)
-Accept-Encoding: br,deflate,gzip,x-gzip
-
-{
-  "schoolId": 3,
-  "userType": "SCHOOL_LEADER",
-  "username": "李四",
-  "mobile": "18682475901"
-}
-
-<> 2023-05-09T163843.200.json
-
-###
-
-POST http://127.0.0.1:8001/smsLogin?clientId=EDUCATION&clientSecret=EDUCATION&deviceNum=&phone=15907121011&smsCode=666666
-Content-Type: application/x-www-form-urlencoded
-Content-Length: 2
-Proxy-Connection: Keep-Alive
-User-Agent: Apache-HttpClient/4.5.14 (Java/17.0.6)
-Accept-Encoding: br,deflate,gzip,x-gzip
-
-{}
-
-<> 2023-05-09T162346.200.json
-
-###
-

+ 5 - 3
mec-biz/src/main/java/com/ym/mec/biz/service/impl/ImLiveBroadcastRoomServiceImpl.java

@@ -1688,20 +1688,22 @@ public class ImLiveBroadcastRoomServiceImpl extends ServiceImpl<ImLiveBroadcastR
             String wxMpOAuth2AccessTokenUrl = HttpUtil.get(url, null);
             log.info("请求微信授权接口的参数:{}", wxMpOAuth2AccessTokenUrl);
             if (StringUtils.isEmpty(wxMpOAuth2AccessTokenUrl)) {
-                throw new BizException("授权失败,请重新授权");
+                throw new BizException(999,"授权失败,请重新授权");
             }
             Map<String, Object> weChatRes = AliJSONMap.tomap(wxMpOAuth2AccessTokenUrl);
 
             if (!weChatRes.containsKey("openid")) {
                 log.error("获取微信openId失败:[{}]", JSON.toJSONString(weChatRes));
-                throw new BizException("授权失败,请重新授权");
+
+                throw new BizException(999,"授权失败,请重新授权");
             }
 
             return MapUtils.getString(weChatRes, "openid", "");
 
         } catch (Exception e) {
             log.error("获取微信openId失败:[{}]", e.getMessage(), e);
-            throw new BizException("授权失败,请重新授权");
+
+            throw new BizException(999,"授权失败,请重新授权");
         }
 
     }

+ 113 - 96
mec-common/common-core/src/main/java/com/ym/mec/common/controller/BaseController.java

@@ -32,75 +32,92 @@ import java.util.stream.Collectors;
 @ControllerAdvice
 public class BaseController {
 
-	private final static Logger logger = LoggerFactory.getLogger(BaseController.class);
-
-	public static <T> HttpResponseResult<T> succeed(T object) {
-		return getResponseData(true, HttpStatus.OK, object, "");
-	}
-
-	public static <T> HttpResponseResult<T> succeed() {
-		return getResponseData(true, HttpStatus.OK, null, "");
-	}
-
-	public static <T> HttpResponseResult<T> succeedData(T obj) {
-		return getResponseData(true, HttpStatus.OK, obj, "操作成功");
-	}
-
-	public static <T> HttpResponseResult<T> warned(String message) {
-		return failed(HttpStatus.MULTI_STATUS, message);
-	}
-
-	public static <T> HttpResponseResult<T> failed() {
-		return failed("");
-	}
-
-	public static <T> HttpResponseResult<T> failed(String msg) {
-		return failed(HttpStatus.INTERNAL_SERVER_ERROR, msg);
-	}
-
-	public static <T> HttpResponseResult<T> failed(HttpStatus statusCode, String msg) {
-		return getResponseData(false, statusCode, null, msg);
-	}
-
-	public static <T> HttpResponseResult<T> failed(HttpStatus statusCode, T data, String msg) {
-		return getResponseData(false, statusCode, data, msg);
-	}
-
-	private static <T> HttpResponseResult<T> getResponseData(boolean status, HttpStatus statusCode, T data, String message) {
-		HttpResponseResult<T> obj = new HttpResponseResult<T>();
-		obj.setStatus(status);
-		obj.setCode(statusCode.value());
-		obj.setData(data);
-		obj.setMsg(message);
-		return obj;
-	}
-
-	/**
-	 * 处理一般异常
-	 *
-	 * @param ex
-	 * @param request
-	 * @return
-	 */
-	@ExceptionHandler(Exception.class)
-	public HttpResponseResult<String> handleException(Exception ex, HttpServletRequest request) {
-		Throwable e = ExceptionUtils.getRootCause(ex);
-		if (e == null) {
-			e = ex;
-		}
-		logger.error("System Error", e);
-		// return failed(e.getMessage());
-		if (e instanceof BizException || e instanceof ThirdpartyException) {
-			if(e.getMessage().equals("205")){
-				return failed(HttpStatus.RESET_CONTENT,e.getMessage());
-			}
-			return failed(e.getMessage());
-		} else if (e instanceof AccessDeniedException) {
-			return failed("禁止访问");
-		}else if(e instanceof BindException){
-			String errors = ((BindException) e).getFieldErrors().stream().map(DefaultMessageSourceResolvable::getDefaultMessage).collect(Collectors.joining("\n"));
-			return failed(errors);
-		} else if(e instanceof MethodArgumentNotValidException){
+    private final static Logger logger = LoggerFactory.getLogger(BaseController.class);
+
+    public static <T> HttpResponseResult<T> succeed(T object) {
+        return getResponseData(true, HttpStatus.OK, object, "");
+    }
+
+    public static <T> HttpResponseResult<T> succeed() {
+        return getResponseData(true, HttpStatus.OK, null, "");
+    }
+
+    public static <T> HttpResponseResult<T> succeedData(T obj) {
+        return getResponseData(true, HttpStatus.OK, obj, "操作成功");
+    }
+
+    public static <T> HttpResponseResult<T> warned(String message) {
+        return failed(HttpStatus.MULTI_STATUS, message);
+    }
+
+    public static <T> HttpResponseResult<T> failed() {
+        return failed("");
+    }
+
+    public static <T> HttpResponseResult<T> failed(String msg) {
+        return failed(HttpStatus.INTERNAL_SERVER_ERROR, msg);
+    }
+
+    public static <T> HttpResponseResult<T> failed(HttpStatus statusCode, String msg) {
+        return getResponseData(false, statusCode, null, msg);
+    }
+
+    public static <T> HttpResponseResult<T> failed(HttpStatus statusCode, T data, String msg) {
+        return getResponseData(false, statusCode, data, msg);
+    }
+
+    private static <T> HttpResponseResult<T> getResponseData(boolean status, HttpStatus statusCode, T data, String message) {
+        HttpResponseResult<T> obj = new HttpResponseResult<T>();
+        obj.setStatus(status);
+        obj.setCode(statusCode.value());
+        obj.setData(data);
+        obj.setMsg(message);
+        return obj;
+    }
+
+    private static <T> HttpResponseResult<T> getResponseData(boolean status, int statusCode, T data, String message) {
+        HttpResponseResult<T> obj = new HttpResponseResult<T>();
+        obj.setStatus(status);
+        obj.setCode(statusCode);
+        obj.setData(data);
+        obj.setMsg(message);
+        return obj;
+    }
+
+    /**
+     * 处理一般异常
+     *
+     * @param ex
+     * @param request
+     * @return
+     */
+    @ExceptionHandler(Exception.class)
+    public HttpResponseResult<String> handleException(Exception ex, HttpServletRequest request) {
+        Throwable e = ExceptionUtils.getRootCause(ex);
+        if (e == null) {
+            e = ex;
+        }
+        logger.error("System Error", e);
+        // return failed(e.getMessage());
+        if (e instanceof BizException || e instanceof ThirdpartyException) {
+            if(e.getMessage().equals("205")){
+                return failed(HttpStatus.RESET_CONTENT,e.getMessage());
+            }
+
+            // 自定义错误码
+            if (e instanceof BizException) {
+                BizException bizException = (BizException) e;
+                return getResponseData(false, bizException.getCode(), null, bizException.getMessage());
+            }
+
+            // 默认返回错误码
+            return failed(e.getMessage());
+        } else if (e instanceof AccessDeniedException) {
+            return failed("禁止访问");
+        }else if(e instanceof BindException){
+            String errors = ((BindException) e).getFieldErrors().stream().map(DefaultMessageSourceResolvable::getDefaultMessage).collect(Collectors.joining("\n"));
+            return failed(errors);
+        } else if(e instanceof MethodArgumentNotValidException){
             MethodArgumentNotValidException validException = (MethodArgumentNotValidException) ex;
             String errorMsg = validException.getBindingResult()
                     .getFieldErrors()
@@ -109,34 +126,34 @@ public class BaseController {
                     .collect(Collectors.joining());
             return failed(errorMsg);
         }
-		try {
-			Map<String,Object> paramMap = new HashMap<>(2);
-			JSONObject jsonObject = new JSONObject();
-			jsonObject.put("content","系统繁忙请及时处理: " + request.getRequestURL() + "   " + e);
-			paramMap.put("text",jsonObject.toJSONString());
-			paramMap.put("msgtype","text");
-			Map<String,String> headers = new HashMap<>(1);
-			headers.put("Content-Type","application/json");
-			HttpUtil.postForHttps(dingTalkRobotsSecurityParam(),JSON.toJSONString(paramMap),headers);
-		}catch (Exception exception){
-			logger.error("System Error", exception);
-		}
+        try {
+            Map<String,Object> paramMap = new HashMap<>(2);
+            JSONObject jsonObject = new JSONObject();
+            jsonObject.put("content","系统繁忙请及时处理: " + request.getRequestURL() + "   " + e);
+            paramMap.put("text",jsonObject.toJSONString());
+            paramMap.put("msgtype","text");
+            Map<String,String> headers = new HashMap<>(1);
+            headers.put("Content-Type","application/json");
+            HttpUtil.postForHttps(dingTalkRobotsSecurityParam(),JSON.toJSONString(paramMap),headers);
+        }catch (Exception exception){
+            logger.error("System Error", exception);
+        }
         if (StringUtils.isNotBlank(e.getMessage())) {
             return failed(e.getMessage());
         }
-		return failed("系统繁忙");
-	}
-
-	public String dingTalkRobotsSecurityParam() throws Exception{
-		Long timestamp = System.currentTimeMillis();
-		String secret = "SEC5e3b73acccb12fc2a2a7d36d416c1967c66adb99a75dce24ecc324b50e528a29";
-		String stringToSign = timestamp + "\n" + secret;
-		Mac mac = Mac.getInstance("HmacSHA256");
-		mac.init(new SecretKeySpec(secret.getBytes("UTF-8"), "HmacSHA256"));
-		byte[] signData = mac.doFinal(stringToSign.getBytes("UTF-8"));
-		String sign = URLEncoder.encode(new String(Base64.encodeBase64(signData)),"UTF-8");
-		StringBuffer sb = new StringBuffer("https://api.dingtalk.com/robot/send?access_token=22d7b3b54ea7f1633c640dfdf17083d0731c3757719a84bd333740a8b18eb035&timestamp=");
-		sb.append(timestamp).append("&sign=").append(sign);
-		return sb.toString();
-	}
+        return failed("系统繁忙");
+    }
+
+    public String dingTalkRobotsSecurityParam() throws Exception{
+        Long timestamp = System.currentTimeMillis();
+        String secret = "SEC5e3b73acccb12fc2a2a7d36d416c1967c66adb99a75dce24ecc324b50e528a29";
+        String stringToSign = timestamp + "\n" + secret;
+        Mac mac = Mac.getInstance("HmacSHA256");
+        mac.init(new SecretKeySpec(secret.getBytes("UTF-8"), "HmacSHA256"));
+        byte[] signData = mac.doFinal(stringToSign.getBytes("UTF-8"));
+        String sign = URLEncoder.encode(new String(Base64.encodeBase64(signData)),"UTF-8");
+        StringBuffer sb = new StringBuffer("https://api.dingtalk.com/robot/send?access_token=22d7b3b54ea7f1633c640dfdf17083d0731c3757719a84bd333740a8b18eb035&timestamp=");
+        sb.append(timestamp).append("&sign=").append(sign);
+        return sb.toString();
+    }
 }

+ 40 - 29
mec-common/common-core/src/main/java/com/ym/mec/common/exception/BizException.java

@@ -4,33 +4,44 @@ import com.ym.mec.util.string.MessageFormatter;
 
 public class BizException extends RuntimeException {
 
-	/**
-	 * 
-	 */
-	private static final long serialVersionUID = -4562885563376566463L;
-
-	public BizException() {
-		super();
-	}
-
-	public BizException(String message) {
-		super(message);
-	}
-
-	public BizException(Throwable cause) {
-		super(cause);
-	}
-
-	public BizException(String message, Throwable cause) {
-		super(message, cause);
-	}
-
-	public BizException(String message, Object... arguments) {
-		super(MessageFormatter.arrayFormat(message, arguments));
-	}
-
-	public BizException(String message, Throwable cause, Object... args) {
-		super(MessageFormatter.arrayFormat(message, args), cause);
-	}
-
+    private int code = 500;
+    /**
+     *
+     */
+    private static final long serialVersionUID = -4562885563376566463L;
+
+    public BizException() {
+        super();
+    }
+
+    public BizException(String message) {
+        super(message);
+    }
+
+    public BizException(Integer code, String message) {
+        super(message);
+        // 统一错误码
+        this.code = code;
+    }
+
+    public BizException(Throwable cause) {
+        super(cause);
+    }
+
+    public BizException(String message, Throwable cause) {
+        super(message, cause);
+    }
+
+    public BizException(String message, Object... arguments) {
+        super(MessageFormatter.arrayFormat(message, arguments));
+    }
+
+    public BizException(String message, Throwable cause, Object... args) {
+        super(MessageFormatter.arrayFormat(message, args), cause);
+    }
+
+    public int getCode() {
+        return code;
+    }
 }
+