1.添加邮箱登录,注册接口
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
package vvpkassistant.User.controller;
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import vvpkassistant.CoinRecords.CoinRecords;
|
||||
@@ -7,16 +8,23 @@ import vvpkassistant.Data.ResponseData;
|
||||
import vvpkassistant.Data.ResponseInfo;
|
||||
import vvpkassistant.Data.WxChatParam;
|
||||
import vvpkassistant.User.mapper.UserDao;
|
||||
import vvpkassistant.User.model.DTO.UserModelDTO;
|
||||
import vvpkassistant.User.model.UserModel;
|
||||
import vvpkassistant.User.model.UserModelVO;
|
||||
import vvpkassistant.User.service.UserService;
|
||||
import vvpkassistant.common.ErrorCode;
|
||||
import vvpkassistant.config.FunctionConfigHolder;
|
||||
import vvpkassistant.Tools.VVRequester;
|
||||
import vvpkassistant.Tools.VVTools;
|
||||
import vvpkassistant.exception.BusinessException;
|
||||
import vvpkassistant.mail.model.MailModel;
|
||||
import vvpkassistant.mail.service.MailService;
|
||||
import vvpkassistant.pk.mapper.PkInfoDao;
|
||||
import vvpkassistant.pk.model.PkInfoModel;
|
||||
import vvpkassistant.pk.model.PkRecordDetail;
|
||||
import vvpkassistant.pk.mapper.PkRecordDetailDao;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -43,6 +51,10 @@ public class UserController {
|
||||
@Autowired
|
||||
private VVRequester vvRequester;
|
||||
|
||||
|
||||
@Resource
|
||||
private UserService userService;
|
||||
|
||||
// 配置用户信息
|
||||
@PostMapping("inputUserInfo")
|
||||
public ResponseData<Object> inputUserInfo(@RequestBody Map<String,Object> param) {
|
||||
@@ -165,29 +177,12 @@ public class UserController {
|
||||
|
||||
}
|
||||
|
||||
//todo 修改用户返回结果
|
||||
|
||||
// 修改用户信息
|
||||
@PostMapping("updateUserInfo")
|
||||
public ResponseData<Object> updateUserInfo(@RequestBody Map<String,Object> map) {
|
||||
UserModel userModel = new UserModel();
|
||||
//设置用户id
|
||||
userModel.setId(Integer.valueOf(map.get("id").toString()));
|
||||
//设置用户头像
|
||||
userModel.setHeaderIcon(map.get("headerIcon").toString());
|
||||
//设置用户昵称
|
||||
userModel.setNickName(map.get("nickName").toString());
|
||||
|
||||
int i = userDao.updateById(userModel);
|
||||
// 返回结果
|
||||
Map<String,Object> result = new HashMap<>();
|
||||
result.put("info", userDao.selectById(map.get("id").toString()));
|
||||
result.put("newAccount", false);
|
||||
if (i == 1){
|
||||
return ResponseData.success(result);
|
||||
}else {
|
||||
throw new BusinessException(ErrorCode.SYSTEM_ERROR);
|
||||
}
|
||||
public ResponseData<Object> updateUserInfo(@RequestBody UserModelDTO userModelDTO) {
|
||||
UserModelVO userModelVO = userService.updateUserInfo( userModelDTO);
|
||||
return ResponseData.success(userModelVO);
|
||||
}
|
||||
|
||||
// 获取用户信息
|
||||
@@ -367,5 +362,13 @@ public class UserController {
|
||||
return ResponseData.success(coinRecords);
|
||||
}
|
||||
|
||||
@PostMapping("/loginWithMail")
|
||||
public ResponseData<Object> loginWithMail(@RequestBody UserModelDTO model) {
|
||||
return ResponseData.success(userService.loginWithMail(model));
|
||||
}
|
||||
|
||||
@PostMapping("/registerWithMail")
|
||||
public ResponseData<Object> mailRegister(@RequestBody UserModelDTO model){
|
||||
return ResponseData.success(userService.addUserWithMail(model));
|
||||
}
|
||||
}
|
||||
|
||||
28
src/main/java/vvpkassistant/User/model/DTO/UserModelDTO.java
Normal file
28
src/main/java/vvpkassistant/User/model/DTO/UserModelDTO.java
Normal file
@@ -0,0 +1,28 @@
|
||||
package vvpkassistant.User.model.DTO;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
|
||||
public class UserModelDTO {
|
||||
@TableId(type = IdType.AUTO)
|
||||
private Integer id; // 主键
|
||||
private String nickName; // 昵称
|
||||
private String phoneNumber; // 手机号码
|
||||
private String headerIcon; // 头像
|
||||
private String openid; // openid
|
||||
private String sessionKey; // session key
|
||||
private Integer status; // 用户状态 0 正常 其他业务逻辑待定
|
||||
private Long createTime; // 创建时间
|
||||
private String userChatId; // 聊天使用的id,使用微信的openid作为标识
|
||||
private Integer points; // 用户积分
|
||||
private Integer inviterId; // 邀请人id
|
||||
private String email;
|
||||
private String newPassword;
|
||||
private String oldPassword;
|
||||
private String password;
|
||||
|
||||
}
|
||||
@@ -20,4 +20,7 @@ public class UserModel {
|
||||
private String userChatId; // 聊天使用的id,使用微信的openid作为标识
|
||||
private Integer points; // 用户积分
|
||||
private Integer inviterId; // 邀请人id
|
||||
private String email;
|
||||
private String password;
|
||||
|
||||
}
|
||||
|
||||
26
src/main/java/vvpkassistant/User/model/UserModelVO.java
Normal file
26
src/main/java/vvpkassistant/User/model/UserModelVO.java
Normal file
@@ -0,0 +1,26 @@
|
||||
package vvpkassistant.User.model;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
import vvpkassistant.Data.WxChatParam;
|
||||
|
||||
@Data
|
||||
public class UserModelVO {
|
||||
private Integer id; // 主键
|
||||
private String nickName; // 昵称
|
||||
private String phoneNumber; // 手机号码
|
||||
private String headerIcon; // 头像
|
||||
private String openid; // openid
|
||||
private String sessionKey; // session key
|
||||
private Integer status; // 用户状态 0 正常 其他业务逻辑待定
|
||||
private Long createTime; // 创建时间
|
||||
private String userChatId; // 聊天使用的id,使用微信的openid作为标识
|
||||
private Integer points; // 用户积分
|
||||
private Integer inviterId; // 邀请人id
|
||||
private String email;
|
||||
private String token;
|
||||
private Boolean newAccount;
|
||||
private WxChatParam chatInfo;
|
||||
}
|
||||
20
src/main/java/vvpkassistant/User/service/UserService.java
Normal file
20
src/main/java/vvpkassistant/User/service/UserService.java
Normal file
@@ -0,0 +1,20 @@
|
||||
package vvpkassistant.User.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import vvpkassistant.User.model.DTO.UserModelDTO;
|
||||
import vvpkassistant.User.model.UserModel;
|
||||
import vvpkassistant.User.model.UserModelVO;
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* @author: ziin
|
||||
* @date: 2025/8/4 16:19
|
||||
*/
|
||||
public interface UserService extends IService<UserModel> {
|
||||
UserModelVO loginWithMail(UserModelDTO model);
|
||||
|
||||
UserModelVO updateUserInfo(UserModelDTO userModelDTO);
|
||||
|
||||
UserModelVO addUserWithMail(UserModelDTO model);
|
||||
}
|
||||
128
src/main/java/vvpkassistant/User/service/UserServiceImpl.java
Normal file
128
src/main/java/vvpkassistant/User/service/UserServiceImpl.java
Normal file
@@ -0,0 +1,128 @@
|
||||
package vvpkassistant.User.service;
|
||||
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import vvpkassistant.Data.WxChatParam;
|
||||
import vvpkassistant.Tools.BcryptUtils;
|
||||
import vvpkassistant.Tools.VVTools;
|
||||
import vvpkassistant.User.mapper.UserDao;
|
||||
import vvpkassistant.User.model.DTO.UserModelDTO;
|
||||
import vvpkassistant.User.model.UserModel;
|
||||
import vvpkassistant.User.model.UserModelVO;
|
||||
import vvpkassistant.common.ErrorCode;
|
||||
import vvpkassistant.exception.BusinessException;
|
||||
import vvpkassistant.mail.model.MailModel;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/*
|
||||
* @author: ziin
|
||||
* @date: 2025/8/4 16:19
|
||||
*/
|
||||
@Service
|
||||
public class UserServiceImpl extends ServiceImpl<UserDao, UserModel> implements UserService {
|
||||
|
||||
@Resource
|
||||
private UserDao userDao;
|
||||
|
||||
@Resource
|
||||
private WxChatParam wxChatParam;
|
||||
|
||||
@Override
|
||||
public UserModelVO loginWithMail(UserModelDTO model) {
|
||||
|
||||
LambdaQueryWrapper<UserModel> lambdaQueryWrapper = new LambdaQueryWrapper<>();
|
||||
lambdaQueryWrapper.eq(UserModel::getEmail,model.getEmail())
|
||||
.in(UserModel::getStatus, 0,2);
|
||||
|
||||
UserModel userModel = userDao.selectOne(lambdaQueryWrapper);
|
||||
if (userModel == null) {
|
||||
throw new BusinessException(ErrorCode.USER_DOES_NOT_EXIST);
|
||||
}
|
||||
|
||||
String password = userModel.getPassword();
|
||||
UserModelVO userModelVO = BeanUtil.copyProperties(userModel, UserModelVO.class);
|
||||
if (BcryptUtils.matchPassword(password, model.getPassword())) {
|
||||
StpUtil.login(userModel.getId());
|
||||
userModelVO.setToken(StpUtil.getTokenValue());
|
||||
userModelVO.setChatInfo(wxChatParam);
|
||||
return userModelVO;
|
||||
}else {
|
||||
throw new BusinessException(ErrorCode.PASSWORD_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public UserModelVO updateUserInfo(UserModelDTO userModelDTO) {
|
||||
|
||||
UserModel userInfo = userDao.selectById(userModelDTO.getId());
|
||||
if (userInfo == null) {
|
||||
throw new BusinessException(ErrorCode.USER_DOES_NOT_EXIST);
|
||||
}
|
||||
// 用户没有密码的情况下设置密码
|
||||
if (userInfo.getPassword() == null) {
|
||||
if (!userModelDTO.getNewPassword().isEmpty()){
|
||||
if (userModelDTO.getNewPassword().length()<6){
|
||||
throw new BusinessException(ErrorCode.PARAMS_ERROR,"密码长度不能小于 6 位");
|
||||
}
|
||||
userModelDTO.setPassword(BcryptUtils.encryptPassword(userModelDTO.getNewPassword()));
|
||||
}
|
||||
}
|
||||
// 用户有密码的情况下重新设置密码
|
||||
if (userInfo.getPassword() != null && userModelDTO.getOldPassword() != null) {
|
||||
if (BcryptUtils.matchPassword(userModelDTO.getOldPassword(), userInfo.getPassword())) {
|
||||
userModelDTO.setPassword(BcryptUtils.encryptPassword(userModelDTO.getNewPassword()));
|
||||
}else {
|
||||
throw new BusinessException(ErrorCode.PASSWORD_ERROR,"旧密码不正确");
|
||||
}
|
||||
}
|
||||
|
||||
UserModel userModel = new UserModel();
|
||||
BeanUtil.copyProperties(userModelDTO, userModel);
|
||||
int i = userDao.updateById(userModel);
|
||||
// 返回结果
|
||||
userDao.selectById(userModel.getId());
|
||||
UserModelVO userModelVO = BeanUtil.copyProperties(userModel, UserModelVO.class);
|
||||
userModelVO.setNewAccount(false);
|
||||
if (i == 1){
|
||||
return userModelVO;
|
||||
}else {
|
||||
throw new BusinessException(ErrorCode.SYSTEM_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public UserModelVO addUserWithMail(UserModelDTO model) {
|
||||
|
||||
LambdaQueryWrapper<UserModel> lambdaQueryWrapper = new LambdaQueryWrapper<>();
|
||||
lambdaQueryWrapper.eq(UserModel::getEmail,model.getEmail());
|
||||
UserModel userModel = userDao.selectOne(lambdaQueryWrapper);
|
||||
if (userModel != null) {
|
||||
throw new BusinessException(ErrorCode.MAIL_ALREADY_EXIST);
|
||||
}
|
||||
if (model.getPassword().length() < 6 ){
|
||||
throw new BusinessException(ErrorCode.PARAMS_ERROR,"密码长度不能小于 6 位");
|
||||
}
|
||||
model.setPassword(BcryptUtils.encryptPassword(model.getPassword()));
|
||||
model.setCreateTime(VVTools.currentTimeStamp());
|
||||
//设置状态为正常
|
||||
model.setStatus(2);
|
||||
//设置积分为0
|
||||
model.setPoints(0);
|
||||
userDao.insert(BeanUtil.copyProperties(model, UserModel.class));
|
||||
// 判断用户是否为邀请用户
|
||||
if (model.getInviterId() != null) {
|
||||
UserModel oldUser = userDao.selectById(model.getInviterId());
|
||||
oldUser.setPoints(oldUser.getPoints() + 10);
|
||||
userDao.updateById(oldUser);
|
||||
}
|
||||
UserModelVO userModelVO = BeanUtil.copyProperties(model, UserModelVO.class);
|
||||
userModelVO.setNewAccount(true);
|
||||
userModelVO.setChatInfo(wxChatParam);
|
||||
return userModelVO;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user