Browse Source

微信小程序api修改

wangliang 1 year ago
parent
commit
042e8a4ce8

+ 17 - 18
sop-business/src/main/java/com/qmth/sop/business/service/impl/TWxappInfoServiceImpl.java

@@ -64,8 +64,8 @@ public class TWxappInfoServiceImpl extends ServiceImpl<TWxappInfoMapper, TWxappI
     @Override
     public LoginResult wxappLogin(String loginCode) throws NoSuchAlgorithmException {
         JSONObject auth = getAuthorization(loginCode);
-        String openid = ByteUtil.toHexAscii(ShaUtils.sha256(auth.getString("openid")));
-        TWxappInfo tWxappInfo = tWxappInfoService.getOne(new QueryWrapper<TWxappInfo>().lambda().eq(TWxappInfo::getOpenId, openid));
+        String openId = ByteUtil.toHexAscii(ShaUtils.sha256(auth.getString(SystemConstant.OPEN_ID)));
+        TWxappInfo tWxappInfo = tWxappInfoService.getOne(new QueryWrapper<TWxappInfo>().lambda().eq(TWxappInfo::getOpenId, openId));
         Optional.ofNullable(tWxappInfo).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("微信用户未绑定"));
         List<SysUser> sysUserList = sysUserService.list(new QueryWrapper<SysUser>().lambda().eq(SysUser::getMobileNumber, tWxappInfo.getPhone()));
         if (CollectionUtils.isEmpty(sysUserList)) {
@@ -109,10 +109,10 @@ public class TWxappInfoServiceImpl extends ServiceImpl<TWxappInfoMapper, TWxappI
             throw ExceptionResultEnum.ERROR.exception("登录失败");
         }
         JSONObject jo = JSONObject.parseObject(ret);
-        if (Objects.nonNull(jo.getInteger("errcode")) && jo.getInteger("errcode").intValue() != 0) {
-            throw ExceptionResultEnum.ERROR.exception("登录失败," + jo.getString("errmsg"));
+        if (Objects.nonNull(jo.getInteger(SystemConstant.ERR_CODE)) && jo.getInteger(SystemConstant.ERR_CODE).intValue() != 0) {
+            throw ExceptionResultEnum.ERROR.exception("登录失败," + jo.getString(SystemConstant.ERR_MSG));
         }
-        String phone = jo.getJSONObject("phone_info").getString("purePhoneNumber");
+        String phone = jo.getJSONObject(SystemConstant.PHONE_INFO).getString(SystemConstant.PURE_PHONE_NUMBER);
         List<SysUser> sysUserList = sysUserService.list(new QueryWrapper<SysUser>().lambda().eq(SysUser::getMobileNumber, phone));
         if (CollectionUtils.isEmpty(sysUserList)) {
             throw ExceptionResultEnum.ERROR.exception("用户不存在");
@@ -125,9 +125,9 @@ public class TWxappInfoServiceImpl extends ServiceImpl<TWxappInfoMapper, TWxappI
             throw ExceptionResultEnum.ERROR.exception("该用户已禁用");
         }
         JSONObject auth = getAuthorization(loginCode);
-        String openid = ByteUtil.toHexAscii(ShaUtils.sha256(auth.getString("openid")));
-        TWxappInfo tWxappInfo = tWxappInfoService.getOne(new QueryWrapper<TWxappInfo>().lambda().eq(TWxappInfo::getOpenId, openid));
-        tWxappInfo = Objects.isNull(tWxappInfo) ? new TWxappInfo(openid, phone) : tWxappInfo;
+        String openId = ByteUtil.toHexAscii(ShaUtils.sha256(auth.getString(SystemConstant.OPEN_ID)));
+        TWxappInfo tWxappInfo = tWxappInfoService.getOne(new QueryWrapper<TWxappInfo>().lambda().eq(TWxappInfo::getOpenId, openId));
+        tWxappInfo = Objects.isNull(tWxappInfo) ? new TWxappInfo(openId, phone) : tWxappInfo;
         tWxappInfoService.saveOrUpdate(tWxappInfo);
         return sysUserService.login(sysUser.getPassword(), sysUser, AppSourceEnum.SYSTEM);
     }
