163 lines
4.8 KiB
Vue
163 lines
4.8 KiB
Vue
<script setup>
|
||
import { computed, watch, onMounted } from 'vue'
|
||
import { useUserStore } from '@/store/user'
|
||
|
||
const userStore = useUserStore()
|
||
const currentRole = computed(() => userStore.currentRole)
|
||
|
||
onMounted(() => {
|
||
console.log('App Launch')
|
||
updateTabBar()
|
||
|
||
// 🔥 新增:检查用户登录状态和角色,自动跳转到对应首页
|
||
checkLoginAndRedirect()
|
||
})
|
||
|
||
watch(currentRole, () => {
|
||
updateTabBar()
|
||
})
|
||
|
||
// 🔥 新增:检查登录状态并自动跳转
|
||
const checkLoginAndRedirect = () => {
|
||
const token = uni.getStorageSync('token')
|
||
const userInfo = uni.getStorageSync('userInfo')
|
||
const currentRole = uni.getStorageSync('currentRole')
|
||
|
||
console.log('检查登录状态:', { token: !!token, currentRole })
|
||
|
||
// 如果已登录且有角色信息,检查当前页面是否正确
|
||
if (token && userInfo && currentRole) {
|
||
const pages = getCurrentPages()
|
||
const currentPage = pages[pages.length - 1]
|
||
const currentRoute = currentPage.route
|
||
|
||
console.log('当前页面:', currentRoute, '用户角色:', currentRole)
|
||
|
||
// 如果当前在登录页面或首页,需要根据角色跳转
|
||
if (currentRoute === 'pages/index/index' || currentRoute.includes('login')) {
|
||
setTimeout(() => {
|
||
redirectToRoleHome(currentRole)
|
||
}, 100)
|
||
}
|
||
}
|
||
}
|
||
|
||
// 🔥 新增:根据角色跳转到对应首页
|
||
const redirectToRoleHome = (role) => {
|
||
console.log('根据角色跳转首页:', role)
|
||
|
||
switch (role) {
|
||
case 'teacher':
|
||
uni.reLaunch({
|
||
url: '/teacher-package/pages/teacher/index'
|
||
})
|
||
break
|
||
|
||
case 'manager':
|
||
uni.reLaunch({
|
||
url: '/manager-package/pages/manager/index'
|
||
})
|
||
break
|
||
|
||
case 'distributor':
|
||
uni.reLaunch({
|
||
url: '/distributor-package/pages/distributor/index'
|
||
})
|
||
break
|
||
|
||
case 'serviceProvider':
|
||
uni.reLaunch({
|
||
url: '/provider-package/pages/provider/index'
|
||
})
|
||
break
|
||
|
||
case 'user':
|
||
default:
|
||
// 家长保持在通用首页
|
||
break
|
||
}
|
||
}
|
||
|
||
// 根据角色动态更新TabBar
|
||
const updateTabBar = () => {
|
||
const role = currentRole.value || 'user'
|
||
|
||
// 不同角色的TabBar配置(5个标签)
|
||
const tabBarConfig = {
|
||
user: [
|
||
{ text: '首页', pagePath: '/pages/index/index' },
|
||
{ text: '服务', pagePath: '/pages/service/list' },
|
||
{ text: '快速预约', pagePath: '/pages/booking/quick-booking' },
|
||
{ text: '日历', pagePath: '/pages/order/list' },
|
||
{ text: '我的', pagePath: '/pages/user/index' }
|
||
],
|
||
teacher: [
|
||
{ text: '首页', pagePath: '/pages/index/index' },
|
||
{ text: '服务', pagePath: '/pages/service/list' },
|
||
{ text: '快速预约', pagePath: '/pages/booking/quick-booking' },
|
||
{ text: '日历', pagePath: '/pages/order/list' },
|
||
{ text: '我的', pagePath: '/pages/user/index' }
|
||
],
|
||
manager: [
|
||
{ text: '首页', pagePath: '/pages/index/index' },
|
||
{ text: '服务', pagePath: '/pages/service/list' },
|
||
{ text: '快速预约', pagePath: '/pages/booking/quick-booking' },
|
||
{ text: '日历', pagePath: '/pages/order/list' },
|
||
{ text: '我的', pagePath: '/pages/user/index' }
|
||
],
|
||
distributor: [
|
||
{ text: '首页', pagePath: '/pages/index/index' },
|
||
{ text: '服务', pagePath: '/pages/service/list' },
|
||
{ text: '快速预约', pagePath: '/pages/booking/quick-booking' },
|
||
{ text: '日历', pagePath: '/pages/order/list' },
|
||
{ text: '我的', pagePath: '/pages/user/index' }
|
||
],
|
||
serviceProvider: [
|
||
{ text: '首页', pagePath: '/pages/index/index' },
|
||
{ text: '服务', pagePath: '/pages/service/list' },
|
||
{ text: '快速预约', pagePath: '/pages/booking/quick-booking' },
|
||
{ text: '日历', pagePath: '/pages/order/list' },
|
||
{ text: '我的', pagePath: '/pages/user/index' }
|
||
]
|
||
}
|
||
|
||
const config = tabBarConfig[role] || tabBarConfig.user
|
||
|
||
// 更新TabBar文字(添加错误处理,避免在非TabBar页面报错)
|
||
config.forEach((item, index) => {
|
||
try {
|
||
uni.setTabBarItem({
|
||
index: index,
|
||
text: item.text,
|
||
fail: () => {} // 静默失败
|
||
})
|
||
} catch (e) {
|
||
// 忽略非TabBar页面的错误
|
||
}
|
||
})
|
||
|
||
// 设置TabBar样式(深青绿色)
|
||
try {
|
||
uni.setTabBarStyle({
|
||
color: 'rgba(255, 255, 255, 0.7)',
|
||
selectedColor: '#ffffff',
|
||
backgroundColor: '#2d9687',
|
||
borderStyle: 'white',
|
||
fail: () => {} // 静默失败
|
||
})
|
||
} catch (e) {
|
||
// 忽略非TabBar页面的错误
|
||
}
|
||
}
|
||
</script>
|
||
|
||
<style lang="scss">
|
||
@import '@/static/css/common.scss';
|
||
|
||
page {
|
||
background-color: #f8f8f8;
|
||
font-size: 28rpx;
|
||
color: #333;
|
||
}
|
||
</style>
|