/** * 登录入口:默认弹底部抽屉;个别页面用「页面版登录页」兜底 * * 为什么保留两种:抽屉是 Vue 层 fixed 定位,个别真机/低版本基础库上可能弹不出来, * 所以个人中心额外提供一个整页登录(`pages/login/index`),从那里点「登录」就跳页。 * * ⚠️ **不要在启动 / 进入游客页时自动调用**:小程序审核要求不能强制登录, * 只允许在「用户点了需要登录的功能」或「请求真的拿到 401」时唤起。 * 策略见 `config/auth.ts`。 */ import { ref } from 'vue'; /** 抽屉开关(tab 页面各挂一个 ``,共享这个状态) */ export const loginDrawerVisible = ref(false); /** 页面版登录页路径 */ export const LOGIN_PAGE = '/pages/login/index'; /** 这些页面唤起登录时走整页登录(不弹抽屉) */ const PAGE_MODE_ROUTES = [ 'pages/profile/index' ]; /** 当前页面路由(`getCurrentPages()` 栈顶) */ export function currentRoute(): string { try { const pages = getCurrentPages(); const page = pages[pages.length - 1] as { route?: string } | undefined; return (page && page.route) || ''; } catch { return ''; } } /** 跳转到页面版登录(个人中心点「登录」用) */ export function openLoginPage(): void { if (currentRoute() === 'pages/login/index') return; uni.navigateTo({ url: LOGIN_PAGE, // 页面栈满(10 层)时退回重定向 fail: () => uni.redirectTo({ url: LOGIN_PAGE }), }); } /** * 唤起登录:默认弹抽屉;在 `PAGE_MODE_ROUTES` 里的页面自动改用整页登录。 * 只在「用户主动要登录」或「401」时调用,不要在 onLaunch / onShow 里无条件调用。 */ export function openLoginDrawer(): void { if (PAGE_MODE_ROUTES.includes(currentRoute())) { openLoginPage(); return; } loginDrawerVisible.value = true; } export function closeLoginDrawer(): void { loginDrawerVisible.value = false; }