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>
相关推荐
Apifox2 分钟前
如何让 Apifox 发布的在线文档具备更好的调试体验?
前端·后端·测试
2501_916013743 分钟前
App 上架全流程指南,iOS App 上架步骤、App Store 应用发布流程、uni-app 打包上传与审核要点详解
android·ios·小程序·https·uni-app·iphone·webview
咔咔一顿操作5 分钟前
【CSS 3D 交互】打造沉浸式 3D 照片墙:结合 JS 实现拖拽交互
前端·javascript·css·3d·交互·css3
0x0008 分钟前
Uniapp - 自定义 Tabbar 实现
前端·uni-app
用户4582031531710 分钟前
Flexbox布局上手:10分钟告别垂直居中难题
前端·css
牛蛙点点申请出战11 分钟前
仿微信语音 WaveView 实现
android·前端·ios
yiyesushu12 分钟前
react + next.js + ethers v6 项目实例
前端
明远湖之鱼13 分钟前
巧用 Puppeteer + Cheerio:批量生成高质量 Emoji 图片
前端·爬虫·node.js
落笔忆梦15 分钟前
利用浏览器空闲时间优化资源加载与渲染
前端·javascript
艾小码16 分钟前
还在用Vue 2硬撑?升级Vue 3的避坑指南来了!
前端·javascript·vue.js