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

154 lines
4.7 KiB
JavaScript
Raw Normal View History

/**
* Soul创业实验 - 自定义TabBar组件
* 根据后台配置动态显示/隐藏"找伙伴"按钮
*/
console.log('[TabBar] ===== 组件文件开始加载 =====')
const app = getApp()
console.log('[TabBar] App 对象:', app)
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() {
console.log('[TabBar] Component attached 生命周期触发')
this.loadFeatureConfig()
},
ready() {
console.log('[TabBar] Component ready 生命周期触发')
// 如果 attached 中没有成功加载,在 ready 中再次尝试
if (this.data.matchEnabled === undefined || this.data.matchEnabled === null) {
console.log('[TabBar] 在 ready 中重新加载配置')
this.loadFeatureConfig()
}
}
},
// 页面加载时也调用(兼容性更好)
attached() {
console.log('[TabBar] attached() 方法触发')
this.loadFeatureConfig()
},
methods: {
// 加载功能配置
async loadFeatureConfig() {
try {
console.log('[TabBar] 开始加载功能配置...')
console.log('[TabBar] API地址:', app.globalData.baseUrl + '/api/miniprogram/config')
// app.request 的第一个参数是 url 字符串,第二个参数是 options 对象
const res = await app.request('/api/miniprogram/config', {
method: 'GET'
})
// 兼容两种返回格式
let matchEnabled = false
if (res && res.success && res.features) {
console.log('[TabBar] features配置:', JSON.stringify(res.features))
matchEnabled = res.features.matchEnabled === true
console.log('[TabBar] matchEnabled值:', matchEnabled)
} else if (res && res.configs && res.configs.feature_config) {
// 备用格式:从 configs.feature_config 读取
console.log('[TabBar] 使用备用格式从configs读取')
matchEnabled = res.configs.feature_config.matchEnabled === true
console.log('[TabBar] matchEnabled值:', matchEnabled)
} else {
console.log('[TabBar] ⚠️ 未找到features配置使用默认值false')
console.log('[TabBar] res对象keys:', Object.keys(res || {}))
}
this.setData({ matchEnabled }, () => {
console.log('[TabBar] ✅ matchEnabled已设置为:', this.data.matchEnabled)
// 配置加载完成后,根据当前路由设置选中状态
this.updateSelected()
})
// 如果当前在找伙伴页面,但功能已关闭,跳转到首页
if (!matchEnabled) {
const pages = getCurrentPages()
const currentPage = pages[pages.length - 1]
if (currentPage && currentPage.route === 'pages/match/match') {
console.log('[TabBar] 找伙伴功能已关闭从match页面跳转到首页')
wx.switchTab({ url: '/pages/index/index' })
}
}
} catch (error) {
console.log('[TabBar] ❌ 加载功能配置失败:', error)
console.log('[TabBar] 错误详情:', error.message || 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 })
}
}
})