场景:最近公司的应用加上了网关测试访问入口,以及用户打开应用后长时间不操作,登陆态失效问题。
造成结果: #号后面的路由参走丢~ 所有页面都会跳回home路由~ 列: http://localhost:8080/#/mine=> http://localhost:8080/#/
解决思路。某个让用户访问应用时,url 走transfer_page页面 ,然后transfer_page页面把需要的路由参存起来,在让location.replace
期望结果:
http://xxxxx/aproval/transfer_page.html?category=xxxxxxxx
转换为:http://xxxxx/aproval/#/mine?category=xxxxxxxxxxx
<script>
function getParameter(n, s, d) {
let reg = new RegExp("(?:^|[&\\?])" + n + "=([^&#]*)(?:[&#].*|$)");
let val = (s || location.href || '').match(reg);
if (val) {
val = val[1];
}
val = val || '';
return d && val ? decodeURIComponent(val) : val;
}
location.replace(location.href.substring(0, location.href.indexOf('transfer_page')) + '#' + getParameter('router'))
</script>
用户访问的url:https://xxxxxx.gov.cn/zwwxgzt/pf/djzl/transfer_page.html?router=/mine
输出结果:url:https://xxxxxx.gov.cn/zwwxgzt/pf/djzl/#/mine(成功指定到对应页面)
ps:当然中专页里面还可以处理切换pc和移动入口的问题
<script>
function getParameter(n, s, d) {
var reg = new RegExp("(?:^|[&\\?])" + n + "=([^&#]*)(?:[&#].*|$)");
var val = (s || location.href || '').match(reg);
if (val) {
val = val[1];
}
val = val || '';
return d && val ? decodeURIComponent(val) : val;
}
var router = getParameter('router')
var openUrl = ''
var aPath = location.pathname.split("/");
var passId = ''
if(Array.isArray(aPath) && aPath.length >= 2){
passId = aPath[1]
}
// pc客户端访问跳转到pc
var userAgent = navigator.userAgent || ''
if (userAgent !== '') {
// PC Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/605.1.15 (KHTML, like Gecko) wxwork/1.2.2 (MicroMessenger/6.2) WeChat/2.0.4
userAgent = String(userAgent).toLowerCase()
// PC端处理
if (userAgent.indexOf('iphone') === -1 && userAgent.indexOf('android') === -1) {
openUrl = location.href.substring(0, location.href.indexOf(passId)) + passId + '/taskManPC/#/task' + router.replace('taskDetail', 'goingtask').replace('subTaskDetail', 'subTaskDetail')
// 移动端
}else{
openUrl = location.href.substring(0, location.href.indexOf('transfer_page')) + '#' + router
}
}
console.log(openUrl)
location.replace(openUrl)
</script>
网友评论