Files
smt-used-car-h5/common/tabbar.js
T
2026-03-11 11:15:33 +08:00

92 lines
2.3 KiB
JavaScript

/**
* TabBar 配置管理
* 根据用户角色动态显示不同的 tabbar
*
* 角色定义:
* - role=0: 供应商
* - role=1: 买家
* - role=2: 卖家
* - role_wp(有值): 供应商附加身份
*/
import { getUserRoles, ROLE_TYPES, isSupplier, getPrimaryRole } from './role.js'
// 所有可用的 tabbar 配置
export const tabBarConfig = [
{
pagePath: 'pages/index/index',
iconPath: '/static/image/home1.png',
selectedIconPath: '/static/image/home.png',
text: '首页',
// 首页:买家或卖家可见(供应商单独时不可见)
roles: [ROLE_TYPES.BUSINESS, ROLE_TYPES.PROCUREMENT]
},
{
pagePath: 'pages/index/publish',
iconPath: '/static/image/publish1.png',
selectedIconPath: '/static/image/publish.png',
text: '发布',
// 发布:卖家可见
roles: [ROLE_TYPES.PROCUREMENT]
},
{
pagePath: 'pages/index/publishPro',
iconPath: '/static/image/publish1.png',
selectedIconPath: '/static/image/publish.png',
text: '发布',
// 专业发布:有供应商身份可见
isSupplier: true
},
{
pagePath: 'pages/index/mine',
iconPath: '/static/image/mine1.png',
selectedIconPath: '/static/image/mine.png',
text: '我的',
roles: [] // 所有人可见
}
]
/**
* 获取可见的 tabbar 列表
* @returns {Array} 过滤后的 tabbar 列表
*/
export function getVisibleTabBar() {
const userRoles = getUserRoles()
return tabBarConfig.filter(item => {
// 检查 roles 条件
if (item.roles && item.roles.length > 0) {
if (!item.roles.some(r => userRoles.includes(r))) return false
}
// 检查 isSupplier 条件
if (item.isSupplier && !isSupplier()) return false
return true
})
}
/**
* 获取当前用户角色(主角色)
* @returns {string} 主角色名称
*/
export function getUserRole() {
return getPrimaryRole()
}
/**
* 获取当前 tabbar 的索引
* @param {string} currentPath - 当前页面路径
* @returns {number} tabbar 索引,-1 表示不在 tabbar 中
*/
export function getCurrentTabIndex(currentPath) {
const visibleTabs = getVisibleTabBar()
// 移除路径前缀的 / 进行匹配
const path = currentPath.startsWith('/') ? currentPath.slice(1) : currentPath
const index = visibleTabs.findIndex(item => {
const itemPath = item.pagePath.startsWith('/') ? item.pagePath.slice(1) : item.pagePath
return itemPath === path
})
return index
}