注册实现完成
This commit is contained in:
parent
91302cb95a
commit
2361c2e008
|
@ -0,0 +1,19 @@
|
|||
package com.guwan.backend.Handler;
|
||||
|
||||
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
|
||||
import org.apache.ibatis.reflection.MetaObject;
|
||||
import org.springframework.stereotype.Component;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Component
|
||||
public class MyMetaObjectHandler implements MetaObjectHandler {
|
||||
@Override
|
||||
public void insertFill(MetaObject metaObject) {
|
||||
this.strictInsertFill(metaObject, "createdTime", LocalDateTime.class, LocalDateTime.now());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateFill(MetaObject metaObject) {
|
||||
this.strictUpdateFill(metaObject, "lastLoginTime", LocalDateTime.class, LocalDateTime.now());
|
||||
}
|
||||
}
|
|
@ -15,10 +15,8 @@ public class SecurityConstants {
|
|||
"/demo/**", // 测试接口
|
||||
"/api/user/register", // 用户注册
|
||||
"/api/user/login", // 用户登录
|
||||
"/api/user/getEmailCode", // 邮箱注册
|
||||
"/api/user/register/phone", // 手机号注册
|
||||
"/api/user/email/code", // 获取邮箱验证码
|
||||
"/api/user/phone/code" // 获取手机验证码
|
||||
"/api/user/getEmailCode", // 获取邮箱验证码
|
||||
"/api/user/getPhoneCode" // 获取手机验证码
|
||||
);
|
||||
|
||||
/**
|
||||
|
|
|
@ -16,40 +16,11 @@ import org.thymeleaf.context.Context;
|
|||
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("/user")
|
||||
@RequestMapping("/demo")
|
||||
@RequiredArgsConstructor
|
||||
@Validated
|
||||
public class DemoController {
|
||||
|
||||
private final EmailService emailService;
|
||||
|
||||
private final RedisUtils redisUtils;
|
||||
|
||||
@GetMapping("/getEmailCode")
|
||||
public Result getEmailCode(String email) {
|
||||
|
||||
Context context = new Context();
|
||||
context.setVariable("nowDate", DateUtil.now());
|
||||
String code = RandomUtil.randomNumbers(6);
|
||||
redisUtils.set(email, code, 10);
|
||||
context.setVariable("code", code.toCharArray());
|
||||
|
||||
emailService.sendHtmlMessage(email,
|
||||
"养老平台邮箱验证码", "email_template.html", context);
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
|
||||
@GetMapping("/getPhoneCode")
|
||||
public Result getPhoneCode(String phone) throws Exception {
|
||||
|
||||
String random = RandomUtil.randomNumbers(6);
|
||||
|
||||
SmsUtils.sendMessage(phone, random);
|
||||
|
||||
redisUtils.set(phone, random, 10);
|
||||
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -3,11 +3,14 @@ package com.guwan.backend.controller;
|
|||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.util.RandomUtil;
|
||||
import com.guwan.backend.common.Result;
|
||||
import com.guwan.backend.dto.user.EmailDto;
|
||||
import com.guwan.backend.dto.user.PhoneDto;
|
||||
import com.guwan.backend.dto.user.RegisterDTO;
|
||||
import com.guwan.backend.dto.user.UserDTO;
|
||||
import com.guwan.backend.service.EmailService;
|
||||
import com.guwan.backend.service.UserService;
|
||||
import com.guwan.backend.util.RedisUtils;
|
||||
import com.guwan.backend.util.SmsUtils;
|
||||
import com.guwan.backend.vo.user.LoginRequest;
|
||||
import com.guwan.backend.vo.user.RegisterRequest;
|
||||
import com.guwan.backend.vo.user.EmailRegisterRequest;
|
||||
|
@ -94,18 +97,14 @@ public class UserController {
|
|||
}
|
||||
}
|
||||
|
||||
@PostMapping("/register/email")
|
||||
public Result<UserDTO> registerByEmail(@RequestBody @Valid EmailRegisterRequest request) {
|
||||
log.info("邮箱注册: {}", request.getEmail());
|
||||
return Result.success(userService.registerByEmail(request));
|
||||
}
|
||||
|
||||
|
||||
|
||||
@PostMapping("/getEmailCode")
|
||||
public Result getEmailCode(@RequestBody Map<String, Object> params) {
|
||||
public Result getEmailCode(@RequestParam @Email(message = "邮箱格式不正确") String email) {
|
||||
|
||||
String email = (String) params.get("email");
|
||||
log.info("邮箱注册: {}", email);
|
||||
|
||||
Context context = new Context();
|
||||
context.setVariable("nowDate", DateUtil.now());
|
||||
String code = RandomUtil.randomNumbers(6);
|
||||
|
@ -114,39 +113,26 @@ public class UserController {
|
|||
|
||||
emailService.sendHtmlMessage(email,
|
||||
"养老平台邮箱验证码", "email_template.html", context);
|
||||
return Result.success();
|
||||
return Result.success("邮件验证码发送成功");
|
||||
}
|
||||
|
||||
|
||||
@PostMapping("/register/phone")
|
||||
public Result<UserDTO> registerByPhone(@RequestBody @Valid PhoneRegisterRequest request) {
|
||||
log.info("手机号注册: {}", request.getPhone());
|
||||
return Result.success(userService.registerByPhone(request));
|
||||
@PostMapping("/getPhoneCode")
|
||||
public Result registerByPhone(@RequestBody @Valid PhoneDto phoneDto) throws Exception {
|
||||
String phone = phoneDto.getPhone();
|
||||
log.info("手机号注册: {}", phone);
|
||||
String random = RandomUtil.randomNumbers(6);
|
||||
|
||||
SmsUtils.sendMessage(phone, random);
|
||||
|
||||
redisUtils.set(phone, random, 10);
|
||||
|
||||
return Result.success("手机验证码发送成功");
|
||||
}
|
||||
|
||||
@PostMapping("/email/code")
|
||||
public Result<Void> sendEmailCode(
|
||||
@RequestParam @Email(message = "邮箱格式不正确") String email) {
|
||||
try {
|
||||
userService.sendEmailCode(email);
|
||||
return Result.success();
|
||||
} catch (Exception e) {
|
||||
log.error("发送邮箱验证码失败", e);
|
||||
return Result.error(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@PostMapping("/phone/code")
|
||||
public Result<Void> sendPhoneCode(
|
||||
@RequestParam @Pattern(regexp = "^1[3-9]\\d{9}$", message = "手机号格式不正确") String phone) {
|
||||
try {
|
||||
userService.sendPhoneCode(phone);
|
||||
return Result.success();
|
||||
} catch (Exception e) {
|
||||
log.error("发送短信验证码失败", e);
|
||||
return Result.error(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@PostMapping("/password/reset")
|
||||
public Result<Void> resetPassword(@RequestParam @Email String email) {
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
package com.guwan.backend.dto.user;
|
||||
|
||||
import jakarta.validation.constraints.Email;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class EmailDto {
|
||||
/**
|
||||
* 用户邮箱
|
||||
*/
|
||||
@Email(message = "邮箱格式不正确")
|
||||
private String email;
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
package com.guwan.backend.dto.user;
|
||||
|
||||
public class LoginDto {
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package com.guwan.backend.dto.user;
|
||||
|
||||
import jakarta.validation.constraints.Pattern;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class PhoneDto {
|
||||
/**
|
||||
* 用户邮箱
|
||||
*/
|
||||
@Pattern(regexp = "^1[3-9]\\d{9}$", message = "手机号格式不正确")
|
||||
private String phone;
|
||||
}
|
|
@ -1,47 +0,0 @@
|
|||
package com.guwan.backend.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
@TableName(value = "community_post", autoResultMap = true)
|
||||
public class CommunityPost {
|
||||
@TableId(type = IdType.AUTO)
|
||||
private Long id;
|
||||
|
||||
private String title;
|
||||
|
||||
private String content;
|
||||
|
||||
@TableField("author_id")
|
||||
private Long authorId;
|
||||
|
||||
private String category;
|
||||
|
||||
@TableField(typeHandler = JacksonTypeHandler.class)
|
||||
private List<String> tags;
|
||||
|
||||
@TableField(value = "image_urls", typeHandler = JacksonTypeHandler.class)
|
||||
private List<String> imageUrls;
|
||||
|
||||
private Integer likes;
|
||||
|
||||
private Integer comments;
|
||||
|
||||
private Integer views;
|
||||
|
||||
@TableField("created_time")
|
||||
private LocalDateTime createdTime;
|
||||
|
||||
@TableField("updated_time")
|
||||
private LocalDateTime updatedTime;
|
||||
|
||||
private Integer status;
|
||||
}
|
|
@ -1,9 +1,6 @@
|
|||
package com.guwan.backend.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import com.baomidou.mybatisplus.annotation.*;
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
@ -30,10 +27,11 @@ public class User {
|
|||
|
||||
private String bio;
|
||||
|
||||
@TableField("created_time")
|
||||
|
||||
@TableField(fill = FieldFill.INSERT)
|
||||
private LocalDateTime createdTime;
|
||||
|
||||
@TableField("last_login_time")
|
||||
|
||||
@TableField(fill = FieldFill.INSERT_UPDATE)
|
||||
private LocalDateTime lastLoginTime;
|
||||
|
||||
private Integer status;
|
||||
|
|
|
@ -8,6 +8,11 @@ import com.guwan.backend.vo.user.PhoneRegisterRequest;
|
|||
import com.guwan.backend.vo.user.RegisterRequest;
|
||||
|
||||
public interface UserService {
|
||||
/**
|
||||
* 用户注册
|
||||
* @param registerDTO
|
||||
* @return
|
||||
*/
|
||||
UserDTO register(RegisterDTO registerDTO);
|
||||
UserDTO login(LoginRequest request);
|
||||
UserDTO getCurrentUser();
|
||||
|
|
|
@ -54,12 +54,26 @@ public class UserServiceImpl implements UserService {
|
|||
throw new IllegalArgumentException("手机号已被注册");
|
||||
}
|
||||
|
||||
// 校验邮箱验证码
|
||||
String redisEmailCode = (String) redisUtil.get(request.getEmail());
|
||||
|
||||
if (!request.getEmailCode().equals(redisEmailCode)) {
|
||||
throw new IllegalArgumentException("邮箱验证码错误");
|
||||
}
|
||||
|
||||
// 校验手机号验证码
|
||||
String redisPhoneCode = (String) redisUtil.get(request.getPhone());
|
||||
|
||||
if (!request.getPhoneCode().equals(redisPhoneCode)) {
|
||||
throw new IllegalArgumentException("手机验证码错误");
|
||||
}
|
||||
|
||||
User user = new User();
|
||||
|
||||
BeanUtils.copyProperties(request, user);
|
||||
user.setPassword(passwordEncoder.encode(request.getPassword()));
|
||||
user.setPhone(request.getPhone());
|
||||
user.setEmail(request.getEmail());
|
||||
user.setCreatedTime(LocalDateTime.now());
|
||||
user.setStatus(1);
|
||||
|
||||
|
|
Loading…
Reference in New Issue