Althars123 il y a 3 ans
Parent
commit
90eb78e54c

+ 15 - 3
jkt-admin/src/main/java/com/miaxis/system/controller/system/SysLoginController.java

@@ -3,6 +3,7 @@ package com.miaxis.system.controller.system;
 import com.alibaba.fastjson.JSONObject;
 import com.miaxis.apple.service.IAppleService;
 import com.miaxis.common.core.domain.Response;
+import com.miaxis.common.core.domain.WxUserInfo;
 import com.miaxis.common.core.domain.entity.SysMenu;
 import com.miaxis.common.core.domain.entity.SysUser;
 import com.miaxis.common.core.domain.model.LoginBody;
@@ -134,7 +135,11 @@ public class SysLoginController
         if (wxResult.getErrcode() != null){
             throw new CustomException("微信授权无效,请重新授权");
         }
-        String token = loginService.login(wxResult.getOpenid(),null, StudentLoginTypeEnum.AUTHORIZATION_CODE_LOGIN.getCode());
+        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());
+        String token = loginService.login(wxUserInfo.getOpenid(),null, StudentLoginTypeEnum.AUTHORIZATION_CODE_LOGIN.getCode());
         TokenDTO tokenDTO = new TokenDTO();
         tokenDTO.setToken(token);
         tokenDTO.setWxResult(wxResult);
@@ -146,7 +151,14 @@ public class SysLoginController
     @PostMapping("/login/code/test")
     @ApiOperation("用户授权码模式登录--测试")
     public Response<TokenDTO> testloginByAuthorizationCode(String authorizationCode ){
-        String token = loginService.login("oN0Np5sK6JeTRa06hlE4-OkHDlDY",null, StudentLoginTypeEnum.AUTHORIZATION_CODE_LOGIN.getCode());
+        WxUserInfo wxUserInfo = new WxUserInfo();
+        wxUserInfo.setOpenid("oN0Np5sK6JeTRa06hlE4-OkHDlDY");
+        wxUserInfo.setNickname("Sss");
+        wxUserInfo.setSex("0");
+        wxUserInfo.setHeadimgurl("https://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83erGMFzKaOmz6cIaA0J1nicWSlBLdLKY46N5CNCoASiajT7Z3RIfPngPhJsjmqsSjGCFibPU4vCtfz3aw/132");
+        ServletUtils.getRequest().setAttribute("headImage",wxUserInfo.getHeadimgurl());
+        ServletUtils.getRequest().setAttribute("nickName",wxUserInfo.getNickname());
+        String token = loginService.login(wxUserInfo.getOpenid(),null, StudentLoginTypeEnum.AUTHORIZATION_CODE_LOGIN.getCode());
         TokenDTO tokenDTO = new TokenDTO();
         tokenDTO.setToken(token);
         return Response.success(tokenDTO) ;
@@ -160,7 +172,7 @@ public class SysLoginController
     public Response<AppleTokenDTO> ioslogin(String identityToken){
         try {
             JSONObject appleUser = appleService.getAppleUserInfo(identityToken);
-            String token = loginService.login(appleUser.getString("sub"),null, StudentLoginTypeEnum.AUTHORIZATION_CODE_LOGIN.getCode());
+            String token = loginService.login(appleUser.getString("sub"),null, StudentLoginTypeEnum.IOS_CODE_LOGIN.getCode());
             AppleTokenDTO tokenDTO = new AppleTokenDTO();
             tokenDTO.setAppleUserInfo(appleUser);
             tokenDTO.setToken(token);

+ 18 - 0
jkt-common/src/main/java/com/miaxis/common/core/domain/WxUserInfo.java

@@ -0,0 +1,18 @@
+package com.miaxis.common.core.domain;
+
+
+import lombok.Data;
+
+@Data
+public class WxUserInfo {
+     String openid;
+     String nickname;
+     String sex;
+     String province;
+     String city;
+     String country;
+     String headimgurl;
+     String[] privilege;
+     String unionid;
+
+}

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

@@ -7,7 +7,8 @@ package com.miaxis.common.enums;
  */
 public enum StudentLoginTypeEnum
 {
-    AUTHORIZATION_CODE_LOGIN("1", "授权码登录");
+    AUTHORIZATION_CODE_LOGIN("1", "授权码登录"),
+    IOS_CODE_LOGIN("2", "IOS授权码登录");
 
     private final String code;
     private final String info;

+ 2 - 0
jkt-framework/src/main/java/com/miaxis/framework/web/service/SysLoginService.java

@@ -1,6 +1,7 @@
 package com.miaxis.framework.web.service;
 
 import com.miaxis.common.constant.Constants;
+import com.miaxis.common.core.domain.WxUserInfo;
 import com.miaxis.common.core.domain.model.LoginUser;
 import com.miaxis.common.core.redis.RedisCache;
 import com.miaxis.common.enums.StudentLoginTypeEnum;
@@ -97,6 +98,7 @@ public class SysLoginService
     public String login(String principal, String credential, String loginType) {
 
         ServletUtils.getRequest().setAttribute("loginType",loginType);
+
         // 用户验证
         if (StudentLoginTypeEnum.AUTHORIZATION_CODE_LOGIN.getCode().equals(loginType)){
             return authenticate(principal,new OpenIdAuthenticationToken(principal));

+ 21 - 3
jkt-framework/src/main/java/com/miaxis/framework/web/service/UserDetailsServiceImpl.java

@@ -69,14 +69,32 @@ public class UserDetailsServiceImpl implements UserDetailsService
                 UserInfo userInfo = userService.getStudentByOpenId(identification);
                 if (userInfo == null) {
                     userInfo = new UserInfo();
-//                    userInfo.setHeadImage(configService.selectConfigByKey("xcx_user_image"));
-//                    userInfo.setNickName("用户未授权_"+RandomNameUtils.generateName());
+                    userInfo.setHeadImage((String) ServletUtils.getRequest().getAttribute("headImage"));
+                    userInfo.setNickName((String) ServletUtils.getRequest().getAttribute("nickName"));
                     userInfo.setOpenid(identification);
                     userService.saveUserInfo(userInfo);
+                }else {
+                    userInfo.setHeadImage((String) ServletUtils.getRequest().getAttribute("headImage"));
+                    userInfo.setNickName((String) ServletUtils.getRequest().getAttribute("nickName"));
+                    userService.updateStudent(userInfo);
                 }
                 return createLoginUser(userInfo);
             }
-        } else{
+        }
+        else if (StudentLoginTypeEnum.IOS_CODE_LOGIN.getCode().equals(loginType)){
+            {
+                UserInfo userInfo = userService.getStudentByOpenId(identification);
+                if (userInfo == null) {
+                    userInfo = new UserInfo();
+                    userInfo.setOpenid(identification);
+                    userService.saveUserInfo(userInfo);
+                }
+                return createLoginUser(userInfo);
+            }
+        }
+
+
+        else{
             throw new CustomException("登录类型不存在");
         }
 

+ 13 - 0
jkt-service/src/main/java/com/miaxis/wx/service/WxService.java

@@ -30,4 +30,17 @@ public interface WxService {
             @RequestParam("code") String code,
             @RequestParam("grant_type") String grantType);
 
+
+
+    /**
+     * 获取用户信息
+     */
+    @GetMapping(value = "/sns/userinfo")
+    String getUserInfo(
+            @RequestParam("lang") String lang,
+            @RequestParam("access_token") String accessToken,
+            @RequestParam("openid") String openid
+    );
+
+
 }

+ 2 - 0
jkt-system/src/main/java/com/miaxis/system/mapper/SysUserMapper.java

@@ -119,4 +119,6 @@ public interface SysUserMapper
     void saveUserInfo(UserInfo userInfo);
 
     List<AgentVO> getAgentList(@Param("roleId")Long roleId, @Param("userName")String userName);
+
+    void updateStudent(UserInfo userInfo);
 }

+ 2 - 0
jkt-system/src/main/java/com/miaxis/system/service/ISysUserService.java

@@ -179,4 +179,6 @@ public interface ISysUserService
     void saveUserInfo(UserInfo userInfo);
 
     List<AgentVO> getAgentList(Long roleId, String agentName);
+
+    void updateStudent(UserInfo userInfo);
 }

+ 5 - 0
jkt-system/src/main/java/com/miaxis/system/service/impl/SysUserServiceImpl.java

@@ -468,4 +468,9 @@ public class SysUserServiceImpl implements ISysUserService
     public List<AgentVO> getAgentList(Long roleId, String agentName) {
         return userMapper.getAgentList(roleId,agentName);
     }
+
+    @Override
+    public void updateStudent(UserInfo userInfo) {
+        userMapper.updateStudent(userInfo);
+    }
 }

+ 3 - 1
jkt-system/src/main/resources/mapper/system/SysUserMapper.xml

@@ -115,7 +115,9 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 
 	</select>
 
-
+	<insert id="updateStudent" parameterType="com.miaxis.common.core.domain.entity.UserInfo" >
+		update user_info set head_image = #{headImage},nick_name = #{nickName} where openid = #{openid}
+	</insert>
 	<select id="selectUserById" parameterType="Long" resultMap="SysUserResult">
 		<include refid="selectUserVo"/>
 		where u.user_id = #{userId}