<template>
  <div id="app">
    <m-nav-bar v-show="showNavBar" :title="title" />
    <router-view :class="routerViewClass" />
    <m-tab-bar v-show="showTabBar" :current="currentTab" />
  </div>
</template>

<script>
import { mapState, mapActions, mapMutations } from "vuex";
import {sysInfo,getUrlParam}  from "@/utils/common";
export default {
  data() {
    return {
      code: ''
    };
  },
  computed: {
    ...mapState(["currentTab", "title", "showNavBar", "showTabBar"]),

    routerViewClass() {
      let retClass = ["router-view-con"];
      if (this.showNavBar) {
        retClass.push("router-view-nav-bar");
      }
      if (this.showTabBar) {  
        retClass.push("router-view-tab-bar");
      }
      return retClass;
    },
  },
  methods: {
    // ...mapActions(["getShoppingCart"]),
    ...mapMutations(['setAppVersion',"setCurrentTime"]),
    _getSystemTime(){
      this.$api({
        apiName:"getSystemTime",
        success:(res)=>{
          let time=new Date(res).getTime()
          this.setCurrentTime(time)
          this.timeTask = setInterval(() => {
            time+=1000
            this.setCurrentTime(time)
          }, 1000);
        },
        fail: (err)=>{
          this.timeTask = setInterval(() => {
            this.setCurrentTime(+new Date())
          }, 1000);
        }
      })
    }
  },
  mounted() {
    var local = window.location.href // 获取页面url
    var appid = 'wxbdddd81913c795e9'
    let url=window.location.origin+"/"+window.location.hash
    this.code = getUrlParam('code') // 截取code
    if (this.code == null || this.code === ''){
      // 没有code去拿code;
      if(sysInfo.isWechat) {
          window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${encodeURIComponent(url)}&response_type=code&scope=snsapi_base&state=1#wechat_redirect`;
      }
    }else{
      // 获取得code
      // 拿着code去获取openId
      // console.log(window.location)
      // 清除code 防止转发自动添加code
      // window.location.search=window.location.search+"&isUse=1"
      if(!localStorage.getItem('openId')) {
        this.$api({
          apiName: 'getWxOpenId',
          postData: {code:this.code},
          success: (res) => {
            localStorage.setItem('openId',res)
          },
          error: () => {
            // console.log(error)
            if(sysInfo.isWechat) {
                window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${encodeURIComponent(url)}&response_type=code&scope=snsapi_base&state=1#wechat_redirect`;
            }
            this.code=''
          },
        })
      }
    }
    this.getVersionCode(e => {
      this.setAppVersion(e)
    }) 
    this._getSystemTime()
  }
};
</script>

<style lang="less">
#app {
  width: 100vw;
  height: 100%;
  > .m-nav-bar {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    z-index: 10;
  }
  > .router-view-con {
    height: 100%;
  }
  > .m-tab-bar {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
  }

  > .router-view-nav-bar {
    padding-top: @mall-nav-bar-height;
  }
  > .router-view-tab-bar {
    padding-bottom: @mall-tab-bar-height;
  }
}
</style>
