vue前端實現(xiàn)login頁登陸驗證碼代碼示例
更新時間:2024年05月03日 08:16:18 作者:Yoga99
現(xiàn)在我們管理后臺有個需求,就是登錄頁面需要獲取驗證碼,用戶可以輸入驗證碼后進行登錄,這篇文章主要給大家介紹了關(guān)于vue前端實現(xiàn)login頁登陸驗證碼的相關(guān)資料,需要的朋友可以參考下
實現(xiàn)效果

// template
<el-form :model="loginForm" :rules="fieldRules" ref="loginForm" label-position="left" label-width="0px" class="login-container">
<span class="tool-bar"></span>
<h2 class="title">用戶登陸</h2>
<el-form-item prop="account">
<el-input type="text" v-model.trim="loginForm.account" auto-complete="false" placeholder="賬號"></el-input>
</el-form-item>
<el-form-item prop="password" class="item-m10">
<el-input type="password" v-model.trim="loginForm.password" auto-complete="false" placeholder="密碼"></el-input>
</el-form-item>
<el-form-item prop="code" align="left" style="margin-top: 20px">
<el-input v-model="loginForm.code" style="width: 170px" placeholder="驗證碼,點擊圖片刷新"></el-input>
<el-tag class="login-tag-code" @click="getCode">{{ viewCode }}</el-tag>
</el-form-item>
<div class="checked-item">
<el-checkbox v-model="checked">記住密碼</el-checkbox>
</div>
<el-form-item style="width: 100%" class="btn-item">
<el-button style="width: 100%" @click.native.prevent="loginSubmit" :loading="loading">登錄</el-button>
</el-form-item>
// js
// ---------分割線
data () {
return {
viewCode: '',
loginForm: {
account: '',
password: '',
src: '',
code: ''
},
fieldRules: {
account: [{ required: true, message: '請輸入賬號', trigger: 'blur' }],
password: [{ required: true, message: '請輸入密碼', trigger: 'blur' }]
},
checked: false,
// 加載中的標(biāo)志
loading: false
}
},
// ------ 分割線
methods: {
loginSubmit () {
if (!this.loginForm.account || !this.loginForm.password) {
this.$message.error('賬號或密碼不能為空!')
return
}
if (!this.loginForm.code || this.loginForm.code !== this.viewCode) {
this.$message.error('驗證碼不正確!')
return
}
this.loading = true
let userInfo = {
account: this.loginForm.account,
password: this.loginForm.password
}
//登陸接口
this.$api.login
.login(userInfo)
.then((res) => {
if (this.checked) {
let rememberInfo = JSON.stringify({ ...userInfo })
localStorage.setItem('rememberInfo', rememberInfo) // 記住密碼時 保存login
} else {
localStorage.removeItem('rememberInfo')
}
this.$router.push('/') // 登錄成功,跳轉(zhuǎn)到主頁
this.loading = false
})
.catch((err) => {
this.$message({ message: err.message, type: 'error' })
})
},
//獲取驗證碼
getCode () {
this.viewCode = ''
let codeString = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
let codeArray = codeString.split('')
let num = codeArray.length
let newCodeArray = []
for (let i = 0; i < 5; i++) {
let index = Math.floor(Math.random() * num)
newCodeArray.push(codeArray[index])
}
this.viewCode = newCodeArray.join('')
},
},
mounted () {
this.getCode()
if (localStorage.getItem('rememberInfo')) {
// 有上次登錄信息 顯示上次登錄
let rememberData = JSON.parse(localStorage.getItem('rememberInfo'))
const { account, password } = rememberData
this.loginForm.account = account
this.loginForm.password = password
}
}
樣式代碼省略。。。
總結(jié)
到此這篇關(guān)于vue前端實現(xiàn)login頁登陸驗證碼的文章就介紹到這了,更多相關(guān)vue登陸頁驗證碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue transition實現(xiàn)點贊動畫效果的示例
點贊動畫是網(wǎng)頁評論中常見的功能,本文將介紹如何用vue實現(xiàn)這一效果。點贊時愛心縮小變大,變大時略微大一點再變正常,取消點贊時愛心無動畫,同時數(shù)字滾動,+1 時向上滾動,-1 時向下滾動2021-05-05
vue+element表格實現(xiàn)多層數(shù)據(jù)的嵌套方式
這篇文章主要介紹了vue+element表格實現(xiàn)多層數(shù)據(jù)的嵌套方式,具有很好的參考價值。希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09

