Merge pull request #297 from dwyanewang/main

refactor(utils): 优化类型定义和类型安全
This commit is contained in:
菲鸽
2025-09-29 20:28:03 +08:00
committed by GitHub
4 changed files with 55 additions and 14 deletions

30
src/pages/404/index.vue Normal file
View File

@@ -0,0 +1,30 @@
<script lang="ts" setup>
import { HOME_PAGE } from '@/utils'
definePage({
style: {
// 'custom' 表示开启自定义导航栏,默认 'default'
navigationStyle: 'custom',
},
})
function goBack() {
// 当pages.config.ts中配置了tabbar页面时使用switchTab切换到首页
// 否则使用navigateTo返回首页
uni.switchTab({ url: HOME_PAGE })
}
</script>
<template>
<view class="h-screen flex flex-col items-center justify-center">
<view> 404 </view>
<view> 页面不存在 </view>
<button class="mt-6 w-40 text-center" @click="goBack">
返回首页
</button>
</view>
</template>
<style lang="scss" scoped>
//
</style>

View File

@@ -10,6 +10,7 @@ export const isNeedLoginMode = LOGIN_STRATEGY === LOGIN_STRATEGY_MAP.DEFAULT_NEE
export const LOGIN_PAGE = '/pages/login/login'
export const REGISTER_PAGE = '/pages/login/register'
export const NOT_FOUND_PAGE = '/pages/404/index'
export const LOGIN_PAGE_LIST = [LOGIN_PAGE, REGISTER_PAGE]

View File

@@ -7,7 +7,7 @@ import { isMp } from '@uni-helper/uni-env'
import { useTokenStore } from '@/store/token'
import { isPageTabbar, tabbarStore } from '@/tabbar/store'
import { getAllPages, getLastPage, HOME_PAGE, parseUrlToObj } from '@/utils/index'
import { EXCLUDE_LOGIN_PATH_LIST, isNeedLoginMode, LOGIN_PAGE, LOGIN_PAGE_ENABLE_IN_MP } from './config'
import { EXCLUDE_LOGIN_PATH_LIST, isNeedLoginMode, LOGIN_PAGE, LOGIN_PAGE_ENABLE_IN_MP, NOT_FOUND_PAGE } from './config'
export const FG_LOG_ENABLE = false
export function judgeIsExcludePath(path: string) {
@@ -43,6 +43,13 @@ export const navigateToInterceptor = {
path = `${baseDir}/${path}`
}
// 处理路由不存在的情况
if (getAllPages().every(page => page.path !== path)) {
console.warn('路由不存在:', path)
uni.navigateTo({ url: NOT_FOUND_PAGE })
return false // 明确表示阻止原路由继续执行
}
// 处理直接进入路由非首页时tabbarIndex 不正确的问题
tabbarStore.setAutoCurIdx(path)
@@ -104,9 +111,9 @@ export const navigateToInterceptor = {
uni.navigateTo({ url: redirectUrl })
return false // 修改为false阻止原路由继续执行
}
return true // 明确表示允许路由继续执行
}
// #endregion 2/2 默认不需要登录的情况(黑名单策略) ---------------------------
return true // 明确表示允许路由继续执行
},
}

View File

@@ -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<AnyObject, object> & { $page: Page.PageInstance<AnyObject, object> & { 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<string, any>) => {
.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}`