<template>
  <common
    @jihuo="biliJihuo"
    :getUid="getUserid"
    :postUid="postUserid"
    temp="bili"
  ></common>
</template>

<script>
import common from "../treatyColorNew/index.vue";
import util from "../../util";
import { changeBsiteUserid, changeUseridToBsite } from "../../api";
export default {
  data() {
    return {};
  },
  computed: {
    user() {
      return this.$store.state.user;
    },
  },
  methods: {
    biliJihuo() {
      if (this.user.userid) {
        this.postUserid({ eventName: "onActive", userid: this.user.userid });
      } else {
        this.$toast.fail("请先领取专属流量");
      }
    },
    // 免登录userid转换  B站=》我方
    getUserid() {
      let that = this;
      return new Promise(function (resolve, reject) {
        let { bUid } = util.query2obj(location.search);
        that.postUserid({ eventName: "onOK" });
        console.log('onOk')
        if (bUid) {
          let userid = decodeURIComponent(bUid);
          changeBsiteUserid({
            userid: userid,
          }).then((res) => {
            that.$store.commit("setUser", {
              userid: res.data.userid,
              phone: res.data.phone,
            });
            resolve(res.data);
          });
        }
      });
    },
    // B站专用透传 订购成功  点击流量激活时调用
    postUserid(data) {
      if (data.eventName === "onOK") {
        window.parent.postMessage({ type: "onOK" }, "*");
      } else {
        changeUseridToBsite({
          userid: data.userid,
        }).then((res) => {
          window.parent.postMessage(
            {
              type: data.eventName,
              data: { eventName: data.eventName, userid: res.data },
              callbackId: `${data.eventName}Callback`,
            },
            "*"
          );
        });
      }
    },
  },
  components: { common },
};
</script>

<style lang="scss">

</style>
