Explorar el Código

公众号登录

Althars123 hace 2 años
padre
commit
3bf6e35292

+ 35 - 16
sdjk-admin/src/main/java/com/miaxis/system/controller/system/SysLoginController.java

@@ -85,7 +85,11 @@ public class SysLoginController
 
     private static Logger logger = LoggerFactory.getLogger(SysLoginController.class);
 
+    @Value("${zzgzh.appId}")
+    private String gzhAppid;
 
+    @Value("${zzgzh.appSecret}")
+    private String gzhAppSecret;
 
 
     @Value("${app.appId}")
@@ -327,22 +331,37 @@ public class SysLoginController
 
     }
 
-//
-//    /**
-//     * 用户 jscode登录
-//     *
-//     */
-//    @PostMapping("/login/jscode-test")
-//    @ApiOperation("用户jscode登录(测试,固定openid)")
-//    public Response<TokenDTO> getInfoTest(String jscode){
-//        TokenDTO tokenDTO = new TokenDTO();
-//            // 生成令牌
-//        String token = loginService.login("oO7PJ5CPQJo62kZWA3uiUX2KG2s4",null, StudentLoginTypeEnum.AUTHORIZATION_CODE_LOGIN.getCode());
-//        tokenDTO.setToken(token);
-//        return Response.success(tokenDTO) ;
-//
-//
-//    }
+    /**
+     * 用户 openid登录
+     *
+     */
+    @PostMapping("/login/gzhcode")
+    @ApiOperation("公众号微信用户授权码模式登录")
+    public Response<TokenDTO> loginByAuthorizationGzhCode(String authorizationCode ){
+        String wxResultStr = wxService.getWxToken(gzhAppid, gzhAppSecret, 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("openid",wxUserInfo.getOpenid());
+        ServletUtils.getRequest().setAttribute("headImage",wxUserInfo.getHeadimgurl());
+        ServletUtils.getRequest().setAttribute("nickName",wxUserInfo.getNickname());
+        String token = loginService.login(wxUserInfo.getUnionid(),null, StudentLoginTypeEnum.AUTHORIZATION_CODE_GZH_LOGIN.getCode());
+        TokenDTO tokenDTO = new TokenDTO();
+        tokenDTO.setToken(token);
+        tokenDTO.setThirdResult(wxUserInfo);
+        return Response.success(tokenDTO) ;
+
+
+
+    }
 
 
     /**

+ 1 - 0
sdjk-common/src/main/java/com/miaxis/common/enums/StudentLoginTypeEnum.java

@@ -12,6 +12,7 @@ public enum StudentLoginTypeEnum
     IOS_CODE_LOGIN("2", "IOS授权码登录"),
     AUTHORIZATION_CODE_DY_LOGIN("3", "app抖音授权码登录"),
     AUTHORIZATION_CODE_TT_LOGIN("4", "字节小程序授权码登录"),
+    AUTHORIZATION_CODE_GZH_LOGIN("5", "公众号微信授权码登录"),
     ;
 
     private final String code;

+ 1 - 1
sdjk-framework/src/main/java/com/miaxis/framework/config/SecurityConfig.java

@@ -102,7 +102,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter
                 // 对于app 开放页面,允许任意访问
                 .antMatchers("/**/open-api/**","/system/config/configKey/**").permitAll()
                 // 对于登录login 验证码captchaImage 允许匿名访问
-                .antMatchers("/login", "/login/noCode","/captchaImage","/login/code",
+                .antMatchers("/login", "/login/noCode","/captchaImage","/login/code","/login/gzhcode",
                         "/login/code/test","/login/jscode-test","/login/code/ios","/login/dycode",
                         "/login/ttcode","/login/ttcodeTest").anonymous()
                 //.antMatchers("/student/open/**").permitAll()

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

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

+ 25 - 0
sdjk-framework/src/main/java/com/miaxis/framework/web/service/UserDetailsServiceImpl.java

@@ -93,6 +93,31 @@ public class UserDetailsServiceImpl implements UserDetailsService
 
             }
         }
+        else if (StudentLoginTypeEnum.AUTHORIZATION_CODE_GZH_LOGIN.getCode().equals(loginType)){
+            {
+                //由于之前没有保存用户unionid,只有openid,所以这里判定两个都不存在才不存在用户
+                UserInfo userInfoGetByUnionId = userService.getStudentByUnionId(identification);
+                String gzhOpenid = (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.setThirdPlatform("weixin");
+                    userInfo.setGzhOpenid(gzhOpenid);
+                    userInfo.setUnionId(identification);
+                    userInfo.setIsVip(0);
+                    userService.saveUserInfo(userInfo);
+                    return createLoginUser(userService.getStudentByUnionId(identification));
+                }else {
+                    userInfoGetByUnionId.setHeadImage((String) ServletUtils.getRequest().getAttribute("headImage"));
+                    userInfoGetByUnionId.setNickName((String) ServletUtils.getRequest().getAttribute("nickName"));
+                    userInfoGetByUnionId.setThirdPlatform("weixin");
+                    userService.updateStudent(userInfoGetByUnionId);
+                    return createLoginUser(userService.getStudentByUnionId(identification));
+                }
+
+            }
+        }
         else if (StudentLoginTypeEnum.AUTHORIZATION_CODE_DY_LOGIN.getCode().equals(loginType)){
                 UserInfo userInfoGetByUnionId = userService.getStudentByUnionId(identification);
                 String openid = (String)ServletUtils.getRequest().getAttribute("openid");;

+ 1 - 0
sdjk-service/src/main/java/com/miaxis/wx/service/impl/WxGzhServiceImpl.java

@@ -179,6 +179,7 @@ public class WxGzhServiceImpl implements IWxGzhService {
                         UserInfo userInfo = new UserInfo();
                         userInfo.setGzhOpenid(fromUserName);
                         userInfo.setUnionId(unionId);
+                        userInfo.setThirdPlatform("weixin");
                         userInfoService.save(userInfo);
                     }else if(user.getGzhOpenid()==null){
                         user.setGzhOpenid(fromUserName);

+ 2 - 0
sdjk-system/src/main/java/com/miaxis/system/dto/system/TokenDTO.java

@@ -1,5 +1,6 @@
 package com.miaxis.system.dto.system;
 
+import com.miaxis.common.core.domain.WxUserInfo;
 import com.miaxis.common.core.domain.model.WxResult;
 import io.swagger.annotations.ApiModel;
 import io.swagger.annotations.ApiModelProperty;
@@ -19,4 +20,5 @@ public class TokenDTO {
     private String token;
     @ApiModelProperty(value = "第三方登录返回数据")
     private Object thirdResult;
+
 }

+ 2 - 2
sdjk-system/src/main/resources/mapper/system/SysUserMapper.xml

@@ -107,8 +107,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 		where name = #{name}
 	</select>
 	<insert id="saveUserInfo" parameterType="com.miaxis.common.core.domain.entity.UserInfo" >
-		insert into user_info (head_image,nick_name,xcx_openid,app_openid,union_id,third_platform)
-		values (#{headImage},#{nickName},#{xcxOpenid},#{appOpenid},#{unionId},#{thirdPlatform})
+		insert into user_info (head_image,nick_name,xcx_openid,app_openid,union_id,third_platform,gzh_openid)
+		values (#{headImage},#{nickName},#{xcxOpenid},#{appOpenid},#{unionId},#{thirdPlatform},#{gzhOpenid})
 	</insert>
 
 	<select id="getAgentList" resultType="com.miaxis.common.core.domain.vo.AgentVO">