巴青农资商城

partnerRole.js 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /** 交易市场移动端允许登录的角色 role_key(供应商 103 / 承销商 104) */
  2. export const ALLOWED_PARTNER_ROLES = ['supplier', 'distributor']
  3. /** 平台管理角色(如 admin);无 supplier/distributor 时禁止登录 */
  4. export const BLOCKED_PLATFORM_ROLES = ['admin']
  5. export const PARTNER_ROLE_DENIED_MSG = '不是供应商和承销商,请联系管理员'
  6. /**
  7. * 将 getInfo 的 roles 转为字符串数组
  8. * @param {*} raw
  9. * @returns {string[]}
  10. */
  11. export function normalizeRoles(raw) {
  12. if (raw == null) {
  13. return []
  14. }
  15. if (Array.isArray(raw)) {
  16. return raw.map((r) => String(r || '').trim()).filter(Boolean)
  17. }
  18. if (typeof raw === 'object') {
  19. return Object.values(raw)
  20. .map((r) => String(r || '').trim())
  21. .filter(Boolean)
  22. }
  23. if (typeof raw === 'string') {
  24. const text = raw.trim()
  25. return text ? [text] : []
  26. }
  27. return []
  28. }
  29. function roleKey(role) {
  30. return String(role || '').trim().toLowerCase()
  31. }
  32. /**
  33. * roles 中须包含 supplier 或 distributor;仅 admin 等管理角色不可登录
  34. * @param {string[]|*} roles
  35. */
  36. export function hasPartnerRole(roles) {
  37. const list = normalizeRoles(roles)
  38. if (!list.length) {
  39. return false
  40. }
  41. const allowed = new Set(ALLOWED_PARTNER_ROLES.map((r) => r.toLowerCase()))
  42. return list.some((role) => allowed.has(roleKey(role)))
  43. }
  44. export function shouldDenyPartnerLogin(roles) {
  45. return !hasPartnerRole(roles)
  46. }
  47. /**
  48. * 当前登录用户的主身份:supplier | distributor | ''
  49. * 同时含 supplier 与 distributor 时固定为 supplier(界面与接口均走供应商)
  50. */
  51. export function getPrimaryPartnerRole(roles) {
  52. const list = normalizeRoles(roles).map((r) => roleKey(r))
  53. if (list.includes('supplier')) {
  54. return 'supplier'
  55. }
  56. if (list.includes('distributor')) {
  57. return 'distributor'
  58. }
  59. return ''
  60. }
  61. /** Tab 业务页是否展示供应商面板(双角色时不展示承销商) */
  62. export function isEffectiveSupplierRole(roles) {
  63. return getPrimaryPartnerRole(roles) === 'supplier'
  64. }
  65. /** Tab 业务页是否展示承销商面板 */
  66. export function isEffectiveDistributorRole(roles) {
  67. return getPrimaryPartnerRole(roles) === 'distributor'
  68. }
  69. /** 用户身份展示文案 */
  70. export function getPartnerIdentityLabel(roles) {
  71. const kind = getPrimaryPartnerRole(roles)
  72. if (kind === 'supplier') {
  73. return '供应商'
  74. }
  75. if (kind === 'distributor') {
  76. return '承销商'
  77. }
  78. return '--'
  79. }
  80. export function isSupplierRole(roles) {
  81. return normalizeRoles(roles).some((role) => roleKey(role) === 'supplier')
  82. }
  83. export function isDistributorRole(roles) {
  84. return normalizeRoles(roles).some((role) => roleKey(role) === 'distributor')
  85. }