69 行
2.4 KiB
Java
69 行
2.4 KiB
Java
|
|
package com.xuqm.demo.controller;
|
||
|
|
|
||
|
|
import com.xuqm.common.exception.BusinessException;
|
||
|
|
import com.xuqm.common.model.ApiResponse;
|
||
|
|
import com.xuqm.demo.service.DemoUserService;
|
||
|
|
import org.springframework.security.core.Authentication;
|
||
|
|
import org.springframework.web.bind.annotation.*;
|
||
|
|
|
||
|
|
import java.util.List;
|
||
|
|
|
||
|
|
@RestController
|
||
|
|
@RequestMapping("/api/demo")
|
||
|
|
public class DemoUserController {
|
||
|
|
|
||
|
|
private final DemoUserService userService;
|
||
|
|
|
||
|
|
public DemoUserController(DemoUserService userService) {
|
||
|
|
this.userService = userService;
|
||
|
|
}
|
||
|
|
|
||
|
|
@GetMapping("/user/profile")
|
||
|
|
public ApiResponse<DemoUserService.UserProfile> getProfile(
|
||
|
|
@RequestParam String appId,
|
||
|
|
Authentication auth) {
|
||
|
|
String userId = resolveUserId(auth);
|
||
|
|
return ApiResponse.success(userService.getProfile(appId, userId));
|
||
|
|
}
|
||
|
|
|
||
|
|
@PutMapping("/user/profile")
|
||
|
|
public ApiResponse<DemoUserService.UserProfile> updateProfile(
|
||
|
|
@RequestParam String appId,
|
||
|
|
Authentication auth,
|
||
|
|
@RequestBody UpdateProfileRequest body) {
|
||
|
|
String userId = resolveUserId(auth);
|
||
|
|
return ApiResponse.success(
|
||
|
|
userService.updateProfile(appId, userId, body.nickname(), body.avatar(), body.gender()));
|
||
|
|
}
|
||
|
|
|
||
|
|
@PostMapping("/user/reset-password")
|
||
|
|
public ApiResponse<Void> resetPassword(
|
||
|
|
@RequestParam String appId,
|
||
|
|
Authentication auth,
|
||
|
|
@RequestBody ResetPasswordRequest body) {
|
||
|
|
String userId = resolveUserId(auth);
|
||
|
|
if (body.oldPassword() == null || body.newPassword() == null) {
|
||
|
|
return ApiResponse.badRequest("oldPassword and newPassword are required");
|
||
|
|
}
|
||
|
|
userService.resetPassword(appId, userId, body.oldPassword(), body.newPassword());
|
||
|
|
return ApiResponse.ok();
|
||
|
|
}
|
||
|
|
|
||
|
|
@GetMapping("/users/search")
|
||
|
|
public ApiResponse<List<DemoUserService.UserProfile>> searchUsers(
|
||
|
|
@RequestParam String appId,
|
||
|
|
@RequestParam String keyword) {
|
||
|
|
return ApiResponse.success(userService.searchUsers(appId, keyword));
|
||
|
|
}
|
||
|
|
|
||
|
|
private String resolveUserId(Authentication auth) {
|
||
|
|
if (auth == null || !auth.isAuthenticated()) {
|
||
|
|
throw new BusinessException(401, "Not authenticated");
|
||
|
|
}
|
||
|
|
return (String) auth.getPrincipal();
|
||
|
|
}
|
||
|
|
|
||
|
|
public record UpdateProfileRequest(String nickname, String avatar, String gender) {}
|
||
|
|
public record ResetPasswordRequest(String oldPassword, String newPassword) {}
|
||
|
|
}
|