blob: 11f77ce50b9f298be0df9bc6d73f9bee4c296d41 [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;
21301050f5f827d2025-06-09 15:09:33 +080034
35 @Value("${torrent.user-image-dir}")
36 private String uploadDir;
37
38 //private String uploadDir="../var/www/avatars/"; // 配置文件上传目录,例如: /var/www/avatars/
21301050e4db6a92025-06-08 23:42:43 +080039
40 private String accessPath="../avatars/";
刘嘉昕f28ea232025-04-15 16:55:43 +080041
Sure2338188c5d2025-05-28 11:43:06 +080042 @PostMapping("/register")
21301050c519f712025-06-04 17:03:04 +080043 public Map<String, Object> register(@RequestBody User user,@RequestParam String code,@RequestParam String emailcode) {//code是邀请码,emailcode是验证码
44 return userService.register(user,code,emailcode);
刘嘉昕f28ea232025-04-15 16:55:43 +080045 }
46
Sure2338188c5d2025-05-28 11:43:06 +080047 @PostMapping("/login")
48 public Map<String, Object> login(@RequestParam String username,
49 @RequestParam String password) {
50 return userService.login(username, password);
51 }
刘嘉昕f28ea232025-04-15 16:55:43 +080052
Sure2338188c5d2025-05-28 11:43:06 +080053 @PostMapping("/calgrade")
54 public Map<String, Object> calgrade(@RequestParam String username) {
55 return userService.CalGrade(username);
56 }
刘嘉昕f28ea232025-04-15 16:55:43 +080057
Sure2338188c5d2025-05-28 11:43:06 +080058 @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")
21301050e4db6a92025-06-08 23:42:43 +080065 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;
Sure2338188c5d2025-05-28 11:43:06 +080071 }
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
21301050c519f712025-06-04 17:03:04 +080080 @PostMapping("/sendCode")
81 public Map<String, Object> sendCode(@RequestParam String email) {
82 return userService.sendCode(email);
Sure2338188c5d2025-05-28 11:43:06 +080083 }
21301050c519f712025-06-04 17:03:04 +080084
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
21301050e4db6a92025-06-08 23:42:43 +0800100 @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
21301050f5f827d2025-06-09 15:09:33 +0800120// @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// }
21301050e4db6a92025-06-08 23:42:43 +0800147
21301050f5f827d2025-06-09 15:09:33 +0800148// 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 //}
21301050e4db6a92025-06-08 23:42:43 +0800182
183
刘嘉昕f28ea232025-04-15 16:55:43 +0800184}