您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關(guān)Vue怎么實(shí)現(xiàn)驗(yàn)證碼功能的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。
Vue實(shí)現(xiàn)驗(yàn)證碼的具體代碼如下
1.效果
2.代碼
2.1 創(chuàng)建js組件
內(nèi)容
(可直接粘貼過(guò)去,需要改寬度和高度,修改_init方法中的寬和高)
function GVerify (options) { // 創(chuàng)建一個(gè)圖形驗(yàn)證碼對(duì)象,接收options對(duì)象為參數(shù) this.options = { // 默認(rèn)options參數(shù)值 id: '', // 容器Id canvasId: 'verifyCanvas', // canvas的ID width: '80', // 默認(rèn)canvas寬度 height: '30', // 默認(rèn)canvas高度 type: 'number', // 圖形驗(yàn)證碼默認(rèn)類型blend:數(shù)字字母混合類型、number:純數(shù)字、letter:純字母 code: '' } if (Object.prototype.toString.call(options) === '[object Object]') { // 判斷傳入?yún)?shù)類型 for (var i in options) { // 根據(jù)傳入的參數(shù),修改默認(rèn)參數(shù)值 this.options[i] = options[i] } } else { this.options.id = options } this.options.numArr = '0,1,2,3,4,5,6,7,8,9'.split(',') this.options.letterArr = getAllLetter() this._init() this.refresh() } GVerify.prototype = { /** 版本號(hào)**/ version: '1.0.0', /** 初始化方法**/ _init: function () { var con = document.getElementById(this.options.id) var canvas = document.createElement('canvas') // this.options.width = con.offsetWidth > 0 ? con.offsetWidth : '30' // this.options.height = con.offsetHeight > 0 ? con.offsetHeight : '30' this.options.width = '160' this.options.height = '50' canvas.id = this.options.canvasId canvas.width = this.options.width canvas.height = this.options.height canvas.style.cursor = 'pointer' canvas.innerHTML = '您的瀏覽器版本不支持canvas' con.appendChild(canvas) var parent = this canvas.onclick = function () { parent.refresh() } }, /** 生成驗(yàn)證碼**/ refresh: function () { var canvas = document.getElementById(this.options.canvasId) if (canvas.getContext) { var ctx = canvas.getContext('2d') } ctx.textBaseline = 'middle' ctx.fillStyle = randomColor(180, 240) ctx.fillRect(0, 0, this.options.width, this.options.height) if (this.options.type === 'blend') { // 判斷驗(yàn)證碼類型 var txtArr = this.options.numArr.concat(this.options.letterArr) } else if (this.options.type === 'number') { var txtArr = this.options.numArr } else { var txtArr = this.options.letterArr } for (var i = 1; i <= 4; i++) { var txt = txtArr[randomNum(0, txtArr.length)] this.options.code += txt ctx.font = randomNum(this.options.height / 2, this.options.height) + 'px SimHei' // 隨機(jī)生成字體大小 ctx.fillStyle = randomColor(50, 160) // 隨機(jī)生成字體顏色 ctx.shadowOffsetX = randomNum(-3, 3) ctx.shadowOffsetY = randomNum(-3, 3) ctx.shadowBlur = randomNum(-3, 3) ctx.shadowColor = 'rgba(0, 0, 0, 0.3)' var x = this.options.width / 5 * i var y = this.options.height / 2 var deg = randomNum(-30, 30) /** 設(shè)置旋轉(zhuǎn)角度和坐標(biāo)原點(diǎn)**/ ctx.translate(x, y) ctx.rotate(deg * Math.PI / 180) ctx.fillText(txt, 0, 0) /** 恢復(fù)旋轉(zhuǎn)角度和坐標(biāo)原點(diǎn)**/ ctx.rotate(-deg * Math.PI / 180) ctx.translate(-x, -y) } /** 繪制干擾線**/ for (var i = 0; i < 4; i++) { ctx.strokeStyle = randomColor(40, 180) ctx.beginPath() ctx.moveTo(randomNum(0, this.options.width), randomNum(0, this.options.height)) ctx.lineTo(randomNum(0, this.options.width), randomNum(0, this.options.height)) ctx.stroke() } /** 繪制干擾點(diǎn)**/ for (var i = 0; i < this.options.width / 4; i++) { ctx.fillStyle = randomColor(0, 255) ctx.beginPath() ctx.arc(randomNum(0, this.options.width), randomNum(0, this.options.height), 1, 0, 2 * Math.PI) ctx.fill() } }, /** 驗(yàn)證驗(yàn)證碼**/ validate: function (code) { var code = code.toLowerCase() var v_code = this.options.code.toLowerCase() if (code == v_code) { return true } else { return false } } } /** 生成字母數(shù)組**/ function getAllLetter () { var letterStr = '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' return letterStr.split(',') } /** 生成一個(gè)隨機(jī)數(shù)**/ function randomNum (min, max) { return Math.floor(Math.random() * (max - min) + min) } /** 生成一個(gè)隨機(jī)色**/ function randomColor (min, max) { var r = randomNum(min, max) var g = randomNum(min, max) var b = randomNum(min, max) return 'rgb(' + r + ',' + g + ',' + b + ')' } export { GVerify }
2.2 登錄頁(yè)面
2.2.1 引入組件
[....<script>] import { GVerify } from '../../static/js/verifyCode'; [export default { ....]
2.2.2 定義驗(yàn)證對(duì)象
注意 verifyCode
data: function () { return { title: '若晨后臺(tái)管理系統(tǒng)', ruleForm: { username: '', password: '', verifyCode: '' }, rules: { username: [ { required: true, message: '請(qǐng)輸入用戶名', trigger: 'blur' } ], password: [{ required: true, message: '請(qǐng)輸入密碼', trigger: 'blur' }], verifyCode: [ { required: true, message: '請(qǐng)輸入驗(yàn)證碼', trigger: 'blur' } ] }, verifyCode: null } },
2.2.3 初始化節(jié)點(diǎn)
<el-form-item prop="verifyCode" class="verifyCodeItemCss"> <el-input class="verify_css" placeholder="請(qǐng)輸入4位驗(yàn)證碼" v-model="ruleForm.verifyCode" @keyup.enter.native="submitForm('ruleForm')"></el-input> <!--關(guān)鍵 ↓--> <div id="v_container"></div> </el-form-item>
mounted方法中初始化 'v_container' 為div的id
mounted () { this.verifyCode = new GVerify('v_container') }
2.2.4 驗(yàn)證輸入的是否正確
通過(guò)在data中定義的verifyCode對(duì)象的validate()方法,如果輸入正確返回true 錯(cuò)誤返回 false
var that = this // 獲取驗(yàn)證碼 var verifyCode = this.ruleForm.verifyCode var verifyFlag = this.verifyCode.validate(verifyCode) if (!verifyFlag) { that.$notify.error({ title: '系統(tǒng)提示', message: '驗(yàn)證碼輸入錯(cuò)誤' }) return; } else { that.$notify({ title: '系統(tǒng)提示', message: '驗(yàn)證碼輸入正確', type: 'success' }) }
3.全部頁(yè)面代碼
<template> <div class="login-wrap"> <div class="video-bg"> <video muted="" data-autoplay="" loop="" autoplay="" src="https://gss3.baidu.com/6LZ0ej3k1Qd3ote6lo7D0j9wehsv/tieba-movideo/65886292_9687ec67dfc37bfbf847d82b6b52a276_96e56b0f4bfc.mp4" class="video-tvc" id="video-tvc" data-object-fit=""> </video> </div> <div class="ms-title">{{title}}</div> <div class="ms-login"> <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="0px" class="demo-ruleForm"> <el-form-item prop="username"> <el-input v-model="ruleForm.username" placeholder="請(qǐng)輸入賬號(hào)"></el-input> </el-form-item> <el-form-item prop="password"> <el-input type="password" placeholder="請(qǐng)輸入密碼" v-model="ruleForm.password" @keyup.enter.native="submitForm('ruleForm')"></el-input> </el-form-item> <el-form-item prop="verifyCode" class="verifyCodeItemCss"> <el-input class="verify_css" placeholder="請(qǐng)輸入4位驗(yàn)證碼" v-model="ruleForm.verifyCode" @keyup.enter.native="submitForm('ruleForm')"></el-input> <div id="v_container"></div> </el-form-item> <div class="login-btn"> <el-button class="loginBtn" 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: '若晨后臺(tái)管理系統(tǒng)', ruleForm: { username: '', password: '', verifyCode: '' }, rules: { username: [ { required: true, message: '請(qǐng)輸入用戶名', trigger: 'blur' } ], password: [{ required: true, message: '請(qǐng)輸入密碼', trigger: 'blur' }], verifyCode: [ { required: true, message: '請(qǐng)輸入驗(yàn)證碼', trigger: 'blur' } ] }, verifyCode: null } }, mounted () { // 隨機(jī)播放幀數(shù) this.videoCut() this.verifyCode = new GVerify('v_container') }, methods: { submitForm (formName) { var that = this // 獲取驗(yàn)證碼 var verifyCode = this.ruleForm.verifyCode var verifyFlag = this.verifyCode.validate(verifyCode) if (!verifyFlag) { that.$notify.error({ title: '系統(tǒng)提示', message: '驗(yàn)證碼輸入錯(cuò)誤' }) return; } else { that.$notify({ title: '系統(tǒng)提示', message: '驗(yàn)證碼輸入正確', 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("請(qǐng)求成功",res) // if(res.data.data != undefined){ // that.$notify({ // title: '系統(tǒng)提示', // 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: '系統(tǒng)提示', // message: '用戶賬戶密碼輸出錯(cuò)誤' // }); // } 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>
感謝各位的閱讀!關(guān)于“Vue怎么實(shí)現(xiàn)驗(yàn)證碼功能”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。