移动端通过携带token访问html页面

项目中有一个页面需要放到移动端,pc端是通过账号密码登录获取token,跳转网页前,在beforeEach中验证是否已有token,但是在移动端就访问这一个页面,为了避免登录就需要直接用token去访问,我是通过?把token拼接在网址后面

判断移动端还是pc端,在beforeEach之前判断,这样pc端就不能通过token去访问

window.location.href=window.location.href.split('?token=')[0]  //跳转网页

   因为在beforeach中一旦判断没有token就会跳转到登录页,这时候token是没有存下来的,所以需要重新访问,而且这样写是为了避免陷入死循环,如果没有token就不会再跳转

function browserRedirect() {
	   var sUserAgent = navigator.userAgent.toLowerCase();
	   if (/ipad|iphone|midp|rv:1.2.3.4|ucweb|android|windows ce|windows mobile/.test(sUserAgent)) {
		   //移动端页面
		  var phonetoken=window.location.href.split('?token=')[1]
		  if(phonetoken){
			 setToken(phonetoken)
			   store.state.token=phonetoken
			   window.location.href=window.location.href.split('?token=')[0]  //跳转网页
               //这样写是为了避免陷入死循环,如果没有token就不会再跳转
		  }
		   
	   } else {
		   //pc端页面
	   }
   }
browserRedirect(); 

这样写移动端是可以通过token去访问项目中所有的页面,如果要限制可访问页面,可以通过判断window.location.href.split('?token=')[0] ,只有是我们允许访问的网址才可跳转

原文地址:https://www.cnblogs.com/kingjordan/p/12026977.html