From e91ae242d742c8756e1dee05f8e58a43c5a0ba99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=9D=99?= Date: Fri, 31 Dec 2021 20:25:35 +0800 Subject: [PATCH] =?UTF-8?q?=E7=9F=AD=E4=BF=A1=E5=8F=91=E9=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.example | 4 ++++ app/Models/SmsCode.php | 20 ++++++++++++++++++++ app/Notifications/SmsCodeCreated.php | 12 +++++++++++- app/Services/SmsCodeService.php | 2 +- config/easysms.php | 8 +++++++- 5 files changed, 43 insertions(+), 3 deletions(-) diff --git a/.env.example b/.env.example index 9e15b488..235c0270 100644 --- a/.env.example +++ b/.env.example @@ -60,3 +60,7 @@ ALIYUN_OSS_DOMAIN = ALIYUN_OSS_USE_SSL = ALIYUN_OSS_STS_ARN = ALIYUN_OSS_STS_HOST = + +ALIYUN_SMS_ACCESS_ID= +ALIYUN_SMS_ACCESS_KEY= +ALIYUN_SMS_SIGN_NAME= diff --git a/app/Models/SmsCode.php b/app/Models/SmsCode.php index e6b79dcd..0a512caa 100644 --- a/app/Models/SmsCode.php +++ b/app/Models/SmsCode.php @@ -53,6 +53,16 @@ class SmsCode extends Model self::TYPE_RESET_PASSWORD, ]; + /** + * 阿里云短信模板 + * + * @var array + */ + public static $aliyunTemplates = [ + // self::TYPE_REGISTER => 'SMS_231444585', + // self::TYPE_RESET_PASSWORD => 'SMS_231444585', + ]; + /** * {@inheritdoc} */ @@ -75,6 +85,16 @@ class SmsCode extends Model return $this->is_use || ($this->expires_at && $this->expires_at->lte(now())); } + /** + * 阿里云短信模板 + * + * @return string + */ + public function getAliyunTemplate() + { + return static::$aliyunTemplates[$this->type] ?? 'SMS_228470015'; + } + /** * @param \Illuminate\Notifications\Notification $notification * @return string diff --git a/app/Notifications/SmsCodeCreated.php b/app/Notifications/SmsCodeCreated.php index db43e92a..47e915b5 100644 --- a/app/Notifications/SmsCodeCreated.php +++ b/app/Notifications/SmsCodeCreated.php @@ -40,6 +40,16 @@ class SmsCodeCreated extends Notification implements ShouldQueue */ public function toSms($notifiable): SmsMessage { - return new SmsMessage(); + return (new SmsMessage())->setTemplate(function ($gateway) { + if ($gateway->getName() == 'aliyun') { + return $this->smsCode->getAliyunTemplate(); + } + })->setContent(function ($gateway) { + return "您的验证码为: {$this->smsCode->code},该验证码5分钟内有效,请勿泄露于他人"; + })->setData(function ($gateway) { + return [ + 'code' => $this->smsCode->code, + ]; + }); } } diff --git a/app/Services/SmsCodeService.php b/app/Services/SmsCodeService.php index d84c5bbe..1fa53d02 100644 --- a/app/Services/SmsCodeService.php +++ b/app/Services/SmsCodeService.php @@ -12,7 +12,7 @@ class SmsCodeService /** * @var int */ - protected $expires = 180; + protected $expires = 300; /** * @param \Illuminate\Contracts\Cache\Repository $cache diff --git a/config/easysms.php b/config/easysms.php index 29e72004..bc703dea 100644 --- a/config/easysms.php +++ b/config/easysms.php @@ -11,7 +11,7 @@ return [ // 默认可用的发送网关 'gateways' => [ - // + 'aliyun', ], ], // 可用的网关配置 @@ -19,5 +19,11 @@ return [ 'errorlog' => [ 'file' => storage_path('logs/easysms.log'), ], + + 'aliyun' => [ + 'access_key_id' => env('ALIYUN_SMS_ACCESS_ID'), + 'access_key_secret' => env('ALIYUN_SMS_ACCESS_KEY'), + 'sign_name' => env('ALIYUN_SMS_SIGN_NAME'), + ], ], ];