<template>
  <div id="app">
    <global-watermark />
    <keep-alive v-if="isRouterAlive">
      <router-view v-if="$route.meta.keepAlive"></router-view>
    </keep-alive>
    <router-view v-if="!$route.meta.keepAlive"></router-view>
  </div>
</template>

<script>
import {getUserInfo} from "@/service/api/category";
import {Toast} from "vant";
import GlobalWatermark from './components/watermark/watermark.vue';
export default {
  name: "",
  data() {
    return {
      isRouterAlive: true,
      qywxCode: '',
    }
  },
  components: {
    VanToast: Toast,
    GlobalWatermark
  },
  provide() { return { reload: this.reload } },
  created() {
    console.log(window.location)
    // const LOCATION_URL = 'https://lhlas-dev.zrubft.com/huiqicha-h5/#/dashboard/home'
    // 企业微信授权获取code
    // const index = window.location.href.indexOf('?');
    // const urlCode = window.location.href.substring(index+1);
    // const arrCode = urlCode.split('&');
    // this.qywxCode = arrCode[0].substring(5);
    const tokenUrl = window.location.href.split('=');
    if (tokenUrl[0].substr(-5) == 'token') {
          const u = navigator.userAgent;
          const isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
          if (tokenUrl[1].substr(-1) == '&') {
            sessionStorage.setItem('token', tokenUrl[1].slice(0, -1))
          } else {
            sessionStorage.setItem('token', tokenUrl[1])
          }
    }
    // Toast({
    //   duration: 18000,
    //   message: window.location.href + '++++++++' + tokenUrl[1]
    // })
    // sessionStorage.setItem('code', this.qywxCode)
    // if (window.location.href.substr(-4) == 'true' || window.location.href.substr(-5) == 'true/') {
    //   if (arrCode[0].substring(0, 4) == 'code') {
    //     console.log('true')
    //   } else {
    //     console.log('false')
    //     window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=wwc754dab83067dd33&redirect_uri=${LOCATION_URL}&response_type=code&scope=snsapi_privateinfo&state=3&agentid=1000037#wechat_redirect`;
    //   }
    // }
    // this.getUserId();
  },
  methods: {
    // 根据企业微信返回code获取userId
    async getUserId(){
      let result = await getUserInfo(this.qywxCode);
      localStorage.setItem('userId', result.userid);
    },
    reload() {
      this.isRouterAlive = false;
      this.$nextTick(() => { this.isRouterAlive = true })
    }

  },
}
</script>

<style lang="less">
#app {
  width: 100%;
  height: 100%;
}

body {
  margin: 0 !important;
}

.van-button--primary {
  background-color: #2372eb !important;
  border: 1px solid #2372eb !important;
}

.van-button--plain {
  background: #ffffff !important;
}

.van-button--plain.van-button--primary {
  color: #2372eb !important;
}
</style>
