menu-regions.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /** 已知区域:标题 → 展示元数据(顺序即首页优先展示序) */
  2. const REGION_DEFS = [
  3. {
  4. match: '市场部',
  5. key: 'market',
  6. title: '市场部',
  7. hint: '存储现场',
  8. tone: 'market'
  9. },
  10. {
  11. match: 'OA工作流',
  12. key: 'oa',
  13. title: 'OA工作流',
  14. hint: '审批协同',
  15. tone: 'oa'
  16. }
  17. ]
  18. const OTHER = {
  19. key: 'other',
  20. title: '其他',
  21. hint: '',
  22. tone: 'other'
  23. }
  24. export function resolveRegionMeta(groupTitle) {
  25. const name = String(groupTitle || '').trim()
  26. if (!name) return { ...OTHER }
  27. const found = REGION_DEFS.find((d) => d.match === name)
  28. if (found) {
  29. return {
  30. key: found.key,
  31. title: found.title,
  32. hint: found.hint,
  33. tone: found.tone
  34. }
  35. }
  36. return {
  37. key: `custom:${name}`,
  38. title: name,
  39. hint: '',
  40. tone: 'other'
  41. }
  42. }
  43. /**
  44. * 按 groupTitle 聚合成区域;已知区域按 REGION_DEFS 顺序,其余按首次出现顺序排在后面。
  45. * @param {Array<{ groupTitle?: string }>} menus
  46. */
  47. export function groupMenusByRegion(menus) {
  48. const list = Array.isArray(menus) ? menus : []
  49. if (!list.length) return []
  50. const buckets = new Map()
  51. for (const menu of list) {
  52. const meta = resolveRegionMeta(menu && menu.groupTitle)
  53. const key = meta.key
  54. if (!buckets.has(key)) {
  55. buckets.set(key, { ...meta, menus: [] })
  56. }
  57. buckets.get(key).menus.push(menu)
  58. }
  59. const ordered = []
  60. for (const def of REGION_DEFS) {
  61. if (buckets.has(def.key)) {
  62. ordered.push(buckets.get(def.key))
  63. buckets.delete(def.key)
  64. }
  65. }
  66. for (const region of buckets.values()) {
  67. ordered.push(region)
  68. }
  69. return ordered
  70. }