<template>
  <view>
    <block v-if="access_token">
      <web-view :src="url + urlPath + '?queryList=' + queryList + '&token=' + access_token" @message="handleGetMessage"></web-view>
    </block>
  </view>
</template>

<script>
import { User } from "@/config/api.js";
import { pcUrl } from "@/config/config";

export default {
  data() {
    return {
      urlPath: "/index",
      queryList: "",
      urlShare: "",
      titleShare: "",
      access_token: "",
    };
  },
  computed: {
    url() {
      return pcUrl;
    },
  },
  methods: {
    // 获取token
    getToken() {
      uni.login({
        success: (result) => {
          User.wechatLog({
            Code: result.code,
          }).then((res) => {
            try {
              let token = res.Data.Authorization;
              this.$db.set("access_token", token);
              this.access_token = token;
            } catch (e) {}
          });
        },
      });
    },
    // 获取web-view 的消息
    handleGetMessage(e) {
      const data = e.detail.data[e.detail.data.length - 1];
      this.urlShare = data.url;
      this.titleShare = data.title;
    },
    // 进入页面 处理数据
    initLoad(optios) {
      let urlType = [
        "index",
        "reportForm",
        "activity",
        "themeActivity",
        "activityDetail",
        "reportDetail",
        "reportSecretDetail",
        "roadEssence",
        "chartPage",
        "specialDetail",
        "specialResearchPage",
        "purchaser",
        "IndustryReport",
      ];
      if (JSON.stringify(optios) !== "{}" && optios.path) {
        let new_url = optios.path.split("/")[2];
        let pathUrl = decodeURIComponent(new_url);
        this.urlPath = urlType.includes(pathUrl) ? "/" + pathUrl : pathUrl == "reportPage" ? "/reportDetail" : "/index";
        this.queryList = optios.query || "";
      }
    },
  },
  onLoad(optios) {
    this.getToken();
    this.initLoad(optios);
  },
  onShareAppMessage() {
    return {
      title: this.isHorzMobile ? "好友向您推荐此内容，上传名片享查研观向免费月卡！" : this.titleShare,
      path: this.urlShare,
    };
  },
};
</script>

<style></style>
