小程序获取手机号验证码
生活随笔
收集整理的這篇文章主要介紹了
小程序获取手机号验证码
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
//發(fā)送驗(yàn)證碼public function sendCode(Request $request){$phone = $request->get('phone');if (empty($phone)) {return ['code' => 10001, 'msg' => '手機(jī)號(hào)不能為空', 'data' => []];}$white = new Number();$res = $white->number($phone);if(!$res){return ['code' => 10002, 'msg' => '手機(jī)號(hào)不能發(fā)送驗(yàn)證碼', 'data' => []];}$cache = Cache::get($phone.date("Y:m:d"));//校驗(yàn)每天5次發(fā)送限制if($cache > 5){return ['code' => 10003, 'msg' => '每天只能發(fā)送五條短信', 'data' => []];}//驗(yàn)證碼$captcha = (new Phone())->sendSms($phone);//短信五分鐘有效期Cache::put($phone,$captcha,60*5);$num = $cache + 1;Cache::put($phone.date("Y:m:d"),$num,60*60*24);return ['code' => 200, 'msg' => '短信發(fā)送成功', 'data' => $captcha];}//綁定手機(jī)號(hào)public function getPhone(Request $request){$params = $request->post();$captcha = Cache::get($params['phone'].'_code');if ($captcha != $params['code']){Cache::get($params['phone'].'_code',null);return ['code'=>403,'msg'=>'驗(yàn)證碼錯(cuò)誤','data'=>[]];}$user = new User();$userData = $user->getUserById($params['user_id']);$userData->phone = $params['phone'];$userData->save();return ['code'=>200,'msg'=>'手機(jī)號(hào)綁定成功','data'=>[]];}
namespace App\Service;class Phone
{public function sendSms($mobile){$smsapi = "http://api.smsbao.com/";$user = "lzc7758521"; //短信平臺(tái)帳號(hào)$pass = md5("asd7758521"); //短信平臺(tái)密碼$code = rand(1000,9999);$content = "您的驗(yàn)證碼為".$code."該驗(yàn)證碼五分鐘內(nèi)有效,請(qǐng)勿泄露給他人";//要發(fā)送的短信內(nèi)容$phone = $mobile;//要發(fā)送短信的手機(jī)號(hào)碼$sendurl = $smsapi."sms?u=".$user."&p=".$pass."&m=".$phone."&c=".urlencode($content);Cache::put($phone.'_code',$code);return $code;}
}
Route::group(['namespace'=>'Api','middleware'=>['CheckJwt','throttle:60,1']],function (){Route::post('zuAdd',[ApiController::class,'zuAdd']);Route::post('phone',[ApiController::class,'getPhoneNumber']);Route::get('sendCode',[ApiController::class,'sendCode']);Route::post('getPhone',[ApiController::class,'getPhone']);Route::post('transfer',[ApiController::class,'transfer']);
});
//獲取驗(yàn)證碼getCode(e){let phone = this.data.phone;let token = wx.getStorageSync('token');//發(fā)送驗(yàn)證碼wx.request({url: 'http://www.transfer.com/api/sendCode',data:{phone},header:{token},success:res=>{if(res.data.code==10002){wx.showToast({title: '手機(jī)號(hào)不能發(fā)送驗(yàn)證碼',})}else{return}if(res.data.code==200){wx.showToast({title: '發(fā)送成功',duration: 1000,success: function () {setTimeout(function () {}, 1000);}})}}})
},
getPhone(){let phone = this.data.phone;let token = wx.getStorageSync('token');let code = this.data.code;wx.request({url: 'http://www.transfer.com/api/getPhone',data:{phone,code},header:{token},method:'POST',success:res=>{if(res.data.code==200){wx.showToast({title: '綁定成功',duration: 1000,success: function () {setTimeout(function () {wx.reLaunch({url: '/pages/index/index'})}, 1000);}})}}})
},
<view>
<button bindtap="apilogin">登錄</button>
<view class="weui-cell__bd" style="margin: 30rpx 0" ><input class="weui-input" bindinput="phone" name="phone" placeholder="手機(jī)號(hào)" /><view class="captcha"><input class="weui-input" name="code" bindinput="code" placeholder="驗(yàn)證碼" /><button type="default" bindtap="getCode" size="mini">獲取驗(yàn)證碼</button></view><button style="margin-top:30px;" bindtap="getPhone" type="warn">獲取手機(jī)號(hào)</button>
</view>
</view>
public function number($phone){$data = self::where('phone',$phone)->first();return $data;}public function getUserById(){$data = self::where('id',1)->first();return $data;}
總結(jié)
以上是生活随笔為你收集整理的小程序获取手机号验证码的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据库笔试面试(第一版)——根据题目完成
- 下一篇: 基层管理之正负向激励