当前位置: 首页 > ai >正文

h5页面路由白名单限制

 1.判断当前设备是移动端还是pc端,设置对应路由移动端为/m开头,pc为/pc开头

pc:

mobile:

export function getDeviceType() {const userAgent = navigator.userAgent;const isMobile = /Android|iPhone|iPad|iPod|Mobile/i.test(userAgent);return isMobile
}export enum DeviceRoutePath {Mobile = '/m',PC = '/pc'
}
const isMobile = getDeviceType();
const deviceType = isMobile ? DeviceRoutePath.Mobile : DeviceRoutePath.PC;
export { deviceType, isMobile }

2.router.ts

import { createRouter, createWebHistory } from 'vue-router'
import ThirdLogin from './modules/thirdLogin'
import { useLoginStore } from '@/store/modules/login'
import Home from './modules/home'
import { deviceType, isMobile } from '@/utils/router'
import { useRouteConfig } from '@/hooks/useRouteConfig'
import whiteRoute from './modules/whiteRoute'// 设备平台检查守卫
const platformGuard = (to: any, from: any, next: any) => {if (to.meta.platform) {if ((isMobile && to.meta.platform === 'pc') ||(!isMobile && to.meta.platform === 'mobile')) {const redirectPath = to.path.replace(isMobile ? '/pc/' : '/m/',isMobile ? '/m/' : '/pc/')return next(redirectPath)}}return next()
}// 认证守卫
const authGuard = (to: any, from: any, next: any) => {const { token } = useLoginStore().loginData// 增加白名单和免验证路径,根据需求自行调整const { isWhiteListRoute, isBypassPath } = useRouteConfig()if (!token && !isWhiteListRoute(to.name as string) && !isBypassPath(to.path)) {// 将当前要访问的路径作为 redirectUrl 参数const redirectUrl = encodeURIComponent(to.fullPath)return next({path: `${deviceType}/home`,query: { redirectUrl }})}return next()
}const routes = [ThirdLogin,Home,whiteRoute,
]const router = createRouter({history: createWebHistory(),routes: routes
})// 注册全局前置守卫
router.beforeEach(async (to, from, next) => {try {// 按顺序执行守卫const guards = [platformGuard, authGuard]// 创建一个执行守卫的函数const executeGuards = async (index = 0) => {if (index >= guards.length) {return next()}await new Promise((resolve) => {guards[index](to, from, (result: any) => {if (result === undefined) {executeGuards(index + 1).then(resolve)} else {next(result)resolve(null)}})})}await executeGuards()} catch (error) {console.error('路由守卫执行错误:', error)next(false)}
})export default router

3.home.ts

import type { RouteRecordRaw } from 'vue-router'
import { deviceType, isMobile } from '@/utils/router';
import Layout from '@/views/home/pc/layout.vue'export default {path: '/',redirect: (to) => deviceType + '/home',component: isMobile ? '' : Layout,//无router-view 可以不使用children: [{path: '/m/home',component: () => import('@/views/home/mobile/home.vue'),name: 'MobileHome',meta: { name: 'Home', platform: 'mobile' }},{path: '/pc/home',component: () => import('@/views/home/pc/home.vue'),name: 'PCHome',meta: { name: 'Home', platform: 'pc' }}]
} as RouteRecordRaw

http://www.xdnf.cn/news/4756.html

相关文章:

  • 数字化转型:概念性名词浅谈(第二十五讲)
  • 油藏模拟开源资源
  • 心跳策略(Heartbeat) 和 Ping/Echo 策略
  • MacBook M2芯片 Sequoia15.4.1 安装免费版VMware Fusion 13.6.3版本
  • Flutter接入ProtoBuff和原生Android通信【性能最优】
  • day23-集合(泛型Set数据结构)
  • A. Row GCD(gcd的基本性质)
  • C++模板【下篇】— 详解模板进阶语法及模板细节
  • 软考知识点汇总
  • [java八股文][Java并发编程面试篇]场景
  • 基于Java实现(PC)民航订票管理系统
  • 关于Bearer Token
  • System-V 共享内存
  • Java流程控制
  • 果汁厂通信革新利器:Ethernet/IP转CANopen协议网关
  • 为什么跨境电商要了解固定IP?常见疑问解析
  • 算法竞赛进阶指南.次小生成树
  • 同比和环比有什么区别?同比和环比的计算方法
  • Oracle OCP认证考试考点详解083系列12
  • RISC-V hardfault分析工具,RTTHREAD-RVBACKTRACE
  • C语言 指针(9)
  • 初学者如何获得WordPress技术支持
  • 模拟内存管理
  • 如何添加二级域名
  • Linux操作系统中的通知机制
  • 单片机 + 图像处理芯片 + TFT彩屏 指示灯控件
  • python小记(十四):Python 中 **参数解包:深入理解与应用实践
  • 【java】oop 结课模拟题版
  • 探索大语言模型(LLM):硅基流动+Cherry studio免费白嫖Qwen3模型
  • librosa.load 容易遇到的采样率问题