oa-app-icons.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /** OA 审批应用图标:与 web-admin/src/utils/oa-app-icons.ts 色对/解析对齐 */
  2. import { MINIO_BASE_URL } from './config.js'
  3. export const OA_APP_ICON_COLOR_PAIRS = [
  4. { bg: '#d1fae5', fg: '#059669' },
  5. { bg: '#cffafe', fg: '#0891b2' },
  6. { bg: '#dbeafe', fg: '#2563eb' },
  7. { bg: '#e0e7ff', fg: '#4f46e5' },
  8. { bg: '#ede9fe', fg: '#7c3aed' },
  9. { bg: '#fce7f3', fg: '#db2777' },
  10. { bg: '#ffedd5', fg: '#ea580c' },
  11. { bg: '#fef3c7', fg: '#d97706' }
  12. ]
  13. export const OA_APP_DEFAULT_EP_ICON = 'Document'
  14. export const OA_APP_DEFAULT_BG = OA_APP_ICON_COLOR_PAIRS[0].bg
  15. export const OA_APP_DEFAULT_FG = OA_APP_ICON_COLOR_PAIRS[0].fg
  16. /** 精选 Element Plus 图标名(PascalCase,与 @element-plus/icons-vue 导出一致) */
  17. export const OA_APP_EP_ICON_NAMES = [
  18. 'Document',
  19. 'Calendar',
  20. 'Clock',
  21. 'Wallet',
  22. 'User',
  23. 'Tickets',
  24. 'Briefcase',
  25. 'Money',
  26. 'Van',
  27. 'OfficeBuilding',
  28. 'Message',
  29. 'Setting',
  30. 'CollectionTag',
  31. 'Checked',
  32. 'Present',
  33. 'Stamp',
  34. 'CreditCard',
  35. 'Notebook',
  36. 'Bell',
  37. 'Location'
  38. ]
  39. const BG_SET = new Set(OA_APP_ICON_COLOR_PAIRS.map((p) => p.bg.toLowerCase()))
  40. const FG_BY_BG = Object.fromEntries(
  41. OA_APP_ICON_COLOR_PAIRS.map((p) => [p.bg.toLowerCase(), p.fg])
  42. )
  43. const ICON_SET = new Set(OA_APP_EP_ICON_NAMES)
  44. export function isEpIconUrl(iconUrl) {
  45. return String(iconUrl || '').trim().startsWith('ep:')
  46. }
  47. export function iconFgFromBg(bg) {
  48. const key = String(bg || '').trim().toLowerCase()
  49. return FG_BY_BG[key] || OA_APP_DEFAULT_FG
  50. }
  51. export function buildEpIconUrl(icon, bg) {
  52. const name = ICON_SET.has(icon) ? icon : OA_APP_DEFAULT_EP_ICON
  53. const color = BG_SET.has(String(bg).toLowerCase())
  54. ? String(bg).toLowerCase()
  55. : OA_APP_DEFAULT_BG
  56. return `ep:${name}|${color}`
  57. }
  58. export function parseEpIconUrl(iconUrl) {
  59. const raw = String(iconUrl || '').trim()
  60. if (!raw.startsWith('ep:')) {
  61. return { icon: OA_APP_DEFAULT_EP_ICON, bg: OA_APP_DEFAULT_BG, fg: OA_APP_DEFAULT_FG }
  62. }
  63. const body = raw.slice(3)
  64. const pipe = body.indexOf('|')
  65. if (pipe < 0) {
  66. return { icon: OA_APP_DEFAULT_EP_ICON, bg: OA_APP_DEFAULT_BG, fg: OA_APP_DEFAULT_FG }
  67. }
  68. let icon = body.slice(0, pipe).trim()
  69. let bg = body.slice(pipe + 1).trim().toLowerCase()
  70. if (!ICON_SET.has(icon)) icon = OA_APP_DEFAULT_EP_ICON
  71. if (!BG_SET.has(bg)) bg = OA_APP_DEFAULT_BG
  72. return { icon, bg, fg: iconFgFromBg(bg) }
  73. }
  74. /**
  75. * 旧版静态图标存 `/oa-app-icons/*.svg`(web-admin/public)。
  76. * 小程序/H5 需走 `static/oa-app-icons/`,否则 5173 下 404。
  77. * 上传图存 MinIO 相对 key(如 `202608/11/xxx.png`),需拼 MINIO_BASE_URL。
  78. */
  79. export function resolveOaAppIconImageSrc(iconUrl) {
  80. const u = String(iconUrl || '').trim()
  81. if (!u || isEpIconUrl(u)) return ''
  82. if (/^https?:\/\//i.test(u)) return u
  83. if (u.startsWith('/oa-app-icons/')) {
  84. return '/static' + u
  85. }
  86. if (u.startsWith('/static/')) return u
  87. const minioBase = String(MINIO_BASE_URL || '').replace(/\/$/, '')
  88. // 同域反代路径:本机 H5 无 /minio 代理时,改写为绝对 MinIO 基址
  89. if (u.startsWith('/minio/zkxw/')) {
  90. if (minioBase && /^https?:\/\//i.test(minioBase)) {
  91. return `${minioBase}/${u.slice('/minio/zkxw/'.length)}`
  92. }
  93. return u
  94. }
  95. if (u.startsWith('/')) return u
  96. return minioBase ? `${minioBase}/${u.replace(/^\//, '')}` : u
  97. }
  98. export function resolveOaAppIcon(iconUrl) {
  99. const u = String(iconUrl || '').trim()
  100. if (!u) {
  101. const p = parseEpIconUrl(buildEpIconUrl(OA_APP_DEFAULT_EP_ICON, OA_APP_DEFAULT_BG))
  102. return { kind: 'ep', icon: p.icon, bg: p.bg, fg: p.fg }
  103. }
  104. if (isEpIconUrl(u)) {
  105. const p = parseEpIconUrl(u)
  106. return { kind: 'ep', icon: p.icon, bg: p.bg, fg: p.fg }
  107. }
  108. return { kind: 'image', src: resolveOaAppIconImageSrc(u) }
  109. }
  110. const EP_ICON_SVG_INNER = {
  111. Document:
  112. '<path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"/><polyline points="14 2 14 8 20 8"/><line x1="8" y1="13" x2="16" y2="13"/><line x1="8" y1="17" x2="16" y2="17"/>',
  113. Calendar:
  114. '<rect x="3" y="4" width="18" height="18" rx="2"/><line x1="16" y1="2" x2="16" y2="6"/><line x1="8" y1="2" x2="8" y2="6"/><line x1="3" y1="10" x2="21" y2="10"/>',
  115. Clock:
  116. '<circle cx="12" cy="12" r="9"/><polyline points="12 7 12 12 15 14"/>',
  117. Wallet:
  118. '<path d="M20 7H4a2 2 0 0 0-2 2v10a2 2 0 0 0 2 2h16a2 2 0 0 0 2-2V9a2 2 0 0 0-2-2z"/><path d="M16 12h4"/><path d="M2 7l2-3h12l2 3"/>',
  119. User:
  120. '<path d="M20 21v-2a4 4 0 0 0-4-4H8a4 4 0 0 0-4 4v2"/><circle cx="12" cy="7" r="4"/>',
  121. Tickets:
  122. '<rect x="3" y="7" width="18" height="10" rx="2"/><line x1="3" y1="11" x2="21" y2="11"/>',
  123. Briefcase:
  124. '<rect x="2" y="7" width="20" height="14" rx="2"/><path d="M16 7V5a2 2 0 0 0-2-2h-4a2 2 0 0 0-2 2v2"/><line x1="2" y1="12" x2="22" y2="12"/>',
  125. Money:
  126. '<circle cx="12" cy="12" r="9"/><path d="M12 7v10"/><path d="M15 9.5c0-1.1-1.3-2-3-2s-3 .9-3 2 1.3 2 3 2 3 .9 3 2-1.3 2-3 2-3-.9-3-2"/>',
  127. Van:
  128. '<path d="M3 17h1a3 3 0 0 0 6 0h4a3 3 0 0 0 6 0h1"/><path d="M3 17V8a2 2 0 0 1 2-2h8l6 4v7"/><circle cx="7.5" cy="17" r="1.5"/><circle cx="16.5" cy="17" r="1.5"/>',
  129. OfficeBuilding:
  130. '<path d="M4 21V5a1 1 0 0 1 1-1h9v17"/><path d="M14 9h6v12"/><line x1="8" y1="9" x2="10" y2="9"/><line x1="8" y1="13" x2="10" y2="13"/><line x1="8" y1="17" x2="10" y2="17"/>',
  131. Message:
  132. '<path d="M21 11.5a8.5 8.5 0 0 1-12.4 7.5L3 21l1.9-5.1A8.5 8.5 0 1 1 21 11.5z"/>',
  133. Setting:
  134. '<circle cx="12" cy="12" r="3"/><path d="M12 1v2M12 21v2M4.2 4.2l1.4 1.4M18.4 18.4l1.4 1.4M1 12h2M21 12h2M4.2 19.8l1.4-1.4M18.4 5.6l1.4-1.4"/>',
  135. CollectionTag:
  136. '<path d="M20.6 13.4l-7.2 7.2a2 2 0 0 1-2.8 0L3 13V3h10l7.6 7.6a2 2 0 0 1 0 2.8z"/><circle cx="7.5" cy="7.5" r="1"/>',
  137. Checked:
  138. '<path d="M22 11.1V12a10 10 0 1 1-5.9-9.1"/><polyline points="22 4 12 14.01 9 11"/>',
  139. Present:
  140. '<polyline points="20 12 20 22 4 22 4 12"/><rect x="2" y="7" width="20" height="5"/><line x1="12" y1="22" x2="12" y2="7"/><path d="M12 7H7.5a2.5 2.5 0 0 1 0-5C11 2 12 7 12 7z"/><path d="M12 7h4.5a2.5 2.5 0 0 0 0-5C13 2 12 7 12 7z"/>',
  141. Stamp:
  142. '<path d="M5 21h14"/><path d="M12 3a4 4 0 0 0-4 4c0 2 1.5 3 4 5 2.5-2 4-3 4-5a4 4 0 0 0-4-4z"/><path d="M6 15h12v3H6z"/>',
  143. CreditCard:
  144. '<rect x="2" y="5" width="20" height="14" rx="2"/><line x1="2" y1="10" x2="22" y2="10"/>',
  145. Notebook:
  146. '<path d="M4 4h12a2 2 0 0 1 2 2v14H6a2 2 0 0 0-2 2V4z"/><line x1="8" y1="8" x2="14" y2="8"/><line x1="8" y1="12" x2="14" y2="12"/>',
  147. Bell:
  148. '<path d="M18 8A6 6 0 0 0 6 8c0 7-3 9-3 9h18s-3-2-3-9"/><path d="M13.7 21a2 2 0 0 1-3.4 0"/>',
  149. Location:
  150. '<path d="M12 21s7-4.5 7-11a7 7 0 1 0-14 0c0 6.5 7 11 7 11z"/><circle cx="12" cy="10" r="2"/>'
  151. }
  152. const BASE64_CHARS =
  153. 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
  154. /** UTF-8 字符串 → base64;不依赖 btoa / Buffer(微信 appservice 两者皆无) */
  155. function utf8ToBase64(str) {
  156. const utf8 = unescape(encodeURIComponent(String(str || '')))
  157. if (typeof btoa === 'function') {
  158. return btoa(utf8)
  159. }
  160. let out = ''
  161. for (let i = 0; i < utf8.length; i += 3) {
  162. const a = utf8.charCodeAt(i)
  163. const b = i + 1 < utf8.length ? utf8.charCodeAt(i + 1) : 0
  164. const c = i + 2 < utf8.length ? utf8.charCodeAt(i + 2) : 0
  165. const n = (a << 16) | (b << 8) | c
  166. out += BASE64_CHARS[(n >> 18) & 63]
  167. out += BASE64_CHARS[(n >> 12) & 63]
  168. out += i + 1 < utf8.length ? BASE64_CHARS[(n >> 6) & 63] : '='
  169. out += i + 2 < utf8.length ? BASE64_CHARS[n & 63] : '='
  170. }
  171. return out
  172. }
  173. /** 生成小程序 <image> 可用的 SVG data-URI(base64,兼容微信) */
  174. export function epIconDataUri(icon, fg) {
  175. const name = ICON_SET.has(icon) ? icon : OA_APP_DEFAULT_EP_ICON
  176. const stroke = fg || OA_APP_DEFAULT_FG
  177. const inner = EP_ICON_SVG_INNER[name] || EP_ICON_SVG_INNER.Document
  178. const svg =
  179. `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="${stroke}" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">${inner}</svg>`
  180. // WeChat <image> 对 charset=utf-8 + encodeURIComponent 偶发不兼容,统一 base64
  181. return 'data:image/svg+xml;base64,' + utf8ToBase64(svg)
  182. }