用户模块

This commit is contained in:
2025-05-07 00:27:51 +08:00
parent ef88f4be1e
commit fa3612648b
17 changed files with 574 additions and 31 deletions

View File

@ -96,7 +96,7 @@ public class FileInfoServiceImpl extends ServiceImpl<FileInfoMapper, FileInfo>
FileInfo fileInfo = this.getOne(lambdaQueryWrapper);
if (fileInfo != null) return fileInfo.getFileView();
// 保存文件
FileInfoAddRequest fileInfoAddRequest = FileInfoAddRequest.builder()
fileInfo = FileInfo.builder()
.name(fileName)
.type(fileType)
.path(filePath)
@ -105,7 +105,6 @@ public class FileInfoServiceImpl extends ServiceImpl<FileInfoMapper, FileInfo>
.biz(biz)
.hashValue(hashValue)
.build();
fileInfo = commonService.copyProperties(fileInfoAddRequest, FileInfo.class);
this.save(fileInfo);
// 创建上传目录,如果不存在
File file = new File(UPLOAD_DIR + filePath);

View File

@ -1,7 +1,7 @@
package com.greenorange.promotion.service.user;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.greenorange.promotion.model.dto.user.UserInfoQueryRequest;
import com.greenorange.promotion.model.dto.user.*;
import com.greenorange.promotion.model.entity.UserInfo;
import com.baomidou.mybatisplus.extension.service.IService;
import jakarta.servlet.http.HttpServletRequest;
@ -23,5 +23,35 @@ public interface UserInfoService extends IService<UserInfo> {
/**
* web端用户登录
*/
String userInfoLogin(String userAccount, String userPassword, HttpServletRequest request);
String userInfoLogin(UserInfoLoginRequest userInfoLoginRequest);
/**
* 小程序用户注册
*/
void userInfoMiniRegister(UserInfoRegisterRequest userInfoRegisterRequest);
/**
* 小程序端用户密码登录
*/
String userInfoMiniLoginByPwd(UserInfoMiniPasswordLoginRequest userInfoMiniPasswordLoginRequest);
/**
* 小程序用户验证码登录
*/
String userInfoMiniLoginByVcd(UserInfoMiniVerifyCodeLoginRequest userInfoMiniVerifyCodeLoginRequest);
/**
* 小程序用户重置密码
*/
void userInfoMiniResetPwd(UserInfoResetRequest userInfoResetRequest);
/**
* 小程序用户获取验证码
*/
String getVerificationCode(String phoneNumber);
}

View File

