阴间需求之跨端登录

0. 缘起

后台要能跳到显示的对应界面,而且还免登录,鬼东西我刚知道真的是一头雾水,组长说能做,然后他就写了个半成品又丢给我

1. 具体

    // 后台部分
    async previewUIConfig(val) {
      let res = await getTokenConvert();
      let token = res?.data ?? "";
      window.open(
        `http://localhost:9500/login?token=${token}&classifyId=${val.classifyId}&enterpriseId=${val.enterpriseId}`
      );
    },

后台需要给显示界面一些用于跳过登录的信息,比如token之类的。

显示界面这里要做判断,是来自正常登录请求还是跳登录的请求。是后者需要拿对应token匹配,并且存入相关信息。

// 动态路由添加 + 权限验证
router.beforeEach(async (to, from, next) => {
    let token = null, classifyId = null, enterpriseId = null, enterpriseName = '';
    if (location.search && to.query.token) {
        const params = location.search.split("&");
        token = params[0].replace("?token=", "");
        classifyId = params[1].replace("classifyId=", "")
        enterpriseId = params[2].replace("enterpriseId=", "");
    }
    if (token) {
        store.commit("SET_TOKEN", token);
        // ACROSS PLATFORM LOGIN : SET INFO
        if (classifyId !== null && enterpriseId != null) {
            store.commit("SET_CLASSIFYID", classifyId);
            let info = { enterpriseId: enterpriseId }
            getEnterpriseInfoById(info).then(res => {
                store.commit("SET_ENTERPRISE", res.data)
            })
            let deliver = {
                classifyId: classifyId,
                platform: 3,
            };
            await store.dispatch("addRoutes", deliver)
        }

        if (store.state.routers.length) {
            enterpriseName = store.state.enterprise.name;
            document.title = enterpriseName + '-sassfd';
            next(`/${store.state.routers[0].router}`);
        } else {
            next("/login")
        }
    }
    ...

const params = location.search.split("&");
location.search在客户端获取Url参数的方法
location.search是从当前URL的?号开始的字符串
如:http://www.baidu.com/s?wd=baidu&cl=3
它的search就是?wd=baidu&cl=3

参考地址
这一段是获取来源路由携带东西的重要代码,我憨憨,没注意到params里面依次取。

人生到处知何似,应似飞鸿踏雪泥。
原文地址:https://www.cnblogs.com/lepanyou/p/15728497.html