<template>
  <div></div>
</template>

<script>
  // import { getToUrl, setToken, setUid } from "../assets/lib/util"; // 封装的缓存方法
  // var url = getToUrl();
  export default {
    name: "Author",
    data() {
      return {
        uid: ""
      };
    },
    created() {
      document.title = "微信授权中";
      var code = this.GetUrlParame("code"); // 截取code
      console.log(code);
      if (!code) {
        let local_hash = location.hash;
        window.localStorage.setItem("local_hash", local_hash);
        let redirect_uri = encodeURIComponent(`${location.origin}${location.pathname}`);
        location.replace(`https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxd1aab06cc88d0a5e&redirect_uri=${redirect_uri}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`);
      } else {
        console.log("拿到code", code);
        console.log("请求登录");

        let inviter = localStorage.getItem("inviter");
        var uid = localStorage.getItem('walk_userData');
        if (uid && inviter == JSON.parse(uid).id) {
          inviter = "";
        }
        let from_type = localStorage.getItem("from_type");
        // alert(inviter + "test2")
        
        this.$http
          .post("/game-bsdk/api/v1/oa_auth_login", {
            js_code: code,
            inviter_id: inviter || "",
            from_type: from_type || ""
          }) //如果有code，说明用户点击了授权  将获取到的code传递给后台
          .then(res => {
            // 返回状态和UId
            console.log("返回的数据", res);
            if (!res) {
              return;
            }
            let result = res.data;
            if (result.code == 0) {
              localStorage.setItem("walk_userData", JSON.stringify(result.data));
              sessionStorage.setItem("new_user", result.data.new_user);
              let last_url = localStorage.getItem("last_url") || "/";
              let url = location.href.split("#")[0];
              // setTimeout(() => {
              //   //   this.$router.go("/MyData");
              location.replace(
                `https://miniapp-api.wxatech.com/app_pages/walk_daka.html?#${last_url}`
              );
              // }, 60000);
            }
            //   this.uid = res.data.UId;
            //   setUid(this.uid);
            //   if (res.data.status && this.uid) {
            //     window.location.replace("http://wenlian.cct.work/dist/#" + url);
            //   }
          });
      }
    },
    methods: {
      // 截取code
      GetUrlParame(parameName) {
        /// 获取地址栏指定参数的值
        /// <param name="parameName">参数名</param>
        // 获取url中跟在问号后面的部分
        var parames = window.location.search;
        // 检测参数是否存在
        if (parames.indexOf(parameName) > -1) {
          var parameValue = "";
          parameValue = parames.substring(
            parames.indexOf(parameName),
            parames.length
          );
          // 检测后面是否还有参数
          if (parameValue.indexOf("&") > -1) {
            // 去除后面多余的参数, 得到最终 parameName=parameValue 形式的值
            parameValue = parameValue.substring(0, parameValue.indexOf("&"));
            // 去掉参数名, 得到最终纯值字符串
            parameValue = parameValue.replace(parameName + "=", "");
            return parameValue;
          }
          return "";
        }
      }
    }
  };
</script>
