zhouhao
2021-11-03 01138b4c3bfd1a69d3a3327e26c371f51fe46978
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
 * @Author: 小明丶
 * @Date: 2019-08-19 15:23:17
 * @LastEditors: 小明丶
 * @LastEditTime: 2021-01-08 10:00:52
 * @Description:
 */
import Vue from "vue";
import VueRouter from 'vue-router';
import routes from './routes';
import Store from '@/store';
Vue.use(VueRouter);
 
let router =  new VueRouter({
    mode:"hash", //history
    routes,
    scrollBehavior (to, from, savedPosition) {
        return {
            x:0,
            y:0
        }
    },
})
 
 
// 全局前置守卫:页面跳转前拦截
router.beforeEach((to, from, next) => {
 
    // 解决叮咚助学自动登录主题色设置问题
    window.sessionStorage.setItem("gotoPagetemp", to.path);
    // if(to.name == 'login-by-wx'){
    //     localStorage.user_pwd = 'undefined'
    // }
    if(to.meta && typeof to.meta.isLogin !=='undefined' ){
        //判断地址是否带有token参数有就截取存储
        if(location.href.split('?')[1]){
            if(location.href.split('?')[1].split('token=')[1]){
                let str = location.href.split('?')[1].split('token=')[1]
                localStorage.hjToken = str
            }
            if(location.href.split('?')[1].split('appToken=')[1]){
                let str = location.href.split('?')[1].split('appToken=')[1]
                if(str.indexOf('&') != -1){
                    var backUrl = str.split('&')[1].split('backUrl=')[1]
                    var appToken = str.split('&')[0]
                }else{
                    var appToken = str
                }
            }
        }
        let user_pwd = localStorage.user_pwd;
        let hjToken = localStorage.hjToken;
        // let appToken = sessionStorage.appToken;
        // 红茄登录
        if(typeof hjToken !='undefined' && hjToken){
            Store.dispatch('loginByToken',{
                vm:Vue.prototype,
                hjToken,
                next
            })
            return
        }
        // app跳转登录
        if(typeof appToken !='undefined' && appToken){
            document.title = ''
            Store.dispatch('loginByAppToken',{
                vm:Vue.prototype,
                appToken,
                backUrl,
                next
            })
            return
        }
        // 自动登录
        if(typeof user_pwd !=='undefined'){
            let userNo = localStorage.user_account,
                password = user_pwd;
            Store.dispatch('login',{
                vm:Vue.prototype,
                userNo,
                password,
                sib_mer_sysPlat: localStorage.sib_mer_sysPlat,
                next
            })
            return
        }
        if(typeof hjToken =='undefined' && !hjToken){
            next()
        }
    }else{
        // 需要登录的页面
        if(Store.state.sessionId || to.name == 'login-by-wx' || to.name == 'supplement'){
            next()
        }else{
            // if(sessionStorage.isLoginByOpenId == 1){
            //     next()
            // }else{
                next('/')
            // }
 
        }
    }
})
 
 
//全局后置守卫:页面跳转后执行
router.afterEach((to, from) => {
 
})
 
export default router;