第三方登录示例:
第三方登录成功后,返回携带特定值的第三方登录链接(/trans):
https://xxxxx.com/trans?eid=62239&token=0ee8ad21d7444fabbabb412bd58c7563
router/index.js
// 全局路由守卫 - 前置守卫
router.beforeEach(( to , from , next) => {
// 当前连接 path 值为'/trans' 即第三方登录链接
if(to.path == '/trans') {
localStorage.setItem("token",to.query.token);
localStorage.setItem("eid",to.query.eid);
next({ path: '/'});
}
var token = localStorage.getItem("token");
// 当前链接非login.vue页面,且token值不存在
if (to.name != 'login' && !token) next({ name: 'login' });
next();
})
login.vue
created() {
// 判断当前 localStorage 是否存在 token 值
if(localStorage.getItem("token")) return this.$router.push('/');
const thirdUrl = "第三方登录地址";
const redirect = "重定向的目标地址";
const source = "来源参数";
window.location.href = 'https://'+thirdUrl+'?redirect='+redirect+'&source='+source;
// navigator.userAgent.indexOf('wxwork') 用于判断当前设备是否为企业微信
}