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>
相关推荐
开发者小天20 分钟前
为什么 /deep/ 现在不推荐使用?
前端·javascript·node.js
如白驹过隙1 小时前
cloudflare缓存配置
前端·缓存
excel1 小时前
JavaScript 异步编程全解析:Promise、Async/Await 与进阶技巧
前端
Jerry说前后端1 小时前
Android 组件封装实践:从解耦到架构演进
android·前端·架构
不如摸鱼去2 小时前
Trae 辅助下的 uni-app 跨端小程序工程化开发实践分享
微信小程序·小程序·uni-app·aigc·ai编程
步行cgn2 小时前
在 HTML 表单中,name 和 value 属性在 GET 和 POST 请求中的对应关系如下:
前端·hive·html
hrrrrb2 小时前
【Java Web 快速入门】十一、Spring Boot 原理
java·前端·spring boot
找不到工作的菜鸟2 小时前
Three.js三大组件:场景(Scene)、相机(Camera)、渲染器(Renderer)
前端·javascript·html
定栓2 小时前
vue3入门-v-model、ref和reactive讲解
前端·javascript·vue.js
专注API从业者2 小时前
基于 Flink 的淘宝实时数据管道设计:商品详情流式处理与异构存储
大数据·前端·数据库·数据挖掘·flink