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

<script>
import './assets/css/reset.css'
import { createNamespacedHelpers } from 'vuex'
const { mapActions: mapIndexActions } = createNamespacedHelpers('user')
const { mapState: mapUserState } = createNamespacedHelpers('user')
export default {
  name: 'App',
  data () {
    return {
      showPage: false, // 不控制显示时机
      code: ''
    }
  },
  created () {
    this.init()
  },
  computed: {
    ...mapUserState({
      userInfo: item => item.userInfo,
      appid: item => item.appid
    })
  },
  methods: {
    ...mapIndexActions(['getAppId', 'getUser', 'getShare']),
    async init () {
      // this.showPage = true
      try {
        await this.getAppId()
        await this.getCode()
        await this.getUser({ code: this.code })
        this.showPage = true
        // this.getShare()
      } catch (err) {
        console.log(err)
      }
    },
    getCode () {
      return new Promise((resolve, reject) => {
        if (location.search && this.toolClass.UrlSearch(location.search).code) {
          this.code = this.toolClass.UrlSearch(location.search).code
          let oldCode = localStorage.getItem('code')
          if (this.code === oldCode) {
            localStorage.setItem('code', this.code)
            this.toolClass.getCode(this.appid)
          } else {
            localStorage.setItem('code', this.code)
            resolve()
          }
        } else {
          this.toolClass.getCode(this.appid)
        }
      })
    }
  }
}
</script>

<style>
#app {
  width: 100%;
  height: 100%;
  position: relative;
  overflow: hidden;
}
</style>
