Laravel 使用阿里云短信发送验证码

永远的零 2019-09-02 AM 2091℃ 0条

安装:composer require mrgoon/aliyun-sms dev-master

在config/app.php里的providers配置

'providers'=>[
        /**
         * 阿里云短信
         */
        Mrgoon\AliSms\ServiceProvider::class,
]

顺便设置别名

'aliases' => [
      /**
         * 阿里云短信
         */
        'AliSms'=>Mrgoon\AliSms\ServiceProvider::class,
]

运行 php artisan vendor:publish 命令

配置config/aliyunsms.php

return [
    'access_key'        => env('ALIYUN_ACCESSKEYID'), // accessKey
    'access_secret'     => env('ALIYUN_ACCESSKEYSECRET'), // accessSecret
    'sign_name'         => env('ALIYUN_SMS_SIGN_NAME'), // 签名
];

在.env文件配置以下三项

ALIYUN_ACCESSKEYID=签名id
ALIYUN_ACCESSKEYSECRET=签名key 密码
ALIYUN_SMS_SIGN_NAME=你的签名

在Driver文件夹新建一个AliAliSMSDrv.php

class AliSMSDrv
{

    /**
     * 发送验证码
     * @param $account
     * @param $msg
     * @return bool|mixed
     */
    public function sendCode($account,$msg)
    {
        $response = $this->sendSMS($account,array('msgno'=>$msg));
        return $response;
    }

    /**
     * 通用
     * @param $mobile   手机号
     * @param $data     数据格式
     * $data = array('key1'=>'value1','key2'=>'value2', …… )
     * @param string $templateCode  短信模板Code
     * @return bool
     */
    public static function sendSMS($mobile, $data, $templateCode='你的模板id') {

        $aliSms = new AliSms();
        $response = $aliSms->sendSms($mobile,$templateCode, $data);
        if($response->Message == 'OK'){
            return true;
        }else {
            return false;
        }

    }

}

接着在需要使用的地方调用

验证码业务逻辑层

<?php
/**
 * 验证码业务逻辑层
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/26
 * Time: 10:12
 */
namespace App\Services;
use App\Models\ValidateCode;
use App\Models\User;
use App\Driver\AliSMSDrv;
use App\Exceptions\WrongException;

class ValidateCodeService extends BaseModelService {
    const PHONE_VALIDATE = 'phone';//获取手机验证码
    private $phone_driver = AliSMSDrv::class;
    protected static function getModel()
    {
        return ValidateCode::class;
    }
    /**
     * 生成随机的短信验证码,修改验证码长度在AppServiceProvider中验证码拓展校验
     */
    private function genValidateCode()
    {
        return mt_rand(000000,999999);
    }
    /**
     * 获取验证码
     */
    public function  getCode($account,$type)
    {
        //生成随机的验证码
        $code = $this->genValidateCode();
        //发送验证码
        $driver = new $this->phone_driver();
        $result = $driver->sendCode($account,$code);
        if (!$result) {
            throw new WrongException('发送失败');
        }
        //删除旧的验证码
        ValidateCode::where(['account'=>$account,'v_type'=>$type])->delete();
        //发送成功后将验证码保存到数据库
        $validate_code = new ValidateCode();
        $validate_code->account = $account;
        $validate_code->v_type = $type;
        $validate_code->send_time = time();
        $validate_code->v_code = $code;
        if(!$validate_code->save()){
            throw new WrongException('发送验证码失败');
        }
        return $code;
    }
 
    /**
     * 验证验证码是否正确
     */
    public function checkCode($mobile,$v_code)
    {
        $limit_secords = 900 + time();
        $record = ValidateCode::where('send_time','<',$limit_secords)
            ->where('account', '=', $mobile)
            ->orderBy('v_id','desc')
            ->first();
        if($record){
            if ($record->v_times >= 5) {
                throw new WrongException('请重新获取验证码');
            }
            $record->v_times = $record->v_times + 1;
            $record->save();
            if($record->v_code == $v_code){
                ValidateCode::destroy($record->v_id);
                return true;
            }
        }
        throw new WrongException('验证码错误');
    }
}

人要么永不做梦,要么梦得有趣;人也必须学会清醒:要么永不清醒,要么清醒得有趣。

评论啦~