front/src/utils/organization-code.ts
zzh a703d1efdd feat: 顾问新客邀请链接与岗位职级字典
注册经 ?advisor= 绑定所属顾问(Account);用户抽屉展示邀请链接;岗位/职级走字典;去掉组织类型岗位。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-03 12:45:13 +08:00

62 lines
1.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import type { OrganizationTreeDto } from '@/api/rbac';
/** 组织类型 → 编码前缀(1 公司 / 2 部门 / 3 小组;岗位走员工档案字典,不作为组织类型) */
export const ORGANIZATION_TYPE_CODES: Record<number, string> = {
1: 'GS', // 公司
2: 'BM', // 部门
3: 'XZ', // 小组
};
export function getOrganizationTypeCode(type?: number | null): string {
return ORGANIZATION_TYPE_CODES[type ?? 0] ?? 'ORG';
}
export function flattenOrganizationTree(
nodes: OrganizationTreeDto[] | null | undefined,
): OrganizationTreeDto[] {
const list: OrganizationTreeDto[] = [];
const walk = (items: OrganizationTreeDto[]) => {
for (const item of items) {
list.push(item);
if (item.children?.length) {
walk(item.children);
}
}
};
if (nodes?.length) {
walk(nodes);
}
return list;
}
/**
* 按「组织类型编码 + 两位序号」生成唯一组织编码,如 BM01、GW02。
*/
export function generateOrganizationCode(
type?: number | null,
organizations: Array<{ code?: string | null }> = [],
): string {
const typeCode = getOrganizationTypeCode(type);
const pattern = new RegExp(`^${escapeRegExp(typeCode)}(\\d+)$`, 'i');
let maxSeq = 0;
for (const org of organizations) {
const code = org.code?.trim();
if (!code) continue;
const matched = code.match(pattern);
if (!matched) continue;
const seq = Number.parseInt(matched[1], 10);
if (Number.isFinite(seq) && seq > maxSeq) {
maxSeq = seq;
}
}
const next = maxSeq + 1;
const width = Math.max(2, String(next).length);
return `${typeCode}${String(next).padStart(width, '0')}`;
}
function escapeRegExp(value: string): string {
return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}