2026-04-14 13:47:38 +08:00
|
|
|
|
package com.label.controller;
|
2026-04-09 15:48:07 +08:00
|
|
|
|
|
2026-04-13 17:13:29 +08:00
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
|
|
import org.apache.shiro.authz.annotation.RequiresRoles;
|
|
|
|
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
|
|
|
|
import org.springframework.web.bind.annotation.PathVariable;
|
|
|
|
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
|
|
|
|
import org.springframework.web.bind.annotation.PutMapping;
|
|
|
|
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
|
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
|
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
|
2026-04-09 15:48:07 +08:00
|
|
|
|
import com.label.common.result.PageResult;
|
|
|
|
|
|
import com.label.common.result.Result;
|
|
|
|
|
|
import com.label.common.shiro.TokenPrincipal;
|
2026-04-14 13:39:24 +08:00
|
|
|
|
import com.label.entity.SysUser;
|
2026-04-14 13:45:15 +08:00
|
|
|
|
import com.label.service.UserService;
|
2026-04-13 17:13:29 +08:00
|
|
|
|
|
2026-04-12 00:15:59 +08:00
|
|
|
|
import io.swagger.v3.oas.annotations.Operation;
|
|
|
|
|
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
2026-04-09 15:48:07 +08:00
|
|
|
|
import jakarta.servlet.http.HttpServletRequest;
|
|
|
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2026-04-14 13:31:50 +08:00
|
|
|
|
* 用户管理接口(5 个端点,全部 ADMIN 权限)。
|
|
|
|
|
|
*/
|
|
|
|
|
|
@Tag(name = "用户管理", description = "管理员维护公司用户")
|
2026-04-09 15:48:07 +08:00
|
|
|
|
@RestController
|
|
|
|
|
|
@RequestMapping("/api/users")
|
|
|
|
|
|
@RequiredArgsConstructor
|
|
|
|
|
|
public class UserController {
|
|
|
|
|
|
|
|
|
|
|
|
private final UserService userService;
|
|
|
|
|
|
|
2026-04-14 13:31:50 +08:00
|
|
|
|
/** GET /api/users — 分页查询用户列表 */
|
|
|
|
|
|
@Operation(summary = "分页查询用户列表")
|
2026-04-09 15:48:07 +08:00
|
|
|
|
@GetMapping
|
|
|
|
|
|
@RequiresRoles("ADMIN")
|
|
|
|
|
|
public Result<PageResult<SysUser>> listUsers(
|
|
|
|
|
|
@RequestParam(defaultValue = "1") int page,
|
|
|
|
|
|
@RequestParam(defaultValue = "20") int pageSize,
|
|
|
|
|
|
HttpServletRequest request) {
|
|
|
|
|
|
return Result.success(userService.listUsers(page, pageSize, principal(request)));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-14 13:31:50 +08:00
|
|
|
|
/** POST /api/users — 创建用户 */
|
|
|
|
|
|
@Operation(summary = "创建用户")
|
2026-04-09 15:48:07 +08:00
|
|
|
|
@PostMapping
|
|
|
|
|
|
@RequiresRoles("ADMIN")
|
|
|
|
|
|
public Result<SysUser> createUser(@RequestBody Map<String, String> body,
|
2026-04-13 17:13:29 +08:00
|
|
|
|
HttpServletRequest request) {
|
2026-04-09 15:48:07 +08:00
|
|
|
|
return Result.success(userService.createUser(
|
|
|
|
|
|
body.get("username"),
|
|
|
|
|
|
body.get("password"),
|
|
|
|
|
|
body.get("realName"),
|
|
|
|
|
|
body.get("role"),
|
|
|
|
|
|
principal(request)));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-14 13:31:50 +08:00
|
|
|
|
/** PUT /api/users/{id} — 更新用户基本信息 */
|
|
|
|
|
|
@Operation(summary = "更新用户基本信息")
|
2026-04-09 15:48:07 +08:00
|
|
|
|
@PutMapping("/{id}")
|
|
|
|
|
|
@RequiresRoles("ADMIN")
|
|
|
|
|
|
public Result<SysUser> updateUser(@PathVariable Long id,
|
2026-04-13 17:13:29 +08:00
|
|
|
|
@RequestBody Map<String, String> body,
|
|
|
|
|
|
HttpServletRequest request) {
|
2026-04-09 15:48:07 +08:00
|
|
|
|
return Result.success(userService.updateUser(
|
|
|
|
|
|
id,
|
|
|
|
|
|
body.get("realName"),
|
|
|
|
|
|
body.get("password"),
|
|
|
|
|
|
principal(request)));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-14 13:31:50 +08:00
|
|
|
|
/** PUT /api/users/{id}/status — 变更用户状态 */
|
|
|
|
|
|
@Operation(summary = "变更用户状态", description = "status:ACTIVE、DISABLED")
|
2026-04-09 15:48:07 +08:00
|
|
|
|
@PutMapping("/{id}/status")
|
|
|
|
|
|
@RequiresRoles("ADMIN")
|
|
|
|
|
|
public Result<Void> updateStatus(@PathVariable Long id,
|
2026-04-13 17:13:29 +08:00
|
|
|
|
@RequestBody Map<String, String> body,
|
|
|
|
|
|
HttpServletRequest request) {
|
2026-04-09 15:48:07 +08:00
|
|
|
|
userService.updateStatus(id, body.get("status"), principal(request));
|
|
|
|
|
|
return Result.success(null);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-14 13:31:50 +08:00
|
|
|
|
/** PUT /api/users/{id}/role — 变更用户角色 */
|
|
|
|
|
|
@Operation(summary = "变更用户角色", description = "role:ADMIN、UPLOADER、VIEWER")
|
2026-04-09 15:48:07 +08:00
|
|
|
|
@PutMapping("/{id}/role")
|
|
|
|
|
|
@RequiresRoles("ADMIN")
|
|
|
|
|
|
public Result<Void> updateRole(@PathVariable Long id,
|
2026-04-13 17:13:29 +08:00
|
|
|
|
@RequestBody Map<String, String> body,
|
|
|
|
|
|
HttpServletRequest request) {
|
2026-04-09 15:48:07 +08:00
|
|
|
|
userService.updateRole(id, body.get("role"), principal(request));
|
|
|
|
|
|
return Result.success(null);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private TokenPrincipal principal(HttpServletRequest request) {
|
|
|
|
|
|
return (TokenPrincipal) request.getAttribute("__token_principal__");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|