liuxiaolong
2019-05-06 8700cf1dc46c350371d865532c2914595187788e
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
// import 'babel-polyfill'
import 'fetch-detector'
import 'fetch-ie8'
 
import Vue from 'vue'
import App from './App'
import router from './router'
import BootstrapVue from 'bootstrap-vue'
import Notifications from 'vue-notification'
import moment from 'moment'
import sweetalert from 'sweetalert'
import globals from './globals'
import Popper from 'popper.js'
import store from './store'
import qs from 'qs'
import {
  httpImg,
  noData,
  fButton,
  vLoading
} from '@/components/common'
import Toast from '@/components/common/toasted'
// import httpAxios from '@/server/common/httpAxios'
import {
  httpGET,
  httpPOST,
  httpPUT,
  httpDEL
} from '@/server/common/httpFetch'
 
import { getLoginUserData } from '@/server/login.js'
// Required to enable animations on dropdowns/tooltips/popovers
Popper.Defaults.modifiers.computeStyle.gpuAcceleration = false
 
Vue.config.productionTip = false
// Vue.prototype.$httpAxios = httpAxios
 
Vue.prototype.$httpGET = httpGET
Vue.prototype.$httpPOST = httpPOST
Vue.prototype.$httpPUT = httpPUT
Vue.prototype.$httpDEL = httpDEL
 
Vue.prototype.$moment = moment
Vue.prototype.$swal = sweetalert
Vue.prototype.$toast = Toast
Vue.use(BootstrapVue)
Vue.use(qs)
Vue.use(httpImg)
Vue.use(noData)
Vue.use(fButton)
Vue.use(vLoading)
Vue.use(Notifications)
// Global RTL flag
Vue.mixin({
  data: globals
})
// 全局导航钩子
router.beforeEach((to, from, next) => {
  // 判断URL中的登陆数据
  let urlLoginInfo = to.query
  if (
    urlLoginInfo &&
    urlLoginInfo.expires_in &&
    urlLoginInfo.expires_in !== '' &&
    urlLoginInfo.loginedInfo &&
    urlLoginInfo.loginedInfo !== ''
  ) {
    sessionStorage.setItem('expires_in', urlLoginInfo.expires_in)
    sessionStorage.setItem('loginedInfo', urlLoginInfo.loginedInfo)
  }
  store.commit('GET_USER_INFO')
  // 判断该路由是否需要登录权限
  if (to.meta.requireAuth) {
    // 通过vuex state获取当前的token是否存在
    let token = sessionStorage.getItem('loginedInfo') && JSON.parse(sessionStorage.getItem('loginedInfo')).access_token
    if (token && token !== '') {
      // 判断用户登陆信息 并存储
      if (
        sessionStorage.getItem('userInfo') &&
        sessionStorage.getItem('userInfo') !== ''
      ) {
        next()
      } else {
        /* 获取用户数据  后跳转 */
        getLoginUserData().then((result) => {
          store.commit('GET_USER_INFO')
          // next()
          router.replace(to.path)
        })
      }
    } else {
      next({
        path: '/login',
        query: {
          redirect: to.fullPath
        } // 将跳转的路由path作为参数,登录成功后跳转到该路由
      })
    }
  } else {
    next()
  }
})
/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  template: '<App/>',
  components: { App }
})