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

<script>
import { mapGetters } from 'vuex'
import config from '@/config'

export default {
  name: 'App',
  data() {
    return {
      code: ''
    }
  },
  computed: {
    ...mapGetters(['isSupertube'])
  },
  async created() {
    // const ua = window.navigator.userAgent.toLowerCase();
    // this.isWechatEnv = ua.match(/micromessenger/i) == 'micromessenger' && window.location.search !== "?from=authorization";
    // if (this.isWechatEnv) {
    //   window.location.href = `${config.wechatUrl}?back=${config.backUrl}&name=${config.title}`
    // }else{
    //   window.location.href = `${config.wechatUrl}/#/wechat?back=${config.backUrl}&name=${config.title}`
    // }

    await this.getMenu()
  },
  mounted() {
    this.jumpToListPageWithDetailPage()
  },
  methods: {
    jumpToListPageWithDetailPage() {
      if (!this.isDetailPage() || this.isSupertube) return
      const listPageUrl = this.getListPageUrl()
      this.$router.push(listPageUrl)
    },
    isDetailPage() {
      const { path } = this.$route
      return path && path.indexOf('Detail')
    },
    getListPageUrl() {
      const { path = '' } = this.$route
      const [listPageUrl] = path.split('Detail')
      return listPageUrl
    },
    async getMenu() {
      this.$store
        .dispatch('user/getMenuList', this)
        .then(async(res) => {
          if (res === 'noToken') {
            await this.logout()
          } else if (res === 'success' || res === 'success-dataV') {
            if (
              this.$route.path === '/' ||
              this.$route.path === '/login' ||
              this.$route.path === '/loadingPage'
            ) {
              if (res === 'success-dataV') {
                await window.vm.$router.replace('/bigScreen/datavScr')
              } else {
                await window.vm.$router.replace('/welcome')
              }
            }
          } else {
            await this.logout()
          }
        })
        .catch((_err) => { this.logout() })
    },
    async logout() {
      if (this.$route.path !== '/login' && this.$route.path !== '/accept') {
        await this.$store.dispatch('user/logout')
        await this.$router.push(`/login`)
      }
    }
  }
}
</script>
<style lang="scss">
@import "./assets/css/index.scss";
//  因为element-ui的confim组件并不是vue生成的不具有[data-v....]，所以全局注入一个class
// .lzx_BtnErr{
//   background: #f56c6c !important;
//   color: #fff !important;
// }
</style>
