uniapp h5页面跳转页面兼容

uniapph5页面使用浏览器自己的按钮返回会出现各种问题,比如onShow不执行,底部加载不执行,所以需要把跳转页面的方法重写,下面的代码加上了鉴权

import {
	getToken
} from '@/utils/auth'

// 登录页面
const loginPage = "/pages/login"

// 页面白名单
const whiteList = [
	'/','/pages/login', '/pages/register', '/pages/common/webview/index'
]

// 检查地址白名单
function checkWhite(url) {
	const path = url.split('?')[0]
	return whiteList.indexOf(path) !== -1
}

// 页面跳转验证拦截器
let list = ["navigateTo", "redirectTo", "reLaunch", "switchTab"]
// 处理通过浏览器按钮返回,触底加载,onShow不执行的bug 跳转页面用redirectTo,跳转时将页面路径记载到历史记录中
list.forEach(item => {
	uni.addInterceptor(item, {
		invoke(to) {
			if (getToken()) {
				let locationUrl = window.location.href.split("#")[1]
				// console.log('pathname:', location.href.split(location.host)[1])
				// if (pageList.includes(locationUrl)) {
				// 	history.pushState({}, '', location.href.split(location.host)[1]);
				// }else if(location.href.split(location.host)[1],includes('/pages/login')||){
				// 	history.pushState({}, '', location.href.split(location.host)[1]);
				// }
				history.pushState({}, '', location.href.split(location.host)[1]);
				if (to.url === loginPage) {
					uni.reLaunch({
						url: "/"
					})
				}
				return true
			} else {
				if (checkWhite(to.url)) {
					return true
				}
				uni.reLaunch({
					url: loginPage
				})
				return false
			}
		},
		fail(err) {
			console.log(err)
		}
	})
})

uni.addInterceptor('navigateBack', {
	invoke(to) {
		history.go(-1)
		return false
	},
	fail(err) {
		console.log(err)
	}
})

上面的js放到main.js中引入

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值