Files
soul/miniprogram/custom-tab-bar/index.js

83 lines
2.1 KiB
JavaScript
Raw Normal View History

/**
* Soul创业实验 - 自定义TabBar组件
* 实现中间突出的"找伙伴"按钮
*/
Component({
data: {
selected: 0,
color: '#8e8e93',
selectedColor: '#00CED1',
list: [
{
pagePath: '/pages/index/index',
text: '首页',
iconType: 'home'
},
{
pagePath: '/pages/chapters/chapters',
text: '目录',
iconType: 'list'
},
{
pagePath: '/pages/match/match',
text: '找伙伴',
iconType: 'match',
isSpecial: true,
hidden: true // 默认隐藏,等配置加载后根据后台设置显示
},
{
pagePath: '/pages/my/my',
text: '我的',
iconType: 'user'
}
],
matchEnabled: false // 找伙伴功能开关(默认隐藏,等待后台配置加载)
},
attached() {
// 初始化时获取当前页面
this.loadFeatureConfig()
},
methods: {
// 加载功能配置
async loadFeatureConfig() {
try {
const app = getApp()
const res = await app.request('/api/db/config')
if (res.success && res.features) {
const matchEnabled = res.features.matchEnabled === true
this.setData({ matchEnabled })
// 更新list隐藏或显示找伙伴
const list = this.data.list.map(item => {
if (item.iconType === 'match') {
return { ...item, hidden: !matchEnabled }
}
return item
})
this.setData({ list })
console.log('[TabBar] 功能配置加载成功,找伙伴功能:', matchEnabled ? '开启' : '关闭')
}
} catch (e) {
console.log('[TabBar] 加载功能配置失败:', e)
// 失败时默认隐藏找伙伴与Web版保持一致
this.setData({ matchEnabled: false })
}
},
switchTab(e) {
const data = e.currentTarget.dataset
const url = data.path
const index = data.index
if (this.data.selected === index) return
wx.switchTab({ url })
}
}
})