<template>
  <div id="login">
    <div class="loginPopple">
      <div class="poRightLogin">
        <div class="navImg">
          <img src="@/assets/images/loginPage/welcome@2x.png" alt="" />
        </div>
        <div class="userNameBox">
          <h4>用户名</h4>
          <input
            type="text"
            placeholder="输入用户名"
            v-model="loginData.username"
          />
        </div>
        <div class="passwordBox">
          <h4>密码</h4>
          <div class="passwordListB">
            <input
              :type="showPassword ? 'text' : 'password'"
              placeholder="输入密码"
              v-model="loginData.password"
            />
            <div class="iconImg" @click="showPassword = !showPassword">
              <img
                v-if="showPassword"
                src="@/assets/images/loginPage/pass.png"
                alt=""
              />
              <img v-else src="@/assets/images/loginPage/passno.png" alt="" />
            </div>
          </div>
        </div>
        <div class="codeBox">
          <input
            type="text"
            placeholder="输入验证码"
            v-model="loginData.code"
          />
          <div class="codeImg" @click="getUuid">
            <img :src="uuidBase64" alt="点击刷新" />
          </div>
        </div>
        <div class="toolList">
          <label class="checkBox"
            ><input type="checkbox" v-model="isRemember" />7天内免登录</label
          >
          <div class="forget">忘记密码</div>
        </div>
        <div class="loginbtnbox" @click="getApi">登录</div>
      </div>
    </div>
  </div>
</template>

<script>
import cookie from "@/utils/cookie.js";
export default {
  data() {
    return {
      showPassword: false, //密码是否可见
      isRemember: false, //是否选中7天内免登录
      uuidBase64: "", //验证码图片
      uuid: "", //验证码编码
      loginData: {
        //登入信息收集
        username: "",
        password: "",
        code: "",
      },
    };
  },
  created() {
    this.getUuid(); //初始化获取验证码方法
    var self = this
    document.onkeydown = function(e){
      var key = window.event.keyCode;
      if(key === 13){
        self.getApi()
      }
    }
  },
  methods: {
    //获取验证码图片
    getUuid() {
      this.$api
        .captchaImage()
        .then((res) => {
          this.uuidBase64 = "data:image/png;base64," + res.data.img;
          this.uuid = res.data.uuid;
        })
        .catch((err) => {
          this.$message.error(err);
        });
    },
    //登录
    getApi() {
      if (!this.loginData.username) {
        this.$message.warning("请输入用户名");
        return;
      }
      if (!this.loginData.password) {
        this.$message.warning("请输入密码");
        return;
      }
      var data = {
        code: this.loginData.code,
        username: this.loginData.username,
        password: this.loginData.password,
        uuid: this.uuid,
      };
      this.$api
        .login(data)
        .then((res) => {
          if (res.code === 200) {
            if (this.isRemember) {
              cookie.setCookie("token", res.token, 7);
            } else {
              cookie.setCookie("token", res.token, 1);
            }
            this.initRoutes();
          } else {
            this.getUuid();
            this.loginData.code = "";
          }
        })
        .catch((err) => {
          this.getUuid();
          this.$message.error(err);
          console.log(err);
        });
    },
    //重定向跳转
    initRoutes() {
      this.$router
        .replace({
          name: "home",
        })
        .catch(() => {});
    },
  },
};
</script>

<style lang="less" scoped>
#login {
  width: 100%;
  background: url("../../assets/images/loginPage/bg@2x.png") no-repeat center;
  background-size: cover;
  min-height: 100vh;
  .loginPopple {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translateX(-50%) translateY(-50%);
    width: 1060px;
    height: 660px;
    background-color: #f9fdfe;
    background: url("../../assets/images/loginPage/bg2@2x.png") no-repeat center;
    background-size: 1060px 660px;
    border-radius: 16px;
    box-shadow: 0px 30px 21px 9px rgba(71, 166, 255, 0.08);
    display: flex;
    align-items: center;
    justify-content: flex-end;
    font-size: 16px;
    // font-family: Alibaba PuHuiTi;
    animation: showStatus 1s;
    .poRightLogin {
      width: 530px;
      height: 592px;
      padding: 0px 71px;
      input {
        font-size: 16px;
      }
      .navImg {
        margin-top: 56px;
        width: 387px;
        height: 22px;
        margin-block: 56px;
        img {
          width: 100%;
          height: 100%;
        }
      }
      .userNameBox {
        margin-bottom: 21px;
        h4 {
          font-weight: 400;
          color: #737fa0;
        }
        input {
          height: 46px;
          line-height: 46px;
          border: none;
          background-color: transparent;
          outline: none;
          border-bottom: 2px solid #a9d0ef;
          width: 100%;
          font-weight: bold;
          color: #2f4379;
          letter-spacing: 1px;
          &::placeholder {
            color: #737fa0;
            font-weight: 400;
          }
        }
      }
      .passwordBox {
        margin-bottom: 29px;
        h4 {
          font-weight: 400;
          color: #737fa0;
        }
        .passwordListB {
          height: 46px;
          display: flex;
          align-items: center;
          border-bottom: 2px solid #a9d0ef;
          input {
            height: 100%;
            line-height: 100%;
            border: none;
            background-color: transparent;
            font-weight: bold;
            color: #2f4379;
            outline: none;
            flex: 1;
            padding-right: 15px;
            letter-spacing: 2px;

            &::placeholder {
              color: #737fa0;
              font-weight: 400;
            }
          }
          .iconImg {
            cursor: pointer;
            width: 18px;
            height: 18px;
            img {
              width: 100%;
              height: 100%;
            }
          }
        }
      }
      .codeBox {
        height: 56px;
        display: flex;
        align-items: center;
        border-bottom: 2px solid #a9d0ef;
        input {
          flex: 1;
          padding-right: 10px;
          border: none;
          background-color: transparent;
          outline: none;
          font-weight: bold;
          letter-spacing: 2px;
          color: #2f4379;
          &::placeholder {
            color: #737fa0;
            font-weight: 400;
          }
        }
        .codeImg {
          width: 100px;
          height: 45px;
          img {
            width: 100%;
            height: 100%;
          }
        }
      }
      .toolList {
        margin: 24px 0px 49px;
        display: flex;
        align-items: center;
        justify-content: space-between;
        .forget {
          cursor: pointer;
          color: #737fa0;
          font-weight: 300;
        }
        .checkBox {
          display: flex;
          align-items: center;
          color: #2f4379;
          input {
            margin-right: 6px;
          }
        }
      }
      .loginbtnbox {
        cursor: pointer;
        margin: 0 auto;
        width: 280px;
        height: 48px;
        text-align: center;
        line-height: 48px;
        border-radius: 8px;
        box-shadow: 0px 10px 16px 4px rgba(47, 67, 121, 0.1);
        background: #2f4379;
        color: #fff;
        transition: all 0.2s;
        &:hover {
          transform: scale(1.03);
        }
      }
    }
  }
}
@keyframes showStatus {
  0% {
    opacity: 0;
  }
  100% {
    opacity: 1;
  }
}
</style>