64 lines
2.2 KiB
TypeScript
64 lines
2.2 KiB
TypeScript
/**
|
||
* 病例 / 接诊页通用展示辅助
|
||
*
|
||
* - maskPhone:手机号脱敏 138****1234
|
||
* - formatGender:0/1 → 女/男
|
||
* - formatPeriod:morning/afternoon → 上午/下午
|
||
* - joinArray:数组 → 顿号拼接,非数组 → toString
|
||
* - makeTextOf:工厂,传入 diag ref/getter,返回闭包后的 textOf 函数
|
||
*
|
||
* 翻译规则(textOf):诊单字典字段由后端 `AppointmentLogic::enrichDiagnosisLabels`
|
||
* 预翻译为 `<field>_text`,前端优先读 `*_text`;找不到时回退原值。
|
||
*/
|
||
|
||
import type { Ref } from 'vue'
|
||
|
||
export function maskPhone(phone?: string | null): string {
|
||
if (!phone) return '—'
|
||
if (phone.length < 11) return phone
|
||
return phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2')
|
||
}
|
||
|
||
export function formatGender(g?: number | null): string {
|
||
if (g === 1) return '男'
|
||
if (g === 0) return '女'
|
||
return '—'
|
||
}
|
||
|
||
export function formatPeriod(period?: string | null): string {
|
||
if (period === 'morning') return '上午'
|
||
if (period === 'afternoon') return '下午'
|
||
return '全天'
|
||
}
|
||
|
||
export function joinArray(arr: unknown): string {
|
||
if (!arr) return ''
|
||
if (Array.isArray(arr)) return arr.filter(Boolean).join('、')
|
||
return String(arr)
|
||
}
|
||
|
||
/**
|
||
* 创建闭包了具体 diag 的 textOf 取值器。
|
||
* 同时支持 `Ref` / `() => any` / 普通对象,避免在调用方处反复样板。
|
||
*/
|
||
export function makeTextOf(diagSource: Ref<any> | (() => any) | Record<string, any>) {
|
||
const resolve = (): Record<string, any> => {
|
||
if (typeof diagSource === 'function') {
|
||
return (diagSource as () => any)() || {}
|
||
}
|
||
if (diagSource && typeof diagSource === 'object' && 'value' in diagSource) {
|
||
return ((diagSource as Ref<any>).value ?? {}) as Record<string, any>
|
||
}
|
||
return (diagSource as Record<string, any>) || {}
|
||
}
|
||
|
||
return function textOf(field: string): string {
|
||
const d = resolve()
|
||
const t = d[field + '_text']
|
||
if (t !== undefined && t !== null && t !== '') return String(t)
|
||
const raw = d[field]
|
||
if (raw === undefined || raw === null || raw === '') return ''
|
||
return Array.isArray(raw) ? raw.join('、') : String(raw)
|
||
}
|
||
}
|