将用户类型 (user_type) 从 userInfo 中分离出来,单独存储到本地存储中,确保用户类型在页面刷新后仍然保持。
// store/user.js
const state = () => ({
// ... 其他字段
userType: getStorage('userType') || 1 // 单独存储用户类型,默认为卖家
})
userType1 (卖家)1: 卖家2: 买家// 设置为买家
useUser.setUserType(2);
// 设置为卖家
useUser.setUserType(1);
const userType = useUser.getUserType();
// 返回: 1 或 2
const isBuyer = computed(() => {
return useUser.getUserType() === 2;
});
localStorage 中,键名为 userTypeuserInfo 更新影响1)// 切换到买家模式
const switchToBuyer = () => {
useUser.setUserType(2);
uni.reLaunch({ url: '/pagesBuyer/home/index' });
};
// 切换到卖家模式
const switchToSeller = () => {
useUser.setUserType(1);
uni.reLaunch({ url: '/pages/index/index' });
};
// Tabbar 组件中根据用户类型显示不同导航
const currentNavList = computed(() => {
if (userInfo.value && Object.keys(userInfo.value).length > 0) {
return isBuyer.value ? buyerNavList.value : navList.value;
}
// 未登录时根据页面路径判断
const currentPath = getCurrentPages()[getCurrentPages().length - 1].route;
const isBuyerPage = currentPath.includes("pagesBuyer");
return isBuyerPage ? buyerNavList.value : navList.value;
});
userType 作为存储键名,避免与其他数据冲突1 (卖家),确保应用正常运行在买家端首页添加了调试信息显示当前用户类型:
<view class="debug-info" v-if="token">
<text>当前用户类型: {{ userType === 1 ? '卖家' : '买家' }} ({{ userType }})</text>
</view>
可以通过这个信息验证用户类型是否正确存储和显示。