瀏覽代碼

简单优化,添加测试用的mockService

ximinghao 3 月之前
父節點
當前提交
e41baada53

+ 10 - 19
src/main/java/com/skyversation/xjcy/oauth/WxAuthService.java

@@ -2,6 +2,7 @@ package com.skyversation.xjcy.oauth;
 
 import com.alibaba.fastjson.JSONObject;
 import com.skyversation.xjcy.util.HttpUtil;
+import lombok.AllArgsConstructor;
 import lombok.Getter;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
@@ -12,11 +13,12 @@ import org.springframework.util.MultiValueMap;
 @Service
 public class WxAuthService {
     @Getter
+    @AllArgsConstructor
     public static class Result {
-        private boolean success;
-        private String error;
-        private String wxOpenId;
-        private String wxSessionKey;
+        private final boolean success;
+        private final String error;
+        private final String wxOpenId;
+        private final String wxSessionKey;
     }
     @Value("${app.wechat.secret-key}")
     private String secretKey;
@@ -37,24 +39,13 @@ public class WxAuthService {
             String sessionKey = jsonObject.getString("session_key");
             Integer errcode = jsonObject.getInteger("errcode");
             if (errcode == 0) {
-                Result result = new Result();
-                result.wxSessionKey = sessionKey;
-                result.wxOpenId = openId;
-                result.success = true;
-                return result;
+                return new Result(true,null,openId,sessionKey);
             }else {
-                Result result = new Result();
-                if (errcode ==40029){
-                    result.error = "提供的code无效";
-                }else {
-                    result.error = "微信鉴权异常"+errcode;
-                }
-                return result;
+                return new Result(false,errcode ==40029?"提供的code无效":"微信鉴权异常"+errcode,null,null);
             }
         } catch (NullPointerException e) {
-            Result result = new Result();
-            result.error = "无法访问wx鉴权接口";
-            return result;
+            return new Result(false,"无法访问wx鉴权接口",null,null);
         }
     }
+
 }

+ 11 - 0
src/main/java/com/skyversation/xjcy/oauth/WxAuthServiceMock.java

@@ -0,0 +1,11 @@
+package com.skyversation.xjcy.oauth;
+
+import org.springframework.stereotype.Service;
+
+@Service
+public class WxAuthServiceMock extends WxAuthService {
+    @Override
+    public Result wxLogin(String code) {
+        return new Result(true,null,"abc1234567890defabc1234567890def","");
+    }
+}