前文提要
注册功能实现
数据表设计
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` bigint(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(128) DEFAULT NULL COMMENT '昵称',
`pwd` varchar(124) DEFAULT NULL COMMENT '密码',
`head_img` varchar(524) DEFAULT NULL COMMENT '头像',
`slogan` varchar(524) DEFAULT NULL COMMENT '用户签名',
`sex` tinyint(2) DEFAULT '1' COMMENT '0表示女,1表示男',
`points` int(10) DEFAULT '0' COMMENT '积分',
`create_time` datetime DEFAULT NULL,
`mail` varchar(64) DEFAULT NULL COMMENT '邮箱',
`secret` varchar(12) DEFAULT NULL COMMENT '盐,用于个人敏感信息处理',
PRIMARY KEY (`id`),
UNIQUE KEY `mail_idx` (`mail`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- 设置数据库唯一索引 避免用户重复注册
alter table user add unique(mail)
校验验证码
/**
* 校验验证码
* @param sendCodeEnum
* @param to
* @param code
* @return
*/
@Override
public boolean checkCode(SendCodeEnum sendCodeEnum, String to, String code) {
String cacheKey = String.format(CacheKey.CHECK_CODE_KEY,sendCodeEnum.name(),to);
String cacheValue = redisTemplate.opsForValue().get(cacheKey);
if(StringUtils.isNotBlank(cacheValue)){
String cacheCode = cacheValue.split("_")[0];
if(cacheCode.equals(code)){
//删除验证码
redisTemplate.delete(cacheKey);
return true;
}
}
return false;
}
账号唯一性校验
/**
* 校验用户账号唯一
*
* @param mail
* @return
*/
private boolean checkUnique(String mail) {
// TODO redis缓存校验
// 先从redis查询是由有该账号,没有则继续查询本地数据库
QueryWrapper queryWrapper = new QueryWrapper<UserDO>().eq("mail", mail);
List<UserDO> list = userMapper.selectList(queryWrapper);
return list.size() > 0 ? false : true;
}
账号初始化
/**
* 用户注册,初始化福利信息 TODO
*
* @param userDO
*/
private void userRegisterInitTask(UserDO userDO) {
}
请求vo
@ApiModel(value = "用户注册对象",description = "用户注册请求对象")
@Data
public class UserRegisterRequest {
@ApiModelProperty(value = "昵称",example = "Anna小姐姐")
private String name;
@ApiModelProperty(value = "密码",example = "12345")
private String pwd;
@ApiModelProperty(value = "头像",example = "https://xdclass-1024shop-img.oss-cn-shenzhen.aliyuncs.com/user/2021/02/03/39473aa1029a430298ac2620dd819962.jpeg")
@JsonProperty("head_img")
private String headImg;
@ApiModelProperty(value = "用户个人性签名",example = "人生需要动态规划,学习需要贪心算法")
private String slogan;
@ApiModelProperty(value = "0表示女,1表示男",example = "1")
private Integer sex;
@ApiModelProperty(value = "邮箱",example = "[email protected]")
private String mail;
@ApiModelProperty(value = "验证码",example = "232343")
private String code;
}
service
public interface UserService {
/**
* 用户注册
* @param registerRequest
* @return
*/
JsonData register(UserRegisterRequest registerRequest);
}
serviceImpl
/**
* 用户注册
* * 邮箱验证码验证
* * 密码加密(TODO)
* * 账号唯一性检查(TODO)
* * 插入数据库
* * 新注册用户福利发放(TODO)
*
* @param registerRequest
* @return
*/
@Override
public JsonData register(UserRegisterRequest registerRequest) {
boolean checkCode = false;
//校验验证码
if (StringUtils.isNotBlank(registerRequest.getMail())) {
checkCode = notifyService.checkCode(SendCodeEnum.USER_REGISTER, registerRequest.getMail(), registerRequest.getCode());
}
if (!checkCode) {
return JsonData.buildResult(BizCodeEnum.CODE_ERROR);
}
UserDO userDO = new UserDO();
BeanUtils.copyProperties(registerRequest, userDO);
userDO.setCreateTime(new Date());
userDO.setSlogan("人生需要动态规划,学习需要贪心算法");
//设置密码 生成秘钥 盐
String secret = "$1$" + CommonUtil.getStringNumRandom(8);
userDO.setSecret(secret);
//密码+盐处理
String cryptPwd = Md5Crypt.md5Crypt(registerRequest.getPwd().getBytes(), secret);
userDO.setPwd(cryptPwd);
//账号唯一性检查 [email protected]
if (checkUnique(userDO.getMail())) {
int rows = userMapper.insert(userDO);
log.info("rows:{},注册成功:{}", rows, userDO.toString());
//新用户注册成功,初始化信息,发放福利等 TODO
userRegisterInitTask(userDO);
return JsonData.buildSuccess();
} else {
return JsonData.buildResult(BizCodeEnum.ACCOUNT_REPEAT);
}
}
controller
/**
* 用户注册
* @param registerRequest
* @return
*/
@ApiOperation("用户注册")
@PostMapping("register")
public JsonData register(@ApiParam("用户注册对象") @RequestBody UserRegisterRequest registerRequest) {
JsonData jsonData = userService.register(registerRequest);
return jsonData;
}
标签:功能,return,registerRequest,用户注册,DEFAULT,userDO,开发,String
From: https://www.cnblogs.com/isChenJY/p/16830208.html