Files
zkxw/web-admin/src/utils/oa-app-icons.ts
T

150 lines
4.8 KiB
TypeScript

/** OA 审批应用图标:ep:Icon|#bg 组合串 + 图片 URL 兼容 */
export type OaAppIconColorPair = { bg: string; fg: string }
/** Spec 锁定 8 组彩色,禁止灰 */
export const OA_APP_ICON_COLOR_PAIRS: readonly OaAppIconColorPair[] = [
{ bg: '#d1fae5', fg: '#059669' },
{ bg: '#cffafe', fg: '#0891b2' },
{ bg: '#dbeafe', fg: '#2563eb' },
{ bg: '#e0e7ff', fg: '#4f46e5' },
{ bg: '#ede9fe', fg: '#7c3aed' },
{ bg: '#fce7f3', fg: '#db2777' },
{ bg: '#ffedd5', fg: '#ea580c' },
{ bg: '#fef3c7', fg: '#d97706' }
] as const
export const OA_APP_DEFAULT_EP_ICON = 'Document'
export const OA_APP_DEFAULT_BG = OA_APP_ICON_COLOR_PAIRS[0].bg
export const OA_APP_DEFAULT_FG = OA_APP_ICON_COLOR_PAIRS[0].fg
/** 精选 Element Plus 图标名(PascalCase,与 @element-plus/icons-vue 导出一致) */
export const OA_APP_EP_ICON_NAMES = [
'Document',
'Calendar',
'Clock',
'Wallet',
'User',
'Tickets',
'Briefcase',
'Money',
'Van',
'OfficeBuilding',
'Message',
'Setting',
'CollectionTag',
'Checked',
'Present',
'Stamp',
'CreditCard',
'Notebook',
'Bell',
'Location'
] as const
export type OaAppEpIconName = (typeof OA_APP_EP_ICON_NAMES)[number]
export type OaAppIconEpResolved = {
kind: 'ep'
icon: OaAppEpIconName | string
bg: string
fg: string
}
export type OaAppIconImageResolved = {
kind: 'image'
src: string
}
export type OaAppIconResolved = OaAppIconEpResolved | OaAppIconImageResolved
/** 旧静态默认图(仅作图片路径兼容兜底,新随机不再使用) */
export const OA_APP_DEFAULT_ICON_URL = '/oa-app-icons/doc-orange.svg'
const env = typeof import.meta !== 'undefined' && (import.meta as any).env
const minioBaseUrl = String(env?.VITE_MINIO_BASE_URL || '').replace(/\/$/, '')
const BG_SET = new Set(OA_APP_ICON_COLOR_PAIRS.map((p) => p.bg.toLowerCase()))
const FG_BY_BG = Object.fromEntries(
OA_APP_ICON_COLOR_PAIRS.map((p) => [p.bg.toLowerCase(), p.fg])
) as Record<string, string>
const ICON_SET = new Set<string>(OA_APP_EP_ICON_NAMES)
export function isEpIconUrl(iconUrl?: string | null): boolean {
return String(iconUrl || '').trim().startsWith('ep:')
}
export function iconFgFromBg(bg?: string | null): string {
const key = String(bg || '').trim().toLowerCase()
return FG_BY_BG[key] || OA_APP_DEFAULT_FG
}
export function buildEpIconUrl(icon: string, bg: string): string {
const name = ICON_SET.has(icon) ? icon : OA_APP_DEFAULT_EP_ICON
const color = BG_SET.has(bg.toLowerCase()) ? bg.toLowerCase() : OA_APP_DEFAULT_BG
return `ep:${name}|${color}`
}
export function parseEpIconUrl(iconUrl?: string | null): {
icon: string
bg: string
fg: string
} {
const raw = String(iconUrl || '').trim()
if (!raw.startsWith('ep:')) {
return { icon: OA_APP_DEFAULT_EP_ICON, bg: OA_APP_DEFAULT_BG, fg: OA_APP_DEFAULT_FG }
}
const body = raw.slice(3)
const pipe = body.indexOf('|')
if (pipe < 0) {
return { icon: OA_APP_DEFAULT_EP_ICON, bg: OA_APP_DEFAULT_BG, fg: OA_APP_DEFAULT_FG }
}
let icon = body.slice(0, pipe).trim()
let bg = body.slice(pipe + 1).trim().toLowerCase()
if (!ICON_SET.has(icon)) icon = OA_APP_DEFAULT_EP_ICON
if (!BG_SET.has(bg)) bg = OA_APP_DEFAULT_BG
return { icon, bg, fg: iconFgFromBg(bg) }
}
/**
* 展示用图片完整地址(仅 kind=image 时使用)。
* ep: 串不要传入此函数拼 MinIO。
*/
export function resolveOaAppIconUrl(iconUrl?: string | null): string {
const u = (iconUrl || '').trim()
if (!u || isEpIconUrl(u)) return OA_APP_DEFAULT_ICON_URL
if (/^https?:\/\//i.test(u)) return u
if (u.startsWith('/')) return u
return minioBaseUrl ? `${minioBaseUrl}/${u.replace(/^\//, '')}` : u
}
export function resolveOaAppIcon(iconUrl?: string | null): OaAppIconResolved {
const u = (iconUrl || '').trim()
if (!u) {
const p = parseEpIconUrl(buildEpIconUrl(OA_APP_DEFAULT_EP_ICON, OA_APP_DEFAULT_BG))
return { kind: 'ep', icon: p.icon, bg: p.bg, fg: p.fg }
}
if (isEpIconUrl(u)) {
const p = parseEpIconUrl(u)
return { kind: 'ep', icon: p.icon, bg: p.bg, fg: p.fg }
}
return { kind: 'image', src: resolveOaAppIconUrl(u) }
}
export function pickRandomOaAppIconUrl(): string {
const icon = OA_APP_EP_ICON_NAMES[Math.floor(Math.random() * OA_APP_EP_ICON_NAMES.length)]
const pair = OA_APP_ICON_COLOR_PAIRS[Math.floor(Math.random() * OA_APP_ICON_COLOR_PAIRS.length)]
return buildEpIconUrl(icon, pair.bg)
}
/** 换底色:保留 icon;若当前非 ep,则用 Document */
export function recolorOaAppIconUrl(iconUrl: string | null | undefined, bg: string): string {
const cur = String(iconUrl || '').trim()
const icon = isEpIconUrl(cur) ? parseEpIconUrl(cur).icon : OA_APP_DEFAULT_EP_ICON
return buildEpIconUrl(icon, bg)
}
export function getDefaultOaAppIconUrl(): string {
return buildEpIconUrl(OA_APP_DEFAULT_EP_ICON, OA_APP_DEFAULT_BG)
}