<template>
  <router-view v-slot="{ Component, route }">
    <keep-alive :include="cacheList">
      <component :is="Component" :key="route.name" />
    </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)
    }
  },
  watch: {
    $route(to, from) {
      const title = to.meta.title || ''
      this.updateTitle(title)
    }
  },
  methods: {
    updateTitle(title) {
      document.title = title
      if (this.$isWechat()) {
        // 微信安卓需要用 iframe 触发标题更新
        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()) {
        // this.$CommonAPI.Post('GetJSSDKConfig', location.href).then(data => {
        let shareinfo = {
          title: title || '泰古润' || this.wechatShareInfo.ShareTitle,
          desc: desc || '汇聚平凡梦想，共创美好未来' || this.wechatShareInfo.ShareContent,
          imgUrl: this.$file(imgUrl || this.wechatShareInfo.ShareLogo),
          link: this.$getShareLink(),
        };
        if (this.$isWechatMini())
          wx.miniProgram.postMessage({ data: { share: shareinfo } });
        data.jsApiList.push('wx-open-launch-weapp');
        data.openTagList = ['wx-open-launch-weapp'];
        wx.config(data);
        wx.ready(() => {
          wx.updateAppMessageShareData(shareinfo);
          wx.updateTimelineShareData(shareinfo);
          if (location.hash == '#/Weapp') {
            document.addEventListener('WeixinOpenTagsError', function (e) {
              alert(e.detail.errMsg); // 无法使用开放标签的错误原因，需回退兼容。仅无法使用开放标签，JS-SDK其他功能不受影响
            });
          }
        });
        wx.error((e) => {
          if (location.hash == '#/Weapp')
            alert(JSON.stringify(e));
        });
        // });
      }
    },
  }
}
</script>