Vue项目登录页面验证码功能demo

775 阅读1分钟

HTML部分


<template>
    <div id="border_div">
      <div class="show_div">
        <div class="name_div">
          <span>姓名:</span>
          <input type="text" v-model="username" @blur="checkName" @keyup.enter="$event.target.blur" placeholder="请输入用户名称">
        </div>
      </div>
      <div class="show_div">
        <div class="password_div">
          <span>密码:</span>
          <input type="password" v-model="userword" @blur="checkWord" @keyup.enter="$event.target.blur" placeholder="请输入用户密码">
        </div>
      </div>
      <div class="showYZM_div">
        <div class="yzm_div">
          <span>验证码:</span>
          <input type="text" v-model="check_yzm" @blur="checkYZM" @keyup.enter="$event.target.blur" placeholder="请输入验证码">
        </div>
        <div class="yzm_picture_div">
          <span >{{yzm_code}}</span>
        </div>
        <div class="yzm_text_div">
          <a href="javascript:void(0);" @click="changeYZM">看不清,换一张试试?</a>
        </div>
        </div>
      <div>
        <div>
          <button @click="login">登录</button>
          <button @click="reset">取消</button>
          <a to="/">忘记密码</a>
        </div>
        <div style="color: red">
          <p v-show="showErrorMssage">{{ErrorMessage}}</p>
        </div>
      </div>
    </div>
</template>

 JS部分


<script>
    export default {
        name: "login",
        data(){
          return{
            username:'',
            userword:'',
            yzm_code:'axaz',
            check_yzm:'',
            check_yzm_status:false,
            yzm_code_length:4,
            ErrorMessage:'',
            showErrorMssage:false,
            user:{name:'zhangzhang',password:'123456'}
          }
        },
      computed:{
      // init(){
      //   console.log("初始化验证码---axaz")
      // }
      },  
      methods:{
        changeYZM:function(){
          let yzm_code = '';//点击更换时-初始验证码
          let yzm_code_length = this.yzm_code_length;//验证码长度获取
          let codeChars = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
            'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');//候选字符
          //通过循环获取yzm_code_length位codeChars中的字符下标,并通过下标获取指定字符,最后拼接
          for (let i = 0; i < yzm_code_length; i++ ) {
            let charNum = Math.floor(Math.random() * 62);//获取随机验证码下标
            yzm_code += codeChars[charNum];//根据下标获取指定字符并拼接
          }
          console.log("修改后验证码--"+yzm_code);
          return this.yzm_code = yzm_code;
        },
      checkName:function () {
          if(this.username == ''){
            this.ErrorMessage = '用户名不能为空';
            this.showErrorMssage = true;
            return;
          }else if(this.username != this.user.name){
            this.ErrorMessage = '用户名输入有误';
            this.showErrorMssage = true;
            return;
          }else {
            this.ErrorMessage = '';
            this.showErrorMssage = false;
          }
        },
      checkWord:function () {
          if(this.userword == ''){
            this.ErrorMessage = '用户密码不能为空';
            this.showErrorMssage = true;
            return;
          }else if(this.userword != this.user.password){
            this.ErrorMessage = '用户密码输入有误';
            this.showErrorMssage = true;
            return;
          }else {
            this.ErrorMessage = '';
            this.showErrorMssage = false;
          }
        },
      checkYZM:function () {
          if(this.check_yzm == ''){
            this.ErrorMessage = '验证码不能为空';
            this.showErrorMssage = true;
            return;
          }else if(this.check_yzm.toUpperCase() != this.yzm_code.toUpperCase()){
            console.log("待校验验证码---"+this.check_yzm.toUpperCase())
            console.log("正确验证码---"+this.yzm_code.toUpperCase())
            this.ErrorMessage = '验证码输入有误';
            this.showErrorMssage = true;
            return;
          }else {
            this.ErrorMessage = '';
            this.showErrorMssage = false;
            this.check_yzm_status = true;
          }
        },
      login:function () {
          if(this.username == this.user.name && this.userword == this.user.password){
            if(this.check_yzm_status){
              console.log("用户登录---"+this.username+"---"+this.userword);
              this.ErrorMessage = '登录成功,欢迎您';
              this.showErrorMssage = true;
              this.$router.push('/Home')
            }else {
              this.ErrorMessage = '请再次校验验证码';
              this.showErrorMssage = true;
              return;
            }
          }
        },
      reset:function () {
          this.username = '';
          this.userword = '';
          this.yzm_code = '';
          this.check_yzm = '';
          this.check_yzm_status = false;
          this.ErrorMessage = '请重新输入用户信息';
          this.showErrorMssage = true;
          this.check_yzm_status = false;
        }

      }
    }
</script>