| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- /** 交易市场移动端允许登录的角色 role_key(供应商 103 / 承销商 104) */
- export const ALLOWED_PARTNER_ROLES = ['supplier', 'distributor']
- /** 平台管理角色(如 admin);无 supplier/distributor 时禁止登录 */
- export const BLOCKED_PLATFORM_ROLES = ['admin']
- export const PARTNER_ROLE_DENIED_MSG = '不是供应商和承销商,请联系管理员'
- /**
- * 将 getInfo 的 roles 转为字符串数组
- * @param {*} raw
- * @returns {string[]}
- */
- export function normalizeRoles(raw) {
- if (raw == null) {
- return []
- }
- if (Array.isArray(raw)) {
- return raw.map((r) => String(r || '').trim()).filter(Boolean)
- }
- if (typeof raw === 'object') {
- return Object.values(raw)
- .map((r) => String(r || '').trim())
- .filter(Boolean)
- }
- if (typeof raw === 'string') {
- const text = raw.trim()
- return text ? [text] : []
- }
- return []
- }
- function roleKey(role) {
- return String(role || '').trim().toLowerCase()
- }
- /**
- * roles 中须包含 supplier 或 distributor;仅 admin 等管理角色不可登录
- * @param {string[]|*} roles
- */
- export function hasPartnerRole(roles) {
- const list = normalizeRoles(roles)
- if (!list.length) {
- return false
- }
- const allowed = new Set(ALLOWED_PARTNER_ROLES.map((r) => r.toLowerCase()))
- return list.some((role) => allowed.has(roleKey(role)))
- }
- export function shouldDenyPartnerLogin(roles) {
- return !hasPartnerRole(roles)
- }
- /**
- * 当前登录用户的主身份:supplier | distributor | ''
- * 同时含 supplier 与 distributor 时固定为 supplier(界面与接口均走供应商)
- */
- export function getPrimaryPartnerRole(roles) {
- const list = normalizeRoles(roles).map((r) => roleKey(r))
- if (list.includes('supplier')) {
- return 'supplier'
- }
- if (list.includes('distributor')) {
- return 'distributor'
- }
- return ''
- }
- /** Tab 业务页是否展示供应商面板(双角色时不展示承销商) */
- export function isEffectiveSupplierRole(roles) {
- return getPrimaryPartnerRole(roles) === 'supplier'
- }
- /** Tab 业务页是否展示承销商面板 */
- export function isEffectiveDistributorRole(roles) {
- return getPrimaryPartnerRole(roles) === 'distributor'
- }
- /** 用户身份展示文案 */
- export function getPartnerIdentityLabel(roles) {
- const kind = getPrimaryPartnerRole(roles)
- if (kind === 'supplier') {
- return '供应商'
- }
- if (kind === 'distributor') {
- return '承销商'
- }
- return '--'
- }
- export function isSupplierRole(roles) {
- return normalizeRoles(roles).some((role) => roleKey(role) === 'supplier')
- }
- export function isDistributorRole(roles) {
- return normalizeRoles(roles).some((role) => roleKey(role) === 'distributor')
- }
|