commit fa76d021353b9e346239a17834638c5168d882f7 Author: xuanchen <122383162@qq.com> Date: Mon Sep 11 09:44:39 2023 +0800 first commit diff --git a/README.md b/README.md new file mode 100644 index 0000000..bf6db09 --- /dev/null +++ b/README.md @@ -0,0 +1,34 @@ +# Jason/Sms 短信发送扩展 + +## 安装 + +``` +$ composer require jasonc/sms + +$ php artisan vendor:publish --tag='sms' + +$ php artisan migrate +``` + +## 短信发送 + +``` +\Sms::send($mobile, $channel = 'DEFAULT'); +``` + + +## 短信验证 + +``` +\Sms::check($mobile, $code, $channel = 'DEFAULT'); +``` + + +扩展验证规则 + +``` +sms_check:MOBILEFIELD,CHANNEL 短信验证码验证 + +MOBILEFIELD:手机号码字段名称 +CHANNEL:验证通道 +``` diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..4ccdcf8 --- /dev/null +++ b/composer.json @@ -0,0 +1,31 @@ +{ + "name": "jasonc/sms", + "description": "", + "license": "MIT", + "authors": [ + { + "name": "C.Jason", + "email": "chenjxlg@163.com" + } + ], + "require": { + "php": ">=7.1.3", + "overtrue/easy-sms": "^2.0", + "laravel/framework": "*" + }, + "autoload": { + "psr-4": { + "Jason\\Sms\\": "src/" + } + }, + "extra": { + "laravel": { + "providers": [ + "Jason\\Sms\\ServiceProvider" + ], + "aliases": { + "Sms": "Jason\\Sms\\Facade" + } + } + } +} diff --git a/config/config.php b/config/config.php new file mode 100644 index 0000000..0a883fd --- /dev/null +++ b/config/config.php @@ -0,0 +1,61 @@ + true, + + // 调试模式发送的验证码 + 'debug_code' => '0000', + + // HTTP 请求的超时时间(秒) + 'timeout' => 5.0, + + // 默认发送配置 + 'default' => [ + // 网关调用策略,默认:顺序调用 + 'strategy' => \Overtrue\EasySms\Strategies\OrderStrategy::class, + // 默认可用的发送网关 + 'gateways' => [ + 'aliyun', + ], + ], + + // 验证码长度 + 'length' => 4, + + // 验证后立即失效 + 'once_used' => true, + + // 模板与通道映射 + 'template' => [ + 'DEFAULT' => '', + 'LOGIN' => '', + 'REGISTER' => '', + ], + + // 可用的网关配置 + 'gateways' => [ + 'errorlog' => [ + 'file' => storage_path('logs/easy-sms.log'), + ], + // 阿里云 AccessKeyID: + 'aliyun' => [ + 'access_key_id' => '', + 'access_key_secret' => '', + 'sign_name' => '', + ], + // 阿里云Rest + 'aliyunrest' => [ + 'app_key' => '', + 'app_secret_key' => '', + 'sign_name' => '', + ], + // 云片 + 'yunpian' => [ + 'api_key' => '', + 'signature' => '', + ], + // ... 具体参数请参考 https://github.com/overtrue/easy-sms/blob/master/README.md + ], +]; diff --git a/database/migrations/2018_11_07_000000_create_sms_table.php b/database/migrations/2018_11_07_000000_create_sms_table.php new file mode 100644 index 0000000..57696ba --- /dev/null +++ b/database/migrations/2018_11_07_000000_create_sms_table.php @@ -0,0 +1,37 @@ +increments('id'); + $table->string('mobile', 16); + $table->string('channel', 16); + $table->string('code', 16); + $table->boolean('used')->default(0); + $table->timestamps(); + + $table->index(['mobile', 'channel']); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('sms'); + } +} diff --git a/src/Exceptions/SmsSendException.php b/src/Exceptions/SmsSendException.php new file mode 100644 index 0000000..f00c608 --- /dev/null +++ b/src/Exceptions/SmsSendException.php @@ -0,0 +1,9 @@ + + * @Date:2018-06-22T16:01:20+0800 + * @return void + */ + public function boot() + { + if ($this->app->runningInConsole()) { + $this->publishes([__DIR__ . '/../config/config.php' => config_path('sms.php')], 'sms'); + $this->loadMigrationsFrom(__DIR__ . '/../database/migrations/'); + } + + /** + * 短信验证码验证 + */ + Validator::extend('sms_check', function ($attribute, $code, $parameters) { + if (empty($code)) { + return false; + } + $mobileFiled = $parameters[0] ?? 'mobile'; + $channel = $parameters[1] ?? 'DEFAULT'; + $mobile = request()->input($mobileFiled); + + return \Sms::check($mobile, $code, $channel); + }); + } + + /** + * 注册服务提供者 + * @Author: + * @Date:2018-06-22T16:01:12+0800 + * @return void + */ + public function register() + { + $this->mergeConfigFrom(__DIR__ . '/../config/config.php', 'sms'); + } + +} diff --git a/src/Sms.php b/src/Sms.php new file mode 100644 index 0000000..3718d40 --- /dev/null +++ b/src/Sms.php @@ -0,0 +1,95 @@ + + * @Date : 2020/1/14 4:34 下午 + * @param string $mobile 手机号码 + * @param string $channel 验证通道 + * @param array $appends 验证码附加内容 + * @return bool + * @throws SmsSendException + */ + public function send(string $mobile, string $channel = 'DEFAULT', array $appends = []) + { + try { + $config = config('sms'); + + if (!isset($config['template'][$channel]) || empty($config['template'][$channel])) { + throw new SmsSendException('不合法的验证通道' . $config['template'][$channel]); + } + + DB::transaction(function () use ($mobile, $channel, $config, $appends) { + $data = []; + if ($config['debug']) { + $data['code'] = $config['debug_code']; + } else { + $data['code'] = sprintf("%0" . $config['length'] . "d", mt_rand(1, pow(10, $config['length']) - 1)); + } + + if (!empty($appends)) { + $data = array_merge($data, $appends); + } + + if ($config['debug'] != true) { + $easySms = new EasySms($config); + $easySms->send($mobile, [ + 'template' => $config['template'][$channel], + 'data' => $data, + ]); + } + + SmsModel::create([ + 'mobile' => $mobile, + 'channel' => $channel, + 'code' => $data['code'], + ]); + }); + + return true; + } catch (\Exception $e) { + throw new SmsSendException($e->getMessage()); + } + } + + /** + * 验证短信 + * @Author: + * @Date :2018-11-07T14:26:38+0800 + * @param string $mobile 手机号码 + * @param string $code 验证码 + * @param string $channel 验证通道 + * @return bool + */ + public function check(string $mobile, string $code, string $channel = 'DEFAULT') + { + $Sms = SmsModel::where('mobile', $mobile)->where('channel', $channel)->orderBy('id', 'desc')->first(); + + if ($Sms) { + if ($Sms->code == $code) { + if ($Sms->used == 1 && config('sms.once_used') && config('sms.debug') == false) { + return false; + } + $Sms->used = 1; + $Sms->save(); + + return true; + } else { + return false; + } + } else { + return false; + } + } + +}