
<template>
  <div class="login" style=" min-width: 650px!important;">
    <div class="login-paper">
      <img src="../assets/img/logbc (2).png" />
    </div>
    <div class="login-content">
      <div class="img">
        <img src="../assets/img/logoland (2).png" />
      </div>
      <div class="section">
        <img class="section-icon" src="../assets/img/log.png" />
        <div class="log-text section-text">欢迎登陆私塾家在线人员管理后台</div>
        <div @click="WxLoginCtl" class="log-qrcode">
          <div class="loginCodeOrword">
            <div class="section-qrcode" v-show="!PasswordOrcode" id="login_container"></div>
            <img
              class="codeLogin"
              @click="loginStyle(1)"
              src="../assets/img/CodeLogin (2).png"
              v-show="!PasswordOrcode"
            />
            <img
              v-show="PasswordOrcode"
              class="codeLogin"
              @click="loginStyle(2)"
              src="../assets/img/ercode (1).png"
            />
            <div
              class="passWordLogin"
              style="min-width: 400px;min-height:400px"
              v-show="PasswordOrcode"
            >
              <div
                class="loginInput"
                style="min-width: 400px;min-height:400px"
                v-show="PasswordOrcode"
              >
                <el-form :model="loginForm" ref="ruleForms" :rules="rules" label-position="left">
                  <el-form-item label="手机号" prop="phoneNumber" class="InputHeight">
                    <el-input v-model="loginForm.phoneNumber" class="inputstyle" autocomplete="off"></el-input>
                    <div class="bottomLine"></div>
                  </el-form-item>
                  <el-form-item label="验证码" prop="Ercode" class="InputHeight">
                    <el-input v-model="loginForm.Ercode" class="inputstyle2" autocomplete="off"></el-input>

                    <div class="linnheight"></div>
                    <div
                      v-if="!Unclicktime"
                      @click="GetVerficationCodeFun('ruleForms')"
                      class="SendErcode"
                    >发送验证码</div>
                    <div v-if="Unclicktime" class="SendErcode">{{TimeClock}}</div>
                    <div class="bottomLine"></div>
                  </el-form-item>
                  <el-form-item>
                    <!-- <div class="SubmitBtn"  @click="submitForm('ruleForms')">
                      登陆
                    </div>-->
                    <el-button
                      class="SubmitBtn"
                      type="primary"
                      size="medium"
                      @click="submitForm('ruleForms')"
                    >登陆</el-button>
                  </el-form-item>
                </el-form>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { APP_ID, RESET_URL, TYPE } from "@/config";
import { Message } from "element-ui";
export default {
  name: "login",
  data() {
    var validPhone = (rule, value, callback) => {
      if (!value) {
        callback(new Error("请输入电话号码"));
      } else if (!this.isvalidPhone(value)) {
        callback(new Error("请输入正确的11位手机号码"));
      } else {
        callback();
      }
    };
    return {
      TimeClock: "",
      Unclicktime: false,
      time: 0,
      loginForm: {
        phoneNumber: "",
        Ercode: ""
      },
      PasswordOrcode: false,
      rules: {
        Ercode: [
          { required: true, message: "请输入验证码", trigger: "blur" },
          { message: "", trigger: "blur" }
        ],

        phoneNumber: [
          { required: true, trigger: "blur", validator: validPhone } //这里需要用到全局变量
        ]
      }
    };
  },
  //监听器
  watch: {
    $route(to, from) {
      //监听路由是否变化
      location.reload();
    }
  },
  created() {
    localStorage.removeItem("accessToken");
    this.getTKByUnionid();
  },
  methods: {
    submitForm(ruleForms) {
      this.$refs[ruleForms].validate(valid => {
        if (valid) {
          this.$store.dispatch("PhoneCodeLogin", {
            phone: this.loginForm.phoneNumber,
            vcode: this.loginForm.Ercode,
            accessToken: ""
          });
        } else {
          return false;
        }
      });
    },
    resetForm(ruleForms) {
      this.$refs[ruleForms].resetFields();
    },
    WxLoginCtl() {},
    isvalidPhone(str) {
      const reg = /^1[3|4|5|7|8][0-9]\d{8}$/;
      return reg.test(str);
    },
    loginStyle(str) {
      if (str == 1) {
        this.PasswordOrcode = true;
      }
      if (str == 2) {
        this.PasswordOrcode = false;
        this.erCodeSearch();
      }
    },
    // 获取验证码
    timer() {
      if (this.time > 0) {
        this.time--;
        this.Unclicktime = true;
        this.TimeClock = this.time + "s后重新获取";
        setTimeout(this.timer, 1000);
      } else {
        this.time = 0;
        this.TimeClock = "获取验证码";
        this.Unclicktime = false;
      }
    },
    async GetVerficationCodeFun(ruleForms) {
      if (this.isvalidPhone(this.loginForm.phoneNumber)) {
        //倒计时函数
        this.time = 60;
        this.timer();
        this.$store.dispatch("GetVerficationCode", {
          phone: this.loginForm.phoneNumber
        });
      } else {
        Message.error("请输入正确电话格式！");
      }
    },
    async getTKByUnionid() {
      if (window.location.href.indexOf("unionid") >= 0) {
        // 如果url中包含unionid,则保存到store中
        let unionid = window.location.href.split("?")[1];
        unionid = unionid.substring(8);
        let WxCode = unionid;
        this.WxCode = WxCode;
        localStorage.setItem("unionid", this.WxCode);
        this.$store.dispatch("UnionidLogin", {
          unionid: this.WxCode
        });
      }
      if (window.location.href.indexOf("code") >= 0) {
        // 如果url中包含code,则保存到store中
        let code = window.location.href.split("#/?")[1];
        code = code.substring(5, code.indexOf("&"));
        let WxCode = code;
        this.WxCode = WxCode;
        this.$store.dispatch("GET_TOKEN", {
          code: this.WxCode,
          type: TYPE
        });
        // 清除重定向url中参数ocde,
        window.history.replaceState({}, "", `${location.pathname}`);
      }
      return;
    },
    erCodeSearch() {
      var obj = new WxLogin({
        self_redirect: false,
        id: "login_container",
        appid: APP_ID,
        scope: "snsapi_login",
        redirect_uri: encodeURIComponent(RESET_URL),
        state: "",
        style: "width: 250px;height: 500px",
        href: ""
      });
    }
  },
  mounted() {
    this.erCodeSearch();
  }
};
</script>
<style lang="stylus" scoped>
@import '../style/index.styl';

