uniapp通过uni.addInterceptor实现路由拦截

注:此拦截不能首次拦截路由跳转的方法('switchTab', 'navigateTo', 'reLaunch', 'redirectTo'),拦截request请求api可以

  1. app.vue 代码
javascript 复制代码
	import { onLaunch} from '@dcloudio/uni-app'
	import permission  from './utils/permission'
	onLaunch(()=>{
		console.log('我是APP蓝翅')
		uni.request({
			url: 'https://apifoxmock.com/m1/4728220-0-default/api/user/getBanner',
			method: 'GET',
			success: (res)=>{
				console.log('res')
			}
		})
		permission()
	})
  1. permission 代码
javascript 复制代码
const checkAuth = function() {
	// 拦截器的API列表
	const apiList = ['switchTab', 'navigateTo', 'reLaunch', 'redirectTo']
	// 白名单列表
	const whitePath = ['/pages/logic/logic']
	apiList.map(item => {
		uni.addInterceptor(item, {
			invoke(args) {
				console.log(args)
				// 取出当前路由地址
				const path = args.url.split('?')[0]
				// 此处通过token来模拟是否登录,具体的根据项目来判断
				const token = uni.getStorageSync('token')
				
				console.log(token)
				// 判断是否在白名单内
				if (whitePath.includes(path)) {
					console.log('不需要登录')
					return 
				} else {
					if (token) {
						console.log('登录')
						// 登录了
						return
					} else {
						console.log('没有登录')
						// 未登录
						uni.navigateTo({
							url: `/pages/logic/logic?redirectTo=${encodeURIComponent(args.url)}`
						})
						return false
					}
				}

			}
		})
	})
}

export default checkAuth
  1. 登录简单实现代码
javascript 复制代码
<template>
	<view class="page-wrap">
		用户名:<input type="text" border="surround" v-model="username" />
		<up-button type="primary" @click="login">登录</up-button>
	</view>
</template>

<script setup>
	import {
		ref
	} from 'vue'
	import {
		onShow,
		onLoad
	} from '@dcloudio/uni-app'
	const username = ref('')
	const redirectPath = ref('')
	onLoad((p)=>{
		console.log(p)
	})
	onShow((props) => {
		console.log(props)
		console.log(redirectPath.value)
		console.log(getCurrentPages())
		const page = getCurrentPages()
		let currentPage = page[page.length - 1]
		redirectPath.value = currentPage?.$page?.options?.redirectTo || ''
	})
	const login = () => {
		if (username.value) {
			uni.setStorageSync('token', username)
			console.log((redirectPath.value))
			uni.redirectTo({
				url: decodeURIComponent(redirectPath.value),
				fail: (err) => {
					console.log(err)
					uni.switchTab({
						url: decodeURIComponent(redirectPath.value),
					})
				}
			})
		} else {
			uni.showToast({
				title: '请输入用户名'
			})
		}
	}
</script>

<style scoped>
	.page-wrap {
		padding: 20rpx;
	}
</style>
相关推荐
程序员大阳25 分钟前
副队长CSS教程(30)–CSS Grid 实战:实现复杂自适应表单布局
前端·css·自适应·grid·表单
Csvn30 分钟前
微前端实现
前端
Chrischaan1 小时前
做 AI 塔罗小程序时,我为什么不让大模型自己抽牌:一次上线复盘
前端
骑着蜗牛撵大象3272 小时前
LangChain 工具调用从声明到闭环:给智能体接上真实的手脚
前端
数据掘金2 小时前
百度统计后台页面数据异常?从手动调用时机开始排查
前端
hunteritself2 小时前
10 层 4K 实时剪辑,国产工作站进入广电制作核心
大数据·前端·人工智能·深度学习·transformer
踩着两条虫2 小时前
我在浏览器里“盖”了一个村:Vue + Three.js 大屏开发踩坑记
前端·vue.js·three.js
默_笙2 小时前
🧀 让大模型的答案"流"出来:NestJS AI 模块 + SSE + RxJS 实战
javascript·人工智能
mbshqqb2 小时前
TypeScript基础教程之同步生成器
前端·javascript·typescript
嘟嘟同学和妮妮同学2 小时前
【升级】一个中国历史可视化站:皇帝时间线梳理、古代思想流派、诗词鉴赏 (2)
前端·html