unis-oa/frontend/src/utils/personLabel.ts

124 lines
5.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/**
* 人员引用的读法:**库里存的是登录账号**(口径见后端 `app/service/personref.py`),
* 这一层负责把账号翻回"姓名(账号)"给人看。
*
* 浏览器手上没有人员档案,所以:
*
* * 缓存里有 → 直接给 `姓名(账号)`;
* * 缓存里没有 → 先原样摊出账号(一个不撒谎的占位),同时把它塞进待查清单,一次批量问
* `/org/persons/label`;
* * 回包填进缓存之后 `version` 自增,读过它的 `computed` 自动重算,界面上名字自己浮出来。
*
* 只有账号一种写法。O2OA 那种 `部门\姓名\身份名` 的身份 DN 不再是引用口径:它是部门路径
* 与姓名的派生值,改名、挪层级、调岗都会让它漂,末段还会被同步改写成姓名。所以这一层
* **不从字符串里切姓名和部门**:认不出的人原样显示,档案问到之前也不编名字。
*/
import { ref, shallowRef } from 'vue'
import { uniqTexts } from '@/utils/legacyCompat'
import * as orgAPI from '@/api/org'
/** 人员引用 -> 展示文本(`姓名(账号)`)。 */
const labelCache = ref<Record<string, string>>({})
/** 人员引用 -> 档案片段,`personParts()` 与要部门的调用方读这一份。 */
const partCache = ref<Record<string, { name: string; account: string; unitName: string }>>({})
/** 已经问过的值:不再重复发请求,问不出来的就一辈子原样显示(后端说了没有这个人)。 */
const requested = new Set<string>()
let batch: string[] = []
let timer: number | null = null
/** 读缓存的 computed 都碰一下它,缓存回填时才会在同一轮里跟着重算。 */
const version = shallowRef(0)
/** 值本身该按哪个账号认:账号就是引用本身,这里只做去空白(没有别的形状要翻译)。 */
export function personRefOf(value: unknown): string {
return String(value ?? '').trim()
}
/** 一次批量问姓名:合并同一轮里所有待查的值,避免一格一次请求。 */
export function requestPersonLabels(values: Iterable<unknown>): void {
const wanted: string[] = []
for (const item of values) {
const raw = personRefOf(item)
if (!raw || requested.has(raw) || labelCache.value[raw]) continue
requested.add(raw)
wanted.push(raw)
}
if (!wanted.length) return
batch.push(...wanted)
if (timer !== null) window.clearTimeout(timer)
timer = window.setTimeout(flush, 60)
}
async function flush(): Promise<void> {
timer = null
// 不用 [...new Set()]:老引擎的 Set 不收迭代器参数,也没有迭代器,攒下的一整批会全丢
const wanted = uniqTexts(batch)
batch = []
if (!wanted.length) return
try {
const rows = await orgAPI.personLabels(wanted)
for (const row of rows) {
const name = row.display_name || row.person_name || ''
const account = row.account || personRefOf(row.value)
labelCache.value[row.value] = name ? `${name}(${account})` : account || row.value
partCache.value[row.value] = {
name: name || account,
account,
unitName: row.unit_name || '',
}
}
version.value += 1
} catch {
// 问不到就继续原样显示账号;把错误抛到渲染层只会把整页表单一起弄没
wanted.forEach((value) => requested.delete(value))
}
}
/** 一个人的展示文本;缓存没到之前先摊账号,不编名字。 */
export function personLabel(value: unknown): string {
void version.value
const raw = personRefOf(value)
if (!raw) return ''
requestPersonLabels([raw])
return labelCache.value[raw] ?? raw
}
/** 格子里的值可能是单值也可能是数组:能翻的翻,翻不动的原样留着。 */
export function personLabelsOf(value: unknown): string {
const list = Array.isArray(value) ? value : value === null || value === undefined || value === '' ? [] : [value]
requestPersonLabels(list)
return list.map((item) => personLabel(item)).filter(Boolean).join('、')
}
/**
* 表格与徽章里的那一格:认得出就念「姓名(账号)」,档案问到之前先摊账号,空值给占位。
*
* 库里人员引用那一列存的就是登录账号,界面上所有"这一格是谁"都从这里读,
* 不再从字符串里切姓名——以前那种切法只在身份 DN 还当引用用的时候成立。
*/
export function personText(value: unknown, placeholder = '--'): string {
return personLabel(value) || placeholder
}
/**
* 人员引用的可读片段。
*
* 只有账号、拿不到人员档案时(流程试跑「换个人发起」就是这种)先用账号凑一份系统带出的料,
* 部门留空;档案问到了就读档案,部门与姓名都是当前值。库里那一格存的就是账号,所以这里
* 没有"从字符串里切出部门"这一步——那种切法在身份 DN 漂过之后就全是错的。
*/
export function personParts(value: unknown): {
unitPath: string
unitName: string
name: string
account: string
} {
void version.value
const account = personRefOf(value)
if (!account) return { unitPath: '', unitName: '', name: '', account: '' }
const hit = partCache.value[account]
if (hit) return { unitPath: hit.unitName, unitName: hit.unitName, name: hit.name, account: hit.account }
requestPersonLabels([account])
return { unitPath: '', unitName: '', name: account, account }
}