feat(mp): U3-1 医生端导航状态保持
- 新增 useNavigationState hook (saveDoctorPage/getDoctorLastPage) - 首页医生重定向使用 getDoctorLastPage 代替硬编码路径 - 医生端工作台入口自动保存最后访问路径 - 医生再次打开首页时自动回到上次使用的医生端页面
This commit is contained in:
43
apps/miniprogram/src/hooks/useNavigationState.ts
Normal file
43
apps/miniprogram/src/hooks/useNavigationState.ts
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
import { useCallback } from 'react';
|
||||||
|
import Taro from '@tarojs/taro';
|
||||||
|
import { useAuthStore } from '@/stores/auth';
|
||||||
|
|
||||||
|
const NAV_STATE_KEY = 'doctor_last_page';
|
||||||
|
|
||||||
|
const DOCTOR_PAGES = [
|
||||||
|
'/pages/pkg-doctor-core/index',
|
||||||
|
'/pages/pkg-doctor-core/patients/index',
|
||||||
|
'/pages/pkg-doctor-core/consultation/index',
|
||||||
|
'/pages/pkg-doctor-core/followup/index',
|
||||||
|
'/pages/pkg-doctor-core/action-inbox/index',
|
||||||
|
];
|
||||||
|
|
||||||
|
export function saveDoctorPage(path: string): void {
|
||||||
|
if (!path.startsWith('/pages/pkg-doctor')) return;
|
||||||
|
try {
|
||||||
|
Taro.setStorageSync(NAV_STATE_KEY, path);
|
||||||
|
} catch { /* ignore */ }
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getDoctorLastPage(): string {
|
||||||
|
try {
|
||||||
|
const saved = Taro.getStorageSync(NAV_STATE_KEY);
|
||||||
|
if (saved && typeof saved === 'string' && saved.startsWith('/pages/pkg-doctor')) {
|
||||||
|
return saved;
|
||||||
|
}
|
||||||
|
} catch { /* ignore */ }
|
||||||
|
return DOCTOR_PAGES[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
export function useNavigationState() {
|
||||||
|
const isDoctor = useAuthStore((s) => s.isDoctor);
|
||||||
|
|
||||||
|
const navigateToDoctorHome = useCallback(() => {
|
||||||
|
if (!isDoctor()) return false;
|
||||||
|
const lastPage = getDoctorLastPage();
|
||||||
|
Taro.navigateTo({ url: lastPage });
|
||||||
|
return true;
|
||||||
|
}, [isDoctor]);
|
||||||
|
|
||||||
|
return { navigateToDoctorHome, saveDoctorPage, getDoctorLastPage };
|
||||||
|
}
|
||||||
@@ -6,6 +6,7 @@ import { useAuthStore } from '../../stores/auth';
|
|||||||
import { useUIStore } from '../../stores/ui';
|
import { useUIStore } from '../../stores/ui';
|
||||||
import { navigateToLogin } from '../../utils/navigate';
|
import { navigateToLogin } from '../../utils/navigate';
|
||||||
import { usePageData } from '@/hooks/usePageData';
|
import { usePageData } from '@/hooks/usePageData';
|
||||||
|
import { getDoctorLastPage } from '@/hooks/useNavigationState';
|
||||||
// useThrottledDidShow removed — unused import
|
// useThrottledDidShow removed — unused import
|
||||||
import { api } from '@/services/request';
|
import { api } from '@/services/request';
|
||||||
import type { Article } from '@/services/article';
|
import type { Article } from '@/services/article';
|
||||||
@@ -352,8 +353,9 @@ export default function Index() {
|
|||||||
useDidShow(() => {
|
useDidShow(() => {
|
||||||
if (shouldRedirect && !redirectingRef.current) {
|
if (shouldRedirect && !redirectingRef.current) {
|
||||||
redirectingRef.current = true;
|
redirectingRef.current = true;
|
||||||
|
const target = getDoctorLastPage();
|
||||||
Taro.reLaunch({
|
Taro.reLaunch({
|
||||||
url: '/pages/pkg-doctor-core/index',
|
url: target,
|
||||||
fail: () => {
|
fail: () => {
|
||||||
redirectingRef.current = false;
|
redirectingRef.current = false;
|
||||||
console.warn('跳转医生端失败,停留患者首页');
|
console.warn('跳转医生端失败,停留患者首页');
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import { safeNavigateTo } from '@/utils/navigate';
|
|||||||
import { useAuthStore } from '@/stores/auth';
|
import { useAuthStore } from '@/stores/auth';
|
||||||
import { useDoctorClass } from '@/hooks/useDoctorClass';
|
import { useDoctorClass } from '@/hooks/useDoctorClass';
|
||||||
import { usePageData } from '@/hooks/usePageData';
|
import { usePageData } from '@/hooks/usePageData';
|
||||||
|
import { saveDoctorPage } from '@/hooks/useNavigationState';
|
||||||
import { getDashboard, type DoctorDashboard } from '@/services/doctor/dashboard';
|
import { getDashboard, type DoctorDashboard } from '@/services/doctor/dashboard';
|
||||||
import Loading from '@/components/Loading';
|
import Loading from '@/components/Loading';
|
||||||
import ContentCard from '@/components/ui/ContentCard';
|
import ContentCard from '@/components/ui/ContentCard';
|
||||||
@@ -68,6 +69,7 @@ export default function DoctorHome() {
|
|||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
usePageData(loadDashboard, { throttleMs: 10000 });
|
usePageData(loadDashboard, { throttleMs: 10000 });
|
||||||
|
saveDoctorPage('/pages/pkg-doctor-core/index');
|
||||||
|
|
||||||
const getValue = (key: keyof DoctorDashboard): number | string => {
|
const getValue = (key: keyof DoctorDashboard): number | string => {
|
||||||
if (!dashboard) return '-';
|
if (!dashboard) return '-';
|
||||||
|
|||||||
Reference in New Issue
Block a user