153 lines
5.4 KiB
JavaScript
153 lines
5.4 KiB
JavaScript
// tab-bar/index.js — 三端固定,无身份切换
|
||
const app = getApp();
|
||
import { getTabRole } from '../utils/primary-role.js';
|
||
|
||
const roleCfg = {
|
||
normal: {
|
||
name: '点单老板',
|
||
tabList: [
|
||
{ pagePath: 'pages/index/index', text: '商城', iconPath: '/images/home.png', selectedIconPath: '/images/home.png' },
|
||
{ pagePath: 'pages/category/category', text: '分类', iconPath: '/images/category.png', selectedIconPath: '/images/category.png' },
|
||
{ pagePath: 'pages/messages/messages', text: '消息', iconPath: '/images/messages.png', selectedIconPath: '/images/messages.png' },
|
||
{ pagePath: 'pages/mine/mine', text: '我的', iconPath: '/images/mine.png', selectedIconPath: '/images/mine.png' },
|
||
],
|
||
},
|
||
dashou: {
|
||
name: '接单员',
|
||
tabList: [
|
||
{ pagePath: 'pages/accept-order/accept-order', text: '接单池', iconPath: '/images/order-pool.png', selectedIconPath: '/images/order-pool.png' },
|
||
{ pagePath: 'pages/fighter-orders/fighter-orders', text: '订单', iconPath: '/images/orders.png', selectedIconPath: '/images/orders.png' },
|
||
{ pagePath: 'pages/messages/messages', text: '消息', iconPath: '/images/messages.png', selectedIconPath: '/images/messages.png' },
|
||
{ pagePath: 'pages/fighter/fighter', text: '我的', iconPath: '/images/mine.png', selectedIconPath: '/images/mine.png' },
|
||
],
|
||
},
|
||
shangjia: {
|
||
name: '商家',
|
||
tabList: [
|
||
{ pagePath: 'pages/merchant-home/merchant-home', text: '首页', iconPath: '/images/home.png', selectedIconPath: '/images/home.png' },
|
||
{ pagePath: 'pages/messages/messages', text: '消息', iconPath: '/images/messages.png', selectedIconPath: '/images/messages.png' },
|
||
{ pagePath: 'pages/merchant-orders/merchant-orders', text: '订单', iconPath: '/images/orders.png', selectedIconPath: '/images/orders.png' },
|
||
{ pagePath: 'pages/merchant/merchant', text: '我的', iconPath: '/images/mine.png', selectedIconPath: '/images/mine.png' },
|
||
],
|
||
},
|
||
};
|
||
|
||
const TAB_BAR_NATIVE_PAGES = new Set([
|
||
'pages/accept-order/accept-order',
|
||
'pages/fighter-orders/fighter-orders',
|
||
'pages/messages/messages',
|
||
'pages/fighter/fighter',
|
||
]);
|
||
|
||
Component({
|
||
data: {
|
||
selectedIndex: 0,
|
||
tabList: [],
|
||
currentRole: 'normal',
|
||
badgeText: '',
|
||
},
|
||
|
||
lifetimes: {
|
||
attached() {
|
||
this.refresh();
|
||
this._restoreBadgeFromCache();
|
||
this._badgeListener = (data) => {
|
||
if (data.badgeText !== undefined) {
|
||
this.setData({ badgeText: data.badgeText });
|
||
}
|
||
};
|
||
this._roleListener = () => this.refresh();
|
||
app.on('tabBarBadgeChanged', this._badgeListener);
|
||
app.on('currentRoleChanged', this._roleListener);
|
||
},
|
||
detached() {
|
||
if (this._badgeListener) app.off('tabBarBadgeChanged', this._badgeListener);
|
||
if (this._roleListener) app.off('currentRoleChanged', this._roleListener);
|
||
},
|
||
},
|
||
|
||
pageLifetimes: {
|
||
show() {
|
||
this.refresh();
|
||
this._restoreBadgeFromCache();
|
||
this._syncUnreadWithDebounce();
|
||
},
|
||
},
|
||
|
||
methods: {
|
||
_restoreBadgeFromCache() {
|
||
const mm = app.globalData.messageManager || {};
|
||
let badgeText = mm.tabBarBadgeText || '';
|
||
if (!badgeText) {
|
||
try {
|
||
badgeText = wx.getStorageSync('tabBarMessageBadge') || '';
|
||
} catch (e) {}
|
||
}
|
||
if (!badgeText && mm.unreadTotal > 0) {
|
||
badgeText = mm.unreadTotal > 99 ? '99+' : String(mm.unreadTotal);
|
||
}
|
||
if (!badgeText) {
|
||
try {
|
||
const saved = wx.getStorageSync('messageUnreadTotal');
|
||
if (saved > 0) badgeText = saved > 99 ? '99+' : String(saved);
|
||
} catch (e) {}
|
||
}
|
||
if (badgeText && badgeText !== this.data.badgeText) {
|
||
this.setData({ badgeText });
|
||
}
|
||
if (app.restoreTabBarBadge) app.restoreTabBarBadge();
|
||
},
|
||
|
||
_syncUnreadWithDebounce() {
|
||
if (this._syncTimer) clearTimeout(this._syncTimer);
|
||
this._syncTimer = setTimeout(() => {
|
||
if (app.globalData.goEasyConnection) {
|
||
app.globalData.goEasyConnection.autoReconnect = true;
|
||
}
|
||
if (app.startImWhenReady) {
|
||
app.startImWhenReady();
|
||
} else {
|
||
if (app.ensureConnection) app.ensureConnection();
|
||
if (app.loadConversations) app.loadConversations();
|
||
}
|
||
}, 200);
|
||
},
|
||
|
||
refresh() {
|
||
const token = wx.getStorageSync('token');
|
||
let role = getTabRole(app);
|
||
if (!token) {
|
||
role = 'normal';
|
||
}
|
||
app.globalData.currentRole = role;
|
||
wx.setStorageSync('currentRole', role);
|
||
|
||
let cfg = roleCfg[role];
|
||
if (!cfg) {
|
||
cfg = roleCfg.normal;
|
||
role = 'normal';
|
||
}
|
||
|
||
const list = cfg.tabList.map((v, i) => ({ ...v, index: i }));
|
||
const pages = getCurrentPages();
|
||
const curRoute = pages.length ? pages[pages.length - 1].route : '';
|
||
let idx = list.findIndex((t) => t.pagePath === curRoute);
|
||
if (idx === -1) idx = 0;
|
||
this.setData({ currentRole: role, tabList: list, selectedIndex: idx });
|
||
},
|
||
|
||
switchTab(e) {
|
||
const path = e.currentTarget.dataset.path;
|
||
const idx = e.currentTarget.dataset.index;
|
||
if (idx === this.data.selectedIndex) return;
|
||
const url = '/' + path;
|
||
// tabBar 注册页用 switchTab;其余页用 reLaunch(redirectTo 对 tab 页会失败)
|
||
if (TAB_BAR_NATIVE_PAGES.has(path)) {
|
||
wx.switchTab({ url });
|
||
} else {
|
||
wx.reLaunch({ url });
|
||
}
|
||
},
|
||
},
|
||
});
|