blob: b6aaab74f3e0c5f8485a778fcebe3fd6d84a008a [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.DynamicService;
//// import com.example.myproject.service.TaskService;
//// import com.example.myproject.service.UserService;
//// import org.springframework.beans.factory.annotation.Autowired;
//// import org.springframework.http.ResponseEntity;
//// import org.springframework.web.bind.annotation.*;
//// import org.springframework.web.multipart.MultipartFile;
//
//// import javax.servlet.http.HttpServletRequest;
//// import java.util.Map;
//// import java.util.Optional;
//
//// import java.util.List;
//// import java.util.ArrayList;
//
//
//// @RestController
//// @RequestMapping("/echo/user")
//// public class UserController {
//
//// @Autowired
//// private UserService userService;
//
//// @Autowired
//// private UserRepository userRepository;
//
//// @Autowired
//// private DynamicService dynamicService;
//
//// // 接口:生成邀请码
//// @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 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, 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);
//// }
//
//// // 获取用户所有好友的基本信息
//// @GetMapping("/{userId}/friends")
//// public List<Map<String, Object>> getUserFriends(@PathVariable("userId") Long userId) {
//// List<Long> friendIds = dynamicService.getAllFriendIds(userId); // 注意这里用的是实例对象
//// List<Map<String, Object>> friends = new ArrayList<>();
//
//// for (Long friendId : friendIds) {
//// Optional<Users> userOpt = userRepository.findById(friendId);
//// if (userOpt.isPresent()) {
//// Users user = userOpt.get();
//// Map<String, Object> friendInfo = Map.of(
//// "id", user.getUserId(),
//// "avatar", user.getAvatarUrl() != null ? user.getAvatarUrl() : "https://example.com/default-avatar.jpg",
//// "nickname", user.getUsername() != null ? user.getUsername() : "未知用户",
//// "email", user.getEmail() != null ? user.getEmail() : "未填写"
//// );
//// friends.add(friendInfo);
//// }
//// }
//
//// return friends;
//// }
//
//// @PostMapping("/{userId}/uploadAvatar")
//// public Map<String, Object> uploadAvatar(
//// @PathVariable Long userId,
//// @RequestParam("file") MultipartFile file) {
//// return userService.uploadUserAvatar(userId, file);
//// }
//
//
//
//// }
//
//package com.example.myproject.controller;
//
//import com.example.myproject.entity.FriendRelation;
//import com.example.myproject.entity.Users;
//import com.example.myproject.repository.FriendRelationRepository;
//import com.example.myproject.repository.UserRepository;
//import com.example.myproject.service.DynamicService;
//import com.example.myproject.service.UserService;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.web.bind.annotation.*;
//import org.springframework.web.multipart.MultipartFile;
//
//import java.util.*;
//
//
//@RestController
//@RequestMapping("/echo/user")
//public class UserController {
//
// @Autowired
// private UserService userService;
//
// @Autowired
// private UserRepository userRepository;
//
// @Autowired
// private DynamicService dynamicService;
//
// @Autowired
// private FriendRelationRepository friendRelationRepository;
//
//
// @PostMapping ("/addFriend")
// public Map<String, Object> addFriend(@RequestBody Map<String, Object> request) {
// //邮箱查用户
// Optional<Users> userOptional = userRepository.findByEmail(request.get("email").toString());
// // 如果用户不存在,返回null或者可以抛出异常
// if (userOptional.isEmpty()) {
// return Map.of("msg", "没有找到该用户"); // 可以返回 null 或者根据需要返回错误信息
// }
// Long userId = Long.parseLong(request.get("userid").toString()); //id查用户
// Map<String, Object> users = userService.getProfile(userId);
// // 如果用户不存在,返回null或者可以抛出异常
// if (users.isEmpty()) {
// return Map.of("msg", "当前登录账号异常"); // 可以返回 null 或者根据需要返回错误信息
// }
//
// FriendRelation newFriendRelation = new FriendRelation();
// newFriendRelation.setUserId(userId);
// newFriendRelation.setCreateTime(new Date());
// newFriendRelation.setFriendId(userOptional.get().getUserId());
//
// FriendRelation newFriendRelations = new FriendRelation();
// newFriendRelations.setCreateTime(new Date());
// newFriendRelations.setUserId(userOptional.get().getUserId());
// newFriendRelations.setFriendId(userId);
//
// friendRelationRepository.save(newFriendRelation);
// friendRelationRepository.save(newFriendRelations);
//
// return Map.of("msg", "添加成功");
// }
//
// // 接口:生成邀请码
// @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 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, 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);
// }
//
// // 获取用户所有好友的基本信息
// @GetMapping("/{userId}/friends")
// public List<Map<String, Object>> getUserFriends(@PathVariable("userId") Long userId) {
// List<Long> friendIds = dynamicService.getAllFriendIds(userId); // 注意这里用的是实例对象
// List<Map<String, Object>> friends = new ArrayList<>();
//
// for (Long friendId : friendIds) {
// Optional<Users> userOpt = userRepository.findById(friendId);
// if (userOpt.isPresent()) {
// Users user = userOpt.get();
// Map<String, Object> friendInfo = Map.of(
// "id", user.getUserId(),
// "avatar", user.getAvatarUrl() != null ? user.getAvatarUrl() : "https://example.com/default-avatar.jpg",
// "nickname", user.getUsername() != null ? user.getUsername() : "未知用户",
// "email", user.getEmail() != null ? user.getEmail() : "未填写"
// );
// friends.add(friendInfo);
// }
// }
//
// return friends;
// }
//
// @PostMapping("/{userId}/uploadAvatar")
// public Map<String, Object> uploadAvatar(
// @PathVariable Long userId,
// @RequestParam("file") MultipartFile file) {
// return userService.uploadUserAvatar(userId, file);
// }
//
//
//
//}
//
// package com.example.myproject.controller;
// import com.example.myproject.entity.Users;
// import com.example.myproject.repository.UserRepository;
// import com.example.myproject.service.DynamicService;
// import com.example.myproject.service.TaskService;
// import com.example.myproject.service.UserService;
// import org.springframework.beans.factory.annotation.Autowired;
// import org.springframework.http.ResponseEntity;
// import org.springframework.web.bind.annotation.*;
// import org.springframework.web.multipart.MultipartFile;
// import javax.servlet.http.HttpServletRequest;
// import java.util.Map;
// import java.util.Optional;
// import java.util.List;
// import java.util.ArrayList;
// @RestController
// @RequestMapping("/echo/user")
// public class UserController {
// @Autowired
// private UserService userService;
// @Autowired
// private UserRepository userRepository;
// @Autowired
// private DynamicService dynamicService;
// // 接口:生成邀请码
// @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 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, 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);
// }
// // 获取用户所有好友的基本信息
// @GetMapping("/{userId}/friends")
// public List<Map<String, Object>> getUserFriends(@PathVariable("userId") Long userId) {
// List<Long> friendIds = dynamicService.getAllFriendIds(userId); // 注意这里用的是实例对象
// List<Map<String, Object>> friends = new ArrayList<>();
// for (Long friendId : friendIds) {
// Optional<Users> userOpt = userRepository.findById(friendId);
// if (userOpt.isPresent()) {
// Users user = userOpt.get();
// Map<String, Object> friendInfo = Map.of(
// "id", user.getUserId(),
// "avatar", user.getAvatarUrl() != null ? user.getAvatarUrl() : "https://example.com/default-avatar.jpg",
// "nickname", user.getUsername() != null ? user.getUsername() : "未知用户",
// "email", user.getEmail() != null ? user.getEmail() : "未填写"
// );
// friends.add(friendInfo);
// }
// }
// return friends;
// }
// @PostMapping("/{userId}/uploadAvatar")
// public Map<String, Object> uploadAvatar(
// @PathVariable Long userId,
// @RequestParam("file") MultipartFile file) {
// return userService.uploadUserAvatar(userId, file);
// }
// }
package com.example.myproject.controller;
import com.example.myproject.entity.FriendRelation;
import com.example.myproject.entity.Users;
import com.example.myproject.repository.FriendRelationRepository;
import com.example.myproject.repository.UserRepository;
import com.example.myproject.service.DynamicService;
import com.example.myproject.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.*;
@RestController
@RequestMapping("/echo/user")
public class UserController {
@Autowired
private UserService userService;
@Autowired
private UserRepository userRepository;
@Autowired
private DynamicService dynamicService;
@Autowired
private FriendRelationRepository friendRelationRepository;
@PostMapping ("/addFriend")
public Map<String, Object> addFriend(@RequestBody Map<String, Object> request) {
//邮箱查用户
Optional<Users> userOptional = userRepository.findByEmail(request.get("email").toString());
// 如果用户不存在,返回null或者可以抛出异常
if (userOptional.isEmpty()) {
return Map.of("msg", "没有找到该用户"); // 可以返回 null 或者根据需要返回错误信息
}
Long userId = Long.parseLong(request.get("userid").toString()); //id查用户
Map<String, Object> users = userService.getProfile(userId);
// 如果用户不存在,返回null或者可以抛出异常
if (users.isEmpty()) {
return Map.of("msg", "当前登录账号异常"); // 可以返回 null 或者根据需要返回错误信息
}
FriendRelation newFriendRelation = new FriendRelation();
newFriendRelation.setUserId(userId);
newFriendRelation.setCreateTime(new Date());
newFriendRelation.setFriendId(userOptional.get().getUserId());
FriendRelation newFriendRelations = new FriendRelation();
newFriendRelations.setCreateTime(new Date());
newFriendRelations.setUserId(userOptional.get().getUserId());
newFriendRelations.setFriendId(userId);
friendRelationRepository.save(newFriendRelation);
friendRelationRepository.save(newFriendRelations);
return Map.of("msg", "添加成功");
}
// 接口:生成邀请码
@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 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, 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);
}
// 获取用户所有好友的基本信息
@GetMapping("/{userId}/friends")
public List<Map<String, Object>> getUserFriends(@PathVariable("userId") Long userId) {
List<Long> friendIds = dynamicService.getAllFriendIds(userId); // 注意这里用的是实例对象
List<Map<String, Object>> friends = new ArrayList<>();
for (Long friendId : friendIds) {
Optional<Users> userOpt = userRepository.findById(friendId);
if (userOpt.isPresent()) {
Users user = userOpt.get();
Map<String, Object> friendInfo = Map.of(
"id", user.getUserId(),
"avatar", user.getAvatarUrl() != null ? user.getAvatarUrl() : "https://example.com/default-avatar.jpg",
"nickname", user.getUsername() != null ? user.getUsername() : "未知用户",
"email", user.getEmail() != null ? user.getEmail() : "未填写"
);
friends.add(friendInfo);
}
}
return friends;
}
@PostMapping("/{userId}/uploadAvatar")
public Map<String, Object> uploadAvatar(
@PathVariable Long userId,
@RequestParam("file") MultipartFile file) {
return userService.uploadUserAvatar(userId, file);
}
@GetMapping("/users/{userId}/share-rate")
public String checkUserShareRate(@PathVariable Long userId) {
return userService.checkUserShareRate(userId);
}
// 用户充值接口
@PostMapping("/recharge")
public ResponseEntity<?> recharge(@RequestParam Long userId, @RequestParam Integer amount) {
String result = userService.recharge(userId, amount);
if (result.startsWith("充值成功")) {
return ResponseEntity.ok(Map.of("status", "success", "message", result));
} else {
return ResponseEntity.badRequest().body(Map.of("status", "failure", "message", result));
}
}
}