blob: 4a23ea917c4877e19ccc87795b1306673e380a23 [file] [log] [blame]
刘嘉昕f28ea232025-04-15 16:55:43 +08001package com.pt5.pthouduan.controller;
2
3import com.pt5.pthouduan.entity.User;
4import com.pt5.pthouduan.service.UserService;
5import org.springframework.beans.factory.annotation.Autowired;
21301050e4db6a92025-06-08 23:42:43 +08006import org.springframework.beans.factory.annotation.Value;
7import org.springframework.http.ResponseEntity;
刘嘉昕f28ea232025-04-15 16:55:43 +08008import org.springframework.web.bind.annotation.*;
9import org.springframework.stereotype.Controller;
21301050e4db6a92025-06-08 23:42:43 +080010import org.springframework.web.multipart.MultipartFile;
刘嘉昕f28ea232025-04-15 16:55:43 +080011
21301050e4db6a92025-06-08 23:42:43 +080012import java.io.File;
13import java.io.IOException;
14import java.nio.file.Files;
15import java.nio.file.Path;
16import java.nio.file.Paths;
17import java.nio.file.StandardCopyOption;
18import java.text.SimpleDateFormat;
19import java.util.*;
Sure2338188c5d2025-05-28 11:43:06 +080020
刘嘉昕f28ea232025-04-15 16:55:43 +080021/**
22 * <p>
23 * 前端控制器
24 * </p>
25 *
26 * @author ljx
27 * @since 2025-04-14
28 */
Sure2338188c5d2025-05-28 11:43:06 +080029@RestController
刘嘉昕f28ea232025-04-15 16:55:43 +080030@RequestMapping("/user")
31public class UserController {
32 @Autowired
33 private UserService userService;
21301050e4db6a92025-06-08 23:42:43 +080034 private String uploadDir="../var/www/avatars/"; // 配置文件上传目录,例如: /var/www/avatars/
35
36 private String accessPath="../avatars/";
刘嘉昕f28ea232025-04-15 16:55:43 +080037
Sure2338188c5d2025-05-28 11:43:06 +080038 @PostMapping("/register")
21301050c519f712025-06-04 17:03:04 +080039 public Map<String, Object> register(@RequestBody User user,@RequestParam String code,@RequestParam String emailcode) {//code是邀请码,emailcode是验证码
40 return userService.register(user,code,emailcode);
刘嘉昕f28ea232025-04-15 16:55:43 +080041 }
42
Sure2338188c5d2025-05-28 11:43:06 +080043 @PostMapping("/login")
44 public Map<String, Object> login(@RequestParam String username,
45 @RequestParam String password) {
46 return userService.login(username, password);
47 }
刘嘉昕f28ea232025-04-15 16:55:43 +080048
Sure2338188c5d2025-05-28 11:43:06 +080049 @PostMapping("/calgrade")
50 public Map<String, Object> calgrade(@RequestParam String username) {
51 return userService.CalGrade(username);
52 }
刘嘉昕f28ea232025-04-15 16:55:43 +080053
Sure2338188c5d2025-05-28 11:43:06 +080054 @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")
21301050e4db6a92025-06-08 23:42:43 +080061 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;
Sure2338188c5d2025-05-28 11:43:06 +080067 }
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
21301050c519f712025-06-04 17:03:04 +080076 @PostMapping("/sendCode")
77 public Map<String, Object> sendCode(@RequestParam String email) {
78 return userService.sendCode(email);
Sure2338188c5d2025-05-28 11:43:06 +080079 }
21301050c519f712025-06-04 17:03:04 +080080
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
21301050e4db6a92025-06-08 23:42:43 +080096 @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
刘嘉昕f28ea232025-04-15 16:55:43 +0800180}