mirror of
https://github.com/201206030/novel.git
synced 2025-04-27 07:30:50 +00:00
feat: 增加用户登录接口
This commit is contained in:
parent
2f001f9777
commit
032d768f49
@ -3,11 +3,14 @@ package io.github.xxyopen.novel.controller.front;
|
|||||||
import io.github.xxyopen.novel.core.common.resp.RestResp;
|
import io.github.xxyopen.novel.core.common.resp.RestResp;
|
||||||
import io.github.xxyopen.novel.core.common.util.IpUtils;
|
import io.github.xxyopen.novel.core.common.util.IpUtils;
|
||||||
import io.github.xxyopen.novel.core.constant.ApiRouterConsts;
|
import io.github.xxyopen.novel.core.constant.ApiRouterConsts;
|
||||||
|
import io.github.xxyopen.novel.dto.req.UserLoginReqDto;
|
||||||
import io.github.xxyopen.novel.dto.req.UserRegisterReqDto;
|
import io.github.xxyopen.novel.dto.req.UserRegisterReqDto;
|
||||||
|
import io.github.xxyopen.novel.dto.resp.UserLoginRespDto;
|
||||||
import io.github.xxyopen.novel.service.UserService;
|
import io.github.xxyopen.novel.service.UserService;
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
import jakarta.validation.Valid;
|
import jakarta.validation.Valid;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
@ -31,7 +34,15 @@ public class UserController {
|
|||||||
@PostMapping("register")
|
@PostMapping("register")
|
||||||
public RestResp<String> register(@Valid UserRegisterReqDto dto, HttpServletRequest request) {
|
public RestResp<String> register(@Valid UserRegisterReqDto dto, HttpServletRequest request) {
|
||||||
dto.setUserKey(IpUtils.getRealIp(request));
|
dto.setUserKey(IpUtils.getRealIp(request));
|
||||||
return userService.getImgVerifyCode(dto);
|
return userService.register(dto);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户登录接口
|
||||||
|
*/
|
||||||
|
@GetMapping("login")
|
||||||
|
public RestResp<UserLoginRespDto> login(@Valid UserLoginReqDto dto) {
|
||||||
|
return userService.login(dto);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -57,6 +57,16 @@ public enum ErrorCodeEnum {
|
|||||||
* */
|
* */
|
||||||
USER_NAME_EXIST("A0111","用户名已存在"),
|
USER_NAME_EXIST("A0111","用户名已存在"),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户账号不存在
|
||||||
|
* */
|
||||||
|
USER_ACCOUNT_NOT_EXIST("A0201","用户账号不存在"),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户密码错误
|
||||||
|
* */
|
||||||
|
USER_PASSWORD_ERROR("A0210","用户密码错误"),
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 二级宏观错误码,用户请求参数错误
|
* 二级宏观错误码,用户请求参数错误
|
||||||
* */
|
* */
|
||||||
|
@ -0,0 +1,23 @@
|
|||||||
|
package io.github.xxyopen.novel.dto.req;
|
||||||
|
|
||||||
|
import jakarta.validation.constraints.NotBlank;
|
||||||
|
import jakarta.validation.constraints.Pattern;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户登录 请求DTO
|
||||||
|
*
|
||||||
|
* @author xiongxiaoyang
|
||||||
|
* @date 2022/5/16
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class UserLoginReqDto {
|
||||||
|
|
||||||
|
@NotBlank(message="手机号不能为空!")
|
||||||
|
@Pattern(regexp="^1[3|4|5|6|7|8|9][0-9]{9}$",message="手机号格式不正确!")
|
||||||
|
private String username;
|
||||||
|
|
||||||
|
@NotBlank(message="密码不能为空!")
|
||||||
|
private String password;
|
||||||
|
|
||||||
|
}
|
@ -1,5 +1,6 @@
|
|||||||
package io.github.xxyopen.novel.dto.resp;
|
package io.github.xxyopen.novel.dto.resp;
|
||||||
|
|
||||||
|
import lombok.Builder;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -8,6 +9,7 @@ import lombok.Data;
|
|||||||
* @date 2022/5/17
|
* @date 2022/5/17
|
||||||
*/
|
*/
|
||||||
@Data
|
@Data
|
||||||
|
@Builder
|
||||||
public class UserLoginRespDto {
|
public class UserLoginRespDto {
|
||||||
|
|
||||||
private String nickName;
|
private String nickName;
|
||||||
|
@ -1,7 +1,9 @@
|
|||||||
package io.github.xxyopen.novel.service;
|
package io.github.xxyopen.novel.service;
|
||||||
|
|
||||||
import io.github.xxyopen.novel.core.common.resp.RestResp;
|
import io.github.xxyopen.novel.core.common.resp.RestResp;
|
||||||
|
import io.github.xxyopen.novel.dto.req.UserLoginReqDto;
|
||||||
import io.github.xxyopen.novel.dto.req.UserRegisterReqDto;
|
import io.github.xxyopen.novel.dto.req.UserRegisterReqDto;
|
||||||
|
import io.github.xxyopen.novel.dto.resp.UserLoginRespDto;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 会员模块 服务类
|
* 会员模块 服务类
|
||||||
@ -16,5 +18,12 @@ public interface UserService {
|
|||||||
* @param dto 注册参数
|
* @param dto 注册参数
|
||||||
* @return JWT
|
* @return JWT
|
||||||
* */
|
* */
|
||||||
RestResp<String> getImgVerifyCode(UserRegisterReqDto dto);
|
RestResp<String> register(UserRegisterReqDto dto);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户登录
|
||||||
|
* @param dto 登录参数
|
||||||
|
* @return JWT + 昵称
|
||||||
|
* */
|
||||||
|
RestResp<UserLoginRespDto> login(UserLoginReqDto dto);
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,9 @@ import io.github.xxyopen.novel.core.constant.SystemConfigConsts;
|
|||||||
import io.github.xxyopen.novel.core.util.JwtUtils;
|
import io.github.xxyopen.novel.core.util.JwtUtils;
|
||||||
import io.github.xxyopen.novel.dao.entity.UserInfo;
|
import io.github.xxyopen.novel.dao.entity.UserInfo;
|
||||||
import io.github.xxyopen.novel.dao.mapper.UserInfoMapper;
|
import io.github.xxyopen.novel.dao.mapper.UserInfoMapper;
|
||||||
|
import io.github.xxyopen.novel.dto.req.UserLoginReqDto;
|
||||||
import io.github.xxyopen.novel.dto.req.UserRegisterReqDto;
|
import io.github.xxyopen.novel.dto.req.UserRegisterReqDto;
|
||||||
|
import io.github.xxyopen.novel.dto.resp.UserLoginRespDto;
|
||||||
import io.github.xxyopen.novel.manager.VerifyCodeManager;
|
import io.github.xxyopen.novel.manager.VerifyCodeManager;
|
||||||
import io.github.xxyopen.novel.service.UserService;
|
import io.github.xxyopen.novel.service.UserService;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
@ -18,6 +20,7 @@ import org.springframework.util.DigestUtils;
|
|||||||
|
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 会员模块 服务实现类
|
* 会员模块 服务实现类
|
||||||
@ -36,7 +39,7 @@ public class UserServiceImpl implements UserService {
|
|||||||
private final JwtUtils jwtUtils;
|
private final JwtUtils jwtUtils;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public RestResp<String> getImgVerifyCode(UserRegisterReqDto dto) {
|
public RestResp<String> register(UserRegisterReqDto dto) {
|
||||||
// 校验图形验证码是否正确
|
// 校验图形验证码是否正确
|
||||||
if (!verifyCodeManager.imgVerifyCodeOk(dto.getUserKey(), dto.getVelCode())) {
|
if (!verifyCodeManager.imgVerifyCodeOk(dto.getUserKey(), dto.getVelCode())) {
|
||||||
// 图形验证码校验失败
|
// 图形验证码校验失败
|
||||||
@ -66,4 +69,30 @@ public class UserServiceImpl implements UserService {
|
|||||||
// 生成JWT 并返回
|
// 生成JWT 并返回
|
||||||
return RestResp.ok(jwtUtils.generateToken(userInfo.getId(), SystemConfigConsts.NOVEL_FRONT_KEY));
|
return RestResp.ok(jwtUtils.generateToken(userInfo.getId(), SystemConfigConsts.NOVEL_FRONT_KEY));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public RestResp<UserLoginRespDto> login(UserLoginReqDto dto) {
|
||||||
|
// 查询用户信息
|
||||||
|
QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
|
||||||
|
queryWrapper.eq(DatabaseConsts.UserInfoTable.ColumnEnum.USERNAME.getName()
|
||||||
|
, dto.getUsername())
|
||||||
|
.last(DatabaseConsts.SqlEnum.LIMIT_1.getSql());
|
||||||
|
UserInfo userInfo = userInfoMapper.selectOne(queryWrapper);
|
||||||
|
if (Objects.isNull(userInfo)) {
|
||||||
|
// 用户不存在
|
||||||
|
throw new BusinessException(ErrorCodeEnum.USER_ACCOUNT_NOT_EXIST);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 判断密码是否正确
|
||||||
|
if (!Objects.equals(userInfo.getPassword()
|
||||||
|
, DigestUtils.md5DigestAsHex(dto.getPassword().getBytes(StandardCharsets.UTF_8)))) {
|
||||||
|
// 密码错误
|
||||||
|
throw new BusinessException(ErrorCodeEnum.USER_PASSWORD_ERROR);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 登录成功,生成JWT并返回
|
||||||
|
return RestResp.ok(UserLoginRespDto.builder()
|
||||||
|
.jwt(jwtUtils.generateToken(userInfo.getId(), SystemConfigConsts.NOVEL_FRONT_KEY))
|
||||||
|
.nickName(userInfo.getNickName()).build());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user