utils.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import i18n from '@/locales'
  2. import { PERMISSION as POLICY_MAP } from '@/constants/permission'
  3. import { RESOURCES_MAP, SERVICES_MAP } from './constants'
  4. /**
  5. * 翻译权限展示名
  6. * 1. 优先查找 SERVICES_MAP RESOURCES_MAP 指定的名
  7. * 2. 查找 i18n 中 `${service}.${resourceKey}` 的名称
  8. * 3. 查找 i18n 中 `dictionary.${resourceKey}` 的名称
  9. * 4. 都找不到时,返回 resourceKey
  10. * 建议:ce 中涉及的,可在 dictionary 和 SERVICES_MAP、RESOURCES_MAP 添加,ee 和 oem 涉及的,添加至 该模块`${service}.${resourceKey}` 即可
  11. */
  12. export function transformLabel (config, resourceKey, service) {
  13. if (config) {
  14. if (config.i18n) return i18n.t(config.i18n)
  15. return config.label
  16. } else if (service && i18n.te(`${service}.${resourceKey}`)) {
  17. return i18n.t(`${service}.${resourceKey}`)
  18. }
  19. return i18n.te(`dictionary.${resourceKey}`) ? i18n.t(`dictionary.${resourceKey}`) : resourceKey
  20. }
  21. /**
  22. * 根据定义的 POLICY_MAP 生成 POLICY_GROUPS 供设置权限表单使用
  23. *
  24. * @returns { Array }
  25. */
  26. export function genPolicyGroups () {
  27. const temp = {}
  28. // 存放找到的resources,保证唯一性
  29. const resources = []
  30. for (const key in POLICY_MAP) {
  31. const item = POLICY_MAP[key]
  32. // 将 POLICY_MAP 每项的 array value 第一项拿出来作为 key
  33. // 如果没有声明初始化进行
  34. if (!temp[item[0]]) {
  35. temp[item[0]] = {
  36. label: transformLabel(SERVICES_MAP[item[0]], item[0]),
  37. service: item[0],
  38. resources: [],
  39. }
  40. }
  41. // 由验证<resource>为唯一项,改为验证<service>_<resource>
  42. if (!resources.includes(`${item[0]}_${item[1]}`)) {
  43. resources.push(`${item[0]}_${item[1]}`)
  44. const res = {
  45. label: transformLabel(RESOURCES_MAP[item[1]], item[1], item[0]),
  46. resource: item[1],
  47. }
  48. if (RESOURCES_MAP[item[1]] && RESOURCES_MAP[item[1]].extras) {
  49. res.extras = RESOURCES_MAP[item[1]].extras
  50. }
  51. temp[item[0]].resources.push(res)
  52. }
  53. }
  54. const ret = Object.values(temp).map(item => {
  55. return {
  56. ...item,
  57. resources: [...item.resources, { label: i18n.t('iam.others'), resource: '*' }],
  58. }
  59. })
  60. return ret
  61. }