vue前端實(shí)現(xiàn)驗(yàn)證碼登錄功能
登錄時(shí)圖形驗(yàn)證
方法一:插件(vue移動(dòng)端(PC端)圖形驗(yàn)證碼)
vue移動(dòng)端(PC端)圖形驗(yàn)證碼
vue2-verify
地址:vue2-verify的npmjs地址
安裝使用:
npm i vue2-verify
支持的驗(yàn)證碼類型:
- 常規(guī)驗(yàn)證碼picture 常規(guī)的驗(yàn)證碼由數(shù)字和字母構(gòu)成,用戶輸入不區(qū)分大小寫,可變形成漢字驗(yàn)證。
- 運(yùn)算驗(yàn)證碼compute 運(yùn)算驗(yàn)證碼主要通過(guò)給出數(shù)字的加減乘運(yùn)算,填寫運(yùn)算結(jié)果進(jìn)行驗(yàn)證。
- 滑動(dòng)驗(yàn)證碼slide 通過(guò)簡(jiǎn)單的滑動(dòng)即可完成驗(yàn)證,應(yīng)用與移動(dòng)端體驗(yàn)很好。
- 拼圖驗(yàn)證碼puzzle 拼圖。
- 選字驗(yàn)證碼pick 通過(guò)按順序點(diǎn)選圖中的漢字完成驗(yàn)證,ie瀏覽器要求9或以上。
方法二:插件(一款拼圖驗(yàn)證碼)
slide-verify(圖片建議傳、不傳的話默認(rèn)圖片加載非常慢)
安裝使用:
npm install --save vue-monoplasty-slide-verify
地址:
方法三:頁(yè)面實(shí)例(點(diǎn)擊圖案可以切換字符)
vue實(shí)現(xiàn)登錄時(shí)圖形驗(yàn)證碼
1.新建 Identify.vue 組件
<template>
<div>
<canvas
id="s-canvas"
:width="contentWidth"
:height="contentHeight"></canvas>
</div>
</template>
<script>
export default {
name: "identify",
props: {
identifyCode: {
type: String,
default: ''
},
fontSizeMin: {
type: Number,
default: 28
},
fontSizeMax: {
type: Number,
default: 40
},
backgroundColorMin: {
type: Number,
default: 180
},
backgroundColorMax: {
type: Number,
default: 240
},
colorMin: {
type: Number,
default: 50
},
colorMax: {
type: Number,
default: 160
},
lineColorMin: {
type: Number,
default: 40
},
lineColorMax: {
type: Number,
default: 180
},
dotColorMin: {
type: Number,
default: 0
},
dotColorMax: {
type: Number,
default: 255
},
contentWidth: {
type: Number,
default: 130
},
contentHeight: {
type: Number,
default: 40
}
},
methods:{
// 生成一個(gè)隨機(jī)數(shù)
randomNum (min, max) {
return Math.floor(Math.random() * (max - min) + min)
},
// 生成一個(gè)隨機(jī)的顏色
randomColor (min, max) {
let r = this.randomNum(min, max)
let g = this.randomNum(min, max)
let b = this.randomNum(min, max)
return 'rgb(' + r + ',' + g + ',' + b + ')'
},
drawPic () {
let canvas = document.getElementById('s-canvas')
let ctx = canvas.getContext('2d')
ctx.textBaseline = 'bottom'
// 繪制背景
ctx.fillStyle = this.randomColor(
this.backgroundColorMin,
this.backgroundColorMax
)
ctx.fillRect(0, 0, this.contentWidth, this.contentHeight)
// 繪制文字
for (let i = 0; i < this.identifyCode.length; i++) {
this.drawText(ctx, this.identifyCode[i], i)
}
this.drawLine(ctx)
this.drawDot(ctx)
},
drawText (ctx, txt, i) {
ctx.fillStyle = this.randomColor(this.colorMin, this.colorMax)
ctx.font =
this.randomNum(this.fontSizeMin, this.fontSizeMax) + 'px SimHei'
let x = (i + 1) * (this.contentWidth / (this.identifyCode.length + 1))
let y = this.randomNum(this.fontSizeMax, this.contentHeight - 5)
let deg = this.randomNum(-30, 30)
// 修改坐標(biāo)原點(diǎn)和旋轉(zhuǎn)角度
ctx.translate(x, y)
ctx.rotate(deg * Math.PI / 270)
ctx.fillText(txt, 0, 0)
// 恢復(fù)坐標(biāo)原點(diǎn)和旋轉(zhuǎn)角度
ctx.rotate(-deg * Math.PI / 270)
ctx.translate(-x, -y)
},
drawLine (ctx) {
// 繪制干擾線
for (let i = 0; i < 2; i++) {
ctx.strokeStyle = this.randomColor(
this.lineColorMin,
this.lineColorMax
)
ctx.beginPath()
ctx.moveTo(
this.randomNum(0, this.contentWidth),
this.randomNum(0, this.contentHeight)
)
ctx.lineTo(
this.randomNum(0, this.contentWidth),
this.randomNum(0, this.contentHeight)
)
ctx.stroke()
}
},
drawDot (ctx) {
// 繪制干擾點(diǎn)
for (let i = 0; i < 20; i++) {
ctx.fillStyle = this.randomColor(0, 255)
ctx.beginPath()
ctx.arc(
this.randomNum(0, this.contentWidth),
this.randomNum(0, this.contentHeight),
1,
0,
2 * Math.PI
)
ctx.fill()
}
}
},
watch: {
identifyCode () {
this.drawPic()
}
},
mounted () {
this.drawPic()
}
}
</script>
<style lang="scss" scoped>
#s-canvas {
height: 38px;
}
</style>2.在父組件 index.vue注冊(cè)使用
<template>
<div @click="refreshCode" style="cursor: pointer;">
<Identify :identifyCode="identifyCode" ></Identify>
</div>
</template>
<script>
import Identify from '@/components/test/identify'
export default {
name: "index",
components:{
Identify
},
data(){
return {
identifyCode: '',
// 驗(yàn)證碼規(guī)則
identifyCodes: '123456789ABCDEFGHGKMNPQRSTUVWXYZ',
}
},
methods:{
// 切換驗(yàn)證碼
refreshCode() {
this.identifyCode = ''
this.makeCode(this.identifyCodes, 4)
console.log(this.identifyCode)
},
// 生成隨機(jī)驗(yàn)證碼
makeCode(o, l) {
for (let i = 0; i<l; i++) {
this.identifyCode += this.identifyCodes[
Math.floor(Math.random() * (this.identifyCodes.length - 0) + 0)
]
}
},
mounted() {
this.refreshCode()
}
}
</script>
<style scoped>
</style>到此這篇關(guān)于vue前端實(shí)現(xiàn)登錄時(shí)加驗(yàn)證碼的文章就介紹到這了,更多相關(guān)vue驗(yàn)證碼登錄內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
在Vue中實(shí)現(xiàn)拖拽功能的實(shí)例
Vue實(shí)現(xiàn)拖拽功能的基本原理是監(jiān)聽(tīng)鼠標(biāo)事件,實(shí)時(shí)更新拖拽元素的位置,最后在合適的時(shí)機(jī)停止拖拽并更新元素位置,在Vue中,我們可以通過(guò)綁定相關(guān)事件來(lái)實(shí)現(xiàn)這一功能2025-02-02
15 分鐘掌握vue-next函數(shù)式api(小結(jié))
這篇文章主要介紹了15 分鐘掌握vue-next函數(shù)式api(小結(jié)),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
Vue3?響應(yīng)式高階用法之customRef()的使用
customRef()是Vue3的高級(jí)工具,允許開(kāi)發(fā)者創(chuàng)建具有復(fù)雜依賴跟蹤和自定義更新邏輯的ref對(duì)象,本文詳細(xì)介紹了customRef()的使用場(chǎng)景、基本用法、功能詳解以及最佳實(shí)踐,包括防抖、異步更新等用例,旨在幫助開(kāi)發(fā)者更好地理解和使用這一強(qiáng)大功能2024-09-09
vue.js click點(diǎn)擊事件獲取當(dāng)前元素對(duì)象的操作
這篇文章主要介紹了vue.js click點(diǎn)擊事件獲取當(dāng)前元素對(duì)象的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08
詳解如何在Vue2中實(shí)現(xiàn)useDraggable
這篇文章主要為大家詳細(xì)介紹了Vue2中實(shí)現(xiàn)useDraggable的相關(guān)知識(shí),文中的示例代碼簡(jiǎn)潔易懂,對(duì)我們深入了解vue有一定的幫助,需要的小伙伴可以參考下2023-12-12
使用mint-ui實(shí)現(xiàn)省市區(qū)三級(jí)聯(lián)動(dòng)效果的示例代碼
下面小編就為大家分享一篇使用mint-ui實(shí)現(xiàn)省市區(qū)三級(jí)聯(lián)動(dòng)效果的示例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-02-02
element-ui 關(guān)于獲取select 的label值方法
今天小編就為大家分享一篇element-ui 關(guān)于獲取select 的label值方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
解決vue cli使用typescript后打包巨慢的問(wèn)題
這篇文章主要介紹了解決vue cli使用typescript后打包巨慢的問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09

