Laravel6.* 集成阿里云短信,发送验证码或其它信息(废弃)
2019-10-27 admin laravel 1964
已经有更好的扩展包,github地址。支持目前市面多家服务商,一套写法兼容所有平台,简单配置即可灵活增减服务商,内置多种服务商轮询策略、支持自定义轮询策略,统一的返回值格式,便于日志与监控,自动轮询选择可用的服务商等等。功能更强,因此该文废弃,以下内容仅供查询。
首先安装Laravel阿里云短信扩展,安装过程非常简单,执行:
composer require mrgoon/aliyun-sms dev-master
在config/app.php里的providers添加一行:
Mrgoon\AliSms\ServiceProvider::class,
在aliases添加一行设置别名:
'AliSms'=>Mrgoon\AliSms\ServiceProvider::class,
接下来,添加系统服务。确认php已开启了exec扩展(平时为了安全关闭),执行:
php artisan vendor:publish
如果没有开启,会报错:
[ERROR] exec() has been disabled for security reasons
执行成功后,会提醒选择生成哪个配置文件,找到
Provider: Mrgoon\AliSms\ServiceProvider
看看前面是什么数字,就输入这个数字回车即可。
配置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=qianming
现在可以测试发送短信了:
$aliSms = new AliSms(); $response = $aliSms->sendSms('phone number', 'SMS_code', ['name'=> 'value in your template']); //dump($response);
为了更方便在业务顺使用,我们以短信验证码方式为例,在app/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; } }}
接着在需要使用的地方调用验证码业务逻辑层
/** * 验证码业务逻辑层 * 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('验证码错误'); }}
教程完毕!