手机 验证码 发送

调用京东万象手机验证码接口

 /**
     * @param number $phone 手机号码
     * @param string $content 信息模板
     * @return bool|string
     */
    public function sendmsg($phone, $content)
    {
        //配置中调取
        $gateway = config('phone.gateway');
        $appkey = config('phone.appkey');

        $url = $gateway . '?appkey=' . $appkey . "&content=" . $content . "&mobile=" . $phone;

        $url .= '&mobile=' . $phone . '&content=' . $content;
       $res = curl_request($url, false, [], true);
        //处理结果
        if (!$res) {
            return '请求发送失败';
        }
        //解析结果
        $arr = json_decode($res, true);
        if (isset($arr['code']) && $arr['code'] == 10000) {
            //短信接口调用成功
            return true;
        } else {
            return '短信发送失败';
        }
    }

接受手机号码并进行验证

 /**
     * @param number $phone 手机号码
     * @return \think\response\Json
     */
    public function phone_code($phone){
        $phone = input('phone') ?? '';
        if (!preg_match('/^1[3-9]\d{9}$/', $phone)) return json(['code' => 1001, 'msg' => '手机号不符合规则', 'data' => []]);
        //判断 是否重复发送
        $last_time = \cache('week_time_' . $phone);
        if ((time() - $last_time) < 60) {
            return json(['code' => 1001, 'msg' => '验证码获取太过频繁,请一分钟后再试', 'data' => []]);
        }
        //调用 函数 发送 随机验证码
        $number = rand(1111, 9999);
        $content = "【品优购】你的验证码是:{$number},3分钟内有效!";
        //调用 公共方法

        $result = $this->sendmsg($phone, $content);

        if ($result) {
            //记录住验证码内容
            cache("week_" . $phone, $number, 60);
            //记录发送验证码的时间
            cache("week_time_" . $phone, time());
            return json(['code' => 200, 'msg' => '验证码发送成功', 'data' => ['phone' => $phone]]);
        } else {
            return json(['code' => 1001, 'msg' => '服务器繁忙,请稍候再试', 'data' => ['phone' => $phone]]);
        }
    }

封装公共函数


if (!function_exists('phone_code')) {
    function phone_code($phone){
        $phone = input('phone') ?? '';
        if (!preg_match('/^1[3-9]\d{9}$/', $phone)) return ['code'=>1001,'msg'=>'手机号不符合规则','data'=>''] ;
        //判断 是否重复发送
        $last_time = \cache('week_time_' . $phone);
        if ((time() - $last_time) < 60)  return ['code'=>1001,'msg'=>'验证码获取太过频繁,请一分钟后再试','data'=>''];

        //调用 函数 发送 随机验证码
        $number = rand(1111, 9999);
        $content = "【品优购】你的验证码是:{$number},3分钟内有效!";
        //调用 公共方法
        $result = sendmsg($phone, $content);

        if ( $result && $result['code'] == 200 ) {
            //记录住验证码内容
            cache("week_" . $phone, $number, 60);
            //记录发送验证码的时间
            cache("week_time_" . $phone, time());
            return ['code'=>200,'msg'=>$result['msg'],'data'=>''];
        } else {
            return ['code'=>1001,'msg'=>$result['msg'],'data'=>''];
        }
    }
}

if (!function_exists('sendmsg')) {
    //使用curl_request函数调用短信接口发送短信
    function sendmsg($phone, $number = '',$content = '')
    {
        //从配置中取出请求地址、appkey
        $gateway = config('phone.gateway');
        $appkey = config('phone.appkey');
        //https://way.jd.com/chuangxin/dxjk?mobile=13568813957&content=【创信】你的验证码是:5873,3分钟内有效!&appkey=您申请的APPKEY
        $url = $gateway . '?appkey=' . $appkey;
        if ($content && $number = ''){
            $url .= '&mobile=' . $phone . '&content=' . $content;
            //get
            //$res = curl_request($url, false, [], true);
            //post请求
            $params = [
                'mobile' => $phone,
                'content' => $content
            ];
        }else{
            $content =  $content = "【品优购】你的验证码是:{$number},3分钟内有效!";
            $url .= '&mobile=' . $phone . '&content=' . $content;
            //get
            //$res = curl_request($url, false, [], true);
            //post请求
            $params = [
                'mobile' => $phone,
                'content' => $content
            ];
        }

        $res = curl_request($url, true, $params, true);
        //处理结果
        if (!$res) {
            return ['code'=>1001,'msg'=>'请求发送失败','data'=>''];
        }
        //解析结果
        $arr = json_decode($res, true);
        if (isset($arr['code']) && $arr['code'] == 10000) {
            //短信接口调用成功
            return ['code'=>200,'msg'=>'验证码发送成功','data'=>''];
        } else {
            return ['code'=>1001,'msg'=>'验证码发送失败','data'=>''];
        }
    }


}

发送 和登录

 public function test()
   {
       $phone = input('phone') ?? '';
       //
       $res =  phone_code($phone);
       $number = rand(000,9999);
       $res = sendmsg($phone,$number ,'');
       if ( $res['code'] == 200) return json(['code'=>200,'msg'=>$res['msg'],'data'=>$res]);
       return json(['code'=>1001,'msg'=>$res['msg'],'data'=>'']);
   }

   public function plogin(Request $request)
   {

       $params = input();
       $validate = Validate::rule([
           'phone'=> 'require|mobile',
           'code' => 'require|length:4'
       ]);
       cache('week_'.$params['phone'],'1234',300);

       if (!$validate->check($params)) return json(['code'=>1001,'msg'=>$validate->getError(),'data'=>'']);
       $old_code = cache('week_'.$params['phone']) ?? '';
       cache('week_'.$params['phone'],'');
       halt($old_code);
       if ($old_code != $params['code']) return json(['code'=>1001,'msg'=>'验证码错误','data'=>'']);
       $model = \app\model\User::where('phone',$params['phone'])->find();
       if (!$model) return json(['code'=>1001,'msg'=>'用户不存在 请先注册','data'=>'']);
       return json(['code'=>200,'msg'=>'登录成功','data'=>'']);

   }

调用方法 进行发送

 $params = input();
        $validte = Validate::rule([
            'captcha' => 'require|length:4|captcha',
            'phone' => 'require|mobile'
        ]);
        if (! $validte->check($params) ) return json(['code'=>1001,'msg'=>$validte->getError(),'data'=>'']);

        $res = phone_code($params['phone']);
        if ($res && $res['code'] == 200){
             return json(['code'=>200,'msg'=>'验证码发送成功','data'=>$res['data']]);
        }
        return json(['code'=>1001,'msg'=>$res['msg'],'data'=>'']);

一个手机号每天限制发送次数

 public function index()
    {
        //
        $phone = input('phone');
        $key = date('Ymd',time()).$phone ;
        $click = cache($key) ?? 0;
        if ($click >= 5){
            return json(['code'=>1001,'msg'=>'验证码发送次数上限','data'=>'已发送'.$click.'次']);
        }else{
            //这里调用公共函数  发送 验证码
            $res = phone_code($phone);
            if ($res && $res['code'] == 200){
                $click ++;
                cache($key,$click,3600*24);
                return json(['code'=>200,'msg'=>'验证码发送成功','data'=>$res['data']]);
            }else{
                return json(['code'=>1001,'msg'=>'验证码发送失败','data'=>'']);
            }
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值