You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
348 lines
11 KiB
348 lines
11 KiB
Component({
|
|
/**
|
|
* 组件的属性列表
|
|
*/
|
|
properties: {
|
|
},
|
|
|
|
/**
|
|
* 组件的初始数据
|
|
*/
|
|
data: {
|
|
selected: 'index',
|
|
show: true, // 控制tab-bar显示状态
|
|
// 记录tabBar数据,用于匹配
|
|
tabBarItems: [
|
|
{ key: 'index', route: 'pages/index/index' },
|
|
{ key: 'buyer', route: 'pages/buyer/index' },
|
|
{ key: 'seller', route: 'pages/seller/index' },
|
|
{ key: 'profile', route: 'pages/profile/index' }
|
|
]
|
|
},
|
|
|
|
/**
|
|
* 组件的方法列表
|
|
*/
|
|
methods: {
|
|
// 切换tab页面的方法 - 增强版,改进状态管理
|
|
switchTab(e) {
|
|
try {
|
|
const data = e.currentTarget.dataset
|
|
let url = data.path
|
|
const key = data.key
|
|
|
|
console.log('点击tab项:', key, '原始路径:', url)
|
|
|
|
// 确保路径格式正确 - 移除可能的前缀斜杠
|
|
if (url.startsWith('/')) {
|
|
url = url.substring(1)
|
|
}
|
|
|
|
console.log('修正后路径:', url)
|
|
|
|
// 更新全局数据 - 先更新全局状态,确保状态一致性
|
|
const app = getApp()
|
|
if (app && app.globalData) {
|
|
app.globalData.currentTab = key
|
|
console.log('同步选中状态到全局数据:', key)
|
|
}
|
|
|
|
// 立即更新UI选中状态
|
|
this.setData({
|
|
selected: key
|
|
})
|
|
|
|
// 无论跳转到哪个页面,先确保用户身份被正确设置
|
|
if (key === 'buyer' || key === 'seller') {
|
|
const userId = wx.getStorageSync('userId');
|
|
if (userId) {
|
|
// 更新用户类型
|
|
let users = wx.getStorageSync('users');
|
|
if (typeof users !== 'object' || users === null) {
|
|
users = {};
|
|
}
|
|
if (!users[userId]) {
|
|
users[userId] = {};
|
|
}
|
|
users[userId].type = key;
|
|
wx.setStorageSync('users', users);
|
|
|
|
// 更新标签
|
|
let tags = wx.getStorageSync('tags');
|
|
if (typeof tags !== 'object' || tags === null) {
|
|
tags = {};
|
|
}
|
|
tags[userId] = tags[userId] || [];
|
|
tags[userId] = tags[userId].filter(tag => !tag.startsWith('身份:'));
|
|
tags[userId].push(`身份:${key}`);
|
|
wx.setStorageSync('tags', tags);
|
|
}
|
|
}
|
|
|
|
// 特殊处理:点击货源页面时检查登录状态和入驻状态
|
|
if (key === 'seller' && url === 'pages/seller/index') {
|
|
console.log('点击货源页面,开始检查登录状态和入驻状态...');
|
|
|
|
// 首先检查登录状态
|
|
const userId = wx.getStorageSync('userId');
|
|
const userInfo = wx.getStorageSync('userInfo');
|
|
|
|
if (!userId || !userInfo) {
|
|
console.log('用户未登录,跳转到登录或入驻页面');
|
|
wx.navigateTo({
|
|
url: '/pages/settlement/index',
|
|
success: (res) => {
|
|
console.log('跳转到入驻页面成功:', res);
|
|
},
|
|
fail: (err) => {
|
|
console.error('跳转到入驻页面失败:', err);
|
|
this.navigateToTabPage(url);
|
|
}
|
|
});
|
|
} else {
|
|
// 用户已登录,检查合作商状态
|
|
const settlementStatus = wx.getStorageSync('settlement_status');
|
|
console.log('检查合作商状态:', settlementStatus);
|
|
|
|
if (!settlementStatus || settlementStatus === '') {
|
|
console.log('合作商状态不存在,用户未入驻');
|
|
wx.navigateTo({
|
|
url: '/pages/settlement/index'
|
|
});
|
|
} else if (settlementStatus === 'underreview') {
|
|
console.log('合作商状态为审核中,跳转到货源页面显示审核中内容');
|
|
this.navigateToTabPage(url);
|
|
} else if (settlementStatus === 'approved' || settlementStatus === 'incooperation') {
|
|
console.log('合作商状态为审核通过,正常跳转到货源页面');
|
|
this.navigateToTabPage(url);
|
|
} else {
|
|
console.log('其他状态,跳转到入驻页面');
|
|
wx.navigateTo({
|
|
url: '/pages/settlement/index'
|
|
});
|
|
}
|
|
}
|
|
} else {
|
|
// 其他tab页面正常跳转
|
|
this.navigateToTabPage(url)
|
|
}
|
|
} catch (e) {
|
|
console.error('switchTab方法执行错误:', e)
|
|
}
|
|
},
|
|
|
|
// 跳转到tab页面的通用方法
|
|
navigateToTabPage(url) {
|
|
console.log('使用switchTab跳转到tabbar页面:', url)
|
|
wx.switchTab({
|
|
url: '/' + url,
|
|
success: (res) => {
|
|
console.log('switchTab成功:', url, res)
|
|
},
|
|
fail: (err) => {
|
|
console.error('switchTab失败:', url, err)
|
|
console.log('尝试使用reLaunch跳转...')
|
|
wx.reLaunch({
|
|
url: '/' + url,
|
|
success: (res) => {
|
|
console.log('reLaunch成功:', url, res)
|
|
},
|
|
fail: (err) => {
|
|
console.error('reLaunch也失败:', url, err)
|
|
}
|
|
})
|
|
}
|
|
})
|
|
},
|
|
|
|
// 强制更新选中状态
|
|
forceUpdateSelectedState(key) {
|
|
try {
|
|
this.setData({
|
|
selected: key
|
|
})
|
|
console.log('强制更新选中状态:', key)
|
|
// 再次同步全局数据,确保双向一致性
|
|
const app = getApp()
|
|
if (app && app.globalData) {
|
|
app.globalData.currentTab = key
|
|
}
|
|
} catch (e) {
|
|
console.error('强制更新选中状态失败:', e)
|
|
}
|
|
},
|
|
|
|
// 恢复到之前的状态
|
|
restorePreviousState() {
|
|
try {
|
|
const app = getApp()
|
|
if (app && app.globalData && app.globalData.currentTab) {
|
|
this.setData({
|
|
selected: app.globalData.currentTab
|
|
})
|
|
console.log('恢复选中状态到:', app.globalData.currentTab)
|
|
}
|
|
} catch (e) {
|
|
console.error('恢复状态失败:', e)
|
|
}
|
|
},
|
|
|
|
// 跳转到鸡蛋估价页面 - 现已改为未开放页面
|
|
goToEvaluatePage() {
|
|
wx.navigateTo({
|
|
url: '/pages/notopen/index'
|
|
})
|
|
},
|
|
|
|
// 从全局数据同步状态的方法 - 增强版
|
|
syncFromGlobalData() {
|
|
try {
|
|
const app = getApp()
|
|
const pages = getCurrentPages()
|
|
let currentRoute = ''
|
|
|
|
// 获取当前页面路由
|
|
if (pages && pages.length > 0) {
|
|
const currentPage = pages[pages.length - 1]
|
|
currentRoute = currentPage.route
|
|
console.log('当前页面路由:', currentRoute)
|
|
}
|
|
|
|
// 根据当前页面路由确定应该选中的tab
|
|
let shouldSelect = 'index'
|
|
for (let item of this.data.tabBarItems) {
|
|
if (item.route === currentRoute) {
|
|
shouldSelect = item.key
|
|
break
|
|
}
|
|
}
|
|
|
|
// 检查全局数据中是否有控制tab-bar显示的状态
|
|
let showTabBar = true
|
|
if (app && app.globalData && typeof app.globalData.showTabBar !== 'undefined') {
|
|
showTabBar = app.globalData.showTabBar
|
|
}
|
|
|
|
// 更新全局数据和本地数据,确保一致性
|
|
if (app && app.globalData) {
|
|
app.globalData.currentTab = shouldSelect
|
|
}
|
|
|
|
// 只在状态不一致时更新,避免频繁重绘
|
|
const updates = {}
|
|
if (this.data.selected !== shouldSelect) {
|
|
updates.selected = shouldSelect
|
|
console.log('根据当前页面路由更新选中状态:', shouldSelect)
|
|
}
|
|
|
|
if (this.data.show !== showTabBar) {
|
|
updates.show = showTabBar
|
|
console.log('更新tab-bar显示状态:', showTabBar)
|
|
}
|
|
|
|
if (Object.keys(updates).length > 0) {
|
|
this.setData(updates)
|
|
}
|
|
} catch (e) {
|
|
console.error('从全局数据同步状态失败:', e)
|
|
}
|
|
},
|
|
|
|
// 开始监听全局tab-bar显示状态变化
|
|
startTabBarStatusListener() {
|
|
// 使用定时器定期检查全局状态
|
|
this.tabBarStatusTimer = setInterval(() => {
|
|
try {
|
|
const app = getApp()
|
|
if (app && app.globalData && typeof app.globalData.showTabBar !== 'undefined') {
|
|
const showTabBar = app.globalData.showTabBar
|
|
if (this.data.show !== showTabBar) {
|
|
this.setData({
|
|
show: showTabBar
|
|
})
|
|
console.log('tab-bar显示状态更新:', showTabBar)
|
|
}
|
|
}
|
|
} catch (e) {
|
|
console.error('监听tab-bar状态失败:', e)
|
|
}
|
|
}, 100) // 每100ms检查一次,确保响应迅速
|
|
}
|
|
},
|
|
|
|
// 组件生命周期
|
|
lifetimes: {
|
|
// 组件挂载时执行
|
|
attached() {
|
|
console.log('tabBar组件挂载')
|
|
// 初始化时从全局数据同步一次状态,使用较长延迟确保页面完全加载
|
|
setTimeout(() => {
|
|
this.syncFromGlobalData()
|
|
}, 100)
|
|
|
|
// 监听全局tab-bar显示状态变化
|
|
this.startTabBarStatusListener()
|
|
},
|
|
|
|
// 组件被移动到节点树另一个位置时执行
|
|
moved() {
|
|
console.log('tabBar组件移动')
|
|
// 组件移动时重新同步状态
|
|
this.syncFromGlobalData()
|
|
},
|
|
|
|
// 组件卸载时执行
|
|
detached() {
|
|
console.log('tabBar组件卸载')
|
|
// 清理定时器
|
|
if (this.tabBarStatusTimer) {
|
|
clearInterval(this.tabBarStatusTimer)
|
|
}
|
|
}
|
|
},
|
|
|
|
// 页面生命周期
|
|
pageLifetimes: {
|
|
// 页面显示时执行
|
|
show() {
|
|
console.log('页面显示')
|
|
const pages = getCurrentPages()
|
|
if (pages && pages.length > 0) {
|
|
const currentPage = pages[pages.length - 1]
|
|
// 对于profile页面,使用更长的延迟以避免服务器错误影响
|
|
if (currentPage.route === 'pages/profile/index') {
|
|
setTimeout(() => {
|
|
this.syncFromGlobalData()
|
|
// 额外确保profile页面状态正确
|
|
setTimeout(() => {
|
|
this.forceUpdateSelectedState('profile')
|
|
}, 200)
|
|
}, 200)
|
|
} else {
|
|
// 其他页面使用适当延迟
|
|
setTimeout(() => {
|
|
this.syncFromGlobalData()
|
|
}, 50)
|
|
}
|
|
}
|
|
|
|
// 页面显示时默认显示tab-bar,除非有全局控制
|
|
const app = getApp()
|
|
if (app && app.globalData && typeof app.globalData.showTabBar === 'undefined') {
|
|
this.setData({
|
|
show: true
|
|
})
|
|
}
|
|
},
|
|
|
|
// 页面隐藏时执行
|
|
hide() {
|
|
console.log('页面隐藏')
|
|
// 页面隐藏时保存当前选中状态到全局
|
|
const app = getApp()
|
|
if (app && app.globalData) {
|
|
app.globalData.currentTab = this.data.selected
|
|
}
|
|
}
|
|
}
|
|
})
|