From 07717327fe9e03b1ab6c02cda8148c1afb29a347 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E7=BB=B4?= <908280968@qq.com> Date: Mon, 29 Sep 2025 15:56:16 +0800 Subject: [PATCH] =?UTF-8?q?refactor(utils):=20=E4=BC=98=E5=8C=96=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=E5=AE=9A=E4=B9=89=E5=92=8C=E7=B1=BB=E5=9E=8B=E5=AE=89?= =?UTF-8?q?=E5=85=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加 PageInstance 类型定义 - 为 pages 和 subPackages 添加类型注解 - 移除不必要的类型断言 - 处理可能的 undefined 情况 - 保持原有功能不变的情况下增强类型安全 --- src/utils/index.ts | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/src/utils/index.ts b/src/utils/index.ts index d887df4..89d14c7 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1,12 +1,15 @@ +import type { PageMetaDatum, SubPackages } from '@uni-helper/vite-plugin-uni-pages' import { isMpWeixin } from '@uni-helper/uni-env' import { pages, subPackages } from '@/pages.json' +export type PageInstance = Page.PageInstance & { $page: Page.PageInstance & { fullPath: string } } + export function getLastPage() { // getCurrentPages() 至少有1个元素,所以不再额外判断 // const lastPage = getCurrentPages().at(-1) // 上面那个在低版本安卓中打包会报错,所以改用下面这个【虽然我加了 src/interceptions/prototype.ts,但依然报错】 const pages = getCurrentPages() - return pages[pages.length - 1] + return pages[pages.length - 1] as PageInstance } /** @@ -15,20 +18,20 @@ export function getLastPage() { * redirectPath 如 '/pages/demo/base/route-interceptor' */ export function currRoute() { - const lastPage = getLastPage() + const lastPage = getLastPage() as PageInstance if (!lastPage) { return { path: '', query: {}, } } - const currRoute = (lastPage as any).$page + const currRoute = lastPage.$page // console.log('lastPage.$page:', currRoute) // console.log('lastPage.$page.fullpath:', currRoute.fullPath) // console.log('lastPage.$page.options:', currRoute.options) // console.log('lastPage.options:', (lastPage as any).options) // 经过多端测试,只有 fullPath 靠谱,其他都不靠谱 - const { fullPath } = currRoute as { fullPath: string } + const { fullPath } = currRoute // console.log(fullPath) // eg: /pages/login/login?redirect=%2Fpages%2Fdemo%2Fbase%2Froute-interceptor (小程序) // eg: /pages/login/login?redirect=%2Fpages%2Froute-interceptor%2Findex%3Fname%3Dfeige%26age%3D30(h5) @@ -69,9 +72,9 @@ export function parseUrlToObj(url: string) { * 这里设计得通用一点,可以传递 key 作为判断依据,默认是 excludeLoginPath, 与 route-block 配对使用 * 如果没有传 key,则表示所有的 pages,如果传递了 key, 则表示通过 key 过滤 */ -export function getAllPages(key = 'excludeLoginPath') { +export function getAllPages(key?: string) { // 这里处理主包 - const mainPages = pages + const mainPages = (pages as PageMetaDatum[]) .filter(page => !key || page[key]) .map(page => ({ ...page, @@ -79,14 +82,14 @@ export function getAllPages(key = 'excludeLoginPath') { })) // 这里处理分包 - const subPages: any[] = [] - subPackages.forEach((subPageObj) => { + const subPages: PageMetaDatum[] = [] + ;(subPackages as SubPackages).forEach((subPageObj) => { // console.log(subPageObj) const { root } = subPageObj subPageObj.pages .filter(page => !key || page[key]) - .forEach((page: { path: string } & Record) => { + .forEach((page) => { subPages.push({ ...page, path: `/${root}/${page.path}`, @@ -100,14 +103,14 @@ export function getAllPages(key = 'excludeLoginPath') { export function getCurrentPageI18nKey() { const routeObj = currRoute() - const currPage = pages.find(page => `/${page.path}` === routeObj.path) + const currPage = (pages as PageMetaDatum[]).find(page => `/${page.path}` === routeObj.path) if (!currPage) { console.warn('路由不正确') return '' } console.log(currPage) console.log(currPage.style.navigationBarTitleText) - return currPage.style.navigationBarTitleText + return currPage.style?.navigationBarTitleText || '' } /** @@ -153,4 +156,4 @@ export const isDoubleTokenMode = import.meta.env.VITE_AUTH_MODE === 'double' * 首页路径,通过 page.json 里面的 type 为 home 的页面获取,如果没有,则默认是第一个页面 * 通常为 /pages/index/index */ -export const HOME_PAGE = `/${pages.find(page => page.type === 'home')?.path || pages[0].path}` +export const HOME_PAGE = `/${(pages as PageMetaDatum[]).find(page => page.type === 'home')?.path || (pages as PageMetaDatum[])[0].path}`