100 lines
3.1 KiB
Java
100 lines
3.1 KiB
Java
package com.nanxiislet.admin.controller;
|
|
|
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
import com.nanxiislet.admin.common.result.R;
|
|
import com.nanxiislet.admin.entity.SysRole;
|
|
import com.nanxiislet.admin.service.SysRoleService;
|
|
import io.swagger.v3.oas.annotations.Operation;
|
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
|
import jakarta.annotation.Resource;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
import java.util.List;
|
|
|
|
/**
|
|
* 角色管理控制器
|
|
*
|
|
* @author NanxiIslet
|
|
* @since 2024-01-08
|
|
*/
|
|
@Slf4j
|
|
@RestController
|
|
@RequestMapping("/system/role")
|
|
@Tag(name = "角色管理", description = "角色CRUD、权限分配等")
|
|
public class SysRoleController {
|
|
|
|
@Resource
|
|
private SysRoleService roleService;
|
|
|
|
@GetMapping("/page")
|
|
@Operation(summary = "分页查询角色")
|
|
public R<IPage<SysRole>> page(
|
|
@RequestParam(defaultValue = "1") Integer page,
|
|
@RequestParam(defaultValue = "10") Integer pageSize,
|
|
@RequestParam(required = false) String keyword) {
|
|
return R.ok(roleService.listPage(page, pageSize, keyword));
|
|
}
|
|
|
|
@GetMapping("/list")
|
|
@Operation(summary = "获取所有有效角色")
|
|
public R<List<SysRole>> list() {
|
|
return R.ok(roleService.listAll());
|
|
}
|
|
|
|
@GetMapping("/list-all")
|
|
@Operation(summary = "获取所有角色选项(简化版)")
|
|
public R<List<java.util.Map<String, String>>> listAll() {
|
|
List<SysRole> roles = roleService.listAll();
|
|
List<java.util.Map<String, String>> options = roles.stream()
|
|
.map(role -> {
|
|
java.util.Map<String, String> map = new java.util.HashMap<>();
|
|
map.put("code", role.getCode());
|
|
map.put("name", role.getName());
|
|
return map;
|
|
})
|
|
.toList();
|
|
return R.ok(options);
|
|
}
|
|
|
|
@GetMapping("/{id}")
|
|
@Operation(summary = "获取角色详情")
|
|
public R<SysRole> getById(@PathVariable Long id) {
|
|
return R.ok(roleService.getById(id));
|
|
}
|
|
|
|
@PostMapping
|
|
@Operation(summary = "创建角色")
|
|
public R<Long> create(@RequestBody SysRole role) {
|
|
return R.ok(roleService.create(role));
|
|
}
|
|
|
|
@PutMapping("/{id}")
|
|
@Operation(summary = "更新角色")
|
|
public R<Void> update(@PathVariable Long id, @RequestBody SysRole role) {
|
|
role.setId(id);
|
|
roleService.updateRole(role);
|
|
return R.ok();
|
|
}
|
|
|
|
@DeleteMapping("/{id}")
|
|
@Operation(summary = "删除角色")
|
|
public R<Void> delete(@PathVariable Long id) {
|
|
roleService.deleteRole(id);
|
|
return R.ok();
|
|
}
|
|
|
|
@GetMapping("/{id}/menus")
|
|
@Operation(summary = "获取角色的菜单ID列表")
|
|
public R<List<Long>> getRoleMenus(@PathVariable Long id) {
|
|
return R.ok(roleService.getRoleMenuIds(id));
|
|
}
|
|
|
|
@PostMapping("/{id}/menus")
|
|
@Operation(summary = "分配菜单权限")
|
|
public R<Void> assignMenus(@PathVariable Long id, @RequestBody List<Long> menuIds) {
|
|
roleService.assignMenus(id, menuIds);
|
|
return R.ok();
|
|
}
|
|
}
|