uniapp實(shí)現(xiàn)注冊發(fā)送獲取驗(yàn)證碼功能
說明
驗(yàn)證碼服務(wù)可自行去申請,不過好像是要企業(yè)才能申請,這里
結(jié)合uview組件庫+mockjs在本地實(shí)現(xiàn)發(fā)送并獲取驗(yàn)證碼
配置
驗(yàn)證碼組件u-verification-code官網(wǎng)不再維護(hù),但不影響使用可在uniapp直接配置。

在項(xiàng)目pages.json文件中配置好路徑
"easycom": {
"^u-(.*)": "@/uview-ui/components/u-$1/u-$1.vue"
},
mockjs使用
根目錄創(chuàng)建mock文件夾,分別創(chuàng)建mock/index.js,mock/code.json

index.js
import Mock from 'mockjs'
import code from './code.json'
Mock.mock('/mock/code',{code:200,data:code,msg:"獲取驗(yàn)證碼成功"})
code.json
[ "1234","4553","2346","9765","4567","4589","0087","4538","8796" ]
封裝axios,創(chuàng)建請求api
根目錄下創(chuàng)建utils
utils/mockRequest.js
import axios from '@/js_sdk/gangdiedao-uni-axios'
//根據(jù)自身情況引入axios
// 創(chuàng)建自定義接口服務(wù)實(shí)例
const http = axios.create({
baseURL: '/mock',//根據(jù)個人后端情況,修改此處baseURL
timeout: 6000, // 不可超過 manifest.json 中配置 networkTimeout的超時時間
withCredentials: true,
headers: {
'Content-Type': 'application/json',
//'X-Requested-With': 'XMLHttpRequest',
},
})
// 攔截器 在請求之前攔截 添加token
http.interceptors.request.use(config => {
//請求前有關(guān)處理邏輯
return config
})
// 響應(yīng)后的攔截
http.interceptors.response.use(response => {
//響應(yīng)攔截處理邏輯
return response;
}, error => {
return Promise.reject(error.message)
})
export default http
根目錄創(chuàng)建api文件夾
api/index.js
import mockRequest from '@/utils/mockRequest.js'
//驗(yàn)證碼
export const getCode = () => mockRequest.get('/code')
main.js注冊全局api
import * as API from '@/api/index.js' Vue.prototype.$API = API
前端調(diào)用
template
<u-input style="width: 300rpx" v-model="code" placeholder="請輸入驗(yàn)證碼" />
<view class="bd-primary xs orange br60 flex row-center" @click="sendSmsFun" hover-class="none">
<!-- 獲取驗(yàn)證碼 -->
<u-verification-code unique-key="register" ref="uCode" @change="codeChange">
</u-verification-code>
<view class="xs">{{codeTips}}</view>
</view>
script
async sendSmsFun() {
// if (!this.$refs.uCode.canGetCode) return
if (!this.registerForm.mobilePhoneNumber) {
this.$common.errorToShow('請?zhí)顚懯謾C(jī)號信息~');
return;
}
console.log("111");
const {
data: res
} = await this.$API.getCode()
console.log(res);
if (res.code == 200) {
this.$common.successToShow(res.msg)
this.$refs.uCode.start();
//獲取隨機(jī)驗(yàn)證碼
var random = Math.floor(Math.random() * 9)
this.code = res.data[random]
}
},
codeChange(tip) {
this.codeTips = tip
},效果


總結(jié)
到此這篇關(guān)于uniapp實(shí)現(xiàn)注冊發(fā)送獲取驗(yàn)證碼功能的文章就介紹到這了,更多相關(guān)uniapp發(fā)送獲取驗(yàn)證碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Raphael帶文本標(biāo)簽可拖動的圖形實(shí)現(xiàn)代碼
Javascript和Raphael順便學(xué)習(xí)了一下,主要是為了實(shí)現(xiàn)一個可拖動的矩形同時矩形上還得顯示標(biāo)簽,網(wǎng)上關(guān)于這方面的知識提的很是于是本人自不量力寫了一下,感興趣的你可不要錯過了哈,希望可以幫助到你2013-02-02
JavaScript中獲取鼠標(biāo)位置相關(guān)屬性總結(jié)
這篇文章主要介紹了JavaScript中獲取鼠標(biāo)位置相關(guān)屬性總結(jié),本文重點(diǎn)在搞清楚這些屬性的區(qū)別,需要的朋友可以參考下2014-10-10
js生成1到100的隨機(jī)數(shù)最簡單的實(shí)現(xiàn)方法
在本篇文章里小編給大家整理了關(guān)于js生成1到100的隨機(jī)數(shù)最簡單的實(shí)現(xiàn)方法,有需要的朋友們可以學(xué)習(xí)下。2020-02-02
JavaScript使用Math.Min返回兩個數(shù)中較小數(shù)的方法
這篇文章主要介紹了JavaScript使用Math.Min返回兩個數(shù)中較小數(shù)的方法,涉及javascript中Math.Min方法的使用技巧,非常具有實(shí)用價值,需要的朋友可以參考下2015-04-04
bootstrap paginator分頁插件的兩種使用方式實(shí)例詳解
Bootstrap Paginator是一款基于Bootstrap的js分頁插件,下面通過本文給大家介紹bootstrap paginator分頁插件的兩種使用方式,一起看看吧2017-11-11
JavaScript 中實(shí)現(xiàn) use strict的方法及優(yōu)勢
本教程將討論JavaScript中的use strict特性,在這里,我們將通過不同的示例了解如何在JavaScript代碼語句中創(chuàng)建和執(zhí)行use strict關(guān)鍵字,需要的朋友可以參考下2023-09-09

