完善了用户登录

This commit is contained in:
chen-xin-zhi 2024-11-04 10:18:18 +08:00
parent c70d33a17a
commit e94aacd7fe
8 changed files with 152 additions and 3 deletions

View File

@ -163,6 +163,14 @@
<version>1.2.62</version>
</dependency>
<!-- 配置注解处理器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>

View File

@ -0,0 +1,44 @@
package com.cultural.heritage.config;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Data
@Slf4j
@Configuration
@ConfigurationProperties(prefix = "wx.mini")
public class WxOpenConfig {
private String appId;
private String appSecret;
private WxMaService wxMaService;
/**
* 单例模式
*/
public WxMaService getWxMaService() {
if (wxMaService != null) {
return wxMaService;
}
synchronized (this) {
if (wxMaService != null) {
return wxMaService;
}
WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
config.setAppid(appId);
config.setSecret(appSecret);
WxMaService service = new WxMaServiceImpl();
service.setWxMaConfig(config);
wxMaService = service;
return wxMaService;
}
}
}

View File

@ -1,11 +1,14 @@
package com.cultural.heritage.controller.user;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cultural.heritage.common.BaseResponse;
import com.cultural.heritage.common.ErrorCode;
import com.cultural.heritage.common.ResultUtils;
import com.cultural.heritage.config.WxOpenConfig;
import com.cultural.heritage.exception.BusinessException;
import com.cultural.heritage.exception.ThrowUtils;
import com.cultural.heritage.model.dto.CommonRequest;
@ -17,6 +20,7 @@ import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.util.DigestUtils;
@ -41,6 +45,10 @@ public class UserController {
private UserService userService;
@Resource
private WxOpenConfig wxOpenConfig;
/**
* 用户登录
* @param userLoginRequest 用户登录请求体
@ -61,6 +69,34 @@ public class UserController {
return ResultUtils.success(userVO);
}
/**
* 用户登录微信小程序
* @param code 用户登录凭证
* @param request http
* @return 用户登录信息
*/
@GetMapping("/login/wx_open")
public BaseResponse<UserVO> userLoginByWxOpen(@RequestParam("code") String code, HttpServletRequest request) {
WxMaJscode2SessionResult sessionInfo;
try {
WxMaService wxMaService = wxOpenConfig.getWxMaService();
sessionInfo = wxMaService.jsCode2SessionInfo(code);
String openid = sessionInfo.getOpenid();
if (StringUtils.isAnyBlank(openid)) {
throw new BusinessException(ErrorCode.SYSTEM_ERROR);
}
return ResultUtils.success(userService.userLoginByMpOpen(sessionInfo, request));
} catch (WxErrorException e) {
log.error("userLoginByWxOpen error", e);
throw new BusinessException(ErrorCode.SYSTEM_ERROR, "登录失败,系统错误");
}
}
/**
* 退出登录
* @param request http
@ -72,6 +108,17 @@ public class UserController {
return ResultUtils.success(result);
}
/**
* 获取当前登录用户
* @param request http
* @return 用户登录信息
*/
@GetMapping("/get/login")
public BaseResponse<UserVO> getLoginUser(HttpServletRequest request) {
User user = userService.getLoginUser(request);
return ResultUtils.success(userService.getUserVO(user));
}
/**

View File

@ -31,4 +31,7 @@ public class WeChatController {
return jsonObject;
}
}

View File

@ -37,6 +37,7 @@ public class UserVO implements Serializable {
*/
private String userRole;
private static final long serialVersionUID = 1L;
}

View File

@ -1,5 +1,6 @@
package com.cultural.heritage.service.user;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.cultural.heritage.model.dto.user.UserQueryRequest;
@ -45,4 +46,10 @@ public interface UserService extends IService<User> {
* 获取脱敏的用户信息
*/
List<UserVO> getUserVO(List<User> userList);
/**
* 用户登录微信小程序
*/
UserVO userLoginByMpOpen(WxMaJscode2SessionResult sessionInfo, HttpServletRequest request);
}

View File

@ -1,10 +1,13 @@
package com.cultural.heritage.service.user.impl;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cultural.heritage.common.ErrorCode;
import com.cultural.heritage.constant.CommonConstant;
import com.cultural.heritage.exception.BusinessException;
import com.cultural.heritage.exception.ThrowUtils;
import com.cultural.heritage.mapper.UserMapper;
import com.cultural.heritage.model.dto.user.UserQueryRequest;
import com.cultural.heritage.model.entity.User;
@ -110,6 +113,9 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us
return currentUser;
}
/**
* 获取用户查询条件
*/
@Override
public QueryWrapper<User> getQueryWrapper(UserQueryRequest userQueryRequest) {
if (userQueryRequest == null) {
@ -142,4 +148,36 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us
return userList.stream().map(this::getUserVO).collect(Collectors.toList());
}
/**
* 用户登录微信小程序
*/
@Override
public UserVO userLoginByMpOpen(WxMaJscode2SessionResult sessionInfo, HttpServletRequest request) {
String openid = sessionInfo.getOpenid();
// 查询用户是否存在
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("miniOpenId", openid);
User user = this.getOne(queryWrapper);
// 被封号禁止登录
if (user != null && UserRoleEnum.BAN.getValue().equals(user.getUserRole())) {
throw new BusinessException(ErrorCode.FORBIDDEN_ERROR, "该用户已封禁,禁止登录");
}
// 用户不存在则创建
if (user == null) {
user = new User();
String userAccount = RandomUtil.randomNumbers(14);
String userPassword = RandomUtil.randomString(16);
user.setUserAccount(userAccount);
user.setUserPassword(userPassword);
user.setMiniOpenId(openid);
user.setUserName("普通用户");
boolean result = this.save(user);
ThrowUtils.throwIf(!result, ErrorCode.SYSTEM_ERROR, "登录失败");
// 记住用户的登录态
}
request.getSession().setAttribute(USER_LOGIN_STATE, user);
return this.getUserVO(user);
}
}

View File

@ -47,7 +47,8 @@ hwyun:
endPoint: obs.cn-north-4.myhuaweicloud.com
bucketName: carbon2
weixin:
wx:
mini:
appid: wx3f968a09e31d6bed
secret: 847bdda7c2b01e88d59948b9ba50ef8d
appId: wx3f968a09e31d6bed
appSecret: 847bdda7c2b01e88d59948b9ba50ef8d