zhongrj
2023-06-27 d74b495736f0c04522ec54274b70fed0ded2878d
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/**
 * 全站权限配置
 *
 */
import md5 from 'js-md5'
import request from '@/router/axios';
import { Message } from 'element-ui'
 
import router from './router/router'
import store from './store'
import {validatenull} from '@/util/validate'
import {getToken} from '@/util/auth'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
NProgress.configure({showSpinner: false});
const lockPage = store.getters.website.lockPage; //锁屏页
router.beforeEach((to, from, next) => {
  const meta = to.meta || {};
  const isMenu = meta.menu === undefined ? to.query.menu : meta.menu;
  store.commit('SET_IS_MENU', isMenu === undefined);
  if (getToken()) {
    if (store.getters.isLock && to.path !== lockPage) { //如果系统激活锁屏,全部跳转到锁屏页
      next({path: lockPage})
    } else if (to.path === '/login') { //如果登录成功访问登录页跳转到主页
      next({path: '/'})
    } else {
      //如果用户信息为空则获取用户信息,获取用户信息失败,跳转到登录页
      if (store.getters.token.length === 0) {
        store.dispatch('FedLogOut').then(() => {
          next({path: '/login'})
        })
      } else {
        const value = to.query.src || to.fullPath;
        const label = to.query.name || to.name;
        const meta = to.meta || router.$avueRouter.meta || {};
        const i18n = to.query.i18n;
        if (to.query.target) {
          window.open(value)
        } else if (meta.isTab !== false && !validatenull(value) && !validatenull(label)) {
          store.commit('ADD_TAG', {
            label: label,
            value: value,
            params: to.params,
            query: to.query,
            meta: (() => {
              if (!i18n) {
                return meta
              }
              return {
                i18n: i18n
              }
            })(),
            group: router.$avueRouter.group || []
          });
        }
        next()
      }
    }
  } else {
    //判断是否需要认证,没有登录访问去登录页
    if (meta.isAuth === false) {
      next()
    } else {
      if (to.path == '/wel/index') {
        // console.log('进入首页-----')
        var a = to.query.securitySupervisionSystem;
        if (a == undefined || typeof a != "string") {
          // console.log("无securitySupervisionSystem参数  跳回登入");
          next('/login');
          next('/login');
          return;
        }
        // console.log("有securitySupervisionSystem参数");
        try {
          // console.log("判断securitySupervisionSystem是否能被JSOn解码");
          var obj = JSON.parse(a);
          if (typeof obj == "object" && obj && obj.tokenMY == '987654321S') {
            // console.log("成功解码 并为对象");
            debugger
            var data = {
              tenantId: obj.data.dip,
              username: obj.data.rese,
              password: md5(obj.data.sap),
              grant_type: 'password',
              scope: 'all',
              type: 'account'
            };
            request({
              url: '/api/blade-auth/oauth/token',
              method: 'post',
              headers: {
                'Authorization': 'Basic c2FiZXI6c2FiZXJfc2VjcmV0',
                "Tenant-Id": "000000"
              },
              params: data
            }).then(res => {
              const data = res.data;
              if (data.error_description) {
                // console.log("登入失败");
                Message({
                  message: data.error_description,
                  type: 'error'
                })
                next('/login');
                next('/login');
                return;
              } else {
                // console.log("登入成功");
                store.commit('SET_TOKEN', data.access_token);
                store.commit('SET_REFRESH_TOKEN', data.refresh_token);
                store.commit('SET_TENANT_ID', data.tenant_id);
                store.commit('SET_USER_INFO', data);
                store.commit('DEL_ALL_TAG');
                store.commit('CLEAR_LOCK');
                next('/wel');
                return;
              }
            })
            return;
          } else {
            // console.log("成功解码 不是对象");
            next('/login');
            next('/login');
            return;
          }
        } catch {
          // console.log('不是Json对象 跳回登入');
          next('/login');
          next('/login');
          return;
        }
        // return;
      }
      next('/login')
    }
  }
})
 
router.afterEach(() => {
  NProgress.done();
  let title = store.getters.tag.label;
  let i18n = store.getters.tag.meta.i18n;
  //title = router.$avueRouter.generateTitle(title, i18n)
  title = router.$avueRouter.generateTitle(title, i18n);
  //判断登录页的情况
     if (router.history.current.fullPath === "/login") {
       title = "登录";
     }
  //根据当前的标签也获取label的值动态设置浏览器标题
  router.$avueRouter.setTitle(title);
});