Vue elementUI實(shí)現(xiàn)免密登陸與號(hào)碼綁定功能
前言
前端代碼的框架采用vue.js + elementUI 這套較為簡(jiǎn)單的方式實(shí)現(xiàn),以及typescript語(yǔ)法更方便閱讀。
首先來(lái)編寫(xiě)發(fā)送驗(yàn)證碼函數(shù), 登錄,綁定,解綁的業(yè)務(wù)都需要發(fā)送驗(yàn)證碼功能,通過(guò)currentVerifyingType 來(lái)區(qū)別當(dāng)前驗(yàn)證碼種類(lèi)。也就是在服務(wù)端的Purpose目的。
VerifyingType 可以為LOGIN
,UNBIND_PHONENUMBER
或BIND_PHONENUMBER
async sendVerificationCode(phoneNumber, type) { this.currentVerifyingType = type; this.smsSendCd = 60; this.timer = setInterval(() => { this.smsSendCd--; if (this.smsSendCd <= 0) { clearInterval(this.timer); } }, 1000); await request(`${this.host}${this.prefix}/Captcha/Send`, "post", { userId: this.userInfo == null ? null : this.userInfo.id, phoneNumber: phoneNumber, type: type, }) .catch((re) => { var res = re.response.data; this.errorMessage(res.error.message); }) .then((re) => { var res = re.data.result; this.successMessage("發(fā)送驗(yàn)證碼成功"); }); }
注意幾個(gè)關(guān)鍵的全局變量
userInfo: null, //用戶對(duì)象 currentVerifyingType: null, //當(dāng)前發(fā)送驗(yàn)證碼的用途 smsSendCd: 0, //發(fā)送驗(yàn)證碼的冷卻時(shí)間,默認(rèn)60s loginForm: { //登錄對(duì)象 username: "", password: "", }, token: null, //登錄憑證Token verifyNumber: null, //填寫(xiě)的驗(yàn)證碼
登錄功能
創(chuàng)建手機(jī)號(hào)輸入控件
<el-input ref="username" v-model="loginForm.username" :placeholder="'請(qǐng)輸入手機(jī)號(hào)'" type="text" tabindex="1" autocomplete="on"> <template slot="prepend">+86</template> </el-input>
創(chuàng)建驗(yàn)證碼控件,并添加一個(gè)按鈕用于發(fā)送驗(yàn)證碼,點(diǎn)擊后觸發(fā)sendVerificationCode
<el-input ref="password" v-model="loginForm.password" :type="passwordType" :placeholder="'發(fā)送驗(yàn)證碼后鍵入驗(yàn)證碼'" tabindex="2" autocomplete="on" @blur="capsTooltip = false" @keyup.enter.native="handleLogin" > <el-button slot="append" :disabled="smsSendCd > 0" @click="sendVerificationCode(loginForm.username, 'LOGIN')" >{{ smsSendCd == 0 ? "發(fā)送驗(yàn)證碼" : smsSendCd + "后重試" }} </el-button> </el-input>
登錄函數(shù),將驗(yàn)證電話號(hào)碼(即用戶名)和驗(yàn)證碼
async handleLogin() { this.loading = true; let phoneNumber = this.loginForm.username; let password = this.loginForm.password; phoneNumber = phoneNumber.trim(); await request(`${this.host}api/TokenAuth/Authenticate`, "post", { phoneNumber, password, }); }
登錄完成后,將Token存入Cookie
.then(async (res) => { var data = res.data.result; setToken(data.accessToken);
綁定/解綁功能
創(chuàng)建新手機(jī)號(hào)輸入框,若沒(méi)有綁定手機(jī),附帶綁定按鈕,按下后將發(fā)送驗(yàn)證碼;若已綁定手機(jī),需要先解綁,才能綁定新號(hào)碼,附帶解綁按鈕,按下后將發(fā)送驗(yàn)證碼
<el-input v-model="userInfo.phoneNumber"> </el-input> <el-button v-if="!userInfo.isPhoneNumberConfirmed" size="mini" type="primary" :disabled="smsSendCd > 0" @click=" sendVerificationCode(userInfo.phoneNumber, 'BIND_PHONENUMBER') " >{{ smsSendCd == 0 ? "驗(yàn)證手機(jī)號(hào)" : smsSendCd + "后重試" }} </el-button> <el-button v-else size="mini" type="danger" :disabled="smsSendCd > 0" @click=" sendVerificationCode(userInfo.phoneNumber, 'UNBIND_PHONENUMBER') " >{{ smsSendCd == 0 ? "解綁手機(jī)號(hào)" : smsSendCd + "后重試" }} </el-button>
創(chuàng)建校驗(yàn)短信驗(yàn)證碼控件
<el-input v-model="verifyNumber"> <el-button slot="append" type="primary" size="mini" @click="verify"> 完成驗(yàn)證 </el-button> </el-input>
創(chuàng)建校驗(yàn)短信驗(yàn)證碼函數(shù),
async verify() { var action = null; if (this.currentVerifyingType == "BIND_PHONENUMBER") { action = "Bind"; } else if (this.currentVerifyingType == "UNBIND_PHONENUMBER") { action = "Unbind"; } else { action = "Verify"; } await request(`${this.host}${this.prefix}/Captcha/${action}`, "post", { token: this.verifyNumber, }) .catch((re) => { var res = re.response.data; this.errorMessage(res.error.message); }) .then((re) => { var res = re.data; if (res.success) { this.successMessage("綁定成功"); window.location.reload() } }); }
獲取用戶信息功能
登錄成功后我們要拿到當(dāng)前用戶的信息,存入userInfo對(duì)象,并在頁(yè)面上簡(jiǎn)單展示
<span>{<!--{cke_protected}{C}%3C!%2D%2D%20%2D%2D%3E-->{ userInfo }}</span>
創(chuàng)建一個(gè)獲取當(dāng)前用戶的函數(shù)
async getCurrentUser() { await request( `${this.host}${this.prefix}/User/GetCurrentUser`, "get", null ) .catch((re) => { var res = re.response.data; this.errorMessage(res.error.message); }) .then(async (re) => { var result = re.data.result as any; this.userInfo = result; this.token = getToken(); clearInterval(this.timer); this.smsSendCd = 0; this.currentVerifyingType = null; this.successMessage("登錄成功"); }); }
此函數(shù)將在成功登錄之后調(diào)用,也用于已登錄狀態(tài)的情況下,打開(kāi)網(wǎng)頁(yè)時(shí)調(diào)用,在handleLogin函數(shù)中,在請(qǐng)求登錄api后編寫(xiě)續(xù)操作
.then(async (res) => { var data = res.data.result; setToken(data.accessToken); await this.getCurrentUser(); })
獲取用戶信息功能
登出, 將Token以及用戶信息置空
<el-button :loading="loading" type="danger" style="width: 100%" @click.native.prevent="logout"> 退出登錄 </el-button>
logout() { setToken(null); this.token = null; this.userInfo = null; },
至此,已完成了所有的工作
最終效果
項(xiàng)目地址
到此這篇關(guān)于Vue elementUI實(shí)現(xiàn)免密登陸與號(hào)碼綁定功能的文章就介紹到這了,更多相關(guān)Vue免密登陸與號(hào)碼綁定內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
js表格排序?qū)嵗治觯ㄖС謎nt,float,date,string四種數(shù)據(jù)類(lèi)型)
這篇文章主要介紹了js表格排序?qū)嵗治觯ㄖС謎nt,float,date,string四種數(shù)據(jù)類(lèi)型),涉及javascript常用的升序、降序及數(shù)據(jù)類(lèi)型轉(zhuǎn)換等相關(guān)技巧,需要的朋友可以參考下2015-05-05arrayToJson將數(shù)組轉(zhuǎn)化為json格式的js代碼
arrayToJson將數(shù)組轉(zhuǎn)化為json格式的js代碼,需要的朋友可以參考下。2010-10-10js 回車(chē)提交表單兩種實(shí)現(xiàn)方法
js 回車(chē)提交一些新手朋友還是比較陌生的,本文介紹兩種實(shí)現(xiàn)方法:jQuery方法、JavaScript方法,感興趣的朋友可以研究下2012-12-12微信小程序+騰訊地圖開(kāi)發(fā)實(shí)現(xiàn)路徑規(guī)劃繪制
這篇文章主要介紹了微信小程序+騰訊地圖開(kāi)發(fā)實(shí)現(xiàn)路徑規(guī)劃繪制,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05js實(shí)現(xiàn)中文實(shí)時(shí)時(shí)鐘
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)中文實(shí)時(shí)時(shí)鐘,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-01-01在線一元二次方程計(jì)算器實(shí)例(方程計(jì)算器在線計(jì)算)
在線一元二次方程式計(jì)算器實(shí)例分享,大家參考使用吧2013-12-12js中頁(yè)面的重新加載(當(dāng)前頁(yè)面/上級(jí)頁(yè)面)及frame或iframe元素引用介紹
用JavaScript刷新上級(jí)頁(yè)面和當(dāng)前頁(yè)面在某些情況下還是比較實(shí)用的,感興趣的朋友可以了解下另外介紹一下frame或iframe元素的引用方法,希望本文對(duì)你有所幫助2013-01-01