diff --git a/src/main/java/cn/zyp/stusystem/controller/RoleController.java b/src/main/java/cn/zyp/stusystem/controller/RoleController.java new file mode 100644 index 0000000..1847111 --- /dev/null +++ b/src/main/java/cn/zyp/stusystem/controller/RoleController.java @@ -0,0 +1,133 @@ +package cn.zyp.stusystem.controller; + +import cn.zyp.stusystem.common.Result; +import cn.zyp.stusystem.dto.SaveRolePermissionsDTO; +import cn.zyp.stusystem.entity.Role; +import cn.zyp.stusystem.service.RolePermissionService; +import cn.zyp.stusystem.service.RoleService; +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +@RestController +@RequestMapping("/api/role") +public class RoleController { + + @Autowired + private RoleService roleService; + + @Autowired + private RolePermissionService rolePermissionService; + + /** + * 获取角色列表(分页查询) + */ + @GetMapping + public Result> getRoleList( + @RequestParam(defaultValue = "1") Integer current, + @RequestParam(defaultValue = "10") Integer size, + @RequestParam(required = false) String roleName) { + + Page page = new Page<>(current, size); + LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); + + if (roleName != null && !roleName.trim().isEmpty()) { + wrapper.like(Role::getRoleName, roleName); + } + + wrapper.orderByDesc(Role::getCreatedAt); + + Page rolePage = roleService.page(page, wrapper); + + Map result = new HashMap<>(); + result.put("list", rolePage.getRecords()); + result.put("total", rolePage.getTotal()); + + return Result.success(result); + } + + /** + * 获取角色详情 + */ + @GetMapping("/{roleId}") + public Result getRoleById(@PathVariable Long roleId) { + Role role = roleService.getById(roleId); + if (role == null) { + return Result.error(404, "角色不存在"); + } + return Result.success(role); + } + + /** + * 创建角色 + */ + @PostMapping + public Result createRole(@RequestBody Role role) { + if (role.getStatus() == null) { + role.setStatus(1); + } + boolean success = roleService.save(role); + if (success) { + // 返回创建的角色对象(包含ID),这样前端才能获取到角色ID来保存权限 + return Result.success(role); + } else { + return Result.error("创建失败"); + } + } + + /** + * 更新角色 + */ + @PutMapping("/{roleId}") + public Result updateRole(@PathVariable Long roleId, @RequestBody Role role) { + role.setId(roleId); + boolean success = roleService.updateById(role); + return success ? Result.success(true) : Result.error("更新失败"); + } + + /** + * 删除角色 + */ + @DeleteMapping("/{roleId}") + public Result deleteRole(@PathVariable Long roleId) { + Role role = roleService.getById(roleId); + if (role == null) { + return Result.error(404, "角色不存在"); + } + boolean success = roleService.removeById(roleId); + return success ? Result.success(true) : Result.error("删除失败"); + } + + /** + * 获取角色的权限列表 + */ + @GetMapping("/{roleId}/permissions") + public Result> getRolePermissions(@PathVariable Long roleId) { + List permissionCodes = rolePermissionService.getPermissionCodesByRoleId(roleId); + return Result.success(permissionCodes); + } + + /** + * 保存角色的权限配置 + */ + @PostMapping("/{roleId}/permissions") + public Result saveRolePermissions( + @PathVariable Long roleId, + @RequestBody SaveRolePermissionsDTO dto) { + System.out.println("收到权限保存请求: roleId=" + roleId + ", permissions=" + dto.getPermissions()); + try { + rolePermissionService.saveRolePermissions(roleId, dto.getPermissions()); + System.out.println("权限保存成功"); + return Result.success(true); + } catch (Exception e) { + System.out.println("权限保存失败: " + e.getMessage()); + e.printStackTrace(); + return Result.error("保存失败: " + e.getMessage()); + } + } +} \ No newline at end of file diff --git a/src/main/java/cn/zyp/stusystem/controller/UserController.java b/src/main/java/cn/zyp/stusystem/controller/UserController.java new file mode 100644 index 0000000..6eef0bf --- /dev/null +++ b/src/main/java/cn/zyp/stusystem/controller/UserController.java @@ -0,0 +1,52 @@ +package cn.zyp.stusystem.controller; + +import cn.dev33.satoken.annotation.SaCheckLogin; +import cn.dev33.satoken.annotation.SaCheckPermission; +import cn.dev33.satoken.stp.StpUtil; +import cn.zyp.stusystem.common.Result; +import cn.zyp.stusystem.dto.UserSimpleDTO; +import cn.zyp.stusystem.entity.User; +import cn.zyp.stusystem.service.UserService; +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +@RestController +@RequestMapping("/api/user") +public class UserController { + + @Autowired + private UserService userService; + + @SaCheckLogin + @SaCheckPermission("class:edit") // 或者更宽松的权限 + @GetMapping("/teachers") + public Result> listTeachers() { + List teachers = userService.list( + new LambdaQueryWrapper() + .in(User::getRole, Arrays.asList("head_teacher", "teacher")) + .eq(User::getStatus, 1) + .select(User::getId, User::getName) + ).stream() + .map(u -> new UserSimpleDTO(u.getId(), u.getName())) + .collect(Collectors.toList()); + return Result.success(teachers); + } + + /** + * 获取当前用户的权限列表 + */ + @GetMapping("/permissions") + public Result> getUserPermissions() { + // 从Token中获取当前用户ID + Long userId = StpUtil.getLoginIdAsLong(); + List permissions = userService.getUserPermissions(userId); + return Result.success(permissions); + } +} \ No newline at end of file diff --git a/src/main/java/cn/zyp/stusystem/dto/UserSimpleDTO.java b/src/main/java/cn/zyp/stusystem/dto/UserSimpleDTO.java new file mode 100644 index 0000000..4679ab1 --- /dev/null +++ b/src/main/java/cn/zyp/stusystem/dto/UserSimpleDTO.java @@ -0,0 +1,13 @@ +package cn.zyp.stusystem.dto; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +public class UserSimpleDTO { + private Long id; + private String name; +} \ No newline at end of file diff --git a/target/classes/cn/zyp/stusystem/controller/PermissionController.class b/target/classes/cn/zyp/stusystem/controller/PermissionController.class new file mode 100644 index 0000000..b0c044b Binary files /dev/null and b/target/classes/cn/zyp/stusystem/controller/PermissionController.class differ diff --git a/target/classes/cn/zyp/stusystem/controller/RoleController.class b/target/classes/cn/zyp/stusystem/controller/RoleController.class new file mode 100644 index 0000000..ff7f6fc Binary files /dev/null and b/target/classes/cn/zyp/stusystem/controller/RoleController.class differ diff --git a/target/classes/cn/zyp/stusystem/controller/RolePermissionController.class b/target/classes/cn/zyp/stusystem/controller/RolePermissionController.class new file mode 100644 index 0000000..b020f39 Binary files /dev/null and b/target/classes/cn/zyp/stusystem/controller/RolePermissionController.class differ diff --git a/target/classes/cn/zyp/stusystem/controller/UserController.class b/target/classes/cn/zyp/stusystem/controller/UserController.class new file mode 100644 index 0000000..30e9b01 Binary files /dev/null and b/target/classes/cn/zyp/stusystem/controller/UserController.class differ diff --git a/target/classes/cn/zyp/stusystem/dto/SaveRolePermissionsDTO.class b/target/classes/cn/zyp/stusystem/dto/SaveRolePermissionsDTO.class new file mode 100644 index 0000000..320131f Binary files /dev/null and b/target/classes/cn/zyp/stusystem/dto/SaveRolePermissionsDTO.class differ diff --git a/target/classes/cn/zyp/stusystem/dto/UserSimpleDTO.class b/target/classes/cn/zyp/stusystem/dto/UserSimpleDTO.class new file mode 100644 index 0000000..878a8c7 Binary files /dev/null and b/target/classes/cn/zyp/stusystem/dto/UserSimpleDTO.class differ diff --git a/target/classes/cn/zyp/stusystem/entity/Permission.class b/target/classes/cn/zyp/stusystem/entity/Permission.class new file mode 100644 index 0000000..f5fc40b Binary files /dev/null and b/target/classes/cn/zyp/stusystem/entity/Permission.class differ diff --git a/target/classes/cn/zyp/stusystem/entity/Role.class b/target/classes/cn/zyp/stusystem/entity/Role.class new file mode 100644 index 0000000..30b034f Binary files /dev/null and b/target/classes/cn/zyp/stusystem/entity/Role.class differ diff --git a/target/classes/cn/zyp/stusystem/entity/RolePermission.class b/target/classes/cn/zyp/stusystem/entity/RolePermission.class new file mode 100644 index 0000000..531ee0f Binary files /dev/null and b/target/classes/cn/zyp/stusystem/entity/RolePermission.class differ diff --git a/target/classes/cn/zyp/stusystem/mapper/PermissionMapper.class b/target/classes/cn/zyp/stusystem/mapper/PermissionMapper.class new file mode 100644 index 0000000..3ca4a0e Binary files /dev/null and b/target/classes/cn/zyp/stusystem/mapper/PermissionMapper.class differ diff --git a/target/classes/cn/zyp/stusystem/mapper/RoleMapper.class b/target/classes/cn/zyp/stusystem/mapper/RoleMapper.class new file mode 100644 index 0000000..ba5433d Binary files /dev/null and b/target/classes/cn/zyp/stusystem/mapper/RoleMapper.class differ diff --git a/target/classes/cn/zyp/stusystem/mapper/RolePermissionMapper.class b/target/classes/cn/zyp/stusystem/mapper/RolePermissionMapper.class new file mode 100644 index 0000000..b2646ae Binary files /dev/null and b/target/classes/cn/zyp/stusystem/mapper/RolePermissionMapper.class differ diff --git a/target/classes/cn/zyp/stusystem/service/PermissionService.class b/target/classes/cn/zyp/stusystem/service/PermissionService.class new file mode 100644 index 0000000..c3df787 Binary files /dev/null and b/target/classes/cn/zyp/stusystem/service/PermissionService.class differ diff --git a/target/classes/cn/zyp/stusystem/service/RolePermissionService.class b/target/classes/cn/zyp/stusystem/service/RolePermissionService.class new file mode 100644 index 0000000..c12e0a8 Binary files /dev/null and b/target/classes/cn/zyp/stusystem/service/RolePermissionService.class differ diff --git a/target/classes/cn/zyp/stusystem/service/RoleService.class b/target/classes/cn/zyp/stusystem/service/RoleService.class new file mode 100644 index 0000000..cbea065 Binary files /dev/null and b/target/classes/cn/zyp/stusystem/service/RoleService.class differ diff --git a/target/classes/cn/zyp/stusystem/service/impl/PermissionServiceImpl.class b/target/classes/cn/zyp/stusystem/service/impl/PermissionServiceImpl.class new file mode 100644 index 0000000..b415821 Binary files /dev/null and b/target/classes/cn/zyp/stusystem/service/impl/PermissionServiceImpl.class differ diff --git a/target/classes/cn/zyp/stusystem/service/impl/RolePermissionServiceImpl.class b/target/classes/cn/zyp/stusystem/service/impl/RolePermissionServiceImpl.class new file mode 100644 index 0000000..dfd0da4 Binary files /dev/null and b/target/classes/cn/zyp/stusystem/service/impl/RolePermissionServiceImpl.class differ diff --git a/target/classes/cn/zyp/stusystem/service/impl/RoleServiceImpl.class b/target/classes/cn/zyp/stusystem/service/impl/RoleServiceImpl.class new file mode 100644 index 0000000..cf6d5d0 Binary files /dev/null and b/target/classes/cn/zyp/stusystem/service/impl/RoleServiceImpl.class differ diff --git a/target/classes/cn/zyp/stusystem/vo/PermissionVO.class b/target/classes/cn/zyp/stusystem/vo/PermissionVO.class new file mode 100644 index 0000000..a3bf7dd Binary files /dev/null and b/target/classes/cn/zyp/stusystem/vo/PermissionVO.class differ