|
@@ -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);
|
|
|
}
|
|
|
|
|
|
/**
|