添加对于用户的组织、角色分配
This commit is contained in:
@@ -5,6 +5,8 @@ import jakarta.validation.constraints.Pattern;
|
|||||||
import jakarta.validation.constraints.Size;
|
import jakarta.validation.constraints.Size;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 新增或修改角色。
|
* 新增或修改角色。
|
||||||
*/
|
*/
|
||||||
@@ -25,4 +27,7 @@ public class RoleSaveRequest {
|
|||||||
|
|
||||||
/** 1启用 0停用 */
|
/** 1启用 0停用 */
|
||||||
private Integer status;
|
private Integer status;
|
||||||
|
|
||||||
|
/** 顶部导航编码。空列表表示该角色看不到任何顶部栏目。 */
|
||||||
|
private List<String> menuCodes;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,6 +11,8 @@ import lombok.Data;
|
|||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 角色。内置角色不可删除,编码供登录令牌使用。
|
* 角色。内置角色不可删除,编码供登录令牌使用。
|
||||||
@@ -38,6 +40,13 @@ public class SysRole implements Serializable {
|
|||||||
@Schema(description = "1内置 0自建")
|
@Schema(description = "1内置 0自建")
|
||||||
private Integer builtin;
|
private Integer builtin;
|
||||||
|
|
||||||
|
@Schema(description = "1表示顶部导航已初始化")
|
||||||
|
private Integer menuInited;
|
||||||
|
|
||||||
|
@TableField(exist = false)
|
||||||
|
@Schema(description = "可访问的顶部导航编码")
|
||||||
|
private List<String> menuCodes = new ArrayList<>();
|
||||||
|
|
||||||
@TableField(fill = FieldFill.INSERT)
|
@TableField(fill = FieldFill.INSERT)
|
||||||
private LocalDateTime createTime;
|
private LocalDateTime createTime;
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,23 @@
|
|||||||
|
package com.banben.modules.admin.entity;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.annotation.IdType;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableId;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableName;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 角色与顶部导航的对应关系。
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@TableName("sys_role_menu")
|
||||||
|
public class SysRoleMenu implements Serializable {
|
||||||
|
|
||||||
|
@TableId(type = IdType.AUTO)
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
private Long roleId;
|
||||||
|
|
||||||
|
private String menuCode;
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package com.banben.modules.admin.mapper;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
|
import com.banben.modules.admin.entity.SysRoleMenu;
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 角色导航权限。
|
||||||
|
*/
|
||||||
|
@Mapper
|
||||||
|
public interface SysRoleMenuMapper extends BaseMapper<SysRoleMenu> {
|
||||||
|
}
|
||||||
@@ -5,15 +5,22 @@ import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
|||||||
import com.banben.common.BusinessException;
|
import com.banben.common.BusinessException;
|
||||||
import com.banben.modules.admin.dto.RoleSaveRequest;
|
import com.banben.modules.admin.dto.RoleSaveRequest;
|
||||||
import com.banben.modules.admin.entity.SysRole;
|
import com.banben.modules.admin.entity.SysRole;
|
||||||
|
import com.banben.modules.admin.entity.SysRoleMenu;
|
||||||
import com.banben.modules.admin.mapper.SysRoleMapper;
|
import com.banben.modules.admin.mapper.SysRoleMapper;
|
||||||
|
import com.banben.modules.admin.mapper.SysRoleMenuMapper;
|
||||||
import com.banben.modules.admin.service.RoleService;
|
import com.banben.modules.admin.service.RoleService;
|
||||||
import com.banben.modules.auth.entity.SysUser;
|
import com.banben.modules.auth.entity.SysUser;
|
||||||
import com.banben.modules.auth.mapper.SysUserMapper;
|
import com.banben.modules.auth.mapper.SysUserMapper;
|
||||||
|
import com.banben.modules.auth.nav.NavMenus;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.LinkedHashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 角色增删改。ADMIN、USER 为内置角色。
|
* 角色增删改。ADMIN、USER 为内置角色。
|
||||||
@@ -23,13 +30,28 @@ import java.util.List;
|
|||||||
public class RoleServiceImpl implements RoleService {
|
public class RoleServiceImpl implements RoleService {
|
||||||
|
|
||||||
private final SysRoleMapper roleMapper;
|
private final SysRoleMapper roleMapper;
|
||||||
|
private final SysRoleMenuMapper roleMenuMapper;
|
||||||
private final SysUserMapper userMapper;
|
private final SysUserMapper userMapper;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<SysRole> listAll() {
|
public List<SysRole> listAll() {
|
||||||
return roleMapper.selectList(new LambdaQueryWrapper<SysRole>()
|
List<SysRole> roles = roleMapper.selectList(new LambdaQueryWrapper<SysRole>()
|
||||||
.orderByDesc(SysRole::getBuiltin)
|
.orderByDesc(SysRole::getBuiltin)
|
||||||
.orderByAsc(SysRole::getId));
|
.orderByAsc(SysRole::getId));
|
||||||
|
Map<Long, List<String>> menus = roleMenuMapper.selectList(new LambdaQueryWrapper<>()).stream()
|
||||||
|
.collect(Collectors.groupingBy(SysRoleMenu::getRoleId,
|
||||||
|
Collectors.mapping(SysRoleMenu::getMenuCode, Collectors.toList())));
|
||||||
|
for (SysRole role : roles) {
|
||||||
|
if (isAdminCode(role)) {
|
||||||
|
role.setMenuCodes(new ArrayList<>(NavMenus.CODES));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
role.setMenuCodes(menus.getOrDefault(role.getId(), List.of()).stream()
|
||||||
|
.filter(NavMenus::contains)
|
||||||
|
.distinct()
|
||||||
|
.toList());
|
||||||
|
}
|
||||||
|
return roles;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -38,6 +60,8 @@ public class RoleServiceImpl implements RoleService {
|
|||||||
role.setBuiltin(0);
|
role.setBuiltin(0);
|
||||||
fill(role, request, null);
|
fill(role, request, null);
|
||||||
roleMapper.insert(role);
|
roleMapper.insert(role);
|
||||||
|
replaceMenus(role.getId(), request.getMenuCodes());
|
||||||
|
markMenusInited(role.getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -59,6 +83,13 @@ public class RoleServiceImpl implements RoleService {
|
|||||||
.set(SysUser::getRole, role.getCode())
|
.set(SysUser::getRole, role.getCode())
|
||||||
.eq(SysUser::getRoleId, id));
|
.eq(SysUser::getRoleId, id));
|
||||||
}
|
}
|
||||||
|
if (isAdminCode(role)) {
|
||||||
|
replaceMenus(id, NavMenus.CODES);
|
||||||
|
markMenusInited(id);
|
||||||
|
} else if (request.getMenuCodes() != null) {
|
||||||
|
replaceMenus(id, request.getMenuCodes());
|
||||||
|
markMenusInited(id);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -72,6 +103,7 @@ public class RoleServiceImpl implements RoleService {
|
|||||||
throw new BusinessException("该角色下还有用户,不能删除");
|
throw new BusinessException("该角色下还有用户,不能删除");
|
||||||
}
|
}
|
||||||
roleMapper.deleteById(id);
|
roleMapper.deleteById(id);
|
||||||
|
roleMenuMapper.delete(new LambdaQueryWrapper<SysRoleMenu>().eq(SysRoleMenu::getRoleId, id));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -81,8 +113,12 @@ public class RoleServiceImpl implements RoleService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void ensure(String code, String name, String remark) {
|
private void ensure(String code, String name, String remark) {
|
||||||
Long count = roleMapper.selectCount(new LambdaQueryWrapper<SysRole>().eq(SysRole::getCode, code));
|
SysRole existing = roleMapper.selectOne(new LambdaQueryWrapper<SysRole>().eq(SysRole::getCode, code));
|
||||||
if (count != null && count > 0) {
|
if (existing != null) {
|
||||||
|
if ("ADMIN".equalsIgnoreCase(code)) {
|
||||||
|
replaceMenus(existing.getId(), NavMenus.CODES);
|
||||||
|
markMenusInited(existing.getId());
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
SysRole role = new SysRole();
|
SysRole role = new SysRole();
|
||||||
@@ -92,6 +128,8 @@ public class RoleServiceImpl implements RoleService {
|
|||||||
role.setStatus(1);
|
role.setStatus(1);
|
||||||
role.setBuiltin(1);
|
role.setBuiltin(1);
|
||||||
roleMapper.insert(role);
|
roleMapper.insert(role);
|
||||||
|
replaceMenus(role.getId(), NavMenus.CODES);
|
||||||
|
markMenusInited(role.getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void fill(SysRole role, RoleSaveRequest request, Long selfId) {
|
private void fill(SysRole role, RoleSaveRequest request, Long selfId) {
|
||||||
@@ -121,4 +159,35 @@ public class RoleServiceImpl implements RoleService {
|
|||||||
private boolean isBuiltin(SysRole role) {
|
private boolean isBuiltin(SysRole role) {
|
||||||
return role.getBuiltin() != null && role.getBuiltin() == 1;
|
return role.getBuiltin() != null && role.getBuiltin() == 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean isAdminCode(SysRole role) {
|
||||||
|
return role.getCode() != null && "ADMIN".equalsIgnoreCase(role.getCode());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void replaceMenus(Long roleId, List<String> menuCodes) {
|
||||||
|
List<String> codes = menuCodes == null ? new ArrayList<>(NavMenus.CODES) : normalize(menuCodes);
|
||||||
|
roleMenuMapper.delete(new LambdaQueryWrapper<SysRoleMenu>().eq(SysRoleMenu::getRoleId, roleId));
|
||||||
|
for (String code : codes) {
|
||||||
|
SysRoleMenu row = new SysRoleMenu();
|
||||||
|
row.setRoleId(roleId);
|
||||||
|
row.setMenuCode(code);
|
||||||
|
roleMenuMapper.insert(row);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<String> normalize(List<String> menuCodes) {
|
||||||
|
LinkedHashSet<String> codes = new LinkedHashSet<>();
|
||||||
|
for (String code : menuCodes) {
|
||||||
|
if (NavMenus.contains(code)) {
|
||||||
|
codes.add(code);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new ArrayList<>(codes);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void markMenusInited(Long roleId) {
|
||||||
|
roleMapper.update(null, new LambdaUpdateWrapper<SysRole>()
|
||||||
|
.eq(SysRole::getId, roleId)
|
||||||
|
.set(SysRole::getMenuInited, 1));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,20 +1,59 @@
|
|||||||
package com.banben.modules.admin.support;
|
package com.banben.modules.admin.support;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
import com.banben.common.BusinessException;
|
import com.banben.common.BusinessException;
|
||||||
|
import com.banben.modules.admin.entity.SysRoleMenu;
|
||||||
|
import com.banben.modules.admin.mapper.SysRoleMenuMapper;
|
||||||
import com.banben.modules.auth.context.LoginUser;
|
import com.banben.modules.auth.context.LoginUser;
|
||||||
import com.banben.modules.auth.context.UserContext;
|
import com.banben.modules.auth.context.UserContext;
|
||||||
|
import com.banben.modules.auth.entity.SysUser;
|
||||||
|
import com.banben.modules.auth.mapper.SysUserMapper;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 组织、角色、用户管理只允许管理员调用。
|
* 后台管理接口:角色勾选了「后台管理」栏目才能调用。
|
||||||
|
* 权限表暂时不可用时,仍放行角色编码为 ADMIN 的账号,避免把管理员锁在外面。
|
||||||
*/
|
*/
|
||||||
@Component
|
@Component
|
||||||
|
@RequiredArgsConstructor
|
||||||
public class AdminAccess {
|
public class AdminAccess {
|
||||||
|
|
||||||
|
private final SysUserMapper userMapper;
|
||||||
|
private final SysRoleMenuMapper roleMenuMapper;
|
||||||
|
|
||||||
public void require() {
|
public void require() {
|
||||||
LoginUser user = UserContext.get();
|
LoginUser login = UserContext.get();
|
||||||
if (user == null || user.role() == null || !"ADMIN".equalsIgnoreCase(user.role())) {
|
if (login == null || login.id() == null || !allowed(login.id(), login.role())) {
|
||||||
throw new BusinessException(403, "需要管理员权限");
|
throw new BusinessException(403, "需要后台管理权限");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean allowed(Long userId, String roleCode) {
|
||||||
|
if (isAdminCode(roleCode)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
SysUser user = userMapper.selectById(userId);
|
||||||
|
if (user == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (isAdminCode(user.getRole())) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (user.getRoleId() == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
Long count = roleMenuMapper.selectCount(new LambdaQueryWrapper<SysRoleMenu>()
|
||||||
|
.eq(SysRoleMenu::getRoleId, user.getRoleId())
|
||||||
|
.eq(SysRoleMenu::getMenuCode, "admin"));
|
||||||
|
return count != null && count > 0;
|
||||||
|
} catch (Exception ignored) {
|
||||||
|
return isAdminCode(roleCode);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isAdminCode(String roleCode) {
|
||||||
|
return roleCode != null && "ADMIN".equalsIgnoreCase(roleCode);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,6 +11,8 @@ import jakarta.validation.Valid;
|
|||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 认证接口。
|
* 认证接口。
|
||||||
*/
|
*/
|
||||||
@@ -34,6 +36,12 @@ public class AuthController {
|
|||||||
return Result.success(authService.currentUser());
|
return Result.success(authService.currentUser());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Operation(summary = "当前用户可见的顶部导航")
|
||||||
|
@GetMapping("/menus")
|
||||||
|
public Result<List<String>> menus() {
|
||||||
|
return Result.success(authService.currentMenus());
|
||||||
|
}
|
||||||
|
|
||||||
@Operation(summary = "登出")
|
@Operation(summary = "登出")
|
||||||
@PostMapping("/logout")
|
@PostMapping("/logout")
|
||||||
public Result<Void> logout() {
|
public Result<Void> logout() {
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ package com.banben.modules.auth.dto;
|
|||||||
import io.swagger.v3.oas.annotations.media.Schema;
|
import io.swagger.v3.oas.annotations.media.Schema;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 登录响应。
|
* 登录响应。
|
||||||
*/
|
*/
|
||||||
@@ -27,4 +29,7 @@ public class LoginResponse {
|
|||||||
|
|
||||||
@Schema(description = "所属单位")
|
@Schema(description = "所属单位")
|
||||||
private String orgName;
|
private String orgName;
|
||||||
|
|
||||||
|
@Schema(description = "可见的顶部导航编码")
|
||||||
|
private List<String> menus;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,29 @@
|
|||||||
|
package com.banben.modules.auth.nav;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 顶部导航。编码与前端路由一致,角色按编码授权。
|
||||||
|
*/
|
||||||
|
public final class NavMenus {
|
||||||
|
|
||||||
|
public static final List<String> CODES = List.of(
|
||||||
|
"dashboard",
|
||||||
|
"search",
|
||||||
|
"hotwords",
|
||||||
|
"special",
|
||||||
|
"deposit",
|
||||||
|
"analysis",
|
||||||
|
"resource",
|
||||||
|
"collect",
|
||||||
|
"governance",
|
||||||
|
"admin"
|
||||||
|
);
|
||||||
|
|
||||||
|
private NavMenus() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean contains(String code) {
|
||||||
|
return code != null && CODES.contains(code);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,6 +4,8 @@ import com.banben.modules.auth.dto.LoginRequest;
|
|||||||
import com.banben.modules.auth.dto.LoginResponse;
|
import com.banben.modules.auth.dto.LoginResponse;
|
||||||
import com.banben.modules.auth.entity.SysUser;
|
import com.banben.modules.auth.entity.SysUser;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 认证服务。
|
* 认证服务。
|
||||||
*/
|
*/
|
||||||
@@ -18,4 +20,9 @@ public interface AuthService {
|
|||||||
* 查询当前登录用户完整信息。
|
* 查询当前登录用户完整信息。
|
||||||
*/
|
*/
|
||||||
SysUser currentUser();
|
SysUser currentUser();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 当前用户可见的顶部导航编码。
|
||||||
|
*/
|
||||||
|
List<String> currentMenus();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,9 +6,12 @@ import com.banben.modules.auth.context.UserContext;
|
|||||||
import com.banben.modules.auth.dto.LoginRequest;
|
import com.banben.modules.auth.dto.LoginRequest;
|
||||||
import com.banben.modules.auth.dto.LoginResponse;
|
import com.banben.modules.auth.dto.LoginResponse;
|
||||||
import com.banben.modules.admin.entity.SysRole;
|
import com.banben.modules.admin.entity.SysRole;
|
||||||
|
import com.banben.modules.admin.entity.SysRoleMenu;
|
||||||
import com.banben.modules.admin.mapper.SysRoleMapper;
|
import com.banben.modules.admin.mapper.SysRoleMapper;
|
||||||
|
import com.banben.modules.admin.mapper.SysRoleMenuMapper;
|
||||||
import com.banben.modules.auth.entity.SysUser;
|
import com.banben.modules.auth.entity.SysUser;
|
||||||
import com.banben.modules.auth.mapper.SysUserMapper;
|
import com.banben.modules.auth.mapper.SysUserMapper;
|
||||||
|
import com.banben.modules.auth.nav.NavMenus;
|
||||||
import com.banben.modules.auth.service.AuthService;
|
import com.banben.modules.auth.service.AuthService;
|
||||||
import com.banben.modules.auth.util.JwtUtil;
|
import com.banben.modules.auth.util.JwtUtil;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
@@ -17,6 +20,8 @@ import org.springframework.security.crypto.password.PasswordEncoder;
|
|||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 认证服务实现。
|
* 认证服务实现。
|
||||||
@@ -28,6 +33,7 @@ public class AuthServiceImpl implements AuthService {
|
|||||||
|
|
||||||
private final SysUserMapper sysUserMapper;
|
private final SysUserMapper sysUserMapper;
|
||||||
private final SysRoleMapper sysRoleMapper;
|
private final SysRoleMapper sysRoleMapper;
|
||||||
|
private final SysRoleMenuMapper roleMenuMapper;
|
||||||
private final PasswordEncoder passwordEncoder;
|
private final PasswordEncoder passwordEncoder;
|
||||||
private final JwtUtil jwtUtil;
|
private final JwtUtil jwtUtil;
|
||||||
|
|
||||||
@@ -63,9 +69,23 @@ public class AuthServiceImpl implements AuthService {
|
|||||||
response.setRealName(user.getRealName());
|
response.setRealName(user.getRealName());
|
||||||
response.setRole(user.getRole());
|
response.setRole(user.getRole());
|
||||||
response.setOrgName(user.getOrgName());
|
response.setOrgName(user.getOrgName());
|
||||||
|
response.setMenus(menusOf(user));
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<String> currentMenus() {
|
||||||
|
Long userId = UserContext.currentUserId();
|
||||||
|
if (userId == null) {
|
||||||
|
throw new BusinessException("未登录");
|
||||||
|
}
|
||||||
|
SysUser user = sysUserMapper.selectById(userId);
|
||||||
|
if (user == null) {
|
||||||
|
throw new BusinessException("未登录");
|
||||||
|
}
|
||||||
|
return menusOf(user);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public SysUser currentUser() {
|
public SysUser currentUser() {
|
||||||
Long userId = UserContext.currentUserId();
|
Long userId = UserContext.currentUserId();
|
||||||
@@ -78,4 +98,35 @@ public class AuthServiceImpl implements AuthService {
|
|||||||
}
|
}
|
||||||
return user;
|
return user;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private List<String> menusOf(SysUser user) {
|
||||||
|
if (isAdmin(user)) {
|
||||||
|
return new ArrayList<>(NavMenus.CODES);
|
||||||
|
}
|
||||||
|
if (user.getRoleId() == null) {
|
||||||
|
return new ArrayList<>(NavMenus.CODES);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
return roleMenuMapper.selectList(new LambdaQueryWrapper<SysRoleMenu>().eq(SysRoleMenu::getRoleId, user.getRoleId()))
|
||||||
|
.stream()
|
||||||
|
.map(SysRoleMenu::getMenuCode)
|
||||||
|
.filter(NavMenus::contains)
|
||||||
|
.distinct()
|
||||||
|
.toList();
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.warn("读取导航权限失败,暂时开放全部栏目:{}", e.getMessage());
|
||||||
|
return new ArrayList<>(NavMenus.CODES);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isAdmin(SysUser user) {
|
||||||
|
if (user.getRole() != null && "ADMIN".equalsIgnoreCase(user.getRole())) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (user.getRoleId() == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
SysRole role = sysRoleMapper.selectById(user.getRoleId());
|
||||||
|
return role != null && "ADMIN".equalsIgnoreCase(role.getCode());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,32 @@
|
|||||||
|
-- 角色可见的顶部导航。已有角色默认拥有全部栏目,之后可在角色管理里取消勾选。
|
||||||
|
-- menu_inited=1 后再次执行不会把已清空的栏目补回来。
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS sys_role_menu
|
||||||
|
(
|
||||||
|
id BIGSERIAL NOT NULL PRIMARY KEY,
|
||||||
|
role_id BIGINT NOT NULL,
|
||||||
|
menu_code VARCHAR(32) NOT NULL
|
||||||
|
);
|
||||||
|
CREATE UNIQUE INDEX IF NOT EXISTS uk_sys_role_menu ON sys_role_menu (role_id, menu_code);
|
||||||
|
|
||||||
|
COMMENT ON TABLE sys_role_menu IS '角色可访问的顶部导航编码';
|
||||||
|
COMMENT ON COLUMN sys_role_menu.menu_code IS '导航编码,如 dashboard、search';
|
||||||
|
|
||||||
|
ALTER TABLE sys_role ADD COLUMN IF NOT EXISTS menu_inited SMALLINT DEFAULT 0;
|
||||||
|
COMMENT ON COLUMN sys_role.menu_inited IS '1表示顶部导航已按角色保存过,重复执行不再补全';
|
||||||
|
|
||||||
|
INSERT INTO sys_role_menu (role_id, menu_code)
|
||||||
|
SELECT r.id, m.code
|
||||||
|
FROM sys_role r
|
||||||
|
CROSS JOIN (
|
||||||
|
VALUES ('dashboard'), ('search'), ('hotwords'), ('special'),
|
||||||
|
('deposit'), ('analysis'), ('resource'), ('collect'), ('governance')
|
||||||
|
) AS m(code)
|
||||||
|
WHERE r.deleted = 0
|
||||||
|
AND COALESCE(r.menu_inited, 0) = 0
|
||||||
|
AND NOT EXISTS (SELECT 1 FROM sys_role_menu x WHERE x.role_id = r.id);
|
||||||
|
|
||||||
|
UPDATE sys_role
|
||||||
|
SET menu_inited = 1
|
||||||
|
WHERE deleted = 0
|
||||||
|
AND COALESCE(menu_inited, 0) = 0;
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
-- 后台管理成为顶部导航的一项。已有的系统管理员默认拥有该栏目,其它角色需在角色管理里勾选。
|
||||||
|
|
||||||
|
INSERT INTO sys_role_menu (role_id, menu_code)
|
||||||
|
SELECT r.id, 'admin'
|
||||||
|
FROM sys_role r
|
||||||
|
WHERE r.deleted = 0
|
||||||
|
AND upper(r.code) = 'ADMIN'
|
||||||
|
AND NOT EXISTS (
|
||||||
|
SELECT 1 FROM sys_role_menu x WHERE x.role_id = r.id AND x.menu_code = 'admin'
|
||||||
|
);
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
-- 系统管理员补齐全部顶部栏目。可重复执行,已有的不会重复插入。
|
||||||
|
|
||||||
|
INSERT INTO sys_role_menu (role_id, menu_code)
|
||||||
|
SELECT r.id, m.code
|
||||||
|
FROM sys_role r
|
||||||
|
CROSS JOIN (
|
||||||
|
VALUES ('dashboard'), ('search'), ('hotwords'), ('special'),
|
||||||
|
('deposit'), ('analysis'), ('resource'), ('collect'),
|
||||||
|
('governance'), ('admin')
|
||||||
|
) AS m(code)
|
||||||
|
WHERE r.deleted = 0
|
||||||
|
AND upper(r.code) = 'ADMIN'
|
||||||
|
AND NOT EXISTS (
|
||||||
|
SELECT 1 FROM sys_role_menu x WHERE x.role_id = r.id AND x.menu_code = m.code
|
||||||
|
);
|
||||||
@@ -20,6 +20,11 @@ export function getCurrentUser() {
|
|||||||
return request.get<never, UserInfo>('/auth/me')
|
return request.get<never, UserInfo>('/auth/me')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** 当前用户可见的顶部导航编码 */
|
||||||
|
export function myMenus() {
|
||||||
|
return request.get<never, string[]>('/auth/menus')
|
||||||
|
}
|
||||||
|
|
||||||
/** 登出 */
|
/** 登出 */
|
||||||
export function logout() {
|
export function logout() {
|
||||||
return request.post<never, void>('/auth/logout')
|
return request.post<never, void>('/auth/logout')
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ export interface RoleItem {
|
|||||||
remark?: string
|
remark?: string
|
||||||
status?: number
|
status?: number
|
||||||
builtin?: number
|
builtin?: number
|
||||||
|
menuCodes?: string[]
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface RoleSave {
|
export interface RoleSave {
|
||||||
@@ -36,6 +37,7 @@ export interface RoleSave {
|
|||||||
name: string
|
name: string
|
||||||
remark?: string
|
remark?: string
|
||||||
status?: number
|
status?: number
|
||||||
|
menuCodes?: string[]
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface UserItem {
|
export interface UserItem {
|
||||||
|
|||||||
@@ -0,0 +1,24 @@
|
|||||||
|
/** 顶部导航。编码与后端 NavMenus、路由 meta.menu 一致。 */
|
||||||
|
export const NAV_MENUS = [
|
||||||
|
{ code: 'dashboard', title: '首页', path: '/dashboard' },
|
||||||
|
{ code: 'search', title: '资源检索', path: '/search' },
|
||||||
|
{ code: 'hotwords', title: '热词检索', path: '/hotwords' },
|
||||||
|
{ code: 'special', title: '特藏推荐', path: '/special' },
|
||||||
|
{ code: 'deposit', title: '资源呈缴', path: '/deposit' },
|
||||||
|
{ code: 'analysis', title: '数据统计', path: '/analysis' },
|
||||||
|
{ code: 'resource', title: '资源管理', path: '/resource' },
|
||||||
|
{ code: 'collect', title: '数据采集', path: '/collect' },
|
||||||
|
{ code: 'governance', title: '数据治理', path: '/governance' },
|
||||||
|
{ code: 'admin', title: '后台管理', path: '/admin' }
|
||||||
|
] as const
|
||||||
|
|
||||||
|
export type NavCode = (typeof NAV_MENUS)[number]['code']
|
||||||
|
|
||||||
|
export function firstAllowedPath(menus: string[]): string | null {
|
||||||
|
const item = NAV_MENUS.find((menu) => menus.includes(menu.code))
|
||||||
|
return item ? item.path : null
|
||||||
|
}
|
||||||
|
|
||||||
|
export function navTitle(code: string): string {
|
||||||
|
return NAV_MENUS.find((menu) => menu.code === code)?.title || code
|
||||||
|
}
|
||||||
@@ -1,37 +1,49 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { computed } from 'vue'
|
import { computed, onMounted, ref } from 'vue'
|
||||||
import { useRoute, useRouter } from 'vue-router'
|
import { useRoute, useRouter } from 'vue-router'
|
||||||
import { clearAuth, getUser } from '@/utils/auth'
|
import { myMenus } from '@/api/auth'
|
||||||
|
import { NAV_MENUS, firstAllowedPath } from '@/constants/nav'
|
||||||
|
import { clearAuth, getUser, setUser } from '@/utils/auth'
|
||||||
import logoUrl from '@/assets/logo-green.png'
|
import logoUrl from '@/assets/logo-green.png'
|
||||||
import noticeUrl from '@/assets/icon-notice.png'
|
import noticeUrl from '@/assets/icon-notice.png'
|
||||||
import settingUrl from '@/assets/icon-setting.png'
|
|
||||||
import avatarUrl from '@/assets/avatar.png'
|
import avatarUrl from '@/assets/avatar.png'
|
||||||
|
|
||||||
const route = useRoute()
|
const route = useRoute()
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
const user = computed(() => getUser())
|
const user = computed(() => getUser())
|
||||||
|
const allowed = ref<string[] | null>(getUser()?.menus ?? null)
|
||||||
|
|
||||||
const menus = [
|
const menus = computed(() => {
|
||||||
{ path: '/dashboard', title: '首页' },
|
if (allowed.value == null) return NAV_MENUS
|
||||||
{ path: '/search', title: '资源检索' },
|
return NAV_MENUS.filter((menu) => allowed.value!.includes(menu.code))
|
||||||
{ path: '/hotwords', title: '热词检索' },
|
})
|
||||||
{ path: '/special', title: '特藏推荐' },
|
|
||||||
{ path: '/deposit', title: '资源呈缴' },
|
const canSeeCurrent = computed(() => {
|
||||||
{ path: '/analysis', title: '数据统计' },
|
const code = route.meta.menu as string | undefined
|
||||||
{ path: '/resource', title: '资源管理' },
|
if (!code || allowed.value == null) return true
|
||||||
{ path: '/collect', title: '数据采集' },
|
return allowed.value.includes(code)
|
||||||
{ path: '/governance', title: '数据治理' }
|
})
|
||||||
]
|
|
||||||
|
onMounted(async () => {
|
||||||
|
try {
|
||||||
|
const list = await myMenus()
|
||||||
|
allowed.value = list
|
||||||
|
const current = getUser()
|
||||||
|
if (current) setUser({ ...current, menus: list })
|
||||||
|
const code = route.meta.menu as string | undefined
|
||||||
|
if (code && !list.includes(code)) {
|
||||||
|
const next = firstAllowedPath(list)
|
||||||
|
if (next) router.replace(next)
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
// 权限接口暂时不可用时,沿用登录时记下的栏目
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
function logout() {
|
function logout() {
|
||||||
clearAuth()
|
clearAuth()
|
||||||
router.push('/login')
|
router.push('/login')
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 齿轮按钮:新标签页打开后台管理 */
|
|
||||||
function openAdmin() {
|
|
||||||
window.open(router.resolve('/admin').href, '_blank')
|
|
||||||
}
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
@@ -47,7 +59,7 @@ function openAdmin() {
|
|||||||
<a
|
<a
|
||||||
v-for="m in menus"
|
v-for="m in menus"
|
||||||
:key="m.path"
|
:key="m.path"
|
||||||
:class="{ active: route.path === m.path }"
|
:class="{ active: route.path === m.path || route.path.startsWith(m.path + '/') }"
|
||||||
@click="router.push(m.path)"
|
@click="router.push(m.path)"
|
||||||
>
|
>
|
||||||
{{ m.title }}
|
{{ m.title }}
|
||||||
@@ -55,7 +67,6 @@ function openAdmin() {
|
|||||||
</nav>
|
</nav>
|
||||||
<div class="right">
|
<div class="right">
|
||||||
<img class="hicon" :src="noticeUrl" alt="notice" />
|
<img class="hicon" :src="noticeUrl" alt="notice" />
|
||||||
<img class="hicon" :src="settingUrl" alt="setting" title="后台管理" @click="openAdmin" />
|
|
||||||
<img class="avatar" :src="avatarUrl" alt="avatar" />
|
<img class="avatar" :src="avatarUrl" alt="avatar" />
|
||||||
<span class="user">{{ user?.realName || 'admin' }}</span>
|
<span class="user">{{ user?.realName || 'admin' }}</span>
|
||||||
<a class="quit" @click="logout">退出登录</a>
|
<a class="quit" @click="logout">退出登录</a>
|
||||||
@@ -63,7 +74,8 @@ function openAdmin() {
|
|||||||
</header>
|
</header>
|
||||||
|
|
||||||
<main class="site-main">
|
<main class="site-main">
|
||||||
<router-view />
|
<router-view v-if="canSeeCurrent" />
|
||||||
|
<el-empty v-else description="当前角色没有此栏目的访问权限" />
|
||||||
</main>
|
</main>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
@@ -177,16 +189,23 @@ function openAdmin() {
|
|||||||
.nav {
|
.nav {
|
||||||
display: flex;
|
display: flex;
|
||||||
flex: 1;
|
flex: 1;
|
||||||
|
min-width: 0;
|
||||||
justify-content: center;
|
justify-content: center;
|
||||||
gap: 2px;
|
gap: 2px;
|
||||||
height: 100%;
|
height: 100%;
|
||||||
|
overflow-x: auto;
|
||||||
|
scrollbar-width: none;
|
||||||
|
|
||||||
|
&::-webkit-scrollbar {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
|
||||||
a {
|
a {
|
||||||
position: relative;
|
position: relative;
|
||||||
display: flex;
|
display: flex;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
height: 100%;
|
height: 100%;
|
||||||
padding: 0 11px;
|
padding: 0 8px;
|
||||||
color: rgba(255, 255, 255, 0.78);
|
color: rgba(255, 255, 255, 0.78);
|
||||||
font-size: 14px;
|
font-size: 14px;
|
||||||
letter-spacing: 0.5px;
|
letter-spacing: 0.5px;
|
||||||
@@ -198,8 +217,8 @@ function openAdmin() {
|
|||||||
&::after {
|
&::after {
|
||||||
content: '';
|
content: '';
|
||||||
position: absolute;
|
position: absolute;
|
||||||
left: 11px;
|
left: 8px;
|
||||||
right: 11px;
|
right: 8px;
|
||||||
bottom: 0;
|
bottom: 0;
|
||||||
height: 2px;
|
height: 2px;
|
||||||
background: var(--brand-gold);
|
background: var(--brand-gold);
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { createRouter, createWebHistory, type RouteRecordRaw } from 'vue-router'
|
import { createRouter, createWebHistory, type RouteRecordRaw } from 'vue-router'
|
||||||
import BasicLayout from '@/layouts/BasicLayout.vue'
|
import BasicLayout from '@/layouts/BasicLayout.vue'
|
||||||
import { getToken } from '@/utils/auth'
|
import { firstAllowedPath } from '@/constants/nav'
|
||||||
|
import { getToken, getUser } from '@/utils/auth'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 路由表:与招标文件功能模块对应。
|
* 路由表:与招标文件功能模块对应。
|
||||||
@@ -16,67 +17,67 @@ const routes: RouteRecordRaw[] = [
|
|||||||
path: 'dashboard',
|
path: 'dashboard',
|
||||||
name: 'Dashboard',
|
name: 'Dashboard',
|
||||||
component: () => import('@/views/Dashboard.vue'),
|
component: () => import('@/views/Dashboard.vue'),
|
||||||
meta: { title: '首页概览', icon: 'HomeFilled' }
|
meta: { title: '首页概览', icon: 'HomeFilled', menu: 'dashboard' }
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
path: 'search',
|
path: 'search',
|
||||||
name: 'Search',
|
name: 'Search',
|
||||||
component: () => import('@/views/search/SearchView.vue'),
|
component: () => import('@/views/search/SearchView.vue'),
|
||||||
meta: { title: '检索发现', icon: 'Search' }
|
meta: { title: '检索发现', icon: 'Search', menu: 'search' }
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
path: 'hotwords',
|
path: 'hotwords',
|
||||||
name: 'HotWords',
|
name: 'HotWords',
|
||||||
component: () => import('@/views/search/HotWords.vue'),
|
component: () => import('@/views/search/HotWords.vue'),
|
||||||
meta: { title: '热词检索', icon: 'Cloudy' }
|
meta: { title: '热词检索', icon: 'Cloudy', menu: 'hotwords' }
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
path: 'advanced',
|
path: 'advanced',
|
||||||
name: 'Advanced',
|
name: 'Advanced',
|
||||||
component: () => import('@/views/search/AdvancedView.vue'),
|
component: () => import('@/views/search/AdvancedView.vue'),
|
||||||
meta: { title: '高级检索' }
|
meta: { title: '高级检索', menu: 'search' }
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
path: 'resource',
|
path: 'resource',
|
||||||
name: 'Resource',
|
name: 'Resource',
|
||||||
component: () => import('@/views/resource/ResourceList.vue'),
|
component: () => import('@/views/resource/ResourceList.vue'),
|
||||||
meta: { title: '版本资源库', icon: 'Collection' }
|
meta: { title: '版本资源库', icon: 'Collection', menu: 'resource' }
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
path: 'collect',
|
path: 'collect',
|
||||||
name: 'Collect',
|
name: 'Collect',
|
||||||
component: () => import('@/views/collect/CollectView.vue'),
|
component: () => import('@/views/collect/CollectView.vue'),
|
||||||
meta: { title: '数据采集', icon: 'Download' }
|
meta: { title: '数据采集', icon: 'Download', menu: 'collect' }
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
path: 'governance',
|
path: 'governance',
|
||||||
name: 'Governance',
|
name: 'Governance',
|
||||||
component: () => import('@/views/governance/GovernanceView.vue'),
|
component: () => import('@/views/governance/GovernanceView.vue'),
|
||||||
meta: { title: '数据治理', icon: 'MagicStick' }
|
meta: { title: '数据治理', icon: 'MagicStick', menu: 'governance' }
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
path: 'special',
|
path: 'special',
|
||||||
name: 'Special',
|
name: 'Special',
|
||||||
component: () => import('@/views/special/SpecialView.vue'),
|
component: () => import('@/views/special/SpecialView.vue'),
|
||||||
meta: { title: '特藏推荐', icon: 'Star' }
|
meta: { title: '特藏推荐', icon: 'Star', menu: 'special' }
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
path: 'deposit',
|
path: 'deposit',
|
||||||
name: 'Deposit',
|
name: 'Deposit',
|
||||||
component: () => import('@/views/deposit/DepositView.vue'),
|
component: () => import('@/views/deposit/DepositView.vue'),
|
||||||
meta: { title: '资源呈缴', icon: 'Upload' }
|
meta: { title: '资源呈缴', icon: 'Upload', menu: 'deposit' }
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
path: 'analysis',
|
path: 'analysis',
|
||||||
name: 'Analysis',
|
name: 'Analysis',
|
||||||
component: () => import('@/views/analysis/AnalysisView.vue'),
|
component: () => import('@/views/analysis/AnalysisView.vue'),
|
||||||
meta: { title: '统计分析', icon: 'DataAnalysis' }
|
meta: { title: '统计分析', icon: 'DataAnalysis', menu: 'analysis' }
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
path: 'admin',
|
path: 'admin',
|
||||||
name: 'Admin',
|
name: 'Admin',
|
||||||
component: () => import('@/views/admin/AdminView.vue'),
|
component: () => import('@/views/admin/AdminView.vue'),
|
||||||
meta: { title: '后台管理' }
|
meta: { title: '后台管理', menu: 'admin' }
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@@ -107,6 +108,12 @@ router.beforeEach((to) => {
|
|||||||
if (to.path === '/login' && token) {
|
if (to.path === '/login' && token) {
|
||||||
return '/'
|
return '/'
|
||||||
}
|
}
|
||||||
|
const menu = to.meta?.menu as string | undefined
|
||||||
|
const menus = getUser()?.menus
|
||||||
|
if (menu && menus && !menus.includes(menu)) {
|
||||||
|
const next = firstAllowedPath(menus)
|
||||||
|
if (next && next !== to.path) return next
|
||||||
|
}
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -191,7 +191,7 @@ a {
|
|||||||
.el-dialog {
|
.el-dialog {
|
||||||
border: 1px solid var(--card-gold-border);
|
border: 1px solid var(--card-gold-border);
|
||||||
border-radius: var(--radius-sm);
|
border-radius: var(--radius-sm);
|
||||||
overflow: hidden;
|
overflow: visible;
|
||||||
}
|
}
|
||||||
|
|
||||||
.el-dialog__header,
|
.el-dialog__header,
|
||||||
|
|||||||
@@ -9,6 +9,8 @@ export interface UserInfo {
|
|||||||
realName: string
|
realName: string
|
||||||
role: string
|
role: string
|
||||||
orgName: string
|
orgName: string
|
||||||
|
/** 可见的顶部导航编码。未下发时表示尚未刷新,先按全部栏目显示。 */
|
||||||
|
menus?: string[]
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getToken(): string {
|
export function getToken(): string {
|
||||||
|
|||||||
@@ -32,7 +32,8 @@ async function handleLogin() {
|
|||||||
username: res.username,
|
username: res.username,
|
||||||
realName: res.realName,
|
realName: res.realName,
|
||||||
role: res.role,
|
role: res.role,
|
||||||
orgName: res.orgName
|
orgName: res.orgName,
|
||||||
|
menus: res.menus
|
||||||
})
|
})
|
||||||
ElMessage.success('登录成功')
|
ElMessage.success('登录成功')
|
||||||
const redirect = (route.query.redirect as string) || '/'
|
const redirect = (route.query.redirect as string) || '/'
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { onMounted, reactive, ref } from 'vue'
|
import { computed, onMounted, reactive, ref } from 'vue'
|
||||||
import { ElMessage, ElMessageBox } from 'element-plus'
|
import { ElMessage, ElMessageBox } from 'element-plus'
|
||||||
import {
|
import {
|
||||||
createOrg,
|
createOrg,
|
||||||
@@ -22,6 +22,7 @@ import {
|
|||||||
type UserImportResult,
|
type UserImportResult,
|
||||||
type UserItem
|
type UserItem
|
||||||
} from '@/api/system'
|
} from '@/api/system'
|
||||||
|
import { NAV_MENUS, navTitle } from '@/constants/nav'
|
||||||
|
|
||||||
const props = defineProps<{
|
const props = defineProps<{
|
||||||
section: 'org' | 'role' | 'user'
|
section: 'org' | 'role' | 'user'
|
||||||
@@ -42,12 +43,15 @@ const orgForm = reactive<OrgSave & { id?: number }>({
|
|||||||
const roles = ref<RoleItem[]>([])
|
const roles = ref<RoleItem[]>([])
|
||||||
const roleDialog = ref(false)
|
const roleDialog = ref(false)
|
||||||
const roleSaving = ref(false)
|
const roleSaving = ref(false)
|
||||||
const roleForm = reactive<{ id?: number; code: string; name: string; remark: string; status: number; builtin?: number }>({
|
const roleForm = reactive<{ id?: number; code: string; name: string; remark: string; status: number; builtin?: number; menuCodes: string[] }>({
|
||||||
code: '',
|
code: '',
|
||||||
name: '',
|
name: '',
|
||||||
remark: '',
|
remark: '',
|
||||||
status: 1
|
status: 1,
|
||||||
|
menuCodes: NAV_MENUS.map((menu) => menu.code)
|
||||||
})
|
})
|
||||||
|
const isAdminRole = computed(() => roleForm.code.trim().toUpperCase() === 'ADMIN')
|
||||||
|
const allMenuCodes = () => NAV_MENUS.map((menu) => menu.code)
|
||||||
|
|
||||||
const userQuery = reactive<{ current: number; size: number; keyword: string; orgId?: number; roleId?: number }>({
|
const userQuery = reactive<{ current: number; size: number; keyword: string; orgId?: number; roleId?: number }>({
|
||||||
current: 1,
|
current: 1,
|
||||||
@@ -170,6 +174,7 @@ function openRoleCreate() {
|
|||||||
roleForm.remark = ''
|
roleForm.remark = ''
|
||||||
roleForm.status = 1
|
roleForm.status = 1
|
||||||
roleForm.builtin = 0
|
roleForm.builtin = 0
|
||||||
|
roleForm.menuCodes = allMenuCodes()
|
||||||
roleDialog.value = true
|
roleDialog.value = true
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -180,6 +185,9 @@ function openRoleEdit(row: RoleItem) {
|
|||||||
roleForm.remark = row.remark || ''
|
roleForm.remark = row.remark || ''
|
||||||
roleForm.status = row.status ?? 1
|
roleForm.status = row.status ?? 1
|
||||||
roleForm.builtin = row.builtin
|
roleForm.builtin = row.builtin
|
||||||
|
roleForm.menuCodes = row.code === 'ADMIN'
|
||||||
|
? allMenuCodes()
|
||||||
|
: (row.menuCodes ? [...row.menuCodes] : allMenuCodes())
|
||||||
roleDialog.value = true
|
roleDialog.value = true
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -194,7 +202,8 @@ async function saveRole() {
|
|||||||
code: roleForm.code.trim(),
|
code: roleForm.code.trim(),
|
||||||
name: roleForm.name.trim(),
|
name: roleForm.name.trim(),
|
||||||
remark: roleForm.remark.trim() || undefined,
|
remark: roleForm.remark.trim() || undefined,
|
||||||
status: roleForm.status
|
status: roleForm.status,
|
||||||
|
menuCodes: isAdminRole.value ? allMenuCodes() : [...roleForm.menuCodes]
|
||||||
}
|
}
|
||||||
if (roleForm.id) {
|
if (roleForm.id) {
|
||||||
await updateRole(roleForm.id, data)
|
await updateRole(roleForm.id, data)
|
||||||
@@ -216,18 +225,30 @@ async function deleteRole(row: RoleItem) {
|
|||||||
loadRoles()
|
loadRoles()
|
||||||
}
|
}
|
||||||
|
|
||||||
function openUserCreate() {
|
async function openUserCreate() {
|
||||||
|
try {
|
||||||
|
await loadRoles()
|
||||||
|
} catch {
|
||||||
|
// 角色列表失败时仍打开弹窗,下面会提示先去角色管理新增
|
||||||
|
}
|
||||||
userForm.id = undefined
|
userForm.id = undefined
|
||||||
userForm.username = ''
|
userForm.username = ''
|
||||||
userForm.password = ''
|
userForm.password = ''
|
||||||
userForm.realName = ''
|
userForm.realName = ''
|
||||||
userForm.roleId = roles.value.find((role) => role.code === 'USER')?.id
|
userForm.roleId = roles.value.find((role) => role.code === 'USER')?.id ?? roles.value[0]?.id
|
||||||
userForm.orgId = undefined
|
userForm.orgId = undefined
|
||||||
userForm.status = 1
|
userForm.status = 1
|
||||||
userDialog.value = true
|
userDialog.value = true
|
||||||
}
|
}
|
||||||
|
|
||||||
function openUserEdit(row: UserItem) {
|
async function openUserEdit(row: UserItem) {
|
||||||
|
if (!roles.value.length) {
|
||||||
|
try {
|
||||||
|
await loadRoles()
|
||||||
|
} catch {
|
||||||
|
// 沿用已有列表
|
||||||
|
}
|
||||||
|
}
|
||||||
userForm.id = row.id
|
userForm.id = row.id
|
||||||
userForm.username = row.username
|
userForm.username = row.username
|
||||||
userForm.password = ''
|
userForm.password = ''
|
||||||
@@ -381,7 +402,12 @@ onMounted(() => {
|
|||||||
</el-tag>
|
</el-tag>
|
||||||
</template>
|
</template>
|
||||||
</el-table-column>
|
</el-table-column>
|
||||||
<el-table-column prop="remark" label="备注" min-width="200" show-overflow-tooltip />
|
<el-table-column prop="remark" label="备注" min-width="160" show-overflow-tooltip />
|
||||||
|
<el-table-column label="可见栏目" min-width="220" show-overflow-tooltip>
|
||||||
|
<template #default="{ row }">
|
||||||
|
{{ (row.menuCodes || []).map((code: string) => navTitle(code)).join('、') || '无' }}
|
||||||
|
</template>
|
||||||
|
</el-table-column>
|
||||||
<el-table-column label="操作" width="140">
|
<el-table-column label="操作" width="140">
|
||||||
<template #default="{ row }">
|
<template #default="{ row }">
|
||||||
<el-button link type="primary" size="small" @click="openRoleEdit(row)">编辑</el-button>
|
<el-button link type="primary" size="small" @click="openRoleEdit(row)">编辑</el-button>
|
||||||
@@ -495,7 +521,7 @@ onMounted(() => {
|
|||||||
</template>
|
</template>
|
||||||
</el-dialog>
|
</el-dialog>
|
||||||
|
|
||||||
<el-dialog v-model="roleDialog" :title="roleForm.id ? '编辑角色' : '新增角色'" width="480px">
|
<el-dialog v-model="roleDialog" :title="roleForm.id ? '编辑角色' : '新增角色'" width="560px">
|
||||||
<el-form label-width="88px">
|
<el-form label-width="88px">
|
||||||
<el-form-item label="编码" required>
|
<el-form-item label="编码" required>
|
||||||
<el-input v-model="roleForm.code" maxlength="32" :disabled="roleForm.builtin === 1" placeholder="如 EDITOR" />
|
<el-input v-model="roleForm.code" maxlength="32" :disabled="roleForm.builtin === 1" placeholder="如 EDITOR" />
|
||||||
@@ -509,6 +535,16 @@ onMounted(() => {
|
|||||||
<el-radio :value="0">停用</el-radio>
|
<el-radio :value="0">停用</el-radio>
|
||||||
</el-radio-group>
|
</el-radio-group>
|
||||||
</el-form-item>
|
</el-form-item>
|
||||||
|
<el-form-item label="可见栏目">
|
||||||
|
<el-checkbox-group v-model="roleForm.menuCodes" class="nav-checks" :disabled="isAdminRole">
|
||||||
|
<el-checkbox v-for="menu in NAV_MENUS" :key="menu.code" :value="menu.code">
|
||||||
|
{{ menu.title }}
|
||||||
|
</el-checkbox>
|
||||||
|
</el-checkbox-group>
|
||||||
|
<div class="form-tip">
|
||||||
|
{{ isAdminRole ? '系统管理员默认可访问全部栏目' : '未勾选的栏目不会出现在该角色用户的顶部导航中,后台管理也在其中' }}
|
||||||
|
</div>
|
||||||
|
</el-form-item>
|
||||||
<el-form-item label="备注">
|
<el-form-item label="备注">
|
||||||
<el-input v-model="roleForm.remark" type="textarea" maxlength="256" />
|
<el-input v-model="roleForm.remark" type="textarea" maxlength="256" />
|
||||||
</el-form-item>
|
</el-form-item>
|
||||||
@@ -537,9 +573,10 @@ onMounted(() => {
|
|||||||
/>
|
/>
|
||||||
</el-form-item>
|
</el-form-item>
|
||||||
<el-form-item label="角色" required>
|
<el-form-item label="角色" required>
|
||||||
<el-select v-model="userForm.roleId" style="width: 100%">
|
<el-radio-group v-if="roles.length" v-model="userForm.roleId" class="role-picks">
|
||||||
<el-option v-for="role in roles" :key="role.id" :label="role.name" :value="role.id" />
|
<el-radio v-for="role in roles" :key="role.id" :value="role.id">{{ role.name }}</el-radio>
|
||||||
</el-select>
|
</el-radio-group>
|
||||||
|
<div v-else class="form-tip">暂无角色,请先在角色管理中新增</div>
|
||||||
</el-form-item>
|
</el-form-item>
|
||||||
<el-form-item label="组织">
|
<el-form-item label="组织">
|
||||||
<el-tree-select
|
<el-tree-select
|
||||||
@@ -581,4 +618,24 @@ onMounted(() => {
|
|||||||
margin-top: 12px;
|
margin-top: 12px;
|
||||||
justify-content: flex-end;
|
justify-content: flex-end;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.nav-checks {
|
||||||
|
display: flex;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
gap: 4px 16px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.form-tip {
|
||||||
|
width: 100%;
|
||||||
|
margin-top: 4px;
|
||||||
|
font-size: 12px;
|
||||||
|
color: var(--text-muted);
|
||||||
|
line-height: 1.5;
|
||||||
|
}
|
||||||
|
|
||||||
|
.role-picks {
|
||||||
|
display: flex;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
gap: 4px 16px;
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|||||||
Reference in New Issue
Block a user