Просмотр исходного кода

feat(tab-bar): 实现基于角色的健康页面跳转逻辑

- 在 tab-bar 组件中增加对用户登录状态和角色的判断
- 根据角色(患者/非患者)跳转至不同的健康数据页面
- 移除 useAuth 中的导航跳转逻辑,保持职责单一
- 更新公共健康页的跳转逻辑,确保仅患者跳转到专属页面
- 添加错误处理以增强跳转过程的健壮性
mcbaiyun 1 месяц назад
Родитель
Сommit
06a037affa
3 измененных файлов с 30 добавлено и 26 удалено
  1. 16 3
      src/components/tab-bar.vue
  2. 4 20
      src/composables/useAuth.ts
  3. 10 3
      src/pages/public/health/index.vue

+ 16 - 3
src/components/tab-bar.vue

@@ -16,6 +16,7 @@
 </template>
 
 <script setup lang="ts">
+import { isLoggedIn as checkLogin, getRole } from '../composables/useAuth'
 const onTabClick = (index: number) => {
   console.log('Tab clicked:', index)
 
@@ -26,9 +27,21 @@ const onTabClick = (index: number) => {
       })
       break
     case 1: // 健康数据
-      uni.switchTab({
-        url: '/pages/public/health/index'
-      })
+      try {
+        // 本地判断:已登录且 role===3 则跳转到患者端健康页(使用 switchTab,因为该页属于 tab)
+        // 未登录或非患者则降级到公共健康页
+        // TODO: 后续支持根据其他角色(医生/患者家属等)跳转到各自的健康页
+        const logged = checkLogin()
+        const role = logged ? getRole() : null
+        if (logged && role === 3) {
+          uni.switchTab({ url: '/pages/patient/health/index' })
+        } else {
+          uni.switchTab({ url: '/pages/public/health/index' })
+        }
+      } catch (err) {
+        console.error('tab click health redirect error', err)
+        uni.switchTab({ url: '/pages/public/health/index' })
+      }
       break
     case 2: // 个人中心
       uni.switchTab({

+ 4 - 20
src/composables/useAuth.ts

@@ -33,26 +33,10 @@ export function roleToString(r: number | null): string {
   if (r === 4) return '患者家属'
   return r === null ? '' : String(r)
 }
-
-/**
- * 如果当前已登录且本地 role===3(患者),则切换到患者端健康页(tab 页面)。
- * 返回:true 表示已发起跳转,false 表示未跳转。
- */
-export function ensurePatientRedirect(): boolean {
-  try {
-    if (!isLoggedIn()) return false
-    const r = getRole()
-    if (r === 3) {
-      // patient health 在 tabBar 中,使用 switchTab
-      uni.switchTab({ url: '/pages/patient/health/index' })
-      return true
-    }
-    return false
-  } catch (err) {
-    console.error('ensurePatientRedirect error', err)
-    return false
-  }
-}
+// NOTE: navigation helpers (redirects) intentionally removed from this composable.
+// This module should only provide auth-related state/helpers (isLoggedIn/getRole/roleToString).
+// Navigation decisions (例如基于角色的页面跳转) 应在调用方(页面/组件)中实现,
+// 以便保持 composable 的职责单一并避免副作用。
 
 // 可选:导出 reactive 的状态以便组件订阅(当前未被使用,但保留以备扩展)
 export const authState = {

+ 10 - 3
src/pages/public/health/index.vue

@@ -19,7 +19,7 @@ import { ref } from 'vue'
 import { onShow } from '@dcloudio/uni-app'
 import CustomNav from '@/components/custom-nav.vue'
 import TabBar from '@/components/tab-bar.vue'
-import { isLoggedIn as checkLogin, getRole, roleToString, ensurePatientRedirect } from '@/composables/useAuth'
+import { isLoggedIn as checkLogin, getRole, roleToString } from '@/composables/useAuth'
 
 const isLoggedIn = ref<boolean>(checkLogin())
 const roleDisplay = ref<string>('')
@@ -42,8 +42,15 @@ onShow(() => {
 		const r = getRole()
 		roleDisplay.value = roleToString(r)
 
-		// 仅当角色为患者(3)时,发起跳转
-		ensurePatientRedirect()
+		// 仅当角色为患者(3)时,发起跳转到患者端健康页(该页在患者端为 Tab)
+		// 未登录或非患者则留在公共健康页
+		// TODO: 后续可支持医生/家属等其他角色的专属页面
+		if (r === 3) {
+			// patient health 在患者端 tab 中,使用 switchTab
+			console.log('[health/index] redirecting to patient health tab, logged=', logged, 'role=', r)
+			uni.switchTab({ url: '/pages/patient/health/index' })
+			return
+		}
 	} catch (err) {
 		console.error('检查登录态时出错:', err)
 		isLoggedIn.value = false