新增用户注册相关代码
This commit is contained in:
parent
2ace559a6f
commit
eac704659a
@ -3,6 +3,7 @@ package com.guaiguailang.harmony.controller;
|
||||
import com.guaiguailang.harmony.domain.dto.ParamLogin;
|
||||
import com.guaiguailang.harmony.domain.dto.ParamUserAdd;
|
||||
import com.guaiguailang.harmony.domain.dto.ParamUserList;
|
||||
import com.guaiguailang.harmony.domain.vo.ResponseResult;
|
||||
import com.guaiguailang.harmony.service.UserService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
@ -10,7 +11,6 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
||||
@RestController
|
||||
@Tag(name="用户相关接口")
|
||||
@ -57,4 +57,14 @@ public class UserController {
|
||||
return ResponseEntity.ok(userService.addUser(userAddParam));
|
||||
}
|
||||
|
||||
@Operation(
|
||||
summary = "登录",
|
||||
description = "登录",
|
||||
tags = {"用户相关接口"}
|
||||
)
|
||||
@PostMapping("/register")
|
||||
public ResponseEntity registerUser(@RequestBody ParamUserAdd userAddParam){
|
||||
return ResponseEntity.ok(userService.registerUser(userAddParam));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -19,4 +19,5 @@ public class ParamUserAdd {
|
||||
LocalDateTime createTime;
|
||||
String merchantCode; // 商户编号
|
||||
Long id;
|
||||
String code;
|
||||
}
|
||||
|
@ -0,0 +1,15 @@
|
||||
package com.guaiguailang.harmony.domain.entity;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Data
|
||||
public class LogCaptcha {
|
||||
private Long id;
|
||||
private String username;
|
||||
private String code;
|
||||
private String status;
|
||||
private LocalDateTime createTime;
|
||||
private LocalDateTime validTime;
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package com.guaiguailang.harmony.mapper;
|
||||
|
||||
import com.guaiguailang.harmony.domain.entity.LogCaptcha;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Mapper
|
||||
public interface LogCaptchaMapper {
|
||||
|
||||
@Select("SELECT * FROM log_captcha WHERE code = #{code} and username = #{username} " +
|
||||
"and code = #{code} and status = #{status} and valid_time <= #{createTime}")
|
||||
List<LogCaptcha> verifyCode(LogCaptcha logCaptcha);
|
||||
}
|
@ -15,4 +15,6 @@ public interface UserService {
|
||||
ResponseResult getRoleListWhenAddUser(String role);
|
||||
|
||||
ResponseResult addUser(ParamUserAdd userAddParam);
|
||||
|
||||
ResponseResult registerUser(ParamUserAdd userAddParam);
|
||||
}
|
||||
|
@ -4,22 +4,20 @@ import cn.dev33.satoken.stp.StpUtil;
|
||||
import com.github.yitter.idgen.YitIdHelper;
|
||||
import com.guaiguailang.harmony.domain.dto.ParamUserAdd;
|
||||
import com.guaiguailang.harmony.domain.dto.ParamUserList;
|
||||
import com.guaiguailang.harmony.domain.entity.SystemAction;
|
||||
import com.guaiguailang.harmony.domain.entity.SystemPermission;
|
||||
import com.guaiguailang.harmony.domain.entity.SystemRole;
|
||||
import com.guaiguailang.harmony.domain.entity.UserInfo;
|
||||
import com.guaiguailang.harmony.domain.entity.*;
|
||||
import com.guaiguailang.harmony.domain.vo.*;
|
||||
import com.guaiguailang.harmony.mapper.AuthMapper;
|
||||
import com.guaiguailang.harmony.mapper.LogCaptchaMapper;
|
||||
import com.guaiguailang.harmony.mapper.UserMapper;
|
||||
import com.guaiguailang.harmony.service.UserService;
|
||||
import com.guaiguailang.harmony.utils.PasswordEncryptor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.*;
|
||||
|
||||
@ -31,6 +29,8 @@ public class UserServiceImpl implements UserService {
|
||||
private final UserMapper userMapper;
|
||||
@Autowired
|
||||
private final AuthMapper authMapper;
|
||||
@Autowired
|
||||
private LogCaptchaMapper logCaptchaMapper;
|
||||
|
||||
public UserServiceImpl(UserMapper userMapper, AuthMapper authMapper) {
|
||||
this.userMapper = userMapper;
|
||||
@ -177,6 +177,49 @@ public class UserServiceImpl implements UserService {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResponseResult registerUser(ParamUserAdd userAddParam) {
|
||||
// 1 确保有数据,数据要正确(符合要求 账号应当不长于64位字符)。
|
||||
if(userAddParam.getUserName() == null || userAddParam.getUserName().length() > 64)
|
||||
return ResponseResult.error("账号长度应当不超过64位字符");
|
||||
// 2 验证码要查询数据库,确保验证码时间在有效期内容且正确。 根据用户账号去查询验证码表。
|
||||
LogCaptcha logCaptcha = new LogCaptcha();
|
||||
logCaptcha.setCode(userAddParam.getCode());
|
||||
logCaptcha.setUsername(userAddParam.getUserName());
|
||||
logCaptcha.setCreateTime(LocalDateTime.now());
|
||||
logCaptcha.setStatus("1");
|
||||
List<LogCaptcha> logCaptchaList = logCaptchaMapper.verifyCode(logCaptcha);
|
||||
if(logCaptchaList.size() == 0)
|
||||
return ResponseResult.error("验证码错误或已过期");
|
||||
//3 确保数据不重复,应当判断账号是否唯一,不允许重复注册
|
||||
UserInfo userByAccount = userMapper.getUserByAccount(userAddParam.getUserName());
|
||||
if(userByAccount != null)
|
||||
return ResponseResult.error("账号已存在");
|
||||
//4 密码应当使用加密工具加密后再存储
|
||||
String hashPassword= null;
|
||||
try {
|
||||
hashPassword = PasswordEncryptor.hashPassword(userAddParam.getPassword());
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
e.printStackTrace();
|
||||
return ResponseResult.error(e.getMessage());
|
||||
}
|
||||
userAddParam.setPassword(hashPassword);
|
||||
//4 补齐参数: creatorId=0,roleId=user,status=1,id=雪花算法得出的id,createTime=LocalDataTime.now,merchantCode
|
||||
userAddParam.setCreatorId(0L);
|
||||
userAddParam.setRoleId("user");
|
||||
userAddParam.setStatus(1);
|
||||
userAddParam.setId(YitIdHelper.nextId());
|
||||
userAddParam.setCreateTime(LocalDateTime.now());
|
||||
userAddParam.setMerchantCode("");
|
||||
|
||||
//5 调用userMapper.addUser(userAddParam)方法,将用户信息写入数据库。
|
||||
int result = userMapper.addUser(userAddParam);
|
||||
if(result == 1)
|
||||
return ResponseResult.success(userAddParam);
|
||||
else
|
||||
return ResponseResult.error("注册失败");
|
||||
}
|
||||
|
||||
// 方法
|
||||
public UserInfo getUserByAccount(String username){
|
||||
return userMapper.getUserByAccount(username);
|
||||
|
Loading…
Reference in New Issue
Block a user