2024-12-06 20:36:28 +08:00
|
|
|
package com.guwan.backend.controller;
|
|
|
|
|
2024-12-06 22:26:17 +08:00
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
|
|
import cn.hutool.core.util.RandomUtil;
|
2024-12-06 20:36:28 +08:00
|
|
|
import com.guwan.backend.common.Result;
|
2024-12-07 16:21:40 +08:00
|
|
|
import com.guwan.backend.dto.user.*;
|
2024-12-06 22:26:17 +08:00
|
|
|
import com.guwan.backend.service.EmailService;
|
2024-12-06 20:36:28 +08:00
|
|
|
import com.guwan.backend.service.UserService;
|
2024-12-06 22:26:17 +08:00
|
|
|
import com.guwan.backend.util.RedisUtils;
|
2024-12-07 14:43:43 +08:00
|
|
|
import com.guwan.backend.util.SmsUtils;
|
2024-12-06 20:36:28 +08:00
|
|
|
import jakarta.validation.Valid;
|
2024-12-07 18:52:05 +08:00
|
|
|
import jakarta.validation.constraints.Email;
|
2024-12-06 20:36:28 +08:00
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
import org.springframework.validation.annotation.Validated;
|
|
|
|
import org.springframework.web.bind.annotation.*;
|
2024-12-06 22:26:17 +08:00
|
|
|
import org.thymeleaf.context.Context;
|
|
|
|
|
2024-12-06 20:36:28 +08:00
|
|
|
@Slf4j
|
|
|
|
@RestController
|
|
|
|
@RequestMapping("/api/user")
|
|
|
|
@RequiredArgsConstructor
|
|
|
|
@Validated
|
|
|
|
public class UserController {
|
|
|
|
|
|
|
|
private final UserService userService;
|
2024-12-06 22:26:17 +08:00
|
|
|
|
|
|
|
private final EmailService emailService;
|
|
|
|
|
|
|
|
private final RedisUtils redisUtils;
|
2024-12-06 20:36:28 +08:00
|
|
|
|
|
|
|
@PostMapping("/register")
|
|
|
|
public Result<UserDTO> register(@RequestBody @Valid RegisterDTO request) {
|
|
|
|
try {
|
|
|
|
log.info("用户注册: {}", request.getUsername());
|
|
|
|
return Result.success("注册成功", userService.register(request));
|
|
|
|
} catch (IllegalArgumentException e) {
|
|
|
|
return Result.validateFailed(e.getMessage());
|
|
|
|
} catch (Exception e) {
|
|
|
|
log.error("注册失败", e);
|
|
|
|
return Result.error("系统错误");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@PostMapping("/login")
|
2024-12-07 18:52:05 +08:00
|
|
|
public Result<String> login(@RequestBody @Valid LoginDto request) {
|
2024-12-06 20:36:28 +08:00
|
|
|
try {
|
|
|
|
log.info("用户登录: {}", request.getUsername());
|
2024-12-07 18:52:05 +08:00
|
|
|
return Result.success("登录成功", userService.login(request).getToken());
|
2024-12-06 20:36:28 +08:00
|
|
|
} catch (IllegalArgumentException e) {
|
|
|
|
return Result.unauthorized(e.getMessage());
|
|
|
|
} catch (Exception e) {
|
|
|
|
log.error("登录失败", e);
|
|
|
|
return Result.error("系统错误");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@GetMapping("/current")
|
|
|
|
public Result<UserDTO> getCurrentUser() {
|
|
|
|
UserDTO user = userService.getCurrentUser();
|
|
|
|
if (user == null) {
|
|
|
|
return Result.unauthorized("用户未登录");
|
|
|
|
}
|
|
|
|
return Result.success(user);
|
|
|
|
}
|
|
|
|
|
|
|
|
@GetMapping("/{id}")
|
|
|
|
public Result<UserDTO> getUserById(@PathVariable Long id) {
|
|
|
|
UserDTO user = userService.getUserById(id);
|
|
|
|
if (user == null) {
|
|
|
|
return Result.notFound("用户不存在");
|
|
|
|
}
|
|
|
|
return Result.success(user);
|
|
|
|
}
|
|
|
|
|
|
|
|
@PostMapping("/token/refresh")
|
|
|
|
public Result<String> refreshToken(@RequestHeader(value = "Authorization", required = false) String token) {
|
|
|
|
if (token == null || !token.startsWith("Bearer ")) {
|
|
|
|
return Result.error("无效的token");
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
String newToken = userService.refreshToken(token.substring(7));
|
|
|
|
return Result.success(newToken);
|
|
|
|
} catch (Exception e) {
|
|
|
|
log.error("刷新token失败", e);
|
|
|
|
return Result.error(e.getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-07 14:43:43 +08:00
|
|
|
|
2024-12-06 22:26:17 +08:00
|
|
|
|
|
|
|
|
|
|
|
@PostMapping("/getEmailCode")
|
2024-12-07 16:21:40 +08:00
|
|
|
public Result getEmailCode(@RequestBody @Valid EmailDto emailDto) {
|
|
|
|
|
|
|
|
String email = emailDto.getEmail();
|
2024-12-06 22:26:17 +08:00
|
|
|
|
|
|
|
log.info("邮箱注册: {}", email);
|
2024-12-07 14:43:43 +08:00
|
|
|
|
2024-12-06 22:26:17 +08:00
|
|
|
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);
|
2024-12-07 14:43:43 +08:00
|
|
|
return Result.success("邮件验证码发送成功");
|
2024-12-06 22:26:17 +08:00
|
|
|
}
|
|
|
|
|
2024-12-06 20:36:28 +08:00
|
|
|
|
2024-12-07 14:43:43 +08:00
|
|
|
@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("手机验证码发送成功");
|
2024-12-06 20:36:28 +08:00
|
|
|
}
|
|
|
|
|
2024-12-07 19:41:47 +08:00
|
|
|
|
2024-12-06 20:36:28 +08:00
|
|
|
|
2024-12-07 14:43:43 +08:00
|
|
|
|
2024-12-06 20:36:28 +08:00
|
|
|
|
|
|
|
@PostMapping("/password/reset")
|
|
|
|
public Result<Void> resetPassword(@RequestParam @Email String email) {
|
|
|
|
try {
|
|
|
|
userService.resetPassword(email);
|
|
|
|
return Result.success();
|
|
|
|
} catch (Exception e) {
|
|
|
|
log.error("重置密码失败", e);
|
|
|
|
return Result.error(e.getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-07 20:08:40 +08:00
|
|
|
|
2024-12-06 20:36:28 +08:00
|
|
|
|
|
|
|
@PutMapping("/info")
|
|
|
|
public Result<UserDTO> updateUserInfo(@RequestBody @Valid UserDTO userDTO) {
|
|
|
|
try {
|
|
|
|
return Result.success(userService.updateUserInfo(userDTO));
|
|
|
|
} catch (Exception e) {
|
|
|
|
log.error("更新用户信息失败", e);
|
|
|
|
return Result.error(e.getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|