Althars123 2 жил өмнө
parent
commit
78229777e7

+ 32 - 3
nbjk-admin/src/main/java/com/miaxis/system/controller/system/SysLoginController.java

@@ -144,11 +144,10 @@ public class SysLoginController
     }
 
     /**
-     * 用户 openid登录
      *
      */
     @PostMapping("/login/code")
-    @ApiOperation("微信用户授权码模式登录")
+    @ApiOperation("app微信用户授权码模式登录")
     public Response<TokenDTO> loginByWxAuthorizationCode(String authorizationCode ){
         String wxResultStr = wxService.getWxToken(appid, appSecret, authorizationCode, "authorization_code");
         logger.info("微信授权码登录返回值:"+wxResultStr);
@@ -163,7 +162,6 @@ public class SysLoginController
         WxUserInfo wxUserInfo = JSONObject.parseObject(userInfoStr,WxUserInfo.class);
         ServletUtils.getRequest().setAttribute("headImage",wxUserInfo.getHeadimgurl());
         ServletUtils.getRequest().setAttribute("nickName",wxUserInfo.getNickname());
-        ServletUtils.getRequest().setAttribute("third","weixin");
         ServletUtils.getRequest().setAttribute("openid",wxUserInfo.getOpenid());
         String token = loginService.login(wxUserInfo.getUnionid(),null, StudentLoginTypeEnum.AUTHORIZATION_XCX_LOGIN.getCode());
         TokenDTO tokenDTO = new TokenDTO();
@@ -173,6 +171,36 @@ public class SysLoginController
 
 
 
+    }
+
+    /**
+     * 用户 openid登录
+     *
+     */
+    @PostMapping("/login/gzhcode")
+    @ApiOperation("公众号h5用户授权码模式登录")
+    public Response<TokenDTO> loginByAuthorizationCode(String authorizationCode ){
+        String wxResultStr = wxService.getWxToken(appid, appSecret, authorizationCode, "authorization_code");
+        logger.info("微信授权码登录返回值:"+wxResultStr);
+        WxResult wxResult = JSONObject.parseObject(wxResultStr,WxResult.class);
+
+        // 生成令牌
+        if (wxResult.getErrcode() != null){
+            throw new CustomException("微信授权无效,请重新授权");
+        }
+        String userInfoStr = wxService.getUserInfo("zh_CN", wxResult.getAccess_token(), wxResult.getOpenid());
+        WxUserInfo wxUserInfo = JSONObject.parseObject(userInfoStr,WxUserInfo.class);
+        ServletUtils.getRequest().setAttribute("headImage",wxUserInfo.getHeadimgurl());
+        ServletUtils.getRequest().setAttribute("nickName",wxUserInfo.getNickname());
+        ServletUtils.getRequest().setAttribute("openid",wxUserInfo.getOpenid());
+        String token = loginService.login(wxUserInfo.getUnionid(),null, StudentLoginTypeEnum.AUTHORIZATION_H5_LOGIN.getCode());
+        TokenDTO tokenDTO = new TokenDTO();
+        tokenDTO.setToken(token);
+        tokenDTO.setThirdResult(wxUserInfo);
+        return Response.success(tokenDTO) ;
+
+
+
     }
 
 
@@ -198,6 +226,7 @@ public class SysLoginController
             ServletUtils.getRequest().setAttribute("xcxOpenid",wxUserInfo.getOpenid());
             String token = loginService.login(wxUserInfo.getUnionid(),null, StudentLoginTypeEnum.AUTHORIZATION_XCX_LOGIN.getCode());
             tokenDTO.setToken(token);
+            tokenDTO.setThirdResult(wxUserInfo);
             return Response.success(tokenDTO) ;
         }
         else{

+ 2 - 2
nbjk-common/src/main/java/com/miaxis/common/enums/StudentLoginTypeEnum.java

@@ -10,8 +10,8 @@ public enum StudentLoginTypeEnum
 
     AUTHORIZATION_CODE_LOGIN("1", "app微信授权码登录"),
     IOS_CODE_LOGIN("2", "IOS授权码登录"),
-    AUTHORIZATION_XCX_LOGIN("3", "小程序登录");
-    ;
+    AUTHORIZATION_XCX_LOGIN("3", "小程序登录"),
+    AUTHORIZATION_H5_LOGIN("4", "app微信授权码登录");
 
     private final String code;
     private final String info;

+ 1 - 1
nbjk-framework/src/main/java/com/miaxis/framework/web/service/SysLoginService.java

@@ -101,7 +101,7 @@ public class SysLoginService
 
         // 用户验证
         if (StudentLoginTypeEnum.AUTHORIZATION_CODE_LOGIN.getCode().equals(loginType)||StudentLoginTypeEnum.IOS_CODE_LOGIN.getCode().equals(loginType)
-        ||StudentLoginTypeEnum.AUTHORIZATION_XCX_LOGIN.getCode().equals(loginType)){
+        ||StudentLoginTypeEnum.AUTHORIZATION_XCX_LOGIN.getCode().equals(loginType)||StudentLoginTypeEnum.AUTHORIZATION_H5_LOGIN.getCode().equals(loginType)){
             return authenticate(principal,new OpenIdAuthenticationToken(principal));
         }
         else{

+ 23 - 0
nbjk-framework/src/main/java/com/miaxis/framework/web/service/UserDetailsServiceImpl.java

@@ -87,6 +87,29 @@ public class UserDetailsServiceImpl implements UserDetailsService
 
             }
         }
+        else if (StudentLoginTypeEnum.AUTHORIZATION_H5_LOGIN.getCode().equals(loginType)){
+            {
+                UserInfo userInfoGetByUnionId = userService.getStudentByUnionId(identification);
+                String openid = (String)ServletUtils.getRequest().getAttribute("openid");
+                if (userInfoGetByUnionId == null ) {
+                    UserInfo userInfo = new UserInfo();
+                    userInfo.setHeadImage((String) ServletUtils.getRequest().getAttribute("headImage"));
+                    userInfo.setNickName((String) ServletUtils.getRequest().getAttribute("nickName"));
+                    userInfo.setGzhOpenid(openid);
+                    userInfo.setUnionId(identification);
+                    userInfo.setIsVip(0);
+                    userService.saveUserInfo(userInfo);
+                    return createLoginUser(userService.getStudentByUnionId(identification));
+                }else {
+                    UserInfo userInfo = userInfoGetByUnionId ;
+                    userInfo.setHeadImage((String) ServletUtils.getRequest().getAttribute("headImage"));
+                    userInfo.setNickName((String) ServletUtils.getRequest().getAttribute("nickName"));
+                    userService.updateStudent(userInfo);
+                    return createLoginUser(userService.getStudentByUnionId(identification));
+                }
+
+            }
+        }
         else if (StudentLoginTypeEnum.AUTHORIZATION_XCX_LOGIN.getCode().equals(loginType)){
             {
                 UserInfo userInfoGetByUnionId = userService.getStudentByUnionId(identification);