tabbar bug修复

This commit is contained in:
BAKEYi 2026-05-27 15:45:10 +08:00
parent bbe660cbfc
commit 1d611bee0b
2 changed files with 192 additions and 137 deletions

View File

@ -1,10 +1,24 @@
<template> <template>
<uv-tabbar fixed :value="selected" @change="change1" activeColor="#FF4767 " :placeholder="false" <uv-tabbar
:safeAreaInsetBottom="false" :customStyle="{height:`110rpx`}"> v-if="tabbarReady"
<!-- <uv-tabbar-item v-for="(item,index) in $store.state.tabbarList" :key="index" :text="item.text" --> :key="refreshKey"
<uv-tabbar-item v-for="(item,index) in tabbarList" :key="index" :text="item.text" fixed
:badge="getNum(index)" :value="currentTabIndex"
:customStyle="{height:`110rpx`}"> @change="onTabChange"
activeColor="#FF4767"
:placeholder="false"
:safeAreaInsetBottom="false"
:customStyle="{height:'110rpx'}"
>
<uv-tabbar-item
v-for="(item, idx) in tabbarList"
:key="idx"
:name="idx"
:text="item.text"
:badge="getNum(idx)"
:customStyle="{height:'110rpx'}"
@click="onItemClick(idx)"
>
<template v-slot:active-icon> <template v-slot:active-icon>
<image class="icon" :src="item.selectedIconPath"></image> <image class="icon" :src="item.selectedIconPath"></image>
</template> </template>
@ -16,108 +30,146 @@
</template> </template>
<script> <script>
import { mapState, mapMutations } from 'vuex';
import request from "@/utils/request"; import request from "@/utils/request";
export default { export default {
name: "tab-bar", name: "tab-bar",
props: { // props.selected Vuex
selected: {
type: [Number, String]
}
},
data() { data() {
return { return {
tabbarList: [{ refreshKey: 0, //
"iconPath": "/static/images/tabbar/new_home_gray.png", tabbarReady: false, //
"text": "首页", tabbarList: [ // tab
"selectedIconPath": "/static/images/tabbar/new_home.png", {
"pagePath": "/pages/home/home" iconPath: "/static/images/tabbar/new_home_gray.png",
text: "首页",
selectedIconPath: "/static/images/tabbar/new_home.png",
pagePath: "/pages/home/home"
}, },
{ {
"iconPath": "/static/images/tabbar/new_order_gray.png", iconPath: "/static/images/tabbar/new_order_gray.png",
"text": "订单", text: "订单",
"selectedIconPath": "/static/images/tabbar/new_order.png", selectedIconPath: "/static/images/tabbar/new_order.png",
"pagePath": "/pages/order/all-orders" pagePath: "/pages/order/all-orders"
}, },
{ {
"iconPath": "/static/images/tabbar/new_message_gray.png", iconPath: "/static/images/tabbar/new_message_gray.png",
"text": "消息", text: "消息",
"selectedIconPath": "/static/images/tabbar/new_message.png", selectedIconPath: "/static/images/tabbar/new_message.png",
"pagePath": "/pages/message/message" pagePath: "/pages/message/message"
}, },
{ {
"iconPath": "/static/images/tabbar/new_my_gray.png", iconPath: "/static/images/tabbar/new_my_gray.png",
"text": "我的", text: "我的",
"selectedIconPath": "/static/images/tabbar/new_my.png", selectedIconPath: "/static/images/tabbar/new_my.png",
"pagePath": "/pages/my/my" pagePath: "/pages/my/my"
} }
] ]
}; };
}, },
computed: { computed: {
messageNum() { // Vuex
return this.$store.state.messageNum || 0 ...mapState(['currentTabIndex', 'messageNum'])
},
watch: {
// tabbarList
tabbarList: {
deep: true,
handler() {
this.rebuildTabbar();
}
} }
}, },
async created() { created() {
// if (!this.$store.state.tabbarList.length) { //
// let isnNetwork = uni.getStorageSync("isnNetwork"); this.tabbarReady = false;
// if (isnNetwork) { this.$nextTick(() => {
// await this.getButtonImg() this.tabbarReady = true;
// } else { //
// uni.getNetworkType({ // this.getButtonImg();
// success: (res) => { });
// console.log(res.networkType);
// if (res.networkType == "none") {
// this.$store.commit("setTabbarList", this.tabbarList)
// } else {
// //
// uni.setStorageSync("isnNetwork", 1);
// this.getButtonImg()
// }
// },
// });
// }
// }
}, },
methods: { methods: {
...mapMutations(['SET_CURRENT_TAB_INDEX']),
//
setCurrentTabIndex(index) {
this.SET_CURRENT_TAB_INDEX(index);
},
// uv-tabbar
rebuildTabbar() {
this.tabbarReady = false;
this.$nextTick(() => {
this.refreshKey++;
this.tabbarReady = true;
});
},
// tab > 0
getNum(index) { getNum(index) {
if (index == 2) { if (index === 2 && this.messageNum > 0) {
return this.messageNum return this.messageNum;
} else {
return 0
} }
return null;
}, },
//
//
async getButtonImg() { async getButtonImg() {
try {
let res = await request.post("/sj/poster/getButtonImg", {}); let res = await request.post("/sj/poster/getButtonImg", {});
if (res.code == 200) { if (res.code === 200) {
// if (res.data?.photo) {
// this.topBgImage = res.data?.photo + '?time=666'
// }
this.tabbarList.forEach((item, index) => { this.tabbarList.forEach((item, index) => {
if (res.data[index].iconPath) { if (res.data[index]?.iconPath) {
item.iconPath = res.data[index].iconPath item.iconPath = res.data[index].iconPath;
} }
if (res.data[index].selectedIconPath) { if (res.data[index]?.selectedIconPath) {
item.selectedIconPath = res.data[index].selectedIconPath item.selectedIconPath = res.data[index].selectedIconPath;
} }
}) });
this.$store.commit("setTabbarList", this.tabbarList);
//
this.rebuildTabbar();
}
} catch (err) {
console.error('getButtonImg 请求失败', err);
} }
this.$store.commit("setTabbarList", this.tabbarList)
}, },
change1(val) {
// pages tabbar // uv-tabbar change
// 使navigateTo 使 onTabChange(val) {
const idx = parseInt(val, 10);
this.doJump(idx);
},
// item
onItemClick(idx) {
this.doJump(idx);
},
// +
doJump(index) {
const idx = parseInt(index, 10);
if (isNaN(idx) || !this.tabbarList[idx]) return;
//
this.setCurrentTabIndex(idx);
//
const targetPage = this.tabbarList[idx].pagePath;
uni.switchTab({ uni.switchTab({
url: this.tabbarList[val].pagePath url: targetPage,
}) fail: (err) => console.error('switchTab 失败', err)
this.messageUpudateNum() });
//
if (typeof this.messageUpudateNum === 'function') {
this.messageUpudateNum();
} }
} }
} }
};
</script> </script>
<style lang="scss"> <style lang="scss">