@ -1,26 +1,34 @@
package com.greenorange.promotion.service.user.impl;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.greenorange.promotion.annotation.RequiresPermission;
import com.greenorange.promotion.common.ErrorCode;
import com.greenorange.promotion.constant.CommonConstant;
import com.greenorange.promotion.constant.SystemConstant;
import com.greenorange.promotion.constant.UserConstant;
import com.greenorange.promotion.exception.ThrowUtils;
import com.greenorange.promotion.mapper.UserInfoMapper;
import com.greenorange.promotion.model.dto.user.UserInfoQueryRequest;
import com.greenorange.promotion.model.dto.user.*;
import com.greenorange.promotion.model.entity.UserInfo;
import com.greenorange.promotion.service.common.CommonService;
import com.greenorange.promotion.service.user.UserInfoService;
import com.greenorange.promotion.utils.JWTUtils;
import com.greenorange.promotion.utils.RegexUtils;
import com.greenorange.promotion.utils.SendSmsUtil;
import com.greenorange.promotion.utils.SqlUtils;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
* @author 35880
@ -35,6 +43,14 @@ public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo>
private JWTUtils jwtUtils;
@Resource
private CommonService commonService;
@Resource
private RedisTemplate<String, String> redisTemplate;
/**
* 获取查询条件
*/
@ -56,7 +72,9 @@ public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo>
* web端用户登录
*/
@Override
public String userInfoLogin(String userAccount, String userPassword, HttpServletRequest request) {
public String userInfoLogin(UserInfoLoginRequest userInfoLoginRequest) {
String userAccount = userInfoLoginRequest.getUserAccount();
String userPassword = userInfoLoginRequest.getUserPassword();
LambdaQueryWrapper<UserInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.eq(UserInfo::getUserAccount, userAccount).eq(UserInfo::getUserPassword, userPassword);
UserInfo userInfo = this.getOne(lambdaQueryWrapper);
@ -66,6 +84,132 @@ public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo>
payload.put("userPassword", userPassword);
return jwtUtils.generateToken(payload);
}
/**
* 小程序用户注册
*/
@Override
public void userInfoMiniRegister(UserInfoRegisterRequest userInfoRegisterRequest) {
String phoneNumber = userInfoRegisterRequest.getPhoneNumber();
ThrowUtils.throwIf(RegexUtils.isPhoneInvalid(phoneNumber), ErrorCode.PARAMS_ERROR, "手机号格式无效");
LambdaQueryWrapper<UserInfo> phoneNumberLambdaQueryWrapper = new LambdaQueryWrapper<>();
phoneNumberLambdaQueryWrapper.eq(UserInfo::getPhoneNumber, phoneNumber);
UserInfo userInfo = this.getOne(phoneNumberLambdaQueryWrapper);
ThrowUtils.throwIf(userInfo != null, ErrorCode.OPERATION_ERROR, "手机号已注册");
String verificationCode = userInfoRegisterRequest.getVerificationCode();
String code = redisTemplate.opsForValue().get(SystemConstant.VERIFICATION_CODE + ":" + verificationCode);
ThrowUtils.throwIf(code == null, ErrorCode.OPERATION_ERROR, "验证码已失效");
// 移除验证码
redisTemplate.delete(SystemConstant.VERIFICATION_CODE + ":" + verificationCode);
String invitationCode = userInfoRegisterRequest.getInvitationCode();
LambdaQueryWrapper<UserInfo> invitedLambdaQueryWrapper = new LambdaQueryWrapper<>();
invitedLambdaQueryWrapper.eq(UserInfo::getInvitationCode, invitationCode);
UserInfo parentUserInfo = this.getOne(invitedLambdaQueryWrapper);
ThrowUtils.throwIf(parentUserInfo == null, ErrorCode.OPERATION_ERROR, "邀请码错误");
UserInfo myUserInfo = commonService.copyProperties(userInfoRegisterRequest, UserInfo.class);
userInfo.setParentUserId(parentUserInfo.getId());
userInfo.setInvitationCode(RandomUtil.randomNumbers(6));
userInfo.setUserAccount(RandomUtil.randomNumbers(8));
userInfo.setUserRole(UserConstant.DEFAULT_ROLE);
userInfo.setUserAvatar(UserConstant.USER_DEFAULT_AVATAR);
userInfo.setSuperUserList(parentUserInfo.getSuperUserList() + "," + parentUserInfo.getId());
this.save(myUserInfo);
}
/**
* 小程序端用户密码登录
*/
@Override
public String userInfoMiniLoginByPwd(UserInfoMiniPasswordLoginRequest userInfoMiniPasswordLoginRequest) {
String phoneNumber = userInfoMiniPasswordLoginRequest.getPhoneNumber();
ThrowUtils.throwIf(RegexUtils.isPhoneInvalid(phoneNumber), ErrorCode.PARAMS_ERROR, "手机号格式无效");
String userPassword = userInfoMiniPasswordLoginRequest.getUserPassword();
LambdaQueryWrapper<UserInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.eq(UserInfo::getPhoneNumber, phoneNumber).eq(UserInfo::getUserPassword, userPassword);
UserInfo userInfo = this.getOne(lambdaQueryWrapper);
ThrowUtils.throwIf(userInfo == null, ErrorCode.OPERATION_ERROR, "手机号未注册");
Map<String, String> payload = new HashMap<>();
payload.put("phoneNumber", phoneNumber);
payload.put("userPassword", userPassword);
return jwtUtils.generateToken(payload);
}
/**
* 小程序用户验证码登录
*/
@Override
public String userInfoMiniLoginByVcd(UserInfoMiniVerifyCodeLoginRequest userInfoMiniVerifyCodeLoginRequest) {
String phoneNumber = userInfoMiniVerifyCodeLoginRequest.getPhoneNumber();
ThrowUtils.throwIf(RegexUtils.isPhoneInvalid(phoneNumber), ErrorCode.PARAMS_ERROR, "手机号格式无效");
String verificationCode = userInfoMiniVerifyCodeLoginRequest.getVerificationCode();
String code = redisTemplate.opsForValue().get(SystemConstant.VERIFICATION_CODE + ":" + verificationCode);
ThrowUtils.throwIf(code == null, ErrorCode.OPERATION_ERROR, "验证码已失效");
// 移除验证码
redisTemplate.delete(SystemConstant.VERIFICATION_CODE + ":" + verificationCode);
LambdaQueryWrapper<UserInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.eq(UserInfo::getPhoneNumber, phoneNumber);
UserInfo userInfo = this.getOne(lambdaQueryWrapper);
String userPassword = userInfo.getUserPassword();
Map<String, String> payload = new HashMap<>();
payload.put("phoneNumber", phoneNumber);
payload.put("userPassword", userPassword);
return jwtUtils.generateToken(payload);
}
/**
* 小程序用户重置密码
*/
@Override
public void userInfoMiniResetPwd(UserInfoResetRequest userInfoResetRequest) {
String phoneNumber = userInfoResetRequest.getPhoneNumber();
ThrowUtils.throwIf(RegexUtils.isPhoneInvalid(phoneNumber), ErrorCode.PARAMS_ERROR, "手机号格式无效");
String verificationCode = userInfoResetRequest.getVerificationCode();
String code = redisTemplate.opsForValue().get(SystemConstant.VERIFICATION_CODE + ":" + verificationCode);
ThrowUtils.throwIf(code == null, ErrorCode.OPERATION_ERROR, "验证码已失效");
// 移除验证码
redisTemplate.delete(SystemConstant.VERIFICATION_CODE + ":" + verificationCode);
String userPassword = userInfoResetRequest.getUserPassword();
String userConfirmPassword = userInfoResetRequest.getUserConfirmPassword();
ThrowUtils.throwIf(!userPassword.equals(userConfirmPassword), ErrorCode.OPERATION_ERROR, "两次密码不一致");
// 更新用户密码
LambdaUpdateWrapper<UserInfo> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
lambdaUpdateWrapper.eq(UserInfo::getPhoneNumber, phoneNumber).set(UserInfo::getUserPassword, userPassword);
this.update(lambdaUpdateWrapper);
}
/**
* 小程序用户获取验证码
*/
@Override
public String getVerificationCode(String phoneNumber) {
ThrowUtils.throwIf(RegexUtils.isPhoneInvalid(phoneNumber), ErrorCode.PARAMS_ERROR, "手机号格式错误");
String verificationCode = SendSmsUtil.getVerificationCode(phoneNumber);
ThrowUtils.throwIf(verificationCode == null, ErrorCode.OPERATION_ERROR, "验证码获取失败");
redisTemplate.opsForValue().set(SystemConstant.VERIFICATION_CODE + ":" + verificationCode, verificationCode, 5, TimeUnit.MINUTES);
return verificationCode;
}
}