Files
aiot-uniapp/src/tabbar/store.ts
2025-08-18 22:01:55 +08:00

53 lines
1.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { tabbarList as _tabbarList } from './config'
// TODO 1/2: 中间的鼓包tabbarItem的开关
const BULGE_ENABLE = true
/** tabbarList 里面的 path 从 pages.config.ts 得到 */
const tabbarList = _tabbarList.map(item => ({ ...item, path: `/${item.pagePath}` }))
if (BULGE_ENABLE) {
if (tabbarList.length % 2 === 1) {
console.error('tabbar 数量必须是偶数,否则样式很奇怪!!')
}
tabbarList.splice(tabbarList.length / 2, 0, {
isBulge: true,
} as any)
}
export { tabbarList }
export function isPageTabbar(path: string) {
return tabbarList.some(item => item.pagePath === path)
}
/**
* 自定义 tabbar 的状态管理,原生 tabbar 无需关注本文件
* tabbar 状态,增加 storageSync 保证刷新浏览器时在正确的 tabbar 页面
* 使用reactive简单状态而不是 pinia 全局状态
*/
export const tabbarStore = reactive({
curIdx: uni.getStorageSync('app-tabbar-index') || 0,
prevIdx: uni.getStorageSync('app-tabbar-index') || 0,
setCurIdx(idx: number) {
this.curIdx = idx
uni.setStorageSync('app-tabbar-index', idx)
},
setAutoCurIdx(path: string) {
const index = tabbarList.findIndex(item => item.path === path)
console.log('index:', index, path)
// console.log('tabbarList:', tabbarList)
if (index === -1) {
this.setCurIdx(0)
}
else {
this.setCurIdx(index)
}
},
restorePrevIdx() {
if (this.prevIdx === this.curIdx)
return
this.setCurIdx(this.prevIdx)
this.prevIdx = uni.getStorageSync('app-tabbar-index') || 0
},
})