<template>
  <view>
    <view v-if="showShareOverlay" class="share-overlay" @click.self="onShareOverlayClick" />
  </view>
</template>
<script>
import { isWechat, isWxMp, isH5Normal } from '@/service/validation.service';
import localStorageService from '@/service/localStorage.service';
// import { queryStringify } from '@/service/utils.service';
import cfg from '@/config';
import qs from 'qs';
const APP_PATH = 'xyqb://openHttp';

export default {
  props: {
    nWeb: {
      type: Boolean,
      default: false
    },
    params: Object,
    appUrl: String
  },
  data() {
    return {
      showShareOverlay:
        ((isWechat && !isWxMp) || isH5Normal) && !localStorageService.get('vccChannel')
    };
  },
  methods: {
    openAppSchemeUrl() {
      if (isH5Normal) {
        window.location.href = this.appUrl;
        setTimeout(() => {
          this.navToDlApp();
        }, 2000);
      } else if (isWechat && !isWxMp) {
        window.location.href = `${
          cfg.mallUIHost
        }/common/launch?jumpUrl=${APP_PATH}&terminal=2&extraInfo=${encodeURIComponent(
          JSON.stringify({ url: this.appUrl })
        )}`;
      }
    },
    onShareOverlayClick() {
      if (this.nWeb) {
        this.openAppSchemeUrl();
        return;
      }
      let url = `${window.location.origin}${
        window.location.pathname
      }?vccToken={token}&${qs.stringify(this.params)}`;
      console.log(url, '000000');
      if (isH5Normal) {
        url += `&exposure_source=${this.$route.query.exposure_source}`;
        window.location.href = `${APP_PATH}?jumpUrl=${this.appUrl || url}`;
      } else if (isWechat && !isWxMp) {
        url += `&exposure_source=wechat`;
        window.location.href = `${
          cfg.mallUIHost
        }/common/launch?jumpUrl=${APP_PATH}&terminal=2&extraInfo=${encodeURIComponent(
          JSON.stringify({ url: this.appUrl || url })
        )}`;
      }
      setTimeout(() => {
        this.navToDlApp();
      }, 6000);
    },
    navToDlApp() {
      if (process.server) return;
      // window.location.href = 'xyqb://openApp';
      window.location.href = cfg.appDownUrl;
      const nextPage = document.createElement('a');
      nextPage.setAttribute('href', cfg.appDownUrl);
      nextPage.click();
    }
  }
};
</script>

<style lang="less" scoped>
.share-overlay {
  position: fixed;
  top: 0;
  left: 0;
  height: 100%;
  width: 100%;
  z-index: 999;
}
</style>