View File

@ -7,6 +7,9 @@ Vue.use(Vuex);
const store = new Vuex.Store({ const store = new Vuex.Store({
state: { state: {
messageNum: 0, // 底部消息数量 messageNum: 0, // 底部消息数量
// ========== 新增:全局当前选中的 tab 索引 ==========
currentTabIndex: 0, // 用于自定义 tabbar 高亮
// ============================================
sjServiceList: {}, // 商家分配手艺人时服务数据shop/staff sjServiceList: {}, // 商家分配手艺人时服务数据shop/staff
tabbarList: [], // 底部设置 tabbarList: [], // 底部设置
reservationTime: {}, // 预约时刻信息 reservationTime: {}, // 预约时刻信息
@ -14,32 +17,32 @@ const store = new Vuex.Store({
reservationAddress: {}, // 预约地址信息 reservationAddress: {}, // 预约地址信息
selectStore: {}, // 选择的门店信息 selectStore: {}, // 选择的门店信息
wxShareType: 0, // 正式版:0测试版:1体验版:2 wxShareType: 0, // 正式版:0测试版:1体验版:2
//公共的变量这里的变量不能随便修改只能通过触发mutations的方法才能改变
searchCity: { searchCity: {
center: "117.330043,38.372266", center: "117.330043,38.372266",
name: "黄骅", name: "黄骅",
}, },
systemInfo: uni.getSystemInfoSync(), //设备信息 systemInfo: uni.getSystemInfoSync(),
//是否有定位权限
isLocation: false, isLocation: false,
//路由
baseUrl: "", baseUrl: "",
//手机和app信息
version: { version: {
VersionCode: "", VersionCode: "",
DeviceBrand: "", DeviceBrand: "",
DeviceType: "", DeviceType: "",
}, },
//商家信息
sjInfo: {}, sjInfo: {},
workSpaceSJClassList: [], //商家工位分类列表 workSpaceSJClassList: [],
workHourSJClassList: [], //商家工时分类列表 workHourSJClassList: [],
}, },
mutations: { mutations: {
// 写入底部消息数量 // 写入底部消息数量
setMessageNum(state, newval) { setMessageNum(state, newval) {
state.messageNum = newval; state.messageNum = newval;
}, },
// ========== 新增:设置全局当前选中 tab 索引 ==========
SET_CURRENT_TAB_INDEX(state, index) {
state.currentTabIndex = index;
},
// ==============================================
clearServiceState(state) { clearServiceState(state) {
state.reservationTime = {}; state.reservationTime = {};
state.reservationSyr = {}; state.reservationSyr = {};