22301115 | cf6dba2 | 2025-03-25 19:06:21 +0800 | [diff] [blame] | 1 | package com.example.myproject.controller; |
| 2 | |
YelinCui | fdf4ed7 | 2025-05-26 11:49:36 +0800 | [diff] [blame] | 3 | import cn.dev33.satoken.annotation.SaCheckLogin; |
22301115 | cf6dba2 | 2025-03-25 19:06:21 +0800 | [diff] [blame] | 4 | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
YelinCui | fdf4ed7 | 2025-05-26 11:49:36 +0800 | [diff] [blame] | 5 | import com.example.myproject.common.base.PageUtil; |
| 6 | import com.example.myproject.dto.param.TorrentParam; |
| 7 | import com.example.myproject.dto.vo.TorrentVO; |
| 8 | import com.example.myproject.entity.TorrentEntity; |
22301115 | cf6dba2 | 2025-03-25 19:06:21 +0800 | [diff] [blame] | 9 | import com.example.myproject.mapper.UserMapper; |
| 10 | import com.example.myproject.mapper.VerificationTokenMapper; |
| 11 | import com.example.myproject.entity.User; |
| 12 | import com.example.myproject.entity.VerificationToken; |
| 13 | import com.example.myproject.service.EmailService; |
| 14 | import com.example.myproject.service.UserService; |
YelinCui | fdf4ed7 | 2025-05-26 11:49:36 +0800 | [diff] [blame] | 15 | import com.example.myproject.common.base.Result; |
22301115 | cf6dba2 | 2025-03-25 19:06:21 +0800 | [diff] [blame] | 16 | import io.swagger.annotations.Api; |
| 17 | import io.swagger.annotations.ApiOperation; |
| 18 | import io.swagger.annotations.ApiParam; |
YelinCui | fdf4ed7 | 2025-05-26 11:49:36 +0800 | [diff] [blame] | 19 | import io.swagger.v3.oas.annotations.Operation; |
| 20 | import io.swagger.v3.oas.annotations.media.Content; |
| 21 | import io.swagger.v3.oas.annotations.media.Schema; |
| 22 | import io.swagger.v3.oas.annotations.responses.ApiResponse; |
22301115 | cf6dba2 | 2025-03-25 19:06:21 +0800 | [diff] [blame] | 23 | import org.apache.commons.lang3.RandomStringUtils; |
| 24 | import org.slf4j.Logger; |
| 25 | import org.slf4j.LoggerFactory; |
| 26 | import org.springframework.beans.factory.annotation.Autowired; |
| 27 | import org.springframework.http.HttpStatus; |
| 28 | import org.springframework.http.ResponseEntity; |
| 29 | import org.springframework.security.authentication.AuthenticationManager; |
| 30 | import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; |
| 31 | import org.springframework.security.core.Authentication; |
| 32 | import org.springframework.security.core.AuthenticationException; |
| 33 | import org.springframework.security.core.context.SecurityContextHolder; |
| 34 | import org.springframework.web.bind.annotation.*; |
| 35 | |
| 36 | import javax.annotation.Resource; |
| 37 | import java.time.Instant; |
| 38 | import java.time.temporal.ChronoUnit; |
YelinCui | fdf4ed7 | 2025-05-26 11:49:36 +0800 | [diff] [blame] | 39 | import java.util.List; |
22301115 | cf6dba2 | 2025-03-25 19:06:21 +0800 | [diff] [blame] | 40 | |
| 41 | @RestController |
| 42 | @RequestMapping("/user") |
| 43 | @Api(value = "用户管理接口", tags = {"用户管理"}) |
| 44 | public class UserController { |
| 45 | |
| 46 | @Resource |
| 47 | private UserService userService; |
| 48 | |
| 49 | @Autowired |
| 50 | private AuthenticationManager authenticationManager; |
| 51 | |
| 52 | @Autowired |
| 53 | private UserMapper userMapper; // 使用 MyBatis-Plus |
| 54 | |
| 55 | @Autowired |
| 56 | private VerificationTokenMapper verificationTokenMapper; // 替换 JPA |
| 57 | |
| 58 | private static final Logger logger = LoggerFactory.getLogger(UserController.class); |
| 59 | |
| 60 | @PostMapping("/login") |
| 61 | @ApiOperation(value = "用户登录", notes = "使用用户名和密码进行登录") |
| 62 | public Result loginController(@RequestParam @ApiParam(value = "用户名", required = true) String username, |
| 63 | @RequestParam @ApiParam(value = "密码", required = true) String password) { |
| 64 | try { |
| 65 | Authentication authentication = authenticationManager.authenticate( |
| 66 | new UsernamePasswordAuthenticationToken(username, password) |
| 67 | ); |
| 68 | SecurityContextHolder.getContext().setAuthentication(authentication); |
| 69 | |
| 70 | // 使用 MyBatis-Plus 查询 |
| 71 | User user = userMapper.selectOne(new QueryWrapper<User>().eq("username", username)); |
| 72 | |
| 73 | System.out.println("Login successful for user: " + username); |
YelinCui | fdf4ed7 | 2025-05-26 11:49:36 +0800 | [diff] [blame] | 74 | return Result.ok(user); |
22301115 | cf6dba2 | 2025-03-25 19:06:21 +0800 | [diff] [blame] | 75 | } catch (AuthenticationException e) { |
YelinCui | fdf4ed7 | 2025-05-26 11:49:36 +0800 | [diff] [blame] | 76 | return Result.error("登录失败"); |
22301115 | cf6dba2 | 2025-03-25 19:06:21 +0800 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | |
| 80 | @PostMapping("/register") |
| 81 | @ApiOperation(value = "用户注册", notes = "使用用户信息进行注册") |
| 82 | public Result registerController(@RequestBody @ApiParam(value = "新用户信息", required = true) User newUser) { |
| 83 | if (userService.checkEmailExists(newUser.getEmail())) { |
YelinCui | fdf4ed7 | 2025-05-26 11:49:36 +0800 | [diff] [blame] | 84 | return Result.error( "邮箱已被使用,请使用其他邮箱注册或找回密码!"); |
22301115 | cf6dba2 | 2025-03-25 19:06:21 +0800 | [diff] [blame] | 85 | } |
| 86 | boolean success = userService.preRegisterUser(newUser); |
| 87 | if (success) { |
| 88 | User responseUser = new User(); |
| 89 | responseUser.setEmail(newUser.getEmail()); |
YelinCui | fdf4ed7 | 2025-05-26 11:49:36 +0800 | [diff] [blame] | 90 | return Result.ok(); |
22301115 | cf6dba2 | 2025-03-25 19:06:21 +0800 | [diff] [blame] | 91 | } else { |
YelinCui | fdf4ed7 | 2025-05-26 11:49:36 +0800 | [diff] [blame] | 92 | return Result.error("账号已存在或注册失败!"); |
22301115 | cf6dba2 | 2025-03-25 19:06:21 +0800 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | |
| 96 | public static class VerificationRequest { |
| 97 | private String email; |
| 98 | private String code; |
| 99 | |
| 100 | public String getEmail() { return email; } |
| 101 | public void setEmail(String email) { this.email = email; } |
| 102 | public String getCode() { return code; } |
| 103 | public void setCode(String code) { this.code = code; } |
| 104 | } |
| 105 | |
| 106 | @PostMapping("/verify-code") |
| 107 | @ApiOperation(value = "验证邮箱验证码", notes = "验证用户邮箱的验证码") |
| 108 | public Result verifyEmailCode(@RequestBody @ApiParam(value = "验证请求信息", required = true) VerificationRequest verificationRequest) { |
| 109 | String email = verificationRequest.getEmail(); |
| 110 | String code = verificationRequest.getCode(); |
| 111 | boolean isVerified = userService.verifyEmail(email, code); |
| 112 | if (isVerified) { |
YelinCui | fdf4ed7 | 2025-05-26 11:49:36 +0800 | [diff] [blame] | 113 | return Result.ok(); |
22301115 | cf6dba2 | 2025-03-25 19:06:21 +0800 | [diff] [blame] | 114 | } else { |
YelinCui | fdf4ed7 | 2025-05-26 11:49:36 +0800 | [diff] [blame] | 115 | return Result.error( "验证码错误或已过期!"); |
22301115 | cf6dba2 | 2025-03-25 19:06:21 +0800 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | |
| 119 | @Autowired |
| 120 | private EmailService emailService; |
| 121 | |
| 122 | public static class EmailRequest { |
| 123 | private String email; |
| 124 | public String getEmail() { return email; } |
| 125 | public void setEmail(String email) { this.email = email; } |
| 126 | } |
| 127 | |
| 128 | @PostMapping("/get-verification-email") |
| 129 | @ApiOperation(value = "发送验证邮件", notes = "通过电子邮件发送验证邮件") |
| 130 | public ResponseEntity<Result> sendVerificationEmail(@RequestBody @ApiParam(value = "发送验证请求", required = true) EmailRequest emailVerificationRequest) { |
| 131 | String email = emailVerificationRequest.getEmail(); |
| 132 | User user = userMapper.selectOne(new QueryWrapper<User>().eq("email", email)); |
| 133 | if (user == null) { |
| 134 | logger.error("未找到与该邮箱地址相关联的用户: {}", email); |
| 135 | return ResponseEntity.status(HttpStatus.BAD_REQUEST) |
YelinCui | fdf4ed7 | 2025-05-26 11:49:36 +0800 | [diff] [blame] | 136 | .body(Result.error("未找到与该邮箱地址相关联的用户")); |
22301115 | cf6dba2 | 2025-03-25 19:06:21 +0800 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | // 生成验证码 |
| 140 | String token = RandomStringUtils.randomNumeric(6); |
| 141 | Instant expiryDate = Instant.now().plus(1, ChronoUnit.HOURS); |
| 142 | logger.info("生成的验证令牌: {}, 过期时间: {}", token, expiryDate); |
| 143 | |
| 144 | VerificationToken verificationToken = new VerificationToken(token, user.getUsername(), email, user.getPassword(), expiryDate); |
| 145 | |
| 146 | // 保存到 MyBatis-Plus 数据库 |
| 147 | verificationTokenMapper.insert(verificationToken); |
| 148 | |
| 149 | logger.info("验证令牌已保存,用户: {}", user.getUsername()); |
| 150 | emailService.sendVerificationEmail(email, token); |
| 151 | |
YelinCui | fdf4ed7 | 2025-05-26 11:49:36 +0800 | [diff] [blame] | 152 | return ResponseEntity.ok(Result.ok()); |
22301115 | cf6dba2 | 2025-03-25 19:06:21 +0800 | [diff] [blame] | 153 | } |
| 154 | @PostMapping("/checkPassword") |
| 155 | public Result<String> checkPassword(@RequestParam Long userId, @RequestParam String password) { |
| 156 | boolean isPasswordCorrect = userService.checkPassword(userId, password); |
| 157 | if (isPasswordCorrect) { |
YelinCui | fdf4ed7 | 2025-05-26 11:49:36 +0800 | [diff] [blame] | 158 | return Result.ok(); |
22301115 | cf6dba2 | 2025-03-25 19:06:21 +0800 | [diff] [blame] | 159 | } else { |
YelinCui | fdf4ed7 | 2025-05-26 11:49:36 +0800 | [diff] [blame] | 160 | return Result.error("原始密码输入错误"); |
22301115 | cf6dba2 | 2025-03-25 19:06:21 +0800 | [diff] [blame] | 161 | } |
| 162 | } |
YelinCui | fdf4ed7 | 2025-05-26 11:49:36 +0800 | [diff] [blame] | 163 | |
| 164 | |
| 165 | // @SaCheckLogin |
| 166 | // @Operation(summary = "用户收藏列表", description = "获取用户收藏的种子列表-分页-排序") |
| 167 | // @ApiResponse(responseCode = "0", description = "操作成功", |
| 168 | // content = {@Content(mediaType = "application/json", |
| 169 | // schema = @Schema(implementation = TorrentVO.class)) |
| 170 | // }) |
| 171 | // @PostMapping("/favorite/list") |
| 172 | // public Result listFavorites(@RequestBody FavoriteParam param) { |
| 173 | // if (param.getUserId() == null) { |
| 174 | // return Result.error("缺少 userId"); |
| 175 | // } |
| 176 | // |
| 177 | // // 校验排序字段是否合理(可选) |
| 178 | // param.validOrder(param.getOrderKey(TorrentEntity.class)); |
| 179 | // |
| 180 | // PageUtil.startPage(param); |
| 181 | // |
| 182 | // List<TorrentEntity> list = favoriteService.getUserFavoritesPaged(param.getUserId()); |
| 183 | // |
| 184 | // return Result.ok(list, PageUtil.getPage(list)); |
| 185 | // } |
| 186 | // |
| 187 | |
| 188 | |
| 189 | |
22301115 | cf6dba2 | 2025-03-25 19:06:21 +0800 | [diff] [blame] | 190 | } |