account.ts
1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import type { OrgNode, UserPageParams } from '@/api';
import type { SearchPagination } from '@/components';
import type { FieldOption } from '@/components/formSchema';
export function toOrgCascaderOptions(nodes: OrgNode[]): FieldOption[] {
return nodes.map((node) => ({
value: node.id,
label: node.label,
children: node.children?.length ? toOrgCascaderOptions(node.children) : undefined,
}));
}
/** 根据组织 ID 反查级联选择器所需的完整路径。 */
export function findOrgPath(nodes: OrgNode[], targetId: number | undefined): number[] {
if (targetId === undefined) return [];
for (const node of nodes) {
if (node.id === targetId) return [node.id];
const childPath = findOrgPath(node.children ?? [], targetId);
if (childPath.length) return [node.id, ...childPath];
}
return [];
}
/** 为模块内的下拉选项统一补充“全部”。 */
export function withAllOption(options: FieldOption[]): FieldOption[] {
return [{ label: '全部', value: '' }, ...options];
}
export function createAccountPageParams(
values: Record<string, unknown>,
pagination: SearchPagination,
): UserPageParams {
const orgPath = values.orgId;
const orgId = Array.isArray(orgPath) ? orgPath.at(-1) : undefined;
return {
current: pagination.current,
size: pagination.size,
keyword: values.keyword as string | undefined,
mobile: values.mobile as string | undefined,
// “全部”对应空值,不附加组织筛选条件。
orgId: typeof orgId === 'number' ? orgId : undefined,
roleId: values.roleId === '' || values.roleId === undefined ? undefined : Number(values.roleId),
status: values.status === '' || values.status === undefined ? undefined : Number(values.status),
};
}