二、創建雲函數
創建雲函數,命名為sendSMS,實現將獲取到的隨機驗證碼發送到用戶手機短信上。
1 // 雲函數入口文件 2 const cloud = require('wx-server-sdk') 3 4 //cloud.init() 5 cloud.init({ 6 env: cloud.DYNAMIC_CURRENT_ENV 7 }) 8 //雲函數的功能:發送短信驗證碼 9 10 // 雲函數入口函數 11 exports.main = async (event, context) => { 12 const result = await cloud.openid.cloudbase.sendSms({ 13 env:cloud.DYNAMIC_CURRENT_ENV, 14 phone_number_list:[ 15 "+86"+event.phoneNumber 16 ], 17 content:"【公司名稱】驗證碼為:"+event.RandomPhoneCode, 18 }) 19 20 }
1 <!-- 手機號碼 --> 2 <text class="text1">手機號碼</text> 3 <view class="phoneNum"> 4 <input type="text" placeholder="請輸入手機號碼" bindinput="getPhoneNumber"></input> 5 </view> 6 <!-- 驗證碼 --> 7 <text class="text1">驗證碼</text> 8 <view class="phoneCode"> 9 <input type="text" placeholder="請輸入驗證碼" bindinput="getPhoneCode" ></input> 10 <button type="primary" size="mini" bindtap="sendPhoneCode">發送驗證碼</button> 11 </view> 12 <!-- 登錄 --> 13 <view class="loadButton"> 14 <button type="primary" bindtap="loadByPhone">登錄</button> 15 </view>
2、loadByPhone.wxss
1 .text1{ 2 margin:20rpx; 3 } 4 .phoneNum input{ 5 margin: 20rpx; 6 padding-left: 10rpx; 7 height: 80rpx; 8 border: 1rpx solid #c3c3c3; 9 } 10 .phoneCode{ 11 display: flex; 12 align-items: center; 13 } 14 .phoneCode input{ 15 width: 60%; 16 margin: 20rpx; 17 border: 1rpx solid #c3c3c3; 18 padding-left: 10rpx; 19 height: 80rpx; 20 } 21 .phoneCode button{ 22 height: 80rpx; 23 vertical-align: middle; 24 } 25 26 .loadButton button{ 27 margin-top: 50rpx; 28 width: 94% !important; 29 }
3、loadByPhone.js
知識點歸納整理:
- 點擊發送驗證碼按鈕時候的校驗功能
- 點擊【登錄】按鈕時候的校驗功能
- 自定義函數 generateMixed(n),獲取隨機驗證碼
- 調用雲函數sendSMS,實現將獲取的短信驗證碼發送到用戶真實手機短信中
1 //自定義全局變量,存儲輸入的手機號碼和驗證碼 2 let phoneNumber = '' 3 let phoneCode = '' 4 //自定義全局變量,存儲獲取到的隨機驗證碼 5 let RandomPhoneCode = '' 6 Page({ 7 8 data: { 9 10 }, 11 12 //獲取用戶輸入的手機號碼 13 getPhoneNumber(e){ 14 console.log("輸入的手機號碼",e); 15 phoneNumber = e.detail.value 16 }, 17 //獲取用戶輸入的驗證碼 18 getPhoneCode(e){ 19 console.log("輸入的短信驗證碼",e); 20 phoneCode = e.detail.value 21 }, 22 //發送驗證碼 23 sendPhoneCode(){ 24 console.log("點擊了發送短信驗證碼的按鈕"); 25 //調用生成驗證碼的函數generateMixed(n)獲取4位驗證碼,並將生成的4位驗證碼賦給自定義變量RandomPhoneCode 26 RandomPhoneCode = this.generateMixed(4) 27 console.log("生成的隨機驗證碼",RandomPhoneCode); 28 //如果手機號碼不是11位,給予提示,並終止代碼執行 29 if(phoneNumber.length!=11){ 30 wx.showToast({ 31 title: '請輸入11位手機號碼', 32 icon:"none" 33 }) 34 return 35 } 36 //調用雲函數,實現發送短信驗證碼到真實手機號碼上 37 wx.cloud.callFunction({ 38 name:"sendSMS", 39 data:{ 40 phoneNumber:phoneNumber, 41 RandomPhoneCode:RandomPhoneCode 42 } 43 }).then(res=>{ 44 console.log("驗證碼發送到手機短信成功",res); 45 }).catch(err=>{ 46 console.log("驗證碼發送到手機短信失敗",err); 47 }) 48 }, 49 50 //實現驗證登錄功能 51 loadByPhone(){ 52 //如果手機號碼不是11位,給予提示,並終止代碼執行 53 if(phoneNumber.length!=11){ 54 wx.showToast({ 55 title: '請輸入11位手機號碼', 56 icon:"none" 57 }) 58 return 59 } 60 //如果輸入的驗證碼為空或者輸入的驗證碼不等於獲取到的隨機驗證碼,給與提示,並終止代碼執行 61 if(phoneCode.length==0){ 62 console.log("輸入的驗證碼",phoneCode); 63 wx.showToast({ 64 title: '請輸入短信驗證碼', 65 icon:"none" 66 }) 67 return 68 } 69 //校驗用戶輸入的驗證碼是否等於獲取的隨機驗證碼 70 if(phoneCode!=null&phoneCode==RandomPhoneCode){ 71 wx.showToast({ 72 title: '登錄成功', 73 icon:"success", 74 duration:2000, 75 success:function(){ 76 setTimeout(function(){ 77 wx.switchTab({ 78 url: '/pages/index/index', 79 }) 80 },2000) 81 } 82 }) 83 }else{ 84 wx.showToast({ 85 title: '驗證碼錯誤', 86 icon:"none" 87 }) 88 } 89 }, 90 91 //自定義函數。生成隨機驗證碼,n代表幾位 92 generateMixed(n) { 93 let chars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']; 94 let res = ""; 95 for (var i = 0; i < n; i++) { 96 var id = Math.ceil(Math.random() * 35); 97 res += chars[id]; 98 } 99 return res; 100 }, 101 102 })
四、效果展示
-
-
必須開通靜態網站功能
-
2、報錯分析
如果是個人小程序,在調用雲函數發送驗證碼的時候,會有如下報錯。