
import _ from 'lodash'
import Vue from 'vue'
import axios from 'axios'
import ELEMENT from 'element-ui'

import config from './config/index.js'
import Privilege from './common/privilege.js'
import components from './components/index.js'
import directives from './directives/index.js'
import filters from './filters/index.js'
import MixinELEMENT from './mixins/index.js'

import main from './pages/imageplayer/imagePlayerPreview.vue'

Vue.use(MixinELEMENT)
Vue.use(ELEMENT)
Vue.use(components)
Vue.use(directives)
Vue.use(filters)

if (process.env.NODE_ENV === 'development') {
  Vue.config.devtools = true
}
_.merge(axios.defaults, config.axios)

axios.interceptors.response.use(
  res => {
    return res
  },
  err => {
    if (err.message.includes('code 401')) {
      window.location.href = 'index.html'
      return
    }
    return Promise.reject(new Error(err))
  }
)

window.appInit = async function () {
  let res = await axios.get('/ui/application/privs')
  let privilege = new Privilege((res && res.data && res.data.data) || {})
  if (!res || !res.data) {
    console.warn('没有通过接口 /ui/application/privs 取到当前用户的权限配置', privilege)
  }

  window.app = new Vue({
    el: '#imagePlayerPreview',
    render: h => h(main)
  })
}
window.appInit()
