main.js 3.6 KB
Newer Older
E
Evan 已提交
1 2 3 4 5 6 7 8 9
// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import App from './App'
import router from './router'
import mavonEditor from 'mavon-editor'
import 'mavon-editor/dist/css/index.css'
E
Evan 已提交
10
import store from './store'
E
Evan 已提交
11 12 13

var axios = require('axios')
axios.defaults.baseURL = 'http://localhost:8443/api'
E
Evan 已提交
14
// 使请求带上凭证信息
15
axios.defaults.withCredentials = true
E
Evan 已提交
16 17 18 19 20 21

Vue.prototype.$axios = axios
Vue.config.productionTip = false
Vue.use(ElementUI)
Vue.use(mavonEditor)

E
Evan 已提交
22
// 如果前端没有登录信息则直接拦截,如果有则判断后端是否正常登录(防止构造参数绕过)
E
Evan 已提交
23
router.beforeEach((to, from, next) => {
E
Evan 已提交
24 25 26 27 28 29
    if (store.state.user.username && to.path.startsWith('/admin')) {
      axios.get('/authentication').then(resp => {
        console.log('菜单加载成功')
        initAdminMenu(router, store)
      })
    }
E
Evan 已提交
30
    if (to.meta.requireAuth) {
E
Evan 已提交
31
      if (store.state.user.username) {
E
Evan 已提交
32 33 34
        axios.get('/authentication').then(resp => {
          if (resp) next()
        })
E
Evan 已提交
35
        next()
E
Evan 已提交
36 37 38 39 40 41 42 43 44 45 46
      } else {
        next({
          path: 'login',
          query: {redirect: to.fullPath}
        })
      }
    } else {
      next()
    }
  }
)
E
Evan 已提交
47

E
Evan 已提交
48
// http request拦截器,为请求加上 token,测试用
E
Evan 已提交
49
/* axios.interceptors.request.use(
E
Evan 已提交
50
  config => {
E
Evan 已提交
51 52
    // 输出当前状态下的 token
    // console.log(store.state.user.token)
E
Evan 已提交
53
    if (store.state.user.token) {
E
Evan 已提交
54
      // 判断当前是否存在token,如果存在的话,则每个http header都加上token
E
Evan 已提交
55 56
      // config.headers.Token = `token ${JSON.stringify(store.state.user.token)}`
      config.headers.Token = JSON.stringify(store.state.user.token)
E
Evan 已提交
57 58
    } else {
      config.headers.Token = null
E
Evan 已提交
59 60 61 62 63
    }
    return config
  },
  err => {
    return Promise.reject(err)
E
Evan 已提交
64
  }
E
Evan 已提交
65
) */
E
Evan 已提交
66 67 68 69 70 71 72

// http response 拦截器
axios.interceptors.response.use(
  response => {
    return response
  },
  error => {
E
Evan 已提交
73 74 75
    console.log(error.response)
    if (error) {
      router.replace({
E
Evan 已提交
76 77
        path: 'login'
        // query: {redirect: router.currentRoute.fullPath}
E
Evan 已提交
78
      })
E
Evan 已提交
79 80 81 82 83
    }
    // 返回接口返回的错误信息
    return Promise.reject(error.response.data)
  })

E
Evan 已提交
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
export const initAdminMenu = (router, store) => {
  if (store.state.adminMenus.length > 0) {
    return
  }
  axios.get('/menu').then(resp => {
    if (resp && resp.status === 200) {
      var fmtRoutes = formatRoutes(resp.data)
      router.addRoutes(fmtRoutes)
      store.commit('initAdminMenu', fmtRoutes)
    }
  })
}

export const formatRoutes = (routes) => {
  let fmtRoutes = []
  routes.forEach(route => {
    let {
      path,
      component,
      name,
      nameZh,
E
Evan 已提交
105
      iconCls,
E
Evan 已提交
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
      children
    } = route

    if (children) {
      children = formatRoutes(children)
    }
    let fmtRoute = {
      path: path,
      component: resolve => {
        if (component.startsWith('User')) {
          require(['./components/admin/user/' + component + '.vue'], resolve)
        } else if (component.startsWith('Library')) {
          // require(['./library/' + component + '.vue'], resolve)
        } else if (component.startsWith('Admin')) {
          require(['./components/admin/' + component + '.vue'], resolve)
        }
      },
      name: name,
      nameZh: nameZh,
      meta: {
        requireAuth: true
      },
E
Evan 已提交
128
      iconCls: iconCls,
E
Evan 已提交
129 130 131 132 133 134 135
      children: children
    }
    fmtRoutes.push(fmtRoute)
  })
  return fmtRoutes
}

E
Evan 已提交
136 137 138
/* eslint-disable no-new */
new Vue({
  el: '#app',
139
  render: h => h(App),
E
Evan 已提交
140
  router,
E
Evan 已提交
141
  store,
E
Evan 已提交
142
  components: {App},
E
Evan 已提交
143 144
  template: '<App/>'
})