From 18802f863ce10269f7e3b92aec0bab3ed1ce840f Mon Sep 17 00:00:00 2001 From: 8614095 Date: Thu, 25 Dec 2025 21:09:44 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E4=BF=AE=E5=A4=8D=20tabbar=E4=B8=8D?= =?UTF-8?q?=E8=83=BD=E4=BC=A0=E5=8F=82=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/index/category.vue | 18 ++++++++++++------ sheep/router/index.js | 20 +++++++++++++++++++- sheep/store/app.js | 8 ++++++++ 3 files changed, 39 insertions(+), 7 deletions(-) diff --git a/pages/index/category.vue b/pages/index/category.vue index 06aacfb6..9ce51c72 100644 --- a/pages/index/category.vue +++ b/pages/index/category.vue @@ -60,7 +60,7 @@ import sheep from '@/sheep'; import CategoryApi from '@/sheep/api/product/category'; import SpuApi from '@/sheep/api/product/spu'; - import { onLoad } from '@dcloudio/uni-app'; + import { onLoad, onShow } from '@dcloudio/uni-app'; import { computed, reactive } from 'vue'; import { concat } from 'lodash-es'; import { handleTree } from '@/sheep/helper/utils'; @@ -130,14 +130,20 @@ state.pagination.pageNo++; getGoodsList(); } - - onLoad(async (params) => { - await getList(); - + function initMenuIndex() { + const appStore = sheep.$store('app'); + // 处理 tabbar 传参的情况 + const tabbarParams = appStore.paramsForTabbar || {}; + const id = tabbarParams.id; + appStore.clearParamsForTabbar(); // 使用完后清理,避免影响下次跳转 // 首页点击分类的处理:查找满足条件的分类 - const foundCategory = state.categoryList.find((category) => category.id === Number(params.id)); + const foundCategory = state.categoryList.find((category) => category.id === Number(id)); // 如果找到则调用 onMenu 自动勾选相应分类,否则调用 onMenu(0) 勾选第一个分类 onMenu(foundCategory ? state.categoryList.indexOf(foundCategory) : 0); + } + onShow(async () => { + await getList(); + initMenuIndex(); }); function handleScrollToLower() { diff --git a/sheep/router/index.js b/sheep/router/index.js index 0a4ecc30..0595e3e9 100644 --- a/sheep/router/index.js +++ b/sheep/router/index.js @@ -70,8 +70,13 @@ const _go = ( // 跳转底部导航 if (TABBAR.includes(page)) { + // wx.switchTab: url 不支持 queryString + // 设置全局变量 + const params = queryToParams(query); + $store('app').setParamsForTabbar(params); + // 请记得在业务代码里使用完后,清理掉全局状态,避免影响下次跳转 uni.switchTab({ - url, + url: page, }); return; } @@ -109,6 +114,19 @@ function paramsToQuery(params) { return query.join('&'); } +function queryToParams(query) { + if (isEmpty(query)) { + return {}; + } + let params = {}; + let pairs = query.split('&'); + for (let i = 0; i < pairs.length; i++) { + let pair = pairs[i].split('='); + params[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1] || ''); + } + return params; +} + function back() { // #ifdef H5 history.back(); diff --git a/sheep/store/app.js b/sheep/store/app.js index ea778e4a..c17a9382 100644 --- a/sheep/store/app.js +++ b/sheep/store/app.js @@ -11,6 +11,7 @@ import { baseUrl, h5Url } from '@/sheep/config'; const app = defineStore({ id: 'app', state: () => ({ + paramsForTabbar: {}, // 为全局tabbar跳转传参用。原因是 tabbar 无法传参,只能通过全局状态传递 info: { // 应用信息 name: '', // 商城名称 @@ -113,6 +114,13 @@ const app = defineStore({ $router.error('InitError', res.msg || '加载失败'); } }, + // 设置paramsForTabbar + setParamsForTabbar(params = {}) { + this.paramsForTabbar = params; + }, + clearParamsForTabbar() { + this.paramsForTabbar = {}; + } }, persist: { enabled: true,