From 2b2a02fe885ca3f105ef7a3288b48d72e111d7f5 Mon Sep 17 00:00:00 2001 From: MSuMshk <2039814060@qq.com> Date: Thu, 12 Mar 2026 09:20:46 +0800 Subject: [PATCH] feat: implement custom mini-program tab bar --- README.md | 1 + config/index.ts | 7 ++- mini/custom-tab-bar/index.js | 49 +++++++++++++++ mini/custom-tab-bar/index.json | 3 + mini/custom-tab-bar/index.wxml | 20 ++++++ mini/custom-tab-bar/index.wxss | 42 +++++++++++++ src/components/tab-bar/index.vue | 101 ------------------------------ src/pages/home/index.config.ts | 3 +- src/pages/home/index.vue | 10 +-- src/pages/home/styles/base.scss | 5 -- src/pages/menu/index.config.ts | 3 +- src/pages/menu/index.vue | 7 +-- src/pages/orders/index.config.ts | 3 +- src/pages/orders/index.vue | 6 +- src/pages/profile/index.config.ts | 3 +- src/pages/profile/index.vue | 6 +- src/utils/useTabBarSelection.ts | 35 +++++++++++ 17 files changed, 176 insertions(+), 128 deletions(-) create mode 100644 mini/custom-tab-bar/index.js create mode 100644 mini/custom-tab-bar/index.json create mode 100644 mini/custom-tab-bar/index.wxml create mode 100644 mini/custom-tab-bar/index.wxss delete mode 100644 src/components/tab-bar/index.vue create mode 100644 src/utils/useTabBarSelection.ts diff --git a/README.md b/README.md index c023901..a36d828 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,7 @@ pnpm lint ## 目录结构 - `src/pages`:按页面域拆分,每个页面保持 `index.vue + composables + styles` - `src/components`:复用组件目录,统一使用 `src/components//index.vue` +- `mini/custom-tab-bar`:微信小程序原生 `custom-tab-bar` 源文件,会在构建时复制到 `dist/custom-tab-bar` - `src/stores`:`app`、`cart` - `src/services`:请求封装与 Mini API 领域接口 - `src/shared`:类型、常量、mock 数据、格式化函数 diff --git a/config/index.ts b/config/index.ts index 8a4cfb7..42e72d5 100644 --- a/config/index.ts +++ b/config/index.ts @@ -35,7 +35,12 @@ export default defineConfig<'vite'>(async (merge) => { __TENANT_CODE__: JSON.stringify(tenantCode) }, copy: { - patterns: [], + patterns: [ + { + from: 'mini/custom-tab-bar', + to: 'dist/custom-tab-bar' + } + ], options: {} }, framework: 'vue3', diff --git a/mini/custom-tab-bar/index.js b/mini/custom-tab-bar/index.js new file mode 100644 index 0000000..47622d9 --- /dev/null +++ b/mini/custom-tab-bar/index.js @@ -0,0 +1,49 @@ +Component({ + data: { + selected: 0, + list: [ + { + pagePath: 'pages/home/index', + text: '首页', + iconPath: '../assets/tabbar/home.png', + selectedIconPath: '../assets/tabbar/home-active.png' + }, + { + pagePath: 'pages/menu/index', + text: '点餐', + iconPath: '../assets/tabbar/menu.png', + selectedIconPath: '../assets/tabbar/menu-active.png' + }, + { + pagePath: 'pages/orders/index', + text: '订单', + iconPath: '../assets/tabbar/orders.png', + selectedIconPath: '../assets/tabbar/orders-active.png' + }, + { + pagePath: 'pages/profile/index', + text: '我的', + iconPath: '../assets/tabbar/profile.png', + selectedIconPath: '../assets/tabbar/profile-active.png' + } + ] + }, + methods: { + setCurrent(index) { + this.setData({ selected: index }) + }, + switchTab(event) { + const index = Number(event.currentTarget.dataset.index) + const currentItem = this.data.list[this.data.selected] + const targetItem = this.data.list[index] + const currentPath = currentItem ? currentItem.pagePath : '' + + if (!targetItem || currentPath === targetItem.pagePath) { + return + } + + this.setData({ selected: index }) + wx.switchTab({ url: `/${targetItem.pagePath}` }) + } + } +}) diff --git a/mini/custom-tab-bar/index.json b/mini/custom-tab-bar/index.json new file mode 100644 index 0000000..467ce29 --- /dev/null +++ b/mini/custom-tab-bar/index.json @@ -0,0 +1,3 @@ +{ + "component": true +} diff --git a/mini/custom-tab-bar/index.wxml b/mini/custom-tab-bar/index.wxml new file mode 100644 index 0000000..e3ac504 --- /dev/null +++ b/mini/custom-tab-bar/index.wxml @@ -0,0 +1,20 @@ + + + + + {{item.text}} + + + diff --git a/mini/custom-tab-bar/index.wxss b/mini/custom-tab-bar/index.wxss new file mode 100644 index 0000000..08cb746 --- /dev/null +++ b/mini/custom-tab-bar/index.wxss @@ -0,0 +1,42 @@ +.custom-tab-bar { + display: flex; + align-items: flex-start; + padding-top: 14px; + padding-bottom: calc(6px + constant(safe-area-inset-bottom)); + padding-bottom: calc(6px + env(safe-area-inset-bottom)); + min-height: 72px; + background: #ffffff; + border-top: 1rpx solid #e5e7eb; + box-shadow: 0 -4px 18px rgba(15, 23, 42, 0.05); +} + +.custom-tab-bar__item { + flex: 1; + min-height: 44px; + display: flex; + flex-direction: column; + align-items: center; + justify-content: flex-start; +} + +.custom-tab-bar__icon { + width: 26px; + height: 26px; + display: block; + flex-shrink: 0; + margin-bottom: -3px; +} + +.custom-tab-bar__label { + display: block; + margin-top: 0; + font-size: 12px; + line-height: 1; + color: #64748b; + font-weight: 500; +} + +.custom-tab-bar__label--active { + color: #16a34a; + font-weight: 600; +} diff --git a/src/components/tab-bar/index.vue b/src/components/tab-bar/index.vue deleted file mode 100644 index 74d2212..0000000 --- a/src/components/tab-bar/index.vue +++ /dev/null @@ -1,101 +0,0 @@ - - - - - diff --git a/src/pages/home/index.config.ts b/src/pages/home/index.config.ts index 12abc5f..cd12ca7 100644 --- a/src/pages/home/index.config.ts +++ b/src/pages/home/index.config.ts @@ -1,3 +1,4 @@ export default definePageConfig({ - navigationBarTitleText: '首页' + navigationBarTitleText: '首页', + usingComponents: {} }) diff --git a/src/pages/home/index.vue b/src/pages/home/index.vue index e00d5ae..de7681e 100644 --- a/src/pages/home/index.vue +++ b/src/pages/home/index.vue @@ -134,12 +134,6 @@ - - - - - - @@ -154,9 +148,11 @@