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