liufumingren 8f1a8b7ddb feat(miniprogram): 新增 uni-app + Vue3 + TS 微信小程序
- 登录:微信一键登录(code2Session)+ 账号登录双 tab;个人中心为整页登录,其它页走抽屉
- 协议:登录前必须阅读并同意,版本号随登录上报落库
- 不强制登录:启动不弹窗,点功能或遇到 401 才提示(符合小程序审核)
- 页面:首页统计 / 扫码记录 / 个人中心;H5 预览端口 7304

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-10-21 00:47:16 +08:00

58 lines
2.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 登录入口:默认弹底部抽屉;个别页面用「页面版登录页」兜底
*
* 为什么保留两种:抽屉是 Vue 层 fixed 定位,个别真机/低版本基础库上可能弹不出来,
* 所以个人中心额外提供一个整页登录(`pages/login/index`),从那里点「登录」就跳页。
*
* ⚠️ **不要在启动 / 进入游客页时自动调用**:小程序审核要求不能强制登录,
* 只允许在「用户点了需要登录的功能」或「请求真的拿到 401」时唤起。
* 策略见 `config/auth.ts`。
*/
import { ref } from 'vue';
/** 抽屉开关(tab 页面各挂一个 `<login-drawer />`,共享这个状态) */
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;
}