Explorar el Código

Merge pull request #297 from dwyanewang/main

refactor(utils): 优化类型定义和类型安全
菲鸽 hace 7 meses
padre
commit
cc3f953870
Se han modificado 4 ficheros con 55 adiciones y 14 borrados
  1. 30 0
      src/pages/404/index.vue
  2. 1 0
      src/router/config.ts
  3. 9 2
      src/router/interceptor.ts
  4. 15 12
      src/utils/index.ts

+ 30 - 0
src/pages/404/index.vue

@@ -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>

+ 1 - 0
src/router/config.ts

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

+ 9 - 2
src/router/interceptor.ts

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

+ 15 - 12
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 { isMpWeixin } from '@uni-helper/uni-env'
 import { pages, subPackages } from '@/pages.json'
 import { pages, subPackages } from '@/pages.json'
 
 
+export type PageInstance = Page.PageInstance<AnyObject, object> & { $page: Page.PageInstance<AnyObject, object> & { fullPath: string } }
+
 export function getLastPage() {
 export function getLastPage() {
   // getCurrentPages() 至少有1个元素,所以不再额外判断
   // getCurrentPages() 至少有1个元素,所以不再额外判断
   // const lastPage = getCurrentPages().at(-1)
   // const lastPage = getCurrentPages().at(-1)
   // 上面那个在低版本安卓中打包会报错,所以改用下面这个【虽然我加了 src/interceptions/prototype.ts,但依然报错】
   // 上面那个在低版本安卓中打包会报错,所以改用下面这个【虽然我加了 src/interceptions/prototype.ts,但依然报错】
   const pages = getCurrentPages()
   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'
  * redirectPath 如 '/pages/demo/base/route-interceptor'
  */
  */
 export function currRoute() {
 export function currRoute() {
-  const lastPage = getLastPage()
+  const lastPage = getLastPage() as PageInstance
   if (!lastPage) {
   if (!lastPage) {
     return {
     return {
       path: '',
       path: '',
       query: {},
       query: {},
     }
     }
   }
   }
-  const currRoute = (lastPage as any).$page
+  const currRoute = lastPage.$page
   // console.log('lastPage.$page:', currRoute)
   // console.log('lastPage.$page:', currRoute)
   // console.log('lastPage.$page.fullpath:', currRoute.fullPath)
   // console.log('lastPage.$page.fullpath:', currRoute.fullPath)
   // console.log('lastPage.$page.options:', currRoute.options)
   // console.log('lastPage.$page.options:', currRoute.options)
   // console.log('lastPage.options:', (lastPage as any).options)
   // console.log('lastPage.options:', (lastPage as any).options)
   // 经过多端测试,只有 fullPath 靠谱,其他都不靠谱
   // 经过多端测试,只有 fullPath 靠谱,其他都不靠谱
-  const { fullPath } = currRoute as { fullPath: string }
+  const { fullPath } = currRoute
   // console.log(fullPath)
   // console.log(fullPath)
   // eg: /pages/login/login?redirect=%2Fpages%2Fdemo%2Fbase%2Froute-interceptor (小程序)
   // eg: /pages/login/login?redirect=%2Fpages%2Fdemo%2Fbase%2Froute-interceptor (小程序)
   // eg: /pages/login/login?redirect=%2Fpages%2Froute-interceptor%2Findex%3Fname%3Dfeige%26age%3D30(h5)
   // 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 作为判断依据,默认是 excludeLoginPath, 与 route-block 配对使用
  * 如果没有传 key,则表示所有的 pages,如果传递了 key, 则表示通过 key 过滤
  * 如果没有传 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])
     .filter(page => !key || page[key])
     .map(page => ({
     .map(page => ({
       ...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)
     // console.log(subPageObj)
     const { root } = subPageObj
     const { root } = subPageObj
 
 
     subPageObj.pages
     subPageObj.pages
       .filter(page => !key || page[key])
       .filter(page => !key || page[key])
-      .forEach((page: { path: string } & Record<string, any>) => {
+      .forEach((page) => {
         subPages.push({
         subPages.push({
           ...page,
           ...page,
           path: `/${root}/${page.path}`,
           path: `/${root}/${page.path}`,
@@ -100,14 +103,14 @@ export function getAllPages(key = 'excludeLoginPath') {
 
 
 export function getCurrentPageI18nKey() {
 export function getCurrentPageI18nKey() {
   const routeObj = currRoute()
   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) {
   if (!currPage) {
     console.warn('路由不正确')
     console.warn('路由不正确')
     return ''
     return ''
   }
   }
   console.log(currPage)
   console.log(currPage)
   console.log(currPage.style.navigationBarTitleText)
   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 的页面获取,如果没有,则默认是第一个页面
  * 首页路径,通过 page.json 里面的 type 为 home 的页面获取,如果没有,则默认是第一个页面
  * 通常为 /pages/index/index
  * 通常为 /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}`