<template>
  <div class="fle bgColor" v-if="!isSSo">
    <load-ing v-show="requesterLgoinType == 'wechat'"></load-ing>
    <h2
      v-if="requesterLgoinType == 'web'"
      style="font-size: 0.36rem; margin-bottom: 0.36rem; font-weight: 700"
    >
      网络与信息化运维管理系统
    </h2>
    <div v-if="requesterLgoinType == 'web'" style="width: 5rem">
      <cube-input
        v-model="zhanghao"
        placeholder="您的账号为工号或学号"
      ></cube-input>
      <br />
      <cube-input
        v-model="mima"
        type="password"
        placeholder="您的初始密码为学号或工号后四位"
      ></cube-input>
      <br />
      <cube-button @click="zlogin()" style="background: #005395"
        >登录</cube-button
      >
    </div>
  </div>
</template>

<script>
// 注意事项：个人微信需要关注微工作台，否则会导致没有userId（userId为openID）登录失败
import { decode } from "js-base64";
import LoadIng from "./../views/loading.vue";
export default {
  data() {
    return {
      zhanghao: "",
      mima: "",
      requesterLgoinType: "",
      isSSo: true // 是否单点登录
    };
  },
  components: {
    LoadIng
  },
  methods: {
    // 登陆
    login() {
      var that = this;
      that.$http.post("service/auth/wechatlogin", {}).then(function(res) {
        if (res.data.url) {
          window.location.href = res.data.url;
        } else {
          if (res.data.user) {
            that.getConfig();
            localStorage.setItem(
              "loginUser",
              JSON.stringify(res.data.user.user)
            );
            localStorage.setItem(
              "login_requester",
              JSON.stringify(res.data.user.requester)
            );
            this.$router.push({ path: "/main" });
          }
        }
      });
    },
    // 获取code
    getCode() {
      var that = this;
      var url = window.location.href.split("?");
      // alert("window.location.href----:");
      // alert(JSON.stringify(window.location.href));
      if (!url[1]) {
        that.login();
      } else {
        var code = {
          code: url[1],
          name: "微信处理人"
        };
        // alert("2--传code参数：");
        // alert(JSON.stringify(code));
        that.$http.post("service/auth/wechatlogin2", code).then(function(res) {
          if (res.data.state == 200) {
            // alert(3);
            // alert(JSON.stringify(res.data));
            if (res.data.user) {
              that.getConfig();
              localStorage.setItem(
                "loginUser",
                JSON.stringify(res.data.user.user)
              );
              localStorage.setItem(
                "login_requester",
                JSON.stringify(res.data.user.requester)
              );
              this.$router.push({ path: "/main" });
            }
          } else if (res.data.state == 501) {
            that
              .$createDialog({
                type: "alert",
                title: "您的账号被删除，请联系管理员",
                icon: "cubeic-alert"
              })
              .show();
          } else {
            that
              .$createDialog({
                type: "alert",
                title: res.data.remarks,
                icon: "cubeic-alert"
              })
              .show();
          }
        });
      }
    },
    // 假登录
    login2() {
      var that = this;
      this.$http
        .post("service/auth/loginEncrypt", {
          // username: "DingDanDan",
          username: "admin",
          password: "20200326"
        })
        .then(function(res) {
          that.loading = false;
          if (res.data && res.data.state == 200) {
            that.getConfig();
            localStorage.setItem(
              "loginUser",
              JSON.stringify(res.data.data.user)
            );
            localStorage.setItem(
              "login_requester",
              JSON.stringify(res.data.data.requester)
            );
            that.$router.push({ path: "/main" });
          }
        });
    },
    // 获取版本配置（报修主题：报修人/报修科室）
    getConfig() {
      var that = this;
      this.$http
        .post("service/sysinfo/data/fetchDataList/systemConfiguration", {
          idx: 0,
          sum: 1000,
          systemConfiguration: { keyconfig: "repairMain" }
        })
        .then(function(res) {
          console.log(res);
          localStorage.setItem(
            "valConfig",
            JSON.stringify(res.data.list[0].valueconfig)
          );
        });
    },
    // 获取报修人登录方式
    getLoginType() {
      this.$http.post("service/wechat/getLoginType", {}).then(result => {
        if (result.data.status == 200) {
          this.requesterLgoinType = result.data.data;
          window.localStorage.setItem(
            "requesterLgoinType",
            this.requesterLgoinType
          );
          if (this.requesterLgoinType == "wechat") {
            this.getCode();
          } else if (this.requesterLgoinType == "web") {
            // this.$router.push({ path: "/login" });
          }
        }
      });
    },
    // 账号密码登录
    zlogin() {
      console.log(this);
      var that = this;
      var ssoStr = "";
      var ssoJson;
      if (this.isSSo) {
        ssoStr = location.search
          .replace("?", "")
          .split("&")[0]
          .split("=")[1];
        ssoStr = decode(decodeURIComponent(ssoStr));
        ssoJson = JSON.parse(ssoStr);
      }
      var postData = this.isSSo
        ? { username: ssoJson.a, password: ssoJson.u, t: false }
        : { username: this.zhanghao, password: this.mima };
      this.$http.post("service/auth/loginEncrypt", postData).then(res => {
        that.loading = false;
        if (res.data && res.data.state == 200) {
          this.getConfig();
          localStorage.setItem("loginUser", JSON.stringify(res.data.data.user));
          localStorage.setItem(
            "login_requester",
            JSON.stringify(res.data.data.requester)
          );
          this.$router.push({ path: "/main" });
        } else {
          that
            .$createDialog({
              type: "alert",
              title: "系统错误",
              content: "请稍后再试",
              icon: "cubeic-wrong",
              color: "red"
            })
            .show();
        }
      });
    }
  },
  created() {
    this.isSSo = location.search.length > 0;
    if (this.isSSo) {
      this.zlogin();
    }
    this.getLoginType();
  }
};
</script>

<style scoped>
.color {
  color: red;
  font-size: 1rem;
}
.fle {
  display: flex;
  height: 100vh;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  background: url(./../../static/images/weChartBackImage.jpg);
  background-size: cover;
}
/* .bgColor{
    background-color: white
  } */
input {
  border: 0.02rem #999999 solid;
  width: 60%;
  height: 0.8rem;
  margin-top: 0.4rem;
  border-radius: 10px;
  padding-left: 0.24rem;
}
input:nth-child(1) {
  margin-top: 0;
}
input:focus {
  outline: none;
}
.sub {
  width: 40%;
  background-color: rgb(60, 162, 254);
  height: 0.7rem;
  color: white;
  font-size: 0.36rem;
  text-align: center;
  line-height: 0.7rem;
  margin-top: 0.3rem;
  border-radius: 10px;
}
</style>
