<template>
  <div class="login"></div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    return {
      mckey: this.$route.query.mckey,
      query_url:localStorage.getItem('query_url'),
      agent_url: localStorage.getItem('agent_url'),
    }
  },
  props: {
    page: {
      type: String,
      default: ""
    }
  },
  methods: {
    tologin: function () {
      this.$http.post(this.agent_url + "/get_wxlogin_url", {
        query_url: this.query_url,
        page: this.page
      }).then(res => {
        if (!res.data.status) {
          alert("登录失败，" + res.data.info);
        } else {
          location.href = res.data.data.loginurl;
        }
      }, response => {
        alert("网络错误，请重试");
      });
    }
  },
  mounted() {
    if (this.mckey) {
      this.$http.post(this.agent_url + "/h5_get_wxlogin_info", {mckey: this.mckey}).then(response => {
        if (!response.data.status) {
          this.tologin();
          return false;
        }

        let uid = response.data.data.site_uid
        let openid = response.data.data.openid
        if (!uid || !openid) {
          this.tologin();
          return false;
        }

        window.islogin = true;
        this.$cookies.set("site_uid", uid);
        this.$cookies.set("openid_" + uid, openid);
      }, response => {
        alert("网络错误，请重试");
      });
      return;
    }

    if (!window.islogin) {
      this.tologin();
    }
  }
}
</script>
