WebView 微信小程序跳转h5项目,h5项目拿到token

h5vue项目接收token,防止用户从微信跳转h5,token失效,用户体验性差

在App.vue文件中Mouted里面

	getUrlData() {
				let tempStr = window.location.href
				let tempArr = tempStr.split('?')[1] ? tempStr.split('?')[1].split('&') : []
				let returnArr = {}
				tempArr.forEach(element => {
					returnArr[element.split('=')[0]] = element.split('=')[1]
				})
				console.log(returnArr);
				return returnArr
			}

在uniapp项目中,

1新建webView的页面,实现小程序跳转h5页面

<template>
	<view>
		<!-- #ifdef MP -->
		<web-view v-if="webURL" :src="webURL"></web-view>
		<!-- #endif -->
	</view>
</template>

<script>
import url from "../../common/config/baseUrl.js"
export default {
	data() {
		return {
			webURL: ''
		};
	},
	components: {},
	onLoad(option) {
		// console.log("url: " + JSON.stringify(url));
        //对路径进行匹配
		var url1 = option?.webURL;
		if(/*******/.test (url1)){
			this.webURL =decodeURIComponent(url1);
		}else if(/test/.test(url1)){
				this.webURL =decodeURIComponent(url1);
		}
		else{
			this.webURL = decodeURIComponent(url.url) + decodeURIComponent(url1);
		}
		// #ifdef H5
		window.location.href = this.webURL;
		// #endif
		console.log('url: ' + url1, this.webURL);
	},
	onShow() {},
	methods: {}
};
</script>

<style lang="scss" scoped></style>

 2.在需跳转h5的页面上

	goJump(data,index) {
			//#ifdef MP
			/* type:1 内部  2外部 */
			if (data.type == 2) {
				//跳转外部链接
				let url1=data.url+"?token="+this.token
				uni.navigateTo({
				
						url: '../../web-view/web-view?webURL=' + encodeURIComponent(url1)
				});
// encodeURIComponent(url) 函数
//把字符串作为 URI 组件进行编码。
// encodeURIComponent(URIstring)

     // URIstring 必需。一个字符串,含有 URI 组件或其他要编码的文本。

//返回值 

     // URIstring 的副本,其中的某些字符将被十六进制的转义序列进行替换。

说明 

     // 该方法不会对 ASCII 字母和数字进行编码,也不会对这些 ASCII 标点符号进行编码: - _ . ! ~* ' ( ) 。 
      //其他字符(比如 :;/?:@&=+$,# 这些用于分隔 URI 组件的标点符号),都是由一个或多个十六进制的转义序列替换的。
			} else {
				if(index==2){
					console.log("hjjj")
					let shopIndex=1
					uni.setStorageSync("shopIndex",shopIndex)
					console.log("shopIndex: " + JSON.stringify(shopIndex));
					uni.switchTab({
						url:data.url
					})
				}
				uni.navigateTo({
					url: data.url
				});
			}
			//#endif
			//#ifdef H5
			/* type:1 内部  2外部 */
			if (data.type == 2) {
				let url = '';
				//跳转外部链接
				if (/m/.test(window.location.href)) {
					//判断是否为正式环境
					url = 'https://m.*******.cn/' + data.url+"?token="+this.token;
				} else {
					url = 'https://test.*******.cn/' + data.url+"?token="+this.token;
				}
				window.open(url);
			} else {
				uni.navigateTo({
					url: data.url
				});
			}
			//#endif
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值