刘嘉昕 | 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 | e4db6a9 | 2025-06-08 23:42:43 +0800 | [diff] [blame] | 34 | private String uploadDir="../var/www/avatars/"; // 配置文件上传目录,例如: /var/www/avatars/ |
| 35 | |
| 36 | private String accessPath="../avatars/"; |
刘嘉昕 | f28ea23 | 2025-04-15 16:55:43 +0800 | [diff] [blame] | 37 | |
Sure233 | 8188c5d | 2025-05-28 11:43:06 +0800 | [diff] [blame] | 38 | @PostMapping("/register") |
21301050 | c519f71 | 2025-06-04 17:03:04 +0800 | [diff] [blame] | 39 | public Map<String, Object> register(@RequestBody User user,@RequestParam String code,@RequestParam String emailcode) {//code是邀请码,emailcode是验证码 |
| 40 | return userService.register(user,code,emailcode); |
刘嘉昕 | f28ea23 | 2025-04-15 16:55:43 +0800 | [diff] [blame] | 41 | } |
| 42 | |
Sure233 | 8188c5d | 2025-05-28 11:43:06 +0800 | [diff] [blame] | 43 | @PostMapping("/login") |
| 44 | public Map<String, Object> login(@RequestParam String username, |
| 45 | @RequestParam String password) { |
| 46 | return userService.login(username, password); |
| 47 | } |
刘嘉昕 | f28ea23 | 2025-04-15 16:55:43 +0800 | [diff] [blame] | 48 | |
Sure233 | 8188c5d | 2025-05-28 11:43:06 +0800 | [diff] [blame] | 49 | @PostMapping("/calgrade") |
| 50 | public Map<String, Object> calgrade(@RequestParam String username) { |
| 51 | return userService.CalGrade(username); |
| 52 | } |
刘嘉昕 | f28ea23 | 2025-04-15 16:55:43 +0800 | [diff] [blame] | 53 | |
Sure233 | 8188c5d | 2025-05-28 11:43:06 +0800 | [diff] [blame] | 54 | @PostMapping("/changesex") |
| 55 | public Map<String, Object> changsex(@RequestParam String username, |
| 56 | @RequestParam String sex) { |
| 57 | return userService.changesex(username,sex); |
| 58 | } |
| 59 | |
| 60 | @PostMapping("/changeimage") |
21301050 | e4db6a9 | 2025-06-08 23:42:43 +0800 | [diff] [blame] | 61 | public Map<String, Object> changeimage(@RequestParam String username, @RequestParam String image) { |
| 62 | Map<String, Object> response = new HashMap<>(); |
| 63 | userService.changeImage(username,image); |
| 64 | response.put("success", true); |
| 65 | response.put("message", "头像更改成功"); |
| 66 | return response; |
Sure233 | 8188c5d | 2025-05-28 11:43:06 +0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | @PostMapping("/changePassword") |
| 70 | public Map<String, Object> changePassword(@RequestParam String username, |
| 71 | @RequestParam String oldpassword, |
| 72 | @RequestParam String newpassword) { |
| 73 | return userService.changePassword(username,oldpassword,newpassword); |
| 74 | } |
| 75 | |
21301050 | c519f71 | 2025-06-04 17:03:04 +0800 | [diff] [blame] | 76 | @PostMapping("/sendCode") |
| 77 | public Map<String, Object> sendCode(@RequestParam String email) { |
| 78 | return userService.sendCode(email); |
Sure233 | 8188c5d | 2025-05-28 11:43:06 +0800 | [diff] [blame] | 79 | } |
21301050 | c519f71 | 2025-06-04 17:03:04 +0800 | [diff] [blame] | 80 | |
| 81 | @GetMapping("/Info") |
| 82 | public Map<String, Object> getuser(@RequestParam String username) { |
| 83 | return userService.UserInfo(username); |
| 84 | } |
| 85 | |
| 86 | @PostMapping("/creatUser") |
| 87 | public Map<String, Object> creatUser(@RequestBody User user) { |
| 88 | return userService.CreateUser(user); |
| 89 | } |
| 90 | |
| 91 | @PostMapping("/DeleteUser") |
| 92 | public Map<String, Object> DeleteUser(@RequestParam String username) { |
| 93 | return userService.DeleteUser(username); |
| 94 | } |
| 95 | |
21301050 | e4db6a9 | 2025-06-08 23:42:43 +0800 | [diff] [blame] | 96 | @GetMapping("/alluser") |
| 97 | public Map<String, Object> alluser() { |
| 98 | return userService.getAllUser(); |
| 99 | } |
| 100 | |
| 101 | @GetMapping("/finduser") |
| 102 | public Map<String, Object> finduser(@RequestParam String keyword) { |
| 103 | return userService.findUser(keyword); |
| 104 | } |
| 105 | |
| 106 | @GetMapping("/getDecoration") |
| 107 | public Map<String, Object> getDecoration(@RequestParam Long userid) { |
| 108 | return userService.getDecoration(userid); |
| 109 | } |
| 110 | |
| 111 | @GetMapping("/getUserid") |
| 112 | public Map<String, Object> getuserid(@RequestParam String username) { |
| 113 | return userService.getuserid(username); |
| 114 | } |
| 115 | |
| 116 | @PostMapping("/uploadimage") |
| 117 | public ResponseEntity<Map<String, Object>> uploadAvatar(@RequestParam("avatar") MultipartFile file) { |
| 118 | Map<String, Object> response = new HashMap<>(); |
| 119 | |
| 120 | // 1. 验证文件是否为空 |
| 121 | if (file.isEmpty()) { |
| 122 | response.put("success", false); |
| 123 | response.put("message", "请选择要上传的文件"); |
| 124 | return ResponseEntity.badRequest().body(response); |
| 125 | } |
| 126 | |
| 127 | // 2. 验证文件类型 |
| 128 | String contentType = file.getContentType(); |
| 129 | if (!"image/jpeg".equals(contentType) && |
| 130 | !"image/png".equals(contentType) && |
| 131 | !"image/gif".equals(contentType)) { |
| 132 | response.put("success", false); |
| 133 | response.put("message", "只支持JPG/PNG/GIF格式的图片"); |
| 134 | return ResponseEntity.badRequest().body(response); |
| 135 | } |
| 136 | |
| 137 | // 3. 验证文件大小 (前端已验证,后端再次验证) |
| 138 | if (file.getSize() > 10 * 1024 * 1024) { // 10MB |
| 139 | response.put("success", false); |
| 140 | response.put("message", "图片大小不能超过10MB"); |
| 141 | return ResponseEntity.badRequest().body(response); |
| 142 | } |
| 143 | |
| 144 | try { |
| 145 | // 4. 创建上传目录(如果不存在) |
| 146 | File dir = new File(uploadDir); |
| 147 | if (!dir.exists()) { |
| 148 | dir.mkdirs(); |
| 149 | } |
| 150 | System.out.println(dir.getAbsolutePath()); |
| 151 | // 5. 生成唯一文件名 (日期+UUID+后缀) |
| 152 | String originalFilename = file.getOriginalFilename(); |
| 153 | String fileExt = originalFilename.substring(originalFilename.lastIndexOf(".")); |
| 154 | String newFilename = new SimpleDateFormat("yyyyMMdd").format(new Date()) + |
| 155 | "_" + UUID.randomUUID().toString().replace("-", "") + |
| 156 | fileExt.toLowerCase(); |
| 157 | |
| 158 | // 6. 保存文件 |
| 159 | Path path = Paths.get(uploadDir, newFilename); |
| 160 | Files.copy(file.getInputStream(), path); |
| 161 | |
| 162 | // 7. 返回访问URL |
| 163 | String fileUrl = accessPath + newFilename; |
| 164 | |
| 165 | response.put("success", true); |
| 166 | response.put("url", fileUrl); |
| 167 | response.put("message", "头像上传成功"); |
| 168 | |
| 169 | return ResponseEntity.ok(response); |
| 170 | |
| 171 | } catch (IOException e) { |
| 172 | e.printStackTrace(); |
| 173 | response.put("success", false); |
| 174 | response.put("message", "文件上传失败: " + e.getMessage()); |
| 175 | return ResponseEntity.status(500).body(response); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | |
刘嘉昕 | f28ea23 | 2025-04-15 16:55:43 +0800 | [diff] [blame] | 180 | } |