115 lines
2.8 KiB
JavaScript
115 lines
2.8 KiB
JavaScript
/**
|
|
* Soul创业实验 - 自定义TabBar组件
|
|
* 根据后台配置动态显示/隐藏"找伙伴"按钮
|
|
*/
|
|
|
|
const app = getApp()
|
|
|
|
Component({
|
|
data: {
|
|
selected: 0,
|
|
color: '#8e8e93',
|
|
selectedColor: '#00CED1',
|
|
matchEnabled: false, // 找伙伴功能开关,默认关闭
|
|
list: [
|
|
{
|
|
pagePath: '/pages/index/index',
|
|
text: '首页',
|
|
iconType: 'home'
|
|
},
|
|
{
|
|
pagePath: '/pages/chapters/chapters',
|
|
text: '目录',
|
|
iconType: 'list'
|
|
},
|
|
{
|
|
pagePath: '/pages/match/match',
|
|
text: '找伙伴',
|
|
iconType: 'match',
|
|
isSpecial: true
|
|
},
|
|
{
|
|
pagePath: '/pages/my/my',
|
|
text: '我的',
|
|
iconType: 'user'
|
|
}
|
|
]
|
|
},
|
|
|
|
lifetimes: {
|
|
attached() {
|
|
this.loadFeatureConfig()
|
|
}
|
|
},
|
|
|
|
methods: {
|
|
// 加载功能配置
|
|
async loadFeatureConfig() {
|
|
try {
|
|
const res = await app.request({
|
|
url: '/api/db/config',
|
|
method: 'GET'
|
|
})
|
|
|
|
if (res && res.features) {
|
|
const matchEnabled = res.features.matchEnabled === true
|
|
this.setData({ matchEnabled }, () => {
|
|
// 配置加载完成后,根据当前路由设置选中状态
|
|
this.updateSelected()
|
|
})
|
|
|
|
// 如果当前在找伙伴页面,但功能已关闭,跳转到首页
|
|
if (!matchEnabled) {
|
|
const pages = getCurrentPages()
|
|
const currentPage = pages[pages.length - 1]
|
|
if (currentPage && currentPage.route === 'pages/match/match') {
|
|
wx.switchTab({ url: '/pages/index/index' })
|
|
}
|
|
}
|
|
}
|
|
} catch (error) {
|
|
console.log('TabBar加载功能配置失败:', error)
|
|
// 默认关闭找伙伴功能
|
|
this.setData({ matchEnabled: false }, () => {
|
|
this.updateSelected()
|
|
})
|
|
}
|
|
},
|
|
|
|
// 根据当前路由更新选中状态
|
|
updateSelected() {
|
|
const pages = getCurrentPages()
|
|
if (pages.length === 0) return
|
|
|
|
const currentPage = pages[pages.length - 1]
|
|
const route = currentPage.route
|
|
|
|
let selected = 0
|
|
const { matchEnabled } = this.data
|
|
|
|
// 根据路由匹配对应的索引
|
|
if (route === 'pages/index/index') {
|
|
selected = 0
|
|
} else if (route === 'pages/chapters/chapters') {
|
|
selected = 1
|
|
} else if (route === 'pages/match/match') {
|
|
selected = 2
|
|
} else if (route === 'pages/my/my') {
|
|
selected = matchEnabled ? 3 : 2
|
|
}
|
|
|
|
this.setData({ selected })
|
|
},
|
|
|
|
switchTab(e) {
|
|
const data = e.currentTarget.dataset
|
|
const url = data.path
|
|
const index = data.index
|
|
|
|
if (this.data.selected === index) return
|
|
|
|
wx.switchTab({ url })
|
|
}
|
|
}
|
|
})
|