dictLabels.js 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /**
  2. * 业务字典的唯一内存权威(票 07)。
  3. *
  4. * 分层(规格 §4):内存 → 持久化快照 → 原码。这里只管内存这一层与翻译:
  5. * 快照的编解码在 `dictSnapshot.js`,读写 localStorage 在 `services/cockpitDict.js`。
  6. * **不内置第二份可漂移词表** —— 命中就出中文,没命中由调用方决定回原码。
  7. *
  8. * 既有设施词表(泵站类型等)不在此列,迁移另票。
  9. */
  10. export const DICT_TYPES = Object.freeze({
  11. riskLevel: 'risk_level',
  12. riskColor: 'risk_color',
  13. hazardType: 'hazard_type',
  14. hazardLevel: 'hazard_level',
  15. hazardSource: 'hazard_source',
  16. hazardStatus: 'hazard_status',
  17. })
  18. export const DICT_TYPE_LIST = Object.freeze(Object.values(DICT_TYPES))
  19. const field = (row, camel, snake) => {
  20. if (!row || typeof row !== 'object') return ''
  21. const value = row[camel] !== undefined ? row[camel] : row[snake]
  22. return value === null || value === undefined ? '' : String(value).trim()
  23. }
  24. /** 字典端点的行 → `{ dictValue: dictLabel }`;缺值行不收,一行都收不出就回 null。 */
  25. export function toDictLabels(raw) {
  26. const rows = Array.isArray(raw) ? raw
  27. : (raw && Array.isArray(raw.rows) ? raw.rows : (raw && Array.isArray(raw.data) ? raw.data : []))
  28. const result = {}
  29. for (const row of rows) {
  30. const value = field(row, 'dictValue', 'dict_value')
  31. const label = field(row, 'dictLabel', 'dict_label')
  32. if (value && label) result[value] = label
  33. }
  34. return Object.keys(result).length ? result : null
  35. }
  36. /** 纯标签表(键非空、值为非空字符串),避免把任意对象当字典。 */
  37. export function isLabelMap(value) {
  38. if (!value || typeof value !== 'object' || Array.isArray(value)) return false
  39. const entries = Object.entries(value)
  40. return entries.length > 0 && entries.every(([key, label]) => key !== '' && typeof label === 'string' && label !== '')
  41. }
  42. /** 已注入的字典;冻结后整体替换,避免调用方原地改内存。 */
  43. let memory = Object.freeze({})
  44. /**
  45. * 注入一类字典(行数组或已解析的标签表都收)。
  46. *
  47. * 空表**不**覆盖已有值:接口某次返回空列表时,应当继续用上一份可用字典
  48. * (规格「接口失败保留快照」的同一口径),而不是把界面打回原码。
  49. */
  50. export function setDictLabels(type, value) {
  51. if (!DICT_TYPE_LIST.includes(type)) return false
  52. const map = toDictLabels(value) || (isLabelMap(value) ? { ...value } : null)
  53. if (!map) return false
  54. memory = { ...memory, [type]: map }
  55. return true
  56. }
  57. /** 当前已注入的字典表(快照写入用;回的是浅拷贝)。 */
  58. export function dictSnapshotSource() {
  59. return { ...memory }
  60. }
  61. /** 仅测试用:回到「字典未取到」。 */
  62. export function resetDictLabelsForTest() {
  63. memory = Object.freeze({})
  64. }
  65. export function hasDictLabels(type) {
  66. return !!memory[type]
  67. }
  68. /** 码 → 中文;这类字典没取到、或没有这个码,一律回空串,由调用方决定回什么原码。 */
  69. export function dictLabel(type, value) {
  70. const code = value === null || value === undefined ? '' : String(value).trim()
  71. if (!code) return ''
  72. const map = memory[type]
  73. return map && Object.hasOwn(map, code) ? map[code] : ''
  74. }