@@ -144,9 +144,9 @@ public class TWxappInfoServiceImpl extends ServiceImpl<TWxappInfoMapper, TWxappI
     @Transactional
     public LoginResult wxappLoginByEncryptedData(String loginCode, String encryptedData, String iv) throws NoSuchAlgorithmException {
         JSONObject auth = getAuthorization(loginCode);
-        String openid = ByteUtil.toHexAscii(ShaUtils.sha256(auth.getString("openid")));
-        JSONObject jo = decrypt(encryptedData, iv, auth.getString("session_key"));
-        String phone = jo.getString("purePhoneNumber");
+        String openId = ByteUtil.toHexAscii(ShaUtils.sha256(auth.getString(SystemConstant.OPEN_ID)));
+        JSONObject jo = decrypt(encryptedData, iv, auth.getString(SystemConstant.SESSION_KEY));
+        String phone = jo.getString(SystemConstant.PURE_PHONE_NUMBER);
         List<SysUser> sysUserList = sysUserService.list(new QueryWrapper<SysUser>().lambda().eq(SysUser::getMobileNumber, phone));
         if (CollectionUtils.isEmpty(sysUserList)) {
             throw ExceptionResultEnum.ERROR.exception("用户不存在");
@@ -158,8 +158,8 @@ public class TWxappInfoServiceImpl extends ServiceImpl<TWxappInfoMapper, TWxappI
         if (!sysUser.getEnable()) {
             throw ExceptionResultEnum.ERROR.exception("该用户已禁用");
         }
-        TWxappInfo tWxappInfo = tWxappInfoService.getOne(new QueryWrapper<TWxappInfo>().lambda().eq(TWxappInfo::getOpenId, openid));
-        tWxappInfo = Objects.isNull(tWxappInfo) ? new TWxappInfo(openid, phone) : tWxappInfo;
+        TWxappInfo tWxappInfo = tWxappInfoService.getOne(new QueryWrapper<TWxappInfo>().lambda().eq(TWxappInfo::getOpenId, openId));
+        tWxappInfo = Objects.isNull(tWxappInfo) ? new TWxappInfo(openId, phone) : tWxappInfo;
         tWxappInfoService.saveOrUpdate(tWxappInfo);
         return sysUserService.login(sysUser.getPassword(), sysUser, AppSourceEnum.SYSTEM);
     }
@@ -173,7 +173,6 @@ public class TWxappInfoServiceImpl extends ServiceImpl<TWxappInfoMapper, TWxappI
      * @return
      */
     private JSONObject decrypt(String encryptedData, String iv, String sessionKey) {
-        String cipherString = "AES/CBC/PKCS5Padding";
         String jsonStr;
         try {
             BASE64Decoder base64Decoder = new BASE64Decoder();
@@ -190,8 +189,8 @@ public class TWxappInfoServiceImpl extends ServiceImpl<TWxappInfoMapper, TWxappI
             /**
              * 以下为AES-128-CBC解密算法
              */
-            SecretKeySpec skeySpec = new SecretKeySpec(sessionKeyByte, "AES");
-            Cipher cipher = Cipher.getInstance(cipherString);
+            SecretKeySpec skeySpec = new SecretKeySpec(sessionKeyByte, SystemConstant.AES);
+            Cipher cipher = Cipher.getInstance(SystemConstant.AES_CBC_PKCS5);
             IvParameterSpec ivParameterSpec = new IvParameterSpec(ivByte);
             cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivParameterSpec);
             byte[] original = cipher.doFinal(encryptedByte);
@@ -230,8 +229,8 @@ public class TWxappInfoServiceImpl extends ServiceImpl<TWxappInfoMapper, TWxappI
             throw ExceptionResultEnum.ERROR.exception("登录失败");
         }
         JSONObject jo = JSONObject.parseObject(ret);
-        if (jo.containsKey("errcode")) {
-            throw ExceptionResultEnum.ERROR.exception("登录失败," + jo.getString("errmsg"));
+        if (jo.containsKey(SystemConstant.ERR_CODE)) {
+            throw ExceptionResultEnum.ERROR.exception("登录失败," + jo.getString(SystemConstant.ERR_MSG));
         }
         return jo;
     }

+ 8 - 0
sop-common/src/main/java/com/qmth/sop/common/contant/SystemConstant.java

@@ -113,6 +113,14 @@ public class SystemConstant {
     public static final String SECRET = "secret";
     public static final String JS_CODE = "js_code";
     public static final String GRANT_TYPE = "grant_type";
+    public static final String OPEN_ID = "openid";
+    public static final String SESSION_KEY = "session_key";
+    public static final String PHONE_INFO = "phone_info";
+    public static final String PURE_PHONE_NUMBER = "purePhoneNumber";
+    public static final String ERR_CODE = "errcode";
+    public static final String ERR_MSG = "errmsg";
+    public static final String AES_CBC_PKCS5 = "AES/CBC/PKCS5Padding";
+    public static final String AES = "AES";
 
     /**
      * sop列表转换字段