blob: e96df8f66e10f71b47de772561b219b30dd3b383 [file] [log] [blame]
package com.example.myproject.controller;
import com.example.myproject.entity.Users;
import com.example.myproject.repository.UserRepository;
import com.example.myproject.service.TaskService;
import com.example.myproject.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
import java.util.Optional;
@RestController
@RequestMapping("/echo/user")
public class UserController {
@Autowired
private UserService userService;
@Autowired
private UserRepository userRepository;
// 接口:生成邀请码
@PostMapping("/getInviteCode")
public Map<String, Object> generateInviteCode(@RequestBody Map<String, Object> request) {
Long userId = Long.parseLong(request.get("user_id").toString());
return userService.generateInviteCode(userId);
}
//注册
@PostMapping("/register")
public Map<String, Object> register(@RequestBody Map<String, Object> request) {
String username = (String) request.get("username");
String email = (String) request.get("email");
String password = (String) request.get("password");
String role = (String) request.get("role");
String inviteCode = (String) request.get("inviteCode");
// 调用服务层的注册方法
String resultMessage = userService.registerUser(username, email, password, role, inviteCode);
// 返回注册结果
return Map.of("msg", resultMessage);
}
//登录
@PostMapping("/login")
public Map<String, Object> login(@RequestBody Map<String, Object> request) {
String username = (String) request.get("username");
String password = (String) request.get("password");
// 调用服务层的登录方法
String resultMessage = userService.loginUser(username, password);
// 根据登录结果返回不同的响应
if (resultMessage.equals("登录成功")) {
// 查询用户信息
Optional<Users> user = userRepository.findByUsername(username);
if (user.isPresent()) {
// 将用户的所有信息作为返回值
return Map.of("msg", resultMessage, "user", user.get());
} else {
return Map.of("msg", "用户信息查询失败");
}
} else {
return Map.of("msg", resultMessage);
}
}
//修改密码
@PostMapping("/password")
public Map<String, Object> changePassword(@RequestBody Map<String, Object> request) {
Long userId = Long.parseLong(request.get("user_id").toString());
String oldPassword = (String) request.get("old_password");
String newPassword = (String) request.get("new_password");
String confirmPassword = (String) request.get("confirm_password");
// 调用服务层的修改密码方法
String resultMessage = userService.changePassword(userId, oldPassword, newPassword, confirmPassword);
// 返回修改结果
return Map.of("message", resultMessage, "status", resultMessage.equals("密码修改成功") ? "success" : "error");
}
// 获取用户个人资料
@GetMapping("/{userId}/getProfile")
public Map<String, Object> getProfile(@PathVariable("userId") Long userId) {
return userService.getProfile(userId);
}
// 修改用户个人资料
@PutMapping("/{userId}/editProfile")
public Map<String, String> editProfile(
@PathVariable("userId") Long userId,
@RequestBody Map<String, Object> profileData) {
// 获取请求体中的修改数据
String avatarUrl = (String) profileData.get("avatarUrl");
String nickname = (String) profileData.get("nickname");
String gender = (String) profileData.get("gender");
String description = (String) profileData.get("description");
String hobbies = (String) profileData.get("hobbies");
// 调用服务层方法进行修改
boolean updated = userService.editProfile(userId, avatarUrl, nickname, gender, description, hobbies);
// 返回操作结果消息
if (updated) {
return Map.of("message", "用户资料更新成功");
} else {
return Map.of("message", "用户不存在");
}
}
// 计算分享率
@GetMapping("/{user_id}/calculate-share-rate")
public Map<String, Object> calculateShareRate(@PathVariable("user_id") Long userId) {
return userService.calculateShareRate(userId);
}
}