Vue登陆失效\未登陆跳转页面,在登陆后回到当时页面

场景:

直接提供某个页面路径带了很多参数,然后退回到登陆页面,再登陆就回到该页面。

场景一:

登录过期后回到登陆页;

let num = 0 
service.interceptors.response.use(
    (response) => {
        const res = response.data;

        if (res.code !== 200 && res.code !== '200' && !response.config.headers.apisass) {
            if (res.code == 2010) {
                num = num + 1;
                if (num == 1) {
                    return (window.location.href = window.location.origin + '/#/login?backUrl=' + encodeURIComponent(window.location.href));
                }
            }
            if (res.code === 2011) {
                num = num + 1;
                if (num == 1) {
                    return (window.location.href = window.location.origin + '/#/login?backUrl=' + encodeURIComponent(window.location.href));
                } 如果不判断就会多次拼接 window.location.origin是域名
            }
            if (showMsg) {
                Message({
                    message: res.message || res.msg,
                    type: 'error',
                    duration: 2.5 * 1000
                });
                showMsg = false;
                setTimeout(() => {
                    showMsg = true;
                }, 2000);
            }

            return Promise.reject(res.message || res.msg || 'ErrorError');
        } else {
            return res;
        }
    },
}
场景二:

没有登陆直接去

if (!role && to.path !== '/login' && !token) {
        Vue.prototype.$message.info('请登录');
        next('/login?backUrl=' + encodeURIComponent(window.location.href));
        return;
    }
登录处理:
   if (this.$route.query.backUrl) {
                                    window.location.href = decodeURIComponent(this.$route.query.backUrl);
                                } else {
                                    this.$router.push('/loanDataBoard2');
                                }

你可能感兴趣的:(前端,vue.js,前端,javascript)