<template>
  <div>
    <div id="containt" style="margin-bottom: -4px" v-if="type == 1">
      <iframe
        v-if="url"
        :src="url"
        style="width: 100%; height: calc(100vh - 40px)"
      ></iframe>
    </div>
    <component :is="content" v-if="type == 2"></component>
  </div>
</template>

<script>
import { getToken } from "@base/utils/auth";

export default {
  name: "outLink",
  data() {
    return {
      url: null,
      content: null,
      type: 0,
    };
  },
  mounted() {
    let url = this.$route.query.url;
    let route = this.$route.query.route;
    if (route) {
      this.content = require("@/views" + route).default;
      this.type = 2;
    } else {
      let params = this.getCustomReqUrlAndCleanUrl(url);
      let reqUrl = params.custom_req_url;
      if (reqUrl) {
        this.$http({
          url: reqUrl,
          method: `post`,
          data: { thirdUrl: params.newUrl },
          isLoading: true,
          // modalStrictly: true,
          success: (res) => {
            this.url = res.objx;
            this.type = 1;
          },
        });
      } else if (url.indexOf("dw=true") >= 0) {
        this.$http({
          url: USER_PREFIX + `/dw_auth/getDwAsc`,
          method: `post`,
          data: {},
          isLoading: true,
          // modalStrictly: true,
          success: (res) => {
            this.url =
              url.replace("dw=true", "dw=" + res.objx.dw) + "&param=" + res.objx.param;
            this.type = 1;
          },
        });
      } else if (url.indexOf("token=platform_token") >= 0) {
        this.$http({
          url: YX_PREFIX + `/fundPlatform/getToken`,
          method: `post`,
          data: {},
          isLoading: true,
          success: (res) => {
            this.url = url.replace("token=platform_token", "token=" + res.objx);
            this.type = 1;
          },
        });
      } else if (url.indexOf("token=true") >= 0) {
        let token = getToken();
        this.url = url.replace("token=true", "token=" + token);
        this.type = 1;
      } else {
        this.url = url;
        this.type = 1;
      }
    }
  },
  methods: {
    getCustomReqUrlAndCleanUrl(url) {
      // 直接使用正则表达式匹配
      const match = url.match(/[?&]custom_req_url=([^&?#]+)/);
      if (match) {
        const customReqUrl = decodeURIComponent(match[1]);
        // 移除匹配到的参数部分
        const newUrl = url.replace(match[0], match[0].startsWith("?") ? "?" : "");

        return {
          custom_req_url: customReqUrl,
          newUrl: newUrl.replace(/\?$/, "").replace(/\?&/, "?"),
        };
      }

      return {
        custom_req_url: "",
        newUrl: url,
      };
    },
  },
};
</script>

<style></style>
