用户模块:用户注册接口
This commit is contained in:
parent
058c06f038
commit
f058c354bb
@ -6,7 +6,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* MainController
|
||||
* 健康检查
|
||||
*
|
||||
* @author huangge1199
|
||||
* @since 2025/6/28 16:29:11
|
||||
|
@ -0,0 +1,41 @@
|
||||
package com.huangge1199.picture.controller;
|
||||
|
||||
import com.huangge1199.picture.common.R;
|
||||
import com.huangge1199.picture.exception.ErrorCode;
|
||||
import com.huangge1199.picture.exception.ThrowUtils;
|
||||
import com.huangge1199.picture.model.dto.user.UserRegisterRequest;
|
||||
import com.huangge1199.picture.service.UserService;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
* 用户
|
||||
*
|
||||
* @author huangge1199
|
||||
* @since 2025/6/30 10:46:25
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/user")
|
||||
public class UserController {
|
||||
|
||||
@Resource
|
||||
private UserService userService;
|
||||
|
||||
/**
|
||||
* 用户注册
|
||||
*/
|
||||
@PostMapping("/register")
|
||||
public R<Long> userRegister(@RequestBody UserRegisterRequest userRegisterRequest) {
|
||||
ThrowUtils.throwIf(userRegisterRequest == null, ErrorCode.PARAMS_ERROR);
|
||||
String userAccount = userRegisterRequest.getUserAccount();
|
||||
String userPassword = userRegisterRequest.getUserPassword();
|
||||
String checkPassword = userRegisterRequest.getCheckPassword();
|
||||
long result = userService.userRegister(userAccount, userPassword, checkPassword);
|
||||
return R.ok(result);
|
||||
}
|
||||
}
|
||||
|
@ -3,7 +3,6 @@ package com.huangge1199.picture.exception;
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* ErrorCode
|
||||
* 异常信息
|
||||
*
|
||||
* @author huangge1199
|
||||
|
@ -0,0 +1,29 @@
|
||||
package com.huangge1199.picture.exception;
|
||||
|
||||
import com.huangge1199.picture.common.R;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
|
||||
/**
|
||||
* 全局异常处理器
|
||||
*
|
||||
* @author huangge1199
|
||||
* @since 2025/6/30 10:56:33
|
||||
*/
|
||||
@RestControllerAdvice
|
||||
@Slf4j
|
||||
public class GlobalExceptionHandler {
|
||||
|
||||
@ExceptionHandler(MyException.class)
|
||||
public R<?> businessExceptionHandler(MyException e) {
|
||||
log.error("BusinessException", e);
|
||||
return R.fail(e.getCode(), e.getMessage());
|
||||
}
|
||||
|
||||
@ExceptionHandler(RuntimeException.class)
|
||||
public R<?> businessExceptionHandler(RuntimeException e) {
|
||||
log.error("RuntimeException", e);
|
||||
return R.fail(ErrorCode.SYSTEM_ERROR, "系统错误");
|
||||
}
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
package com.huangge1199.picture.exception;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
/**
|
||||
* 自定义业务异常
|
||||
*
|
||||
* @author huangge1199
|
||||
* @since 2025/6/30 10:42:10
|
||||
*/
|
||||
@Setter
|
||||
@Getter
|
||||
public class MyException extends RuntimeException {
|
||||
|
||||
/**
|
||||
* 错误码
|
||||
*/
|
||||
private final int code;
|
||||
|
||||
public MyException(int code, String message) {
|
||||
super(message);
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public MyException(ErrorCode errorCode) {
|
||||
super(errorCode.getMessage());
|
||||
this.code = errorCode.getCode();
|
||||
}
|
||||
|
||||
public MyException(ErrorCode errorCode, String message) {
|
||||
super(message);
|
||||
this.code = errorCode.getCode();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
package com.huangge1199.picture.exception;
|
||||
|
||||
/**
|
||||
* 异常处理工具类
|
||||
*
|
||||
* @author huangge1199
|
||||
* @since 2025/6/30 11:00:12
|
||||
*/
|
||||
public class ThrowUtils {
|
||||
|
||||
/**
|
||||
* 条件成立则抛异常
|
||||
*
|
||||
* @param condition 条件
|
||||
* @param runtimeException 异常
|
||||
*/
|
||||
public static void throwIf(boolean condition, RuntimeException runtimeException) {
|
||||
if (condition) {
|
||||
throw runtimeException;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 条件成立则抛异常
|
||||
*
|
||||
* @param condition 条件
|
||||
* @param errorCode 错误码
|
||||
*/
|
||||
public static void throwIf(boolean condition, ErrorCode errorCode) {
|
||||
throwIf(condition, new MyException(errorCode));
|
||||
}
|
||||
|
||||
/**
|
||||
* 条件成立则抛异常
|
||||
*
|
||||
* @param condition 条件
|
||||
* @param errorCode 错误码
|
||||
* @param message 错误信息
|
||||
*/
|
||||
public static void throwIf(boolean condition, ErrorCode errorCode, String message) {
|
||||
throwIf(condition, new MyException(errorCode, message));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
package com.huangge1199.picture.model.dto.user;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* UserRegisterRequest
|
||||
*
|
||||
* @author huangge1199
|
||||
* @since 2025/6/30 10:38:36
|
||||
*/
|
||||
@Data
|
||||
public class UserRegisterRequest implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 3191241716373120793L;
|
||||
|
||||
/**
|
||||
* 账号
|
||||
*/
|
||||
private String userAccount;
|
||||
|
||||
/**
|
||||
* 密码
|
||||
*/
|
||||
private String userPassword;
|
||||
|
||||
/**
|
||||
* 确认密码
|
||||
*/
|
||||
private String checkPassword;
|
||||
}
|
@ -4,10 +4,28 @@ import com.huangge1199.picture.model.entity.User;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
|
||||
/**
|
||||
* @author hyy
|
||||
* @description 针对表【user(用户)】的数据库操作Service
|
||||
* @createDate 2025-06-30 10:27:45
|
||||
*/
|
||||
* @author hyy
|
||||
* @description 针对表【user(用户)】的数据库操作Service
|
||||
* @createDate 2025-06-30 10:27:45
|
||||
*/
|
||||
public interface UserService extends IService<User> {
|
||||
|
||||
/**
|
||||
* 用户注册
|
||||
*
|
||||
* @param userAccount 用户账户
|
||||
* @param userPassword 用户密码
|
||||
* @param checkPassword 校验密码
|
||||
* @return 新用户 id
|
||||
*/
|
||||
long userRegister(String userAccount, String userPassword, String checkPassword);
|
||||
|
||||
/**
|
||||
* 获取加密后的密码
|
||||
*
|
||||
* @param userPassword 用户密码
|
||||
* @return 加密后的密码
|
||||
*/
|
||||
String getEncryptPassword(String userPassword);
|
||||
|
||||
}
|
||||
|
@ -1,19 +1,69 @@
|
||||
package com.huangge1199.picture.service.impl;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.huangge1199.picture.exception.ErrorCode;
|
||||
import com.huangge1199.picture.exception.MyException;
|
||||
import com.huangge1199.picture.model.entity.User;
|
||||
import com.huangge1199.picture.model.enums.UserRoleEnum;
|
||||
import com.huangge1199.picture.service.UserService;
|
||||
import com.huangge1199.picture.mapper.UserMapper;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.DigestUtils;
|
||||
|
||||
/**
|
||||
* @author hyy
|
||||
* @description 针对表【user(用户)】的数据库操作Service实现
|
||||
* @createDate 2025-06-30 10:27:45
|
||||
*/
|
||||
* @author hyy
|
||||
* @description 针对表【user(用户)】的数据库操作Service实现
|
||||
* @createDate 2025-06-30 10:27:45
|
||||
*/
|
||||
@Service
|
||||
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
|
||||
implements UserService{
|
||||
implements UserService {
|
||||
@Override
|
||||
public long userRegister(String userAccount, String userPassword, String checkPassword) {
|
||||
// 1. 校验
|
||||
if (StrUtil.hasBlank(userAccount, userPassword, checkPassword)) {
|
||||
throw new MyException(ErrorCode.PARAMS_ERROR, "参数为空");
|
||||
}
|
||||
if (userAccount.length() < 4) {
|
||||
throw new MyException(ErrorCode.PARAMS_ERROR, "用户账号过短");
|
||||
}
|
||||
if (userPassword.length() < 8 || checkPassword.length() < 8) {
|
||||
throw new MyException(ErrorCode.PARAMS_ERROR, "用户密码过短");
|
||||
}
|
||||
if (!userPassword.equals(checkPassword)) {
|
||||
throw new MyException(ErrorCode.PARAMS_ERROR, "两次输入的密码不一致");
|
||||
}
|
||||
// 2. 检查是否重复
|
||||
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.eq("userAccount", userAccount);
|
||||
long count = this.baseMapper.selectCount(queryWrapper);
|
||||
if (count > 0) {
|
||||
throw new MyException(ErrorCode.PARAMS_ERROR, "账号重复");
|
||||
}
|
||||
// 3. 加密
|
||||
String encryptPassword = getEncryptPassword(userPassword);
|
||||
// 4. 插入数据
|
||||
User user = new User();
|
||||
user.setUserAccount(userAccount);
|
||||
user.setUserPassword(encryptPassword);
|
||||
user.setUserName("无名");
|
||||
user.setUserRole(UserRoleEnum.USER.getValue());
|
||||
boolean saveResult = this.save(user);
|
||||
if (!saveResult) {
|
||||
throw new MyException(ErrorCode.SYSTEM_ERROR, "注册失败,数据库错误");
|
||||
}
|
||||
return user.getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getEncryptPassword(String userPassword) {
|
||||
// 盐值,混淆密码
|
||||
final String salt = "huangge";
|
||||
return DigestUtils.md5DigestAsHex((salt + userPassword).getBytes());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
@ -8,7 +8,7 @@ spring:
|
||||
# 数据库配置
|
||||
datasource:
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
url: jdbc:mysql://192.168.188.2:3306/long_picture
|
||||
url: jdbc:mysql://192.168.188.2:3306/long-picture
|
||||
username: root
|
||||
password: huangge1199
|
||||
mybatis-plus:
|
||||
|
Loading…
Reference in New Issue
Block a user