blob: e96df8f66e10f71b47de772561b219b30dd3b383 [file] [log] [blame]
22301115cf6dba22025-03-25 19:06:21 +08001package com.example.myproject.controller;
2
223011381c359102025-06-03 15:19:59 +08003import com.example.myproject.entity.Users;
4import com.example.myproject.repository.UserRepository;
5import com.example.myproject.service.TaskService;
22301115cf6dba22025-03-25 19:06:21 +08006import com.example.myproject.service.UserService;
22301115cf6dba22025-03-25 19:06:21 +08007import org.springframework.beans.factory.annotation.Autowired;
22301115cf6dba22025-03-25 19:06:21 +08008import org.springframework.web.bind.annotation.*;
9
223011381c359102025-06-03 15:19:59 +080010import java.util.Map;
11import java.util.Optional;
22301115cf6dba22025-03-25 19:06:21 +080012
13@RestController
223011381c359102025-06-03 15:19:59 +080014@RequestMapping("/echo/user")
22301115cf6dba22025-03-25 19:06:21 +080015public class UserController {
16
223011381c359102025-06-03 15:19:59 +080017 @Autowired
22301115cf6dba22025-03-25 19:06:21 +080018 private UserService userService;
19
20 @Autowired
223011381c359102025-06-03 15:19:59 +080021 private UserRepository userRepository;
22301115cf6dba22025-03-25 19:06:21 +080022
223011381c359102025-06-03 15:19:59 +080023 // 接口:生成邀请码
24 @PostMapping("/getInviteCode")
25 public Map<String, Object> generateInviteCode(@RequestBody Map<String, Object> request) {
26 Long userId = Long.parseLong(request.get("user_id").toString());
27 return userService.generateInviteCode(userId);
22301115cf6dba22025-03-25 19:06:21 +080028 }
29
223011381c359102025-06-03 15:19:59 +080030 //注册
22301115cf6dba22025-03-25 19:06:21 +080031 @PostMapping("/register")
223011381c359102025-06-03 15:19:59 +080032 public Map<String, Object> register(@RequestBody Map<String, Object> request) {
33 String username = (String) request.get("username");
34 String email = (String) request.get("email");
35 String password = (String) request.get("password");
36 String role = (String) request.get("role");
37 String inviteCode = (String) request.get("inviteCode");
38
39 // 调用服务层的注册方法
40 String resultMessage = userService.registerUser(username, email, password, role, inviteCode);
41
42 // 返回注册结果
43 return Map.of("msg", resultMessage);
44 }
45
46 //登录
47 @PostMapping("/login")
48 public Map<String, Object> login(@RequestBody Map<String, Object> request) {
49 String username = (String) request.get("username");
50 String password = (String) request.get("password");
51
52 // 调用服务层的登录方法
53 String resultMessage = userService.loginUser(username, password);
54
55 // 根据登录结果返回不同的响应
56 if (resultMessage.equals("登录成功")) {
57 // 查询用户信息
58 Optional<Users> user = userRepository.findByUsername(username);
59 if (user.isPresent()) {
60 // 将用户的所有信息作为返回值
61 return Map.of("msg", resultMessage, "user", user.get());
62 } else {
63 return Map.of("msg", "用户信息查询失败");
64 }
22301115cf6dba22025-03-25 19:06:21 +080065 } else {
223011381c359102025-06-03 15:19:59 +080066 return Map.of("msg", resultMessage);
22301115cf6dba22025-03-25 19:06:21 +080067 }
68 }
69
223011381c359102025-06-03 15:19:59 +080070 //修改密码
71 @PostMapping("/password")
72 public Map<String, Object> changePassword(@RequestBody Map<String, Object> request) {
73 Long userId = Long.parseLong(request.get("user_id").toString());
74 String oldPassword = (String) request.get("old_password");
75 String newPassword = (String) request.get("new_password");
76 String confirmPassword = (String) request.get("confirm_password");
22301115cf6dba22025-03-25 19:06:21 +080077
223011381c359102025-06-03 15:19:59 +080078 // 调用服务层的修改密码方法
79 String resultMessage = userService.changePassword(userId, oldPassword, newPassword, confirmPassword);
80
81 // 返回修改结果
82 return Map.of("message", resultMessage, "status", resultMessage.equals("密码修改成功") ? "success" : "error");
22301115cf6dba22025-03-25 19:06:21 +080083 }
84
223011381c359102025-06-03 15:19:59 +080085 // 获取用户个人资料
86 @GetMapping("/{userId}/getProfile")
87 public Map<String, Object> getProfile(@PathVariable("userId") Long userId) {
88 return userService.getProfile(userId);
89 }
90
91 // 修改用户个人资料
92 @PutMapping("/{userId}/editProfile")
93 public Map<String, String> editProfile(
94 @PathVariable("userId") Long userId,
95 @RequestBody Map<String, Object> profileData) {
96
97 // 获取请求体中的修改数据
98 String avatarUrl = (String) profileData.get("avatarUrl");
99 String nickname = (String) profileData.get("nickname");
100 String gender = (String) profileData.get("gender");
101 String description = (String) profileData.get("description");
102 String hobbies = (String) profileData.get("hobbies");
103
104 // 调用服务层方法进行修改
105 boolean updated = userService.editProfile(userId, avatarUrl, nickname, gender, description, hobbies);
106
107 // 返回操作结果消息
108 if (updated) {
109 return Map.of("message", "用户资料更新成功");
22301115cf6dba22025-03-25 19:06:21 +0800110 } else {
223011381c359102025-06-03 15:19:59 +0800111 return Map.of("message", "用户不存在");
22301115cf6dba22025-03-25 19:06:21 +0800112 }
113 }
114
223011381c359102025-06-03 15:19:59 +0800115 // 计算分享率
116 @GetMapping("/{user_id}/calculate-share-rate")
117 public Map<String, Object> calculateShareRate(@PathVariable("user_id") Long userId) {
118 return userService.calculateShareRate(userId);
22301115cf6dba22025-03-25 19:06:21 +0800119 }
120
YelinCuifdf4ed72025-05-26 11:49:36 +0800121
22301115cf6dba22025-03-25 19:06:21 +0800122}