刘嘉昕 | f28ea23 | 2025-04-15 16:55:43 +0800 | [diff] [blame] | 1 | package com.pt5.pthouduan.controller; |
| 2 | |
| 3 | import com.pt5.pthouduan.entity.User; |
| 4 | import com.pt5.pthouduan.service.UserService; |
| 5 | import org.springframework.beans.factory.annotation.Autowired; |
21301050 | e4db6a9 | 2025-06-08 23:42:43 +0800 | [diff] [blame] | 6 | import org.springframework.beans.factory.annotation.Value; |
| 7 | import org.springframework.http.ResponseEntity; |
刘嘉昕 | f28ea23 | 2025-04-15 16:55:43 +0800 | [diff] [blame] | 8 | import org.springframework.web.bind.annotation.*; |
| 9 | import org.springframework.stereotype.Controller; |
21301050 | e4db6a9 | 2025-06-08 23:42:43 +0800 | [diff] [blame] | 10 | import org.springframework.web.multipart.MultipartFile; |
刘嘉昕 | f28ea23 | 2025-04-15 16:55:43 +0800 | [diff] [blame] | 11 | |
21301050 | e4db6a9 | 2025-06-08 23:42:43 +0800 | [diff] [blame] | 12 | import java.io.File; |
| 13 | import java.io.IOException; |
| 14 | import java.nio.file.Files; |
| 15 | import java.nio.file.Path; |
| 16 | import java.nio.file.Paths; |
| 17 | import java.nio.file.StandardCopyOption; |
| 18 | import java.text.SimpleDateFormat; |
| 19 | import java.util.*; |
Sure233 | 8188c5d | 2025-05-28 11:43:06 +0800 | [diff] [blame] | 20 | |
刘嘉昕 | f28ea23 | 2025-04-15 16:55:43 +0800 | [diff] [blame] | 21 | /** |
| 22 | * <p> |
| 23 | * 前端控制器 |
| 24 | * </p> |
| 25 | * |
| 26 | * @author ljx |
| 27 | * @since 2025-04-14 |
| 28 | */ |
Sure233 | 8188c5d | 2025-05-28 11:43:06 +0800 | [diff] [blame] | 29 | @RestController |
刘嘉昕 | f28ea23 | 2025-04-15 16:55:43 +0800 | [diff] [blame] | 30 | @RequestMapping("/user") |
| 31 | public class UserController { |
| 32 | @Autowired |
| 33 | private UserService userService; |
21301050 | f5f827d | 2025-06-09 15:09:33 +0800 | [diff] [blame^] | 34 | |
| 35 | @Value("${torrent.user-image-dir}") |
| 36 | private String uploadDir; |
| 37 | |
| 38 | //private String uploadDir="../var/www/avatars/"; // 配置文件上传目录,例如: /var/www/avatars/ |
21301050 | e4db6a9 | 2025-06-08 23:42:43 +0800 | [diff] [blame] | 39 | |
| 40 | private String accessPath="../avatars/"; |
刘嘉昕 | f28ea23 | 2025-04-15 16:55:43 +0800 | [diff] [blame] | 41 | |
Sure233 | 8188c5d | 2025-05-28 11:43:06 +0800 | [diff] [blame] | 42 | @PostMapping("/register") |
21301050 | c519f71 | 2025-06-04 17:03:04 +0800 | [diff] [blame] | 43 | public Map<String, Object> register(@RequestBody User user,@RequestParam String code,@RequestParam String emailcode) {//code是邀请码,emailcode是验证码 |
| 44 | return userService.register(user,code,emailcode); |
刘嘉昕 | f28ea23 | 2025-04-15 16:55:43 +0800 | [diff] [blame] | 45 | } |
| 46 | |
Sure233 | 8188c5d | 2025-05-28 11:43:06 +0800 | [diff] [blame] | 47 | @PostMapping("/login") |
| 48 | public Map<String, Object> login(@RequestParam String username, |
| 49 | @RequestParam String password) { |
| 50 | return userService.login(username, password); |
| 51 | } |
刘嘉昕 | f28ea23 | 2025-04-15 16:55:43 +0800 | [diff] [blame] | 52 | |
Sure233 | 8188c5d | 2025-05-28 11:43:06 +0800 | [diff] [blame] | 53 | @PostMapping("/calgrade") |
| 54 | public Map<String, Object> calgrade(@RequestParam String username) { |
| 55 | return userService.CalGrade(username); |
| 56 | } |
刘嘉昕 | f28ea23 | 2025-04-15 16:55:43 +0800 | [diff] [blame] | 57 | |
Sure233 | 8188c5d | 2025-05-28 11:43:06 +0800 | [diff] [blame] | 58 | @PostMapping("/changesex") |
| 59 | public Map<String, Object> changsex(@RequestParam String username, |
| 60 | @RequestParam String sex) { |
| 61 | return userService.changesex(username,sex); |
| 62 | } |
| 63 | |
| 64 | @PostMapping("/changeimage") |
21301050 | e4db6a9 | 2025-06-08 23:42:43 +0800 | [diff] [blame] | 65 | public Map<String, Object> changeimage(@RequestParam String username, @RequestParam String image) { |
| 66 | Map<String, Object> response = new HashMap<>(); |
| 67 | userService.changeImage(username,image); |
| 68 | response.put("success", true); |
| 69 | response.put("message", "头像更改成功"); |
| 70 | return response; |
Sure233 | 8188c5d | 2025-05-28 11:43:06 +0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | @PostMapping("/changePassword") |
| 74 | public Map<String, Object> changePassword(@RequestParam String username, |
| 75 | @RequestParam String oldpassword, |
| 76 | @RequestParam String newpassword) { |
| 77 | return userService.changePassword(username,oldpassword,newpassword); |
| 78 | } |
| 79 | |
21301050 | c519f71 | 2025-06-04 17:03:04 +0800 | [diff] [blame] | 80 | @PostMapping("/sendCode") |
| 81 | public Map<String, Object> sendCode(@RequestParam String email) { |
| 82 | return userService.sendCode(email); |
Sure233 | 8188c5d | 2025-05-28 11:43:06 +0800 | [diff] [blame] | 83 | } |
21301050 | c519f71 | 2025-06-04 17:03:04 +0800 | [diff] [blame] | 84 | |
| 85 | @GetMapping("/Info") |
| 86 | public Map<String, Object> getuser(@RequestParam String username) { |
| 87 | return userService.UserInfo(username); |
| 88 | } |
| 89 | |
| 90 | @PostMapping("/creatUser") |
| 91 | public Map<String, Object> creatUser(@RequestBody User user) { |
| 92 | return userService.CreateUser(user); |
| 93 | } |
| 94 | |
| 95 | @PostMapping("/DeleteUser") |
| 96 | public Map<String, Object> DeleteUser(@RequestParam String username) { |
| 97 | return userService.DeleteUser(username); |
| 98 | } |
| 99 | |
21301050 | e4db6a9 | 2025-06-08 23:42:43 +0800 | [diff] [blame] | 100 | @GetMapping("/alluser") |
| 101 | public Map<String, Object> alluser() { |
| 102 | return userService.getAllUser(); |
| 103 | } |
| 104 | |
| 105 | @GetMapping("/finduser") |
| 106 | public Map<String, Object> finduser(@RequestParam String keyword) { |
| 107 | return userService.findUser(keyword); |
| 108 | } |
| 109 | |
| 110 | @GetMapping("/getDecoration") |
| 111 | public Map<String, Object> getDecoration(@RequestParam Long userid) { |
| 112 | return userService.getDecoration(userid); |
| 113 | } |
| 114 | |
| 115 | @GetMapping("/getUserid") |
| 116 | public Map<String, Object> getuserid(@RequestParam String username) { |
| 117 | return userService.getuserid(username); |
| 118 | } |
| 119 | |
21301050 | f5f827d | 2025-06-09 15:09:33 +0800 | [diff] [blame^] | 120 | // @PostMapping("/uploadimage") |
| 121 | // public ResponseEntity<Map<String, Object>> uploadAvatar(@RequestParam("avatar") MultipartFile file) { |
| 122 | // Map<String, Object> response = new HashMap<>(); |
| 123 | // |
| 124 | // // 1. 验证文件是否为空 |
| 125 | // if (file.isEmpty()) { |
| 126 | // response.put("success", false); |
| 127 | // response.put("message", "请选择要上传的文件"); |
| 128 | // return ResponseEntity.badRequest().body(response); |
| 129 | // } |
| 130 | // |
| 131 | // // 2. 验证文件类型 |
| 132 | // String contentType = file.getContentType(); |
| 133 | // if (!"image/jpeg".equals(contentType) && |
| 134 | // !"image/png".equals(contentType) && |
| 135 | // !"image/gif".equals(contentType)) { |
| 136 | // response.put("success", false); |
| 137 | // response.put("message", "只支持JPG/PNG/GIF格式的图片"); |
| 138 | // return ResponseEntity.badRequest().body(response); |
| 139 | // } |
| 140 | // |
| 141 | // // 3. 验证文件大小 (前端已验证,后端再次验证) |
| 142 | // if (file.getSize() > 10 * 1024 * 1024) { // 10MB |
| 143 | // response.put("success", false); |
| 144 | // response.put("message", "图片大小不能超过10MB"); |
| 145 | // return ResponseEntity.badRequest().body(response); |
| 146 | // } |
21301050 | e4db6a9 | 2025-06-08 23:42:43 +0800 | [diff] [blame] | 147 | |
21301050 | f5f827d | 2025-06-09 15:09:33 +0800 | [diff] [blame^] | 148 | // try { |
| 149 | // // 4. 创建上传目录(如果不存在) |
| 150 | // File dir = new File(uploadDir); |
| 151 | // if (!dir.exists()) { |
| 152 | // boolean created = dir.mkdirs(); // 递归创建目录 |
| 153 | // if (!created) { |
| 154 | // throw new IOException("无法创建目录:" + uploadDir); |
| 155 | // } |
| 156 | // } |
| 157 | // System.out.println(dir.getAbsolutePath()); |
| 158 | // String filename = System.currentTimeMillis()+"_"+file.getOriginalFilename(); |
| 159 | // Path userimagePath = Paths.get(uploadDir, filename); |
| 160 | // |
| 161 | // // 6. 保存文件 |
| 162 | // Files.createDirectories(userimagePath.getParent()); // 创建目录 |
| 163 | // Files.copy(file.getInputStream(), userimagePath, StandardCopyOption.REPLACE_EXISTING); |
| 164 | // |
| 165 | // // 7. 返回访问URL |
| 166 | // //String fileUrl = accessPath + newFilename; |
| 167 | // String fileUrl = "http://localhost:8080/" + uploadDir + filename; |
| 168 | // |
| 169 | // response.put("success", true); |
| 170 | // response.put("url", fileUrl); |
| 171 | // response.put("message", "头像上传成功"); |
| 172 | // |
| 173 | // return ResponseEntity.ok(response); |
| 174 | // |
| 175 | // } catch (IOException e) { |
| 176 | // e.printStackTrace(); |
| 177 | // response.put("success", false); |
| 178 | // response.put("message", "文件上传失败: " + e.getMessage()); |
| 179 | // return ResponseEntity.status(500).body(response); |
| 180 | // } |
| 181 | //} |
21301050 | e4db6a9 | 2025-06-08 23:42:43 +0800 | [diff] [blame] | 182 | |
| 183 | |
刘嘉昕 | f28ea23 | 2025-04-15 16:55:43 +0800 | [diff] [blame] | 184 | } |