.codeLogin {
  position: absolute;
  top: 0;
  right: 0;
  cursor: pointer;
}

.loginInput {
  position: absolute;
  top: 100px;
  width: 400px;
}

.passWordLogin {
  width: 400px;
  height: 366px;
  box-shadow: 0 0 20px #cccccc;
  border-radius: 5px;
}

.typeWord {
  display: inline-block;
  margin-left: 30px;
  font-size: 20px;
}

.bottomLine {
  width: 100%;
  height: 2px;
  background-color: #cccccc;
  margin: 0 auto;
}

.SubmitBtn {
  width: 200px;
  height: 40px;
  font-size: 16px;
  line-height: 10px;
}

.SendErcode {
  display: inline-block;
  font-size: 10px;
  color: #00B7FF;
  cursor: pointer;
}

.inputstyle {
  width: 250px;
}

.linnheight {
  width: 2px;
  height: 20px;
  background-color: #cccccc;
  display: inline-block;
  margin-right: 10px;
}

.inputstyle2 {
  width: 150px;
}

.InputHeight {
  width: 90%;
  height: 100px;
  margin: 0 auto;
  font-size: 30px;
}

/deep/ .el-input__inner {
  border-style: none !important;
  outline: none !important;
  background-color: transparent;
  border: 0 !important;
}

/deep/ .el-form-item__label {
  font-size: 16px;
  font-weight: 700;
  color: #393939;
}

.loginCodeOrword {
  position: relative;
}

.login-content {
  display: flex;
  -webkit-box-pack: justify;
  -ms-flex-pack: justify;
  justify-content: space-between;
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
}

.login {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  min-width: 650px;

  &-paper {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;

    img {
      width: 100%;
      height: 100%;
      object-fit: cover;
    }
  }

  &-content {
    position: absolute;
    top: 10%;
    left: 15%;
    width: 70%;
    height: 80%;
    background: $color-bg-white;
    border-radius: 4px;
    box-shadow();
    flex-row-between();

    .img {
      margin: 10% 0% 10% 10%;
      flex: 6;

      img {
        width: 100%;
        height: 100%;
        object-fit: cover;
      }
    }

    .section {
      margin: 10%;
      flex: 4;
      text-align: center;

      &-icon {
        width: 50%;
        object-fit: cover;
      }

      &-text {
        color: $color-text-green;
        font-size: $font-size-l;
        margin: 5%;
      }

      &-qrcode {
        box-shadow();
        padding: 10px;
      }
    }
  }
}

@media (min-width: 0px) and (max-width: 960px) {
  .img {
    display: none;
  }
}
</style>