account.ts 1.71 KB
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),
  };
}