Ver código fonte

用户注册

loemkie 1 mês atrás
pai
commit
53349848ed

+ 64 - 4
src/main/java/com/qmrb/system/controller/UserController.java

@@ -1,18 +1,29 @@
 package com.qmrb.system.controller;
 
 
+import com.qmrb.system.framework.security.JwtTokenManager;
+import com.qmrb.system.framework.security.userdetails.SysUserDetails;
+import com.qmrb.system.pojo.bo.UserAuthInfo;
 import com.qmrb.system.pojo.dto.LoginParam;
 import com.qmrb.system.pojo.dto.UserParam;
+import com.qmrb.system.pojo.form.UserForm;
 import com.qmrb.system.pojo.vo.UserInfo;
 import com.qmrb.system.pojo.vo.WxLoginVO;
+import com.qmrb.system.service.SysUserService;
 import com.qmrb.system.service.ToolWxConfigService;
 import com.qmrb.system.service.UserService;
 import com.qmrb.system.wxpayback.api.CommonController;
 import com.qmrb.system.wxpayback.api.CommonResult;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
+import org.springframework.security.core.authority.SimpleGrantedAuthority;
+import org.springframework.security.core.context.SecurityContextHolder;
 import org.springframework.web.bind.annotation.*;
 
+import java.util.List;
+
 /**
  * <p>
  * 用户中心 前端控制器
@@ -25,12 +36,16 @@ import org.springframework.web.bind.annotation.*;
 @RequestMapping("/api/ucenter")
 @Slf4j
 public class UserController extends CommonController {
-
+    @Autowired
+    private  JwtTokenManager jwtTokenManager;
 
     @Autowired
     private UserService userService;
 
     @Autowired
+    private SysUserService sysUserService;
+
+    @Autowired
     private ToolWxConfigService toolWxConfigService;
 
     /**
@@ -40,17 +55,62 @@ public class UserController extends CommonController {
      */
     @PostMapping("/wxLogin")
     public CommonResult<WxLoginVO> wxLogin(@RequestBody  WxLoginVO loginVO){
-        return process(() -> userService.getOpenId(toolWxConfigService.findConf(),loginVO));
+        WxLoginVO wxLoginVO = userService.getOpenId(toolWxConfigService.findConf(),loginVO);
+        if(wxLoginVO != null){
+
+        }
+        return process(() -> wxLoginVO);
     }
 
     /**
-     * 获取当前默认用户信息
+     * 获取当前默认用户信息以及toen信息
      * @param userParam
      * @return
      */
     @GetMapping("/getMyInfo")
     public CommonResult<UserInfo> getMyInfo(UserParam userParam){
-        return process(() -> userService.getMyInfo(userParam));
+        UserInfo userInfo = new UserInfo();
+        if(StringUtils.isNotBlank(userParam.getOpenId())){
+            UserAuthInfo userAuthInfo = sysUserService.getUserAuthInfo(userParam.getOpenId());
+            if(userAuthInfo == null){
+                //如果没有注册,需要注册该微信用户
+                UserForm userForm = new UserForm();
+                userForm.setUsername(userParam.getOpenId());
+                userForm.setNickname(userParam.getNickName());
+                userForm.setAvatar(userParam.getAvatarUrl());
+                userForm.setGender(userParam.getGender());
+                sysUserService.saveUser(userForm);
+                userInfo.setId(userForm.getId().toString());
+                userInfo.setNickName(userParam.getNickName());
+                userInfo.setAvatarUrl(userParam.getAvatarUrl());
+                //生成token
+                userAuthInfo = new UserAuthInfo();
+                userAuthInfo.setUsername(userParam.getOpenId());
+                userInfo.setAccessToken(getToken(userAuthInfo));
+            }else{
+                userInfo.setId(userAuthInfo.getUserId().toString());
+                userInfo.setNickName(userAuthInfo.getNickname());
+                userInfo.setAvatarUrl(userParam.getAvatarUrl());
+                //生成token
+                userInfo.setAccessToken(getToken(userAuthInfo));
+            }
+        }else{
+            //登录失败
+            log.info("获取用户信息失败");
+        }
+        return process(() -> userInfo);
+    }
+
+    private String getToken(UserAuthInfo authInfo){
+        List<SimpleGrantedAuthority> roleList = authInfo.getRoles()
+                .stream().map(role -> new SimpleGrantedAuthority("ROLE_" + role)).toList();
+        UsernamePasswordAuthenticationToken authenticationToken =
+                new UsernamePasswordAuthenticationToken(authInfo.getUsername(), authInfo.getUsername(),
+                        roleList);
+        SysUserDetails sysUserDetails = new SysUserDetails(authInfo);
+        authenticationToken.setDetails(sysUserDetails);
+        SecurityContextHolder.getContext().setAuthentication(authenticationToken);
+        return jwtTokenManager.createToken(authenticationToken);
     }
 
     /**

+ 3 - 1
src/main/java/com/qmrb/system/pojo/dto/UserParam.java

@@ -11,6 +11,8 @@ import lombok.NoArgsConstructor;
 @EqualsAndHashCode(callSuper = false)
 public class UserParam {
     String id;
-    String nickname;
+    String nickName;
     String openId;
+    String avatarUrl;
+    Integer gender;
 }

+ 2 - 0
src/main/java/com/qmrb/system/pojo/vo/UserInfo.java

@@ -13,4 +13,6 @@ public class UserInfo {
     String id;
     String nickName;
     String avatarUrl;
+
+    String accessToken;
 }

+ 4 - 0
src/main/java/com/qmrb/system/pojo/vo/WxLoginVO.java

@@ -11,6 +11,10 @@ import lombok.NoArgsConstructor;
 @EqualsAndHashCode(callSuper = false)
 public class WxLoginVO {
     String code;
+
     String openId;
     String session_key;
+    String nickName;
+    Integer gender;
+    String avatarUrl;
 }

+ 3 - 0
src/main/java/com/qmrb/system/service/impl/UserServiceImpl.java

@@ -46,6 +46,9 @@ public class UserServiceImpl extends ServiceImpl<OrderMapper, Order> implements
         WxLoginVO result = new WxLoginVO();
         result.setOpenId(openid);
         result.setSession_key(sessionKey);
+//        result.setNickName(jsonObject.getStr("nickName"));
+//        result.setAvatarUrl(jsonObject.getStr("avatarUrl"));
+//        result.setGender(jsonObject.getInt("gender"));
         return result;
     }