Vue elementUI實現(xiàn)免密登陸與號碼綁定功能
前言
前端代碼的框架采用vue.js + elementUI 這套較為簡單的方式實現(xiàn),以及typescript語法更方便閱讀。
首先來編寫發(fā)送驗證碼函數(shù), 登錄,綁定,解綁的業(yè)務(wù)都需要發(fā)送驗證碼功能,通過currentVerifyingType 來區(qū)別當(dāng)前驗證碼種類。也就是在服務(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ā)送驗證碼成功"); }); }
注意幾個關(guān)鍵的全局變量
userInfo: null, //用戶對象 currentVerifyingType: null, //當(dāng)前發(fā)送驗證碼的用途 smsSendCd: 0, //發(fā)送驗證碼的冷卻時間,默認(rèn)60s loginForm: { //登錄對象 username: "", password: "", }, token: null, //登錄憑證Token verifyNumber: null, //填寫的驗證碼
登錄功能
創(chuàng)建手機號輸入控件
<el-input ref="username" v-model="loginForm.username" :placeholder="'請輸入手機號'" type="text" tabindex="1" autocomplete="on"> <template slot="prepend">+86</template> </el-input>
創(chuàng)建驗證碼控件,并添加一個按鈕用于發(fā)送驗證碼,點擊后觸發(fā)sendVerificationCode
<el-input ref="password" v-model="loginForm.password" :type="passwordType" :placeholder="'發(fā)送驗證碼后鍵入驗證碼'" 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ā)送驗證碼" : smsSendCd + "后重試" }} </el-button> </el-input>
登錄函數(shù),將驗證電話號碼(即用戶名)和驗證碼
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)建新手機號輸入框,若沒有綁定手機,附帶綁定按鈕,按下后將發(fā)送驗證碼;若已綁定手機,需要先解綁,才能綁定新號碼,附帶解綁按鈕,按下后將發(fā)送驗證碼
<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 ? "驗證手機號" : smsSendCd + "后重試" }} </el-button> <el-button v-else size="mini" type="danger" :disabled="smsSendCd > 0" @click=" sendVerificationCode(userInfo.phoneNumber, 'UNBIND_PHONENUMBER') " >{{ smsSendCd == 0 ? "解綁手機號" : smsSendCd + "后重試" }} </el-button>
創(chuàng)建校驗短信驗證碼控件
<el-input v-model="verifyNumber"> <el-button slot="append" type="primary" size="mini" @click="verify"> 完成驗證 </el-button> </el-input>
創(chuàng)建校驗短信驗證碼函數(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對象,并在頁面上簡單展示
<span>{<!--{cke_protected}{C}%3C!%2D%2D%20%2D%2D%3E-->{ userInfo }}</span>
創(chuàng)建一個獲取當(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)的情況下,打開網(wǎng)頁時調(diào)用,在handleLogin函數(shù)中,在請求登錄api后編寫續(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; },
至此,已完成了所有的工作
最終效果
項目地址
到此這篇關(guān)于Vue elementUI實現(xiàn)免密登陸與號碼綁定功能的文章就介紹到這了,更多相關(guān)Vue免密登陸與號碼綁定內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
js表格排序?qū)嵗治觯ㄖС謎nt,float,date,string四種數(shù)據(jù)類型)
這篇文章主要介紹了js表格排序?qū)嵗治觯ㄖС謎nt,float,date,string四種數(shù)據(jù)類型),涉及javascript常用的升序、降序及數(shù)據(jù)類型轉(zhuǎn)換等相關(guān)技巧,需要的朋友可以參考下2015-05-05arrayToJson將數(shù)組轉(zhuǎn)化為json格式的js代碼
arrayToJson將數(shù)組轉(zhuǎn)化為json格式的js代碼,需要的朋友可以參考下。2010-10-10微信小程序+騰訊地圖開發(fā)實現(xiàn)路徑規(guī)劃繪制
這篇文章主要介紹了微信小程序+騰訊地圖開發(fā)實現(xiàn)路徑規(guī)劃繪制,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05js中頁面的重新加載(當(dāng)前頁面/上級頁面)及frame或iframe元素引用介紹
用JavaScript刷新上級頁面和當(dāng)前頁面在某些情況下還是比較實用的,感興趣的朋友可以了解下另外介紹一下frame或iframe元素的引用方法,希望本文對你有所幫助2013-01-01