Vue实现验证码功能(2)

<el-form-item prop="verifyCode"> <el-input placeholder="请输入4位验证码" v-model="ruleForm.verifyCode" @keyup.enter.native="submitForm('ruleForm')"></el-input> <!--关键 ↓--> <div></div> </el-form-item>

mounted方法中初始化 'v_container' 为div的id

mounted () { this.verifyCode = new GVerify('v_container') }

2.2.4 验证输入的是否正确

通过在data中定义的verifyCode对象的validate()方法,如果输入正确返回true 错误返回 false

var that = this // 获取验证码 var verifyCode = this.ruleForm.verifyCode var verifyFlag = this.verifyCode.validate(verifyCode) if (!verifyFlag) { that.$notify.error({ title: '系统提示', message: '验证码输入错误' }) return; } else { that.$notify({ title: '系统提示', message: '验证码输入正确', type: 'success' }) }

3.全部页面代码

<template> <div> <div> <video muted="" data-autoplay="" loop="" autoplay="" src="https://gss3.baidu.com/6LZ0ej3k1Qd3ote6lo7D0j9wehsv/tieba-movideo/65886292_9687ec67dfc37bfbf847d82b6b52a276_96e56b0f4bfc.mp4" data-object-fit=""> </video> </div> <div>{{title}}</div> <div> <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="0px"> <el-form-item prop="username"> <el-input v-model="ruleForm.username" placeholder="请输入账号"></el-input> </el-form-item> <el-form-item prop="password"> <el-input type="password" placeholder="请输入密码" v-model="ruleForm.password" @keyup.enter.native="submitForm('ruleForm')"></el-input> </el-form-item> <el-form-item prop="verifyCode"> <el-input placeholder="请输入4位验证码" v-model="ruleForm.verifyCode" @keyup.enter.native="submitForm('ruleForm')"></el-input> <div></div> </el-form-item> <div> <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button> </div> <!-- <p>Tips : 用户名和密码随便填。</p> --> </el-form> </div> </div> </template> <script> import { GVerify } from '../../static/js/verifyCode'; export default { data: function () { return { title: '若晨后台管理系统', ruleForm: { username: '', password: '', verifyCode: '' }, rules: { username: [ { required: true, message: '请输入用户名', trigger: 'blur' } ], password: [{ required: true, message: '请输入密码', trigger: 'blur' }], verifyCode: [ { required: true, message: '请输入验证码', trigger: 'blur' } ] }, verifyCode: null } }, mounted () { // 随机播放帧数 this.videoCut() this.verifyCode = new GVerify('v_container') }, methods: { submitForm (formName) { var that = this // 获取验证码 var verifyCode = this.ruleForm.verifyCode var verifyFlag = this.verifyCode.validate(verifyCode) if (!verifyFlag) { that.$notify.error({ title: '系统提示', message: '验证码输入错误' }) return; } else { that.$notify({ title: '系统提示', message: '验证码输入正确', type: 'success' }) } this.$refs[formName].validate(valid => { if (valid) { // 判断是否登录成功 let param = { userName: that.ruleForm.username, passWord: that.ruleForm.password } // this.axios.post(this.$service.user.USER_LOGIN_API.url,param).then(res=>{ // console.log("请求成功",res) // if(res.data.data != undefined){ // that.$notify({ // title: '系统提示', // message: '登录成功', // type:"success" // }); // // 存local // localStorage.setItem('ms_username',res.data.data.userNickName); // localStorage.setItem('token',res.data.data.id); // self.$router.push('/index'); // }else{ // that.$notify.error({ // title: '系统提示', // message: '用户账户密码输出错误' // }); // } localStorage.setItem('ms_username', 'admin') localStorage.setItem('token', 'admin') that.$router.push('/index') } else { console.log('error submit!!') return false } }) }, videoCut () { $('video').on('loadedmetadata', function (event) { var duration = Math.ceil(this.duration) this.currentTime = Math.round(Math.random() * duration) }) } } } </script> <style scoped> .verify_css { width: 45%; } .login-wrap { position: relative; width: 100%; height: 100%; } .ms-title { position: absolute; top: 50%; width: 100%; margin-top: -230px; text-align: center; font-size: 30px; color: #fff; } .ms-login { position: absolute; left: 50%; top: 50%; width: 300px; height: 13rem; margin: -150px 0 0 -190px; padding: 40px; border-radius: 6%; background: #fff; box-shadow: -2px -2px 17px 1px #1e9fff; } .login-btn { text-align: center; } .login-btn button { width: 100%; height: 36px; } .video-bg { min-width: 100%; min-height: 100%; width: 100%; height: 100%; opacity: 0.9; } video { width: 100%; height: 100%; object-fit: cover; /* opacity: 0.6; */ } .loginBtn:hover { box-shadow: 0px 1px 4px rgba(0, 0, 0, 0.3), 0px 0px 20px rgba(0, 0, 0, 0.1) inset; } #v_container { width: auto; height: auto; display: inline-flex; position: relative; top: 1rem; margin-top: -2rem; } </style>

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/3e2ad4d8772e6786603263b508ba4fd9.html