<template>
    <div class="home_wrapper">
        <div class="focus_head">
        </div>
        <div class="focus_info">
            <div class="icon"></div>
            <div class="title"></div>
        </div>
      <van-form @submit="handleLogin">
        <div class="login_from">
            <div class="flex_block">
                <div class="flex_input_main">
                    <div class="icon_wrap user"></div>
                    <div class="input_wrap">
                        <input type="text" v-model="formData.username" class="ipt" placeholder="用户名"/>
                    </div>
                </div>
            </div>
            <div class="flex_block">
                <div class="flex_input_main">
                    <div class="icon_wrap password"></div>
                    <div class="input_wrap">
                        <input :type="show ? 'password' : 'text'" v-model="formData.password" class="ipt" maxlength="16" placeholder="密码"/>
                    </div>
                    <div :class="{'showHidden':true,'ico_hide':show}" @click="showPassword"></div>
                </div>
            </div>
            <div class="flex_block">
                <div class="flex_input_main valid">
                    <div class="icon_wrap valid"></div>
                    <div class="input_wrap">
                        <input type="text" v-model="formData.code" class="ipt" maxlength="4" placeholder="请输入验证码"/>
                    </div>
                </div>
                <div class="valid_main">
                    <img :src="codeUrl" @click="getCode" />
                </div>
            </div>
            <van-checkbox v-model="formData.rememberMe" shape="square">记住密码</van-checkbox>
        </div>
        <div class="login_btn">
<!--            <div class="btn" :loading="loading">登录</div>-->
            <van-button
              class="btn"
              round
              block
              type="info"
              native-type="submit"
              :loading="loading"
            >登录</van-button>
        </div>

      </van-form>
    </div>
</template>

<script>
  import { getCodeImg, getSmsCode } from "@/api/login";
  import Cookies from "js-cookie";
  import { encrypt, decrypt } from "../../utils/jsencrypt";
  //引用wx sdk
  import wx from "weixin-js-sdk";
  export default {
    name: "login",
    data() {
      return {
        formData: {
          username: "", //账号
          password: "", //密码
          // username:'',
          // password:'',
          code: null, //图片验证码
          uuid: null, //识别uuid
          mobile: null, //手机号
          smsCode: null, //短信验证码
          rememberMe:false
        },
        loading: false,
        codeUrl: "", //验证码
        isSmsLogin: false, //是否手机验证码
        computeTime: 0,
        height:0,
        show:true
      };
    },
    created() {
      this.getCode();
      this.height = document.body.clientHeight
      this.getCookie();
      //调用微信公众号方法
      // wx.config({
      //     debug: true, // 开启调试模式,
      //     appId: res.appId, // 必填，企业号的唯一标识，此处填写企业号corpid
      //     timestamp: res.timestamp, // 必填，生成签名的时间戳
      //     nonceStr: res.nonceStr, // 必填，生成签名的随机串
      //     signature: res.signature,// 必填，签名，见附录1
      //     jsApiList: ['scanQRCode'] // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
      //   });
      //分享要用encodeURIComponent()方法
    },
    methods: {
      showPassword(){
        this.show = !this.show;
      },
      getCode() {
        getCodeImg().then((res) => {
          this.formData.uuid = res.uuid;
          this.codeUrl = "data:image/gif;base64," + res.img;
        });
      },
      getCookie() {
        const username = Cookies.get("username");
        const password = Cookies.get("password");
        const rememberMe = Cookies.get("rememberMe");
        this.formData = {
          username: username === undefined ? this.formData.username : username,
          password:
            password === undefined ? this.formData.password : decrypt(password),
          rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
        };
      },
      getSmsCode() {
        if (!this.computeTime) {
          let myreg = /^[1][3,4,5,7,8,9][0-9]{9}$/;
          if (!myreg.test(this.formData.mobile)) {
            this.$dialog.alert({
              message: '手机号格式不正确',
            });
            return false;
          }
          getSmsCode(this.formData.mobile).then((res) => {
            if (res.code === 200) {
              this.$dialog.alert({
                message: '验证码已发送',
              });
              this.loginForm.uuid = res.uuid;
              this.computeTime = 60;
              this.timer = setInterval(() => {
                this.computeTime--;
                if (this.computeTime <= 0) {
                  clearInterval(this.timer);
                }
              }, 1000);
            }
          });
        }
      },
      handleLogin(values) {
          if (this.formData.rememberMe) {
            Cookies.set("username", this.formData.username, { expires: 30 });
            Cookies.set("password", encrypt(this.formData.password), { expires: 30 });
            Cookies.set("rememberMe", this.formData.rememberMe, { expires: 30 });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove("rememberMe");
          }
          //账号密码登录
          if (this.formData.username == "") {
            this.$dialog.alert({
              message: '账号不能为空',
            });
            return false;
          } else if (this.formData.password == "") {
            this.$dialog.alert({
              message: '密码不能为空',
            });
            return false;
          } else if (!this.formData.code) {
            this.$dialog.alert({
              message: '图片验证码不能为空',
            });
            return false;
          }
          this.$store
            .dispatch("Login", this.formData)
            .then(() => {
              this.$router.push({ name: 'sunVillageInfoIndex' || "/" }).catch(() => {});
            })
            .catch(() => {
              this.loading = false;
              this.getCode();
            });
      },
    },
  };
</script>

<style scoped lang="scss">
  /deep/ .van-checkbox__label{
    color: #ffffff;
  }
.home_wrapper{
    background: #f1f2f2;
    width: 100vw;
    min-height: 100vh;
    .focus_head{
        height: 450px;
        background: url('../../assets/images/sunVillage_info/login_head.png') no-repeat;
        background-size: 100% 100%;

    }
    .focus_info{
        margin-top: -74px;
        .icon{
            background: url('../../assets/images/sunVillage_info/login_head_t.png') no-repeat;
            background-size: 100% 100%;
            width: 145px;
            height: 148px;
            margin:0 auto;
        }
        .title{
            width: 512px;
            height: 52px;
            background: url('../../assets/images/sunVillage_info/login_head_nr.png') no-repeat;
            background-size: 100% 100%;
            margin:30px auto 0;
        }
    }
    .login_from{
        width: 676px;
        height: 450px;
        background: url('../../assets/images/sunVillage_info/login_main.png') no-repeat;
        background-size: 100% 100%;
        margin: 50px auto 0;
        padding:60px 50px 0;
        .flex_block{
            margin-bottom: 40px;
            display: flex;
            justify-content:space-between;
            .flex_input_main{
                position: relative;
                display: flex;
                flex: 1;
                align-items:center;
                background: rgba(255,255,255,0.7);
                 border-radius: 62px;
                 height: 62px;
                .icon_wrap{
                    width: 35px;
                    height: 35px;
                    &.user{
                        background: url('../../assets/images/sunVillage_info/login_icon_1.png') no-repeat;
                        background-size: 100% 100%;
                        margin-left: 30px;
                    }
                    &.password{
                        background: url('../../assets/images/sunVillage_info/login_icon_2.png') no-repeat;
                        background-size: 100% 100%;
                        margin-left: 30px;
                    }
                    &.valid{
                         background: url('../../assets/images/sunVillage_info/login_icon_3.png') no-repeat;
                        background-size: 100% 100%;
                        margin-left: 30px;
                    }
                }
                .input_wrap{
                    flex: 1;
                    margin-left: 16px;
                    padding-right: 30px;
                    .ipt{
                        width: 100%;
                        height: 48px;
                        background:transparent;
                        font-size: 30px;
                        color: #3f3d56;
                    }
                }
                .showHidden{
                    width: 32px;
                    height: 32px;
                    background: url('../../assets/images/sunVillage_info/login_icon_4.png') no-repeat;
                    background-size: 100% 100%;
                    position: absolute;
                    right: 38px;
                    cursor: pointer;
                    &.ico_hide{
                        background: url('../../assets/images/sunVillage_info/login_icon_5.png') no-repeat;
                        background-size: 100% 100%;
                    }
                }
                &.valid{
                    flex:0 0 380px;
                }
            }
            .valid_main{
                width: 165px;
                height: 62px;
                border-radius: 30px;
                img{
                  width: 100%;
                  height: 100%;
                  border-radius: 30px;
                }
            }
        }
    }
    .login_btn{
        padding-top: 80px;
        .btn{
          width: 315px;
          height: 70px;
          background:url('../../assets/images/sunVillage_info/login_btn.png') no-repeat;
          background-size: 100% 100%;
          margin:0 auto;
          font-size: 32px;
          color: #fff;
          line-height: 70px;
          text-align: center;
          border: none;
        }
    }
}

</style>
