小程序获取手机号验证码

//发送验证码
    public function sendCode(Request $request)
    {
        $phone = $request->get('phone');
        if (empty($phone)) {
            return ['code' => 10001, 'msg' => '手机号不能为空', 'data' => []];
        }

        $white = new Number();

        $res = $white->number($phone);

        if(!$res){
            return ['code' => 10002, 'msg' => '手机号不能发送验证码', 'data' => []];
        }

        $cache = Cache::get($phone.date("Y:m:d"));

        //校验每天5次发送限制
        if($cache > 5){
            return ['code' => 10003, 'msg' => '每天只能发送五条短信', 'data' => []];
        }

        //验证码
        $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' => '短信发送成功', 'data' => $captcha];

    }

    //绑定手机号
    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'=>'验证码错误','data'=>[]];
        }

        $user = new User();

        $userData = $user->getUserById($params['user_id']);

        $userData->phone = $params['phone'];

        $userData->save();

        return ['code'=>200,'msg'=>'手机号绑定成功','data'=>[]];
    }
namespace App\Service;


class Phone
{
    public function sendSms($mobile)
    {
        $smsapi = "http://api.smsbao.com/";
        $user = "lzc7758521"; //短信平台帐号
        $pass = md5("asd7758521"); //短信平台密码
        $code = rand(1000,9999);
        $content = "您的验证码为".$code."该验证码五分钟内有效,请勿泄露给他人";//要发送的短信内容
        $phone = $mobile;//要发送短信的手机号码
        $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']);
});
 //获取验证码
 getCode(e){
  let phone = this.data.phone;
  let token = wx.getStorageSync('token');
 //发送验证码
 wx.request({
   url: 'http://www.transfer.com/api/sendCode',
    data:{
        phone
    },
    header:{
      token
    },
    success:res=>{
      if(res.data.code==10002){
        wx.showToast({
          title: '手机号不能发送验证码',
        })
      }else{
        return
      }
      if(res.data.code==200){
        wx.showToast({
          title: '发送成功',
          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="手机号" />
  <view class="captcha">
    <input class="weui-input" name="code" bindinput="code" placeholder="验证码" />
    <button type="default" bindtap="getCode"  size="mini">获取验证码</button>
  </view>
 <button style="margin-top:30px;" bindtap="getPhone" type="warn">获取手机号</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;
    }

    原文作者:小考拉爱翔驴
    原文地址: https://blog.csdn.net/qq_42839386/article/details/119989067
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