<template>
  <router-view v-slot="{ Component, route }">
    <keep-alive :include="cacheList">
      <component :is="Component" :key="route.name" @updateShare="updateShare" />
    </keep-alive>
  </router-view>
</template>

<script>
export default {
  name: 'App',
  computed: {
    cacheList() {
      const routes = this.$router.getRoutes()
      return routes.filter(r => r.meta.cache).map(r => r.name)
    }
  },
  data() {
    return {
      wechatShareInfo: {}
    }
  },
  watch: {
    $route(to, from) {
      document.title = this.$route.meta.title || this.sysConfig.Base.SiteName || '';
      const recommend = this.$route.query.RecommendCode;
      if (recommend && !['null', 'undefined'].includes(recommend)) {
        const curRecommend = this.$ls.get('recommend');
        if (Array.isArray(recommend)) {
          this.$ls.set('recommend', recommend[recommend.length - 1]);
        }
        else {
          this.$ls.set('recommend', recommend);
        }
        if (this.$route.query.url == 'Invite' && this.$isLogin()) {
          location.replace('#/');
        }
      }
      if (this.$route.query.openid)
        this.$ls.set('openid', this.$route.query.openid);
      if (this.$route.query.miniopenid)
        this.$ls.set('miniopenid', this.$route.query.miniopenid);
      if (!this.$route.meta.noLogin && !this.$isLogin()) {
        location.replace('#/Login');
      }
      // if (this.$isLogin()) {
      //     this.getTip();
      // }
      // 不要在路由变化时自动调用 updateShare，让子组件通过 emit 来触发
      this.updateShare(undefined, undefined, undefined);
    }
  },
  mounted() {
  },
  methods: {
    updateTitle(title) {
      document.title = title
      if (this.$isWechat()) {
        const iframe = document.createElement('iframe')
        iframe.style.display = 'none'
        iframe.src = 'https://www.baidu.com/favicon.ico?' + Date.now()
        iframe.onload = () => {
          setTimeout(() => {
            document.body.removeChild(iframe)
          }, 0)
        }
        document.body.appendChild(iframe)
      }
    },
    updateShare(title, desc, imgUrl) {
      if (wx && this.$isWechat()) {
        const url = location.href;
        const token = localStorage.getItem('member_token') || '';
        fetch(`${import.meta.env.VITE_API_URL}/v1/client/HWxinfoClient/share`, {
          method: 'POST',
          headers: {
            'Content-Type': 'application/json',
            'Authorization': `Bearer ${token}`,
          },
          body: JSON.stringify(url)
        }).then(res => {
          return res.json();
        }).then(res => {
          const data = res.data || {};
          let shareinfo = {
            title: title || data.title || '泰古润',
            desc: data.contents || '汇聚平凡梦想，共创美好未来',
            imgUrl: imgUrl || data.img,
            link: this.$getShareLink(),
          };
          const jsApiList = data.jsApiList || ['onMenuShareTimeline', 'onMenuShareAppMessage', 'updateAppMessageShareData', 'updateTimelineShareData'];
          const openTagList = data.openTagList || ['wx-open-launch-weapp'];
          jsApiList.push('wx-open-launch-weapp');
          wx.config({
            debug: false,
            appId: data.appid,
            timestamp: Number(data.timestamp) || 0,
            nonceStr: data.noncestr,
            signature: data.signature,
            jsApiList: jsApiList,
            openTagList: openTagList
          });
          wx.ready(() => {
            wx.updateAppMessageShareData(shareinfo);
            wx.updateTimelineShareData(shareinfo);
          });
          wx.error((e) => {
          });
        });
      } else {
      }
    },
  }
}
</script>