From 25c229869ce597274f571ff082b779aa4a337786 Mon Sep 17 00:00:00 2001 From: Utopia Date: Fri, 31 Oct 2025 14:33:47 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=20util=20-=20toLogin?= =?UTF-8?q?Page=20=E8=B7=B3=E8=BD=AC=E5=88=B0=E7=99=BB=E5=BD=95=E9=A1=B5,?= =?UTF-8?q?=20=E5=B8=A6=E9=98=B2=E6=8A=96=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/toLoginPage.ts | 42 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 src/utils/toLoginPage.ts diff --git a/src/utils/toLoginPage.ts b/src/utils/toLoginPage.ts new file mode 100644 index 0000000..5e2d316 --- /dev/null +++ b/src/utils/toLoginPage.ts @@ -0,0 +1,42 @@ +import { LOGIN_PAGE } from '@/router/config' +import { getLastPage } from '@/utils' +import { debounce } from '@/utils/debounce' + +interface ToLoginPageOptions { + /** + * 跳转模式, uni.navigateTo | uni.reLaunch + * @default 'navigateTo' + */ + mode?: 'navigateTo' | 'reLaunch' + /** + * 查询参数 + * @example '?redirect=/pages/home/index' + */ + queryString?: string +} + +/** + * 跳转到登录页, 带防抖处理 + * + * 如果要立即跳转,不做延时,可以使用 `toLoginPage.flush()` 方法 + */ +export const toLoginPage = debounce((options: ToLoginPageOptions = {}) => { + const { mode = 'navigateTo', queryString = '' } = options + + const url = `${LOGIN_PAGE}${queryString}` + + // 获取当前页面路径 + const currentPage = getLastPage() + const currentPath = `/${currentPage.route}` + // 如果已经在登录页,则不跳转 + if (currentPath === LOGIN_PAGE) { + return + } + + if (mode === 'navigateTo') { + uni.navigateTo({ url }) + } + else { + uni.reLaunch({ url }) + } +}, 500)