1:用户管理页面优化 2:角色管理页面优化 3:分配角色权限优化

This commit is contained in:
2025-10-11 12:19:34 +08:00
parent 65e2d4b92f
commit 96da469bef
13 changed files with 382 additions and 335 deletions
+9 -9
View File
@@ -281,16 +281,16 @@ const Welcome: React.FC = () => {
// 订单状态:1. 待支付 2. 已取消 3. 支付成功 4. 设备执行中 5. 素材上传中 6. 素材上传完成 7. 用户已下载完成 8. 退款中 9. 退款成功 10. 退款失败
// Tag 颜色使用 antd v4 预设色
const STATUS_OPTIONS: { value: number; label: string; color?: string }[] = [
{ value: 1, label: '待支付', color: 'gold' },
{ value: 2, label: '已取消' },
{ value: 3, label: '支付成功', color: 'green' },
{ value: 4, label: '设备执行中', color: 'blue' },
{ value: 0, label: '待支付' },
{ value: 1, label: '排队中', color: 'gold' },
{ value: 2, label: '已取消', color: 'red'},
{ value: 3, label: '设备执行完成', color: 'green' },
{ value: 4, label: '正在服务中', color: 'blue' },
{ value: 5, label: '素材上传中', color: 'geekblue' },
{ value: 6, label: '素材上传完成', color: 'cyan' },
{ value: 7, label: '用户已下载完成', color: 'purple' },
{ value: 8, label: '退款中', color: 'orange' },
{ value: 9, label: '退款成功', color: 'lime' },
{ value: 10, label: '退款失败', color: 'red' },
{ value: 6, label: '任务完成', color: 'purple' },
{ value: 7, label: '退款中', color: 'orange' },
{ value: 8, label: '退款成功', color: 'lime' },
{ value: 9, label: '退款失败', color: 'red' },
];
const PAY_TYPE_OPTIONS: { value: number; label: string; color?: string }[] = [
@@ -1,4 +1,4 @@
import React, { useEffect, useState } from 'react';
import React, { useEffect, useState } from 'react';
import { Modal, Tree, message } from 'antd';
import { roleApi } from '@/services/role';
import { buildTree } from '@/utils/tree';
@@ -29,20 +29,20 @@ const AssignAuth: React.FC<AssignAuthProps> = ({ roleId, tenantId, open, onClose
const handleOk = async () => {
if (!roleId) return;
const hide = message.loading('淇濆瓨涓?..');
const hide = message.loading('保存中...');
try {
await roleApi.assignMenu({ roleId, menuIds: checkedKeys });
message.success('鏉冮檺鍒嗛厤鎴愬姛');
message.success('权限分配成功');
onClose();
} catch (e) {
message.error('鏉冮檺鍒嗛厤澶辫触');
message.error('权限分配失败');
} finally {
hide();
}
};
return (
<Modal title="鏉冮檺鍒嗛厤" open={open} destroyOnClose onOk={handleOk} onCancel={onClose}>
<Modal title="权限分配" open={open} destroyOnClose onOk={handleOk} onCancel={onClose}>
<Tree
defaultExpandAll
checkable
@@ -56,4 +56,3 @@ const AssignAuth: React.FC<AssignAuthProps> = ({ roleId, tenantId, open, onClose
export default AssignAuth;
@@ -16,7 +16,6 @@ const RolePage: React.FC = () => {
const [current, setCurrent] = useState<RoleItem | undefined>(undefined);
// 以租户为主:通过 SearchBarPlus 内置租户选择器
const columns = [
{ dataIndex: 'index', valueType: 'indexBorder', width: 48 },
{ title: '租户', dataIndex: 'tenantId', width: 160, search: false, render: (_: any, r: RoleItem) => (r as any)?.tenantName || (r as any)?.tenantId || '-' },
@@ -148,3 +147,4 @@ const RolePage: React.FC = () => {
};
export default RolePage;
@@ -80,10 +80,12 @@ const TenantTreeTable: React.FC<TenantTreeTableProps> = ({ data, loading, onAddC
render: (_: any, rec: TenantTreeNode | undefined) => {
const record = (rec || ({} as TenantTreeNode));
const hasId = !!(record as any)?.tenantId;
const typeVal = (record as any)?.tenantType;
const isDepartment = String(typeVal || '').toUpperCase() === 'DEPARTMENT';
return (
<Space>
<a onClick={() => hasId && onEdit?.(record)}>编辑</a>
{onAddChild ? <a onClick={() => hasId && onAddChild?.(record)}>新增下级</a> : null}
{onAddChild && !isDepartment ? <a onClick={() => hasId && onAddChild?.(record)}>新增下级</a> : null}
<Popconfirm title={`确认删除租户[${record?.tenantName ?? ''}]?`} onConfirm={() => hasId && onDelete?.(record)}>
<a style={{ color: '#1677ff' }}>删除</a>
</Popconfirm>
@@ -36,131 +36,101 @@ const TenantManage: React.FC = () => {
};
const fetchTree = useCallback(async () => {
setLoading(true);
setLoading(true);
try {
// 1) 获取可访问租户树(作为根集合);无则回退当前租户
const res: any = await tenantApi.getAccessibleTenants();
let list: TenantTreeNode[] = [];
if (typeof res?.success !== "undefined") list = res.success ? (res.data || []) : [];
else if (Array.isArray(res)) list = res as TenantTreeNode[];
if (!list || list.length === 0) {
const cur: any = await tenantApi.getCurrentTenant();
if (cur?.success && cur?.data?.tenantId) {
list = [{ tenantId: cur.data.tenantId, tenantName: cur.data.tenantName } as any];
} else {
const stats: any = await tenantApi.getScopeStats();
const id = stats?.data?.currentTenantId;
if (id) list = [{ tenantId: id, tenantName: id } as any];
}
}
// 2) 补齐根节点缺失的必要字段
try {
const res: any = await tenantApi.getAccessibleTenants();
let list: TenantTreeNode[] = [];
if (typeof res?.success !== "undefined") {
list = res.success ? (res.data || []) : [];
} else if (Array.isArray(res)) {
list = res as TenantTreeNode[];
}
if (!list || list.length === 0) {
// 回退:至少返回当前租户
const cur: any = await tenantApi.getCurrentTenant();
if (cur?.success && cur?.data?.tenantId) {
list = [{ tenantId: cur.data.tenantId, tenantName: cur.data.tenantName } as any];
} else {
const stats: any = await tenantApi.getScopeStats();
const id = stats?.data?.currentTenantId;
if (id) list = [{ tenantId: id, tenantName: id } as any];
}
}
// 若只有根节点且无子节点,尝试加载一层子租户以展示层级
if (list && list.length === 1 && !Array.isArray((list[0] as any).children)) {
await Promise.all((list || []).map(async (node: any) => {
if (!node) return;
try {
const parentId = (list[0] as any).tenantId;
const resPage: any = await tenantApi.getTenantList({ parentTenantId: parentId, status: (statusFilter === 'all' ? undefined : statusFilter) as any, current: 1, pageSize: 1000 } as any);
const children = resPage?.data?.data || [];
if (children && children.length) {
(list[0] as any).children = children.map((c: any) => ({ tenantId: c.tenantId, tenantName: c.tenantName, tenantType: c.tenantType, status: c.status, contactPerson: c.contactPerson, contactPhone: c.contactPhone }));
if (node.tenantType == null || node.status == null || node.contactPerson == null || node.contactPhone == null) {
const detail: any = await tenantApi.getTenantDetail(node.tenantId);
const d = detail?.data || {};
if (node.tenantType == null) node.tenantType = d.tenantType;
if (node.status == null) node.status = d.status;
if (node.contactPerson == null) node.contactPerson = d.contactPerson;
if (node.contactPhone == null) node.contactPhone = d.contactPhone;
}
} catch {}
}
// 补充每个根的一层直接子租户,便于默认展开时可见
try {
await Promise.all(
(list || []).map(async (node: any) => {
if (!node) return;
// 顶层节点缺失类型/状态/联系人信息时,补充详情
try {
if (node.tenantType == null || node.status == null || node.contactPerson == null || node.contactPhone == null) {
const detail: any = await tenantApi.getTenantDetail(node.tenantId);
const d = detail?.data || {};
if (node.tenantType == null) node.tenantType = d.tenantType;
if (node.status == null) node.status = d.status;
if (node.contactPerson == null) node.contactPerson = d.contactPerson;
if (node.contactPhone == null) node.contactPhone = d.contactPhone;
}
} catch {}
// 确保至少补一层孩子,方便默认展开后可见
if (!Array.isArray(node.children) || node.children.length === 0) {
const resp: any = await tenantApi.getTenantList({ parentTenantId: node.tenantId, status: (statusFilter === 'all' ? undefined : statusFilter) as any, current: 1, pageSize: 1000 } as any);
const children = resp?.data?.data || [];
if (children && children.length) {
node.children = children.map((c: any) => ({ tenantId: c.tenantId, tenantName: c.tenantName, tenantType: c.tenantType, status: c.status, contactPerson: c.contactPerson, contactPhone: c.contactPhone }));
}
}
})
);
} catch {}
// 递进加载至第三级,保证三级部门可见
const loadChildrenFor = async (nodes?: any[]): Promise<any[]> => {
const resultChildren: any[] = [];
if (!Array.isArray(nodes)) return resultChildren;
await Promise.all(nodes.map(async (node: any) => {
if (!node) return;
// 再次兜底补全基础信息
}));
} catch {}
// 3) 以 BFS 方式递归加载所有层级子租户(直到叶子)
const loadChildren = async (pid: string): Promise<TenantTreeNode[]> => {
const resp: any = await tenantApi.getTenantList({ parentTenantId: pid, status: (statusFilter === 'all' ? undefined : statusFilter) as any, current: 1, pageSize: 1000 } as any);
const items = resp?.data?.data || [];
return (items || []).map((c: any) => ({
tenantId: c.tenantId,
tenantName: c.tenantName,
tenantType: c.tenantType,
status: c.status,
contactPerson: c.contactPerson,
contactPhone: c.contactPhone,
} as any));
};
let current: any[] = list as any[];
while (Array.isArray(current) && current.length) {
const next: any[] = [];
await Promise.all(current.map(async (node: any) => {
if (!node?.tenantId) return;
if (!Array.isArray(node.children) || node.children.length === 0) {
try {
if (node.tenantType == null || node.status == null || node.contactPerson == null || node.contactPhone == null) {
const detail: any = await tenantApi.getTenantDetail(node.tenantId);
const d = detail?.data || {};
if (node.tenantType == null) node.tenantType = d.tenantType;
if (node.status == null) node.status = d.status;
if (node.contactPerson == null) node.contactPerson = d.contactPerson;
if (node.contactPhone == null) node.contactPhone = d.contactPhone;
}
const children = await loadChildren(node.tenantId);
if (children && children.length) node.children = children as any;
} catch {}
const needLoad = !Array.isArray(node.children) || node.children.length === 0;
if (needLoad) {
try {
const resp: any = await tenantApi.getTenantList({ parentTenantId: node.tenantId, status: (statusFilter === 'all' ? undefined : statusFilter) as any, current: 1, pageSize: 1000 } as any);
const children = resp?.data?.data || [];
if (children && children.length) {
node.children = children.map((c: any) => ({ tenantId: c.tenantId, tenantName: c.tenantName, tenantType: c.tenantType, status: c.status, contactPerson: c.contactPerson, contactPhone: c.contactPhone }));
}
} catch {}
}
if (Array.isArray(node.children) && node.children.length) next.push(...node.children);
}));
current = next;
}
// 4) 按状态筛选(全部/启用/停用)
const filterTreeByStatus = (nodes?: any[]): any[] => {
if (statusFilter === 'all') return Array.isArray(nodes) ? nodes : [];
const want = statusFilter;
const walk = (arr?: any[]): any[] => {
if (!Array.isArray(arr)) return [];
const res: any[] = [];
for (const n of arr) {
const v = (typeof (n as any)?.status === 'boolean') ? ((n as any).status ? 1 : 0) : (n as any)?.status;
if (v === want) {
const item: any = { ...n };
if (Array.isArray((n as any).children)) item.children = walk((n as any).children);
res.push(item);
} else if (Array.isArray((n as any).children)) {
const sub = walk((n as any).children);
if (sub.length) res.push(...sub);
}
if (Array.isArray(node.children)) resultChildren.push(...node.children);
}));
return resultChildren;
}
return res;
};
return walk(nodes);
};
try {
const level2 = await loadChildrenFor(list);
await loadChildrenFor(level2);
} catch {}
// 按状态筛选(仅当选择启用或停用时)
const filterTreeByStatus = (nodes?: any[]): any[] => {
if (statusFilter === 'all') return Array.isArray(nodes) ? nodes : [];
const want = statusFilter;
const walk = (arr?: any[]): any[] => {
if (!Array.isArray(arr)) return [];
const res: any[] = [];
for (const n of arr) {
const v = (typeof (n as any)?.status === 'boolean') ? ((n as any).status ? 1 : 0) : (n as any)?.status;
if (v === want) {
const item: any = { ...n };
if (Array.isArray((n as any).children)) item.children = walk((n as any).children);
res.push(item);
} else if (Array.isArray((n as any).children)) {
const sub = walk((n as any).children);
if (sub.length) res.push(...sub);
}
}
return res;
};
return walk(nodes);
};
const finalList = filterTreeByStatus(list);
setTreeCache(list);
setData(finalList);
} catch (e) {
message.error("获取租户数据失败");
} finally { setLoading(false); }
}, [statusFilter]);
const finalList = filterTreeByStatus(list);
setTreeCache(list);
setData(finalList);
} catch (e) {
message.error("获取租户数据失败");
} finally { setLoading(false); }
}, [statusFilter]);
useEffect(() => { fetchTree(); }, [fetchTree]);
@@ -1,5 +1,5 @@
import React from 'react';
import { ModalForm, ProForm, ProFormRadio, ProFormSelect, ProFormText, ProFormTextArea } from '@ant-design/pro-components';
import { ModalForm, ProForm, ProFormRadio, ProFormSelect, ProFormText, ProFormTextArea, ProFormDependency } from '@ant-design/pro-components';
import { systemApi, UserItem } from '@/services/system';
import TenantSelector from '@/components/ui/TenantSelector';
@@ -14,13 +14,26 @@ export interface UserFormProps {
}
const UserForm: React.FC<UserFormProps> = ({ visible, title, initialValues, onVisibleChange, onFinish, showCompany = true }) => {
// 统一初始值:补充 type 默认值;将 roles/roleId 映射为 roleIds(字符串数组)
const mergedInitial = React.useMemo(() => {
const base: any = { type: 'BACKEND', ...(initialValues || {}) };
// 兼容后端仅返回 tenantId 的场景:用于初始化“所属租户”和角色下拉
if (!base.company && base.tenantId) base.company = base.tenantId;
if (!base.roleIds) {
if (Array.isArray(base.roles)) base.roleIds = base.roles.map((r: any) => String(r.id));
else if (typeof base.roleId === 'string') base.roleIds = String(base.roleId).split(',').map((s) => String(s).trim()).filter(Boolean);
}
if (base.roleIds) base.roleIds = (Array.isArray(base.roleIds) ? base.roleIds : [base.roleIds]).map((v: any) => String(v));
return base;
}, [initialValues]);
return (
<ModalForm<UserItem & { company?: string; type?: string }>
title={title}
visible={visible}
onVisibleChange={onVisibleChange}
onFinish={onFinish}
initialValues={{ type: 'BACKEND', ...(initialValues || {}) }}
initialValues={mergedInitial}
modalProps={{ destroyOnClose: true }}
>
<ProForm.Group>
@@ -28,15 +41,39 @@ const UserForm: React.FC<UserFormProps> = ({ visible, title, initialValues, onVi
<ProFormText name="nickname" label="用户昵称" rules={[{ required: true, message: '用户昵称不能为空' }]} />
<ProFormText name="mobile" label="手机号" rules={[{ required: true, message: '手机号不能为空' }]} />
</ProForm.Group>
<ProFormRadio.Group name="sex" label="性别" options={[{ label: '男', value: 0 }, { label: '女', value: 1 }]} />
<ProFormSelect name="roleIds" label="角色" mode="multiple" request={systemApi.roles} placeholder="选择角色" />
<ProFormRadio.Group name="sex" label="性别" options={[{ label: '女', value: 0 }, { label: '男', value: 1 }]} />
{/* 先选择租户,再选择角色 */}
{showCompany ? (
<TenantSelector nameTenant="company" labelTenant="所属租户" hideIncludeChildren />
<TenantSelector
nameTenant="company"
labelTenant="所属租户"
hideIncludeChildren
rulesTenant={[{ required: true, message: '请选择所属租户' }]}
suppressAutoSelect
disableFetchCurrentTenant
/>
) : null}
<ProFormDependency name={["company"]}>
{({ company }) => (
<ProFormSelect
key={company || 'none'}
params={{ tenantId: company || (initialValues as any)?.company || (initialValues as any)?.tenantId }}
name="roleIds"
label="角色"
mode="multiple"
request={async (p: any) => systemApi.roles(p?.tenantId)}
placeholder={company ? '选择角色' : '请先选择所属租户'}
disabled={!company}
rules={[{ required: true, message: '请选择角色(如无可选项,请先到角色管理创建该租户的角色)' }]}
fieldProps={{ showSearch: true, optionFilterProp: 'label' }}
/>
)}
</ProFormDependency>
<ProFormTextArea name="remark" label="备注" />
{/* 类型固定为系统用户,默认也会按 BACKEND 新建 */}
{/* 类型固定为系统用户,默认也会以 BACKEND 新建 */}
<ProFormText name="type" label="用户类型" initialValue="BACKEND" disabled hidden />
</ModalForm>
);