**
传token端:
**
注:window._CONFIG[‘tokenURL’] 是接收token端的地址
1.写到index.html中:
window.addEventListener('message', function(event) {
if (event.origin === window._CONFIG['tokenURL']) {
const { key } = event.data;
const value = localStorage.getItem(key);
event.source.postMessage({wallets: value}, event.origin);
}
}, false);
2.跳转到接收token端时:
window.open(window._CONFIG["tokenURL"] + "?type=isLogin");
3.路由守卫中:
if(to.query && to.fullPath == '/user/login?type=isLogout'){
store.dispatch('Logout').then(() => {
next({
path: '/user/login',
query: {
redirect: '/'
}
})
})
}
**
接收token端:
**
注:window._CONFIG[‘tokenURL’] 是传token端的地址
1.写到index.html中:
<iframe id="bbb-iframe" src="传token端的地址" style="display:none;"></iframe>
2.路由守卫页面(在跳转进入之前判断是否已登录)
// 校验登录
router.beforeEach(function(to, from, next) {
// 如果是从后台管理跳转过来的话走这个判断
if(to.query && to.fullPath == '/?type=isLogin'){
window.onload = function () {
const bbbIframe = document.getElementById("bbb-iframe");
bbbIframe.contentWindow.postMessage(
{ key: "pro__Access-Token" },
window._CONFIG['tokenURL']
);
};
window.addEventListener(
"message",
function (event) {
if (event.origin === window._CONFIG['tokenURL']) {
let tokenObj = JSON.parse(event.data.wallets),
token = "",
datas = {};
if (tokenObj) {
token = tokenObj.value;
let paramss = { name: "token", value: token };
datas = Object.assign(paramss, { startTime: new Date().getTime() });
}
localStorage.setItem("ACCESS_TOKEN", JSON.stringify(datas));
//页面是否登录,本地存储中是否有token(uid)数据,否:跳转登录页面
let item = localStorage.getItem("ACCESS_TOKEN");
if (item) {
item = JSON.parse(item);
let date = new Date().getTime();
// 如果大于就是过期了,如果小于或等于就还没过期
if (date - item.startTime > EXPIRESTIME) {
localStorage.removeItem('ACCESS_TOKEN');
localStorage.removeItem('userInfo');
window.location.href=window._CONFIG['tokenURL']+"/user/login?type=isLogout"
} else {
if (to.path === '/login' || to.path === '/') {
next('/首页')
} else {
next()
}
}
} else {
if (to.path === '/login') {
window.location.href=window._CONFIG['tokenURL']+"/user/login?type=isLogout"
} else {
window.location.href=window._CONFIG['tokenURL']+"/user/login?type=isLogout"
}
}
}
},false);
}else{
//页面是否登录,本地存储中是否有token(uid)数据,否:跳转登录页面
let item = localStorage.getItem("ACCESS_TOKEN");
if (item) {
item = JSON.parse(item);
let date = new Date().getTime();
// 如果大于就是过期了,如果小于或等于就还没过期
if (date - item.startTime > EXPIRESTIME) {
localStorage.removeItem('ACCESS_TOKEN');
localStorage.removeItem('userInfo');
// next('/login')
window.location.href=window._CONFIG['tokenURL']+"/user/login?type=isLogout"
} else {
if (to.path === '/login' || to.path === '/' ) {
next('/首页')
} else {
next()
}
}
} else {
if (to.path === '/login') {
// next()
window.location.href=window._CONFIG['tokenURL']+"/user/login?type=isLogout"
} else {
// next('/login')
window.location.href=window._CONFIG['tokenURL']+"/user/login?type=isLogout"
}
}
}
});