美文网首页
laravel使用阿里大鱼发送短信

laravel使用阿里大鱼发送短信

作者: ONEDAYLOG | 来源:发表于2017-08-16 09:41 被阅读0次

    laravel 发送短信(这个版本阿里大鱼已经归阿里云短信服务了)
    aliyun-sms用了一下这个,但是关键也不是功能很全,只有发短信。

    .env文件配置

    ALIYUN_SMS_AK = (ak)
    ALIYUN_SMS_AS = (as)
    ALIYUN_SMS_SIGN_NAME = (签名名称)
    

    发送短信的基本使用

    $aliSms = new AliSms();
    $response = $aliSms->sendSms('phone number', 'SMS_code', ['name'=> 'value in your template']);
    

    1.基本使用还是不够的加强一下

    session中存入手机号码、发送时间、验证码,用来做后续验证

    public function send_sms(Request $request){
            $data = $request->all();
            $rules = [
                'phone'    => 'required|regex:/^1[34578][0-9]{9}$/'
            ];
            $messages = [
                'phone.required'    => '必须填写手机号码',
                'phone.regex'    => '必须是有效的手机号码',
            ];
    
            $validator = Validator($data, $rules, $messages);
            if ($validator->passes()) {
                if (session('wapsms.time')) {
                    if(session('wapsms.phone') == $data['phone']){
                        if (((strtotime(session('wapsms.time')) + 60 * 15) < time())) {
                            $this->setSessionWapsms($data);
                        }
                    }else{
                        $this->setSessionWapsms($data);
                    }
                } else {
                    $this->setSessionWapsms($data);
                }
    
                $aliSms = new AliSms();
                $response = $aliSms->sendSms(session('wapsms.phone'), 'SMS_84720175', ['code' => session('wapsms.mcode')]);
                if ($response->Code == "OK") {
                    $res = ['success'=>true,'msg'=>'验证码已发送'];
                }
    
            }else{
                $res = ['success'=>false,'msg'=>$validator->messages()->first()];
            }
            return $res;
    
        }
    
        public function setSessionWapsms($data){
            session(['wapsms.time' => date("Y-m-d H:i:s")]);
            $code = rand(100000, 999999);
            session(['wapsms.mcode' => $code]);
            session(['wapsms.phone' => $data['phone']]);
        }
    

    2.验证码提交进行验证

    ...
                if (session('wapsms.time')) {
                    if (((strtotime(session('wapsms.time')) + 60 * 15) < time())) {
                        $res = ['success'=>false,'msg'=>'验证码超时'];
                        return $res;
                    }else{
                        if(session('wapsms.mcode') == $data['code'] && session('wapsms.phone') ==$data['tel']){
                            session()->forget('wapsms');
                        }else{
                            $res = ['success'=>false,'msg'=>'验证码不正确'];
                            return $res;
                        }
                    }
                }else{
                    $res = ['success'=>false,'msg'=>'验证码不正确'];
                    return $res;
                }
    ...
    

    相关文章

      网友评论

          本文标题:laravel使用阿里大鱼发送短信

          本文链接:https://www.haomeiwen.com/subject/cigbrxtx.html