<template>
  <div id="app">
    <router-view />
  </div>
</template>

<script>
import { getGlobalInterface } from '@/api/data.js'
import { getUserInfo } from '@/api/user.js'
// omega埋点工具包
import OmegaTracker from '@didi/omega-tracker/lib/index.esm'
const config = { appKey: 'omega6b172861f4' }

export default {
  name: 'App',
  mounted() {
    window.addEventListener('offline', () => {
      // 网络由正常常到异常时触发
      this.$message({ message: '网络异常，请检查网络！', type: 'error', duration: 2000, offset: 150 })
    })
    window.addEventListener('online', () => {
      location.reload()
    })
    getUserInfo()
      .then(res => {
        localStorage.setItem('realname', res.data.chineseName)
        localStorage.setItem('username', res.data.username)
      })
  },
  created() {
    this.initOmegaTracker()
    this.initRoutes()
  },
  methods: {
    initOmegaTracker() {
      const omega = OmegaTracker.getTracker(config)
      return omega
    },
    initRoutes() {
      this.$store.dispatch('data/setRoutes', this.$router.options.routes)
      // 数据中心动态路由获取
      getGlobalInterface().then(res => {
        this.$store.dispatch('data/setMenu', res.data)
        const dataCenterRoutes = []
        const route = {
          path: '/data',
          component: () => import('@/layout'),
          redirect: '/data/upload-file',
          name: '数据中心',
          meta: { title: '数据中心' },
          children: [
            {
              path: 'upload-file',
              name: 'jar包管理',
              component: () => import('@/views/data/upload.vue'),
              meta: { title: 'jar包管理' }
            }
          ]
        }
        for (const i in res.data) {
          const routeItem = {
            path: res.data[i].subMenus[0].path + '/' + i,
            redirect: res.data[i].subMenus[0].path,
            name: res.data[i].menuName + '/' + i,
            component: () => import('@/views/data/index.vue'),
            meta: { title: res.data[i].menuName },
            children: [{ path: 'dc' }]
          }
          for (const j in res.data[i].subMenus) {
            const routeChildrenItem = {
              path: res.data[i].subMenus[j].path + '/' + i + '/' + j,
              name: res.data[i].subMenus[j].subMenuName + '/' + i + '/' + j,
              component: () => import('@/views/data/index.vue'),
              meta: { title: res.data[i].subMenus[j].subMenuName }
            }
            routeItem.children.push(routeChildrenItem)
          }
          route.children.push(routeItem)
        }
        dataCenterRoutes.push(route)
        this.$router.addRoutes(dataCenterRoutes)
        for (const i in this.$router.options.routes) {
          if (this.$router.options.routes[i].name === '工具集合') {
            this.$router.options.routes[i].children.push(route)
          }
        }
        this.$store.dispatch('data/setRoutes', this.$router.options.routes)
      })
    }
  }
}
</script>

<style>
/* el-table头部 */
.tableHead{
  color: rgba(0, 0, 0, 0.726);
}
/* 分页 */
.el-pagination{
  margin: 2% 0 2% 0;
}
.stylus-content{
    width: 98%;
    padding: 0.3% 1% 1% 1%;
    margin: 0 auto;
    background-color: #fff;
    border-radius: 4px;
}
</style>
