106 lines
2.7 KiB
TypeScript
106 lines
2.7 KiB
TypeScript
/**
|
|
* 权限管理 API
|
|
*
|
|
* 角色管理、权限码查询、用户权限配置等接口封装
|
|
*/
|
|
|
|
import { api } from '@/api/request';
|
|
|
|
/** 角色信息 */
|
|
export interface Role {
|
|
id: number;
|
|
name: string;
|
|
description?: string;
|
|
permission_codes: string[];
|
|
}
|
|
|
|
/** 权限码信息 */
|
|
export interface PermissionItem {
|
|
id: number;
|
|
code: string;
|
|
name: string;
|
|
}
|
|
|
|
/** 用户权限范围条目 */
|
|
export interface ScopeEntry {
|
|
id?: number;
|
|
/** 1组织 2项目 3设备 */
|
|
scope_type: number;
|
|
/** 对应的组织/项目/设备ID */
|
|
scope_id: number;
|
|
/** 1查看 2操作 */
|
|
permission_type: number;
|
|
}
|
|
|
|
/** 用户权限详情 */
|
|
export interface UserPermissionDetail {
|
|
user_id: number;
|
|
name: string;
|
|
phone: string;
|
|
role_level: number;
|
|
organization_id: number | null;
|
|
roles: Role[];
|
|
permission_codes: string[];
|
|
scopes: ScopeEntry[];
|
|
}
|
|
|
|
// ── 角色管理 ──
|
|
|
|
/** 获取所有角色列表 */
|
|
export async function fetchRoles(): Promise<Role[]> {
|
|
const res = await api.get<Role[]>('/roles');
|
|
return res.data ?? [];
|
|
}
|
|
|
|
/** 创建角色 */
|
|
export async function createRole(name: string, description?: string) {
|
|
return api.post('/roles', { name, description });
|
|
}
|
|
|
|
/** 更新角色 */
|
|
export async function updateRole(id: number, name: string, description?: string) {
|
|
return api.put(`/roles/${id}`, { name, description });
|
|
}
|
|
|
|
/** 删除角色 */
|
|
export async function deleteRole(id: number) {
|
|
return api.delete(`/roles/${id}`);
|
|
}
|
|
|
|
/** 设置角色权限(全量替换) */
|
|
export async function setRolePermissions(roleId: number, permissionCodes: string[]) {
|
|
return api.put(`/roles/${roleId}/permissions`, { permission_codes: permissionCodes });
|
|
}
|
|
|
|
// ── 权限码 ──
|
|
|
|
/** 获取所有权限码列表 */
|
|
export async function fetchPermissions(): Promise<PermissionItem[]> {
|
|
const res = await api.get<PermissionItem[]>('/permissions');
|
|
return res.data ?? [];
|
|
}
|
|
|
|
// ── 用户权限 ──
|
|
|
|
/** 获取用户权限详情 */
|
|
export async function fetchUserPermissions(userId: number): Promise<UserPermissionDetail> {
|
|
const res = await api.get<UserPermissionDetail>(`/users/${userId}/permissions`);
|
|
return res.data;
|
|
}
|
|
|
|
/** 设置用户角色(全量替换) */
|
|
export async function setUserRoles(userId: number, roleIds: number[]) {
|
|
return api.put(`/users/${userId}/roles`, { role_ids: roleIds });
|
|
}
|
|
|
|
/** 设置用户权限范围(全量替换) */
|
|
export async function setUserScopes(userId: number, scopes: ScopeEntry[]) {
|
|
return api.put(`/users/${userId}/scopes`, { scopes });
|
|
}
|
|
|
|
/** 获取用户权限范围 */
|
|
export async function fetchUserScopes(userId: number): Promise<ScopeEntry[]> {
|
|
const res = await api.get<ScopeEntry[]>(`/users/${userId}/scopes`);
|
|
return res.data ?? [];
|
|
}
|