feat: 增加用户注册

This commit is contained in:
xiongxiaoyang 2022-05-17 18:16:14 +08:00
parent 0fb2459a0a
commit 2f001f9777
8 changed files with 181 additions and 5 deletions

View File

@ -1,4 +1,4 @@
[![index]( https://s1.ax1x.com/2022/04/27/LLVXqJ.png )]( https://curl.qcloud.com/kgMaOjoq ) [![index]( https://s1.ax1x.com/2022/05/17/O5KOpR.png )]( https://curl.qcloud.com/kgMaOjoq )
[![Github stars](https://img.shields.io/github/stars/201206030/novel?logo=github)](https://github.com/201206030/novel) [![Github stars](https://img.shields.io/github/stars/201206030/novel?logo=github)](https://github.com/201206030/novel)
[![Github forks](https://img.shields.io/github/forks/201206030/novel?logo=github)](https://github.com/201206030/novel) [![Github forks](https://img.shields.io/github/forks/201206030/novel?logo=github)](https://github.com/201206030/novel)

View File

@ -0,0 +1,37 @@
package io.github.xxyopen.novel.controller.front;
import io.github.xxyopen.novel.core.common.resp.RestResp;
import io.github.xxyopen.novel.core.common.util.IpUtils;
import io.github.xxyopen.novel.core.constant.ApiRouterConsts;
import io.github.xxyopen.novel.dto.req.UserRegisterReqDto;
import io.github.xxyopen.novel.service.UserService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 会员模块相关 控制器
*
* @author xiongxiaoyang
* @date 2022/5/17
*/
@RestController
@RequestMapping(ApiRouterConsts.API_FRONT_USER_URL_PREFIX)
@RequiredArgsConstructor
public class UserController {
private final UserService userService;
/**
* 用户注册接口
*/
@PostMapping("register")
public RestResp<String> register(@Valid UserRegisterReqDto dto, HttpServletRequest request) {
dto.setUserKey(IpUtils.getRealIp(request));
return userService.getImgVerifyCode(dto);
}
}

View File

@ -38,15 +38,25 @@ public enum ErrorCodeEnum {
USER_REGISTER_ERROR("A0100","用户注册错误"), USER_REGISTER_ERROR("A0100","用户注册错误"),
/** /**
* 二级宏观错误码用户未同意隐私协议 * 用户未同意隐私协议
* */ * */
USER_NO_AGREE_PRIVATE_ERROR("A0101","用户未同意隐私协议"), USER_NO_AGREE_PRIVATE_ERROR("A0101","用户未同意隐私协议"),
/** /**
* 二级宏观错误码注册国家或地区受限 * 注册国家或地区受限
* */ * */
USER_REGISTER_AREA_LIMIT_ERROR("A0102","注册国家或地区受限"), USER_REGISTER_AREA_LIMIT_ERROR("A0102","注册国家或地区受限"),
/**
* 用户验证码错误
* */
USER_VERIFY_CODE_ERROR("A0240","用户验证码错误"),
/**
* 用户名已存在
* */
USER_NAME_EXIST("A0111","用户名已存在"),
/** /**
* 二级宏观错误码用户请求参数错误 * 二级宏观错误码用户请求参数错误
* */ * */

View File

@ -10,8 +10,29 @@ import lombok.Getter;
*/ */
public class DatabaseConsts { public class DatabaseConsts {
/** /**
* 小说类别 * 用户信息表
*/
public static class UserInfoTable {
@Getter
public enum ColumnEnum {
USERNAME("username");
private String name;
ColumnEnum(String name) {
this.name = name;
}
}
}
/**
* 小说类别表
*/ */
public static class BookCategoryTable { public static class BookCategoryTable {

View File

@ -24,6 +24,9 @@ public class UserRegisterReqDto {
@Pattern(regexp="^\\d{4}$",message="验证码格式不正确!") @Pattern(regexp="^\\d{4}$",message="验证码格式不正确!")
private String velCode; private String velCode;
/**
* 请求用户标识用来表明图形验证码属于哪个用户
* */
private String userKey;
} }

View File

@ -0,0 +1,16 @@
package io.github.xxyopen.novel.dto.resp;
import lombok.Data;
/**
* 用户登录 响应DTO
* @author xiongxiaoyang
* @date 2022/5/17
*/
@Data
public class UserLoginRespDto {
private String nickName;
private String jwt;
}

View File

@ -0,0 +1,20 @@
package io.github.xxyopen.novel.service;
import io.github.xxyopen.novel.core.common.resp.RestResp;
import io.github.xxyopen.novel.dto.req.UserRegisterReqDto;
/**
* 会员模块 服务类
*
* @author xiongxiaoyang
* @date 2022/5/17
*/
public interface UserService {
/**
* 用户注册
* @param dto 注册参数
* @return JWT
* */
RestResp<String> getImgVerifyCode(UserRegisterReqDto dto);
}

View File

@ -0,0 +1,69 @@
package io.github.xxyopen.novel.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.github.xxyopen.novel.core.common.constant.ErrorCodeEnum;
import io.github.xxyopen.novel.core.common.exception.BusinessException;
import io.github.xxyopen.novel.core.common.resp.RestResp;
import io.github.xxyopen.novel.core.constant.DatabaseConsts;
import io.github.xxyopen.novel.core.constant.SystemConfigConsts;
import io.github.xxyopen.novel.core.util.JwtUtils;
import io.github.xxyopen.novel.dao.entity.UserInfo;
import io.github.xxyopen.novel.dao.mapper.UserInfoMapper;
import io.github.xxyopen.novel.dto.req.UserRegisterReqDto;
import io.github.xxyopen.novel.manager.VerifyCodeManager;
import io.github.xxyopen.novel.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
/**
* 会员模块 服务实现类
*
* @author xiongxiaoyang
* @date 2022/5/17
*/
@Service
@RequiredArgsConstructor
public class UserServiceImpl implements UserService {
private final UserInfoMapper userInfoMapper;
private final VerifyCodeManager verifyCodeManager;
private final JwtUtils jwtUtils;
@Override
public RestResp<String> getImgVerifyCode(UserRegisterReqDto dto) {
// 校验图形验证码是否正确
if (!verifyCodeManager.imgVerifyCodeOk(dto.getUserKey(), dto.getVelCode())) {
// 图形验证码校验失败
throw new BusinessException(ErrorCodeEnum.USER_VERIFY_CODE_ERROR);
}
// 校验手机号是否已注册
QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
queryWrapper.eq(DatabaseConsts.UserInfoTable.ColumnEnum.USERNAME.getName()
, dto.getUsername())
.last(DatabaseConsts.SqlEnum.LIMIT_1.getSql());
if (userInfoMapper.selectCount(queryWrapper) > 0) {
// 手机号已注册
throw new BusinessException(ErrorCodeEnum.USER_NAME_EXIST);
}
// 注册成功保存用户信息
UserInfo userInfo = new UserInfo();
userInfo.setPassword(DigestUtils.md5DigestAsHex(dto.getPassword().getBytes(StandardCharsets.UTF_8)));
userInfo.setUsername(dto.getUsername());
userInfo.setNickName(dto.getUsername());
userInfo.setCreateTime(LocalDateTime.now());
userInfo.setUpdateTime(LocalDateTime.now());
userInfo.setSalt("0");
userInfoMapper.insert(userInfo);
// 生成JWT 并返回
return RestResp.ok(jwtUtils.generateToken(userInfo.getId(), SystemConfigConsts.NOVEL_FRONT_KEY));
}
}