<template>
  <div class="home">
    <div class="container" v-if="loginflag">
      <div class="logo">
        <img src="../assets/nxwjlogo.webp"/>
        <h2>武警宁夏总队医院</h2>
      </div>
      <van-button
          class="wxlogin"
          round
          block
          type="info"
          @click="wxlogin"
          color="#46C3B0"
          icon="wxicon"
      >登&#8195;&#8195;录
      </van-button
      >
    </div>
    <div class="container" v-else>
      <div class="logo">
        <img src="../assets/nxwjlogo.webp"/>
      </div>
      <van-loading
          type="spinner"
          size="1.5rem"
          text-color="#000"
          color="#166bcc"
          vertical="true"
          class="loading"
      ></van-loading>
    </div>
  </div>
</template>

<script>
// @ is an alias to /src
import {apiLogin} from "@/request/api.js";
import {Toast, Dialog} from "vant";

export default {
  data() {
    return {
      loginflag: true,
      title: "",
    };
  },
  mounted() {
    // console.log(this.$route.meta.title);
    this.title = this.$route.meta.title;
    if (window.location.href.indexOf("?code=") != -1) {
      this.loginflag = true;
      this.login();
    }
  },
  methods: {
    login() {
      var str = window.location.href;
      var that = this;
      var code = str
          .split("?")[1]
          .split("&")[0]
          .split("=")[1];

      var formData = {
        code: code,
      };
      // alert(formData.code);
      console.log("formData.code",formData.code);
      
      apiLogin(formData).then((res) => {
        //ert("登陆成功")
        Toast("登陆成功");
        console.log("res",res);
        //  alert("token:" + res.data.accessToken);
        // localStorage.setItem("token", res.data.accessToken);
        // localStorage.setItem("openid", res.data.openid);
        localStorage.setItem("token", res.refreshToken);
        localStorage.setItem("userid", res.data.userId);
        // localStorage.setItem("openid", res.data.openid);
       

        //登陆成功后，跳转回医院的服务器
        setTimeout(() => {
          window.location.href =
              "https://nxwj.btlsoln.com/nxwj-gzh/#/" +
              "?token=" +
              res.refreshToken +
              "&userid=" +
              res.data.userId;
        }, 200);
      });
    },
    wxlogin() {
      var appid_text = "wx45acd2b4907cb8f4"; //公众号appid nxwj
      var ourl = "https://open.weixin.qq.com/connect/oauth2/authorize?";
      var appid = "appid=" + appid_text;
      var redirect_uri =
          "&redirect_uri=" +
          encodeURIComponent("https://nxwj.btlsoln.com/nxwj-gzh/#/Login"); //阿里云服务器
      //授权后重定向的回调链接地址，这里为middle中间页
      var response_type = "&response_type=code";
      var scope = "&scope=snsapi_userinfo";
      var state = "&state=STATE";
      var wechat_redirect = "#wechat_redirect";
      var url =
          ourl +
          appid +
          redirect_uri +
          response_type +
          scope +
          state +
          "&connect_redirect=1" +
          wechat_redirect;
      console.log(url);
      //alert(url);
      window.location.href = url; //拼接后的跳转链接
    },
  },
};
</script>

<style scoped lang="scss">
.logo {
  padding-top: 2.5rem;
  text-align: center;
  color: #333;
  padding-bottom: 0.5rem;

  img {
    width: 4rem;
    height: 4rem;
  }

  h2 {
    font-size: 0.6rem;
    padding-top: 0.4rem;
  }
}

::v-deep .van-button__text {
  font-size: 0.6rem;
  padding-left: 0.5rem;
}

.van-icon-wxicon {
  background-image: url("../assets/wxicon.png");
  height: 1rem;
  width: 1rem;
  background-size: 100% 100%;
}

.wxlogin {
  margin-top: 5rem;
}

.loading {
  margin-top: 5rem;
}
</style>
