6
0
Fork 0

短信发送

release
李静 2021-12-31 20:25:35 +08:00
parent da35b47808
commit e91ae242d7
5 changed files with 43 additions and 3 deletions

View File

@ -60,3 +60,7 @@ ALIYUN_OSS_DOMAIN =
ALIYUN_OSS_USE_SSL = ALIYUN_OSS_USE_SSL =
ALIYUN_OSS_STS_ARN = ALIYUN_OSS_STS_ARN =
ALIYUN_OSS_STS_HOST = ALIYUN_OSS_STS_HOST =
ALIYUN_SMS_ACCESS_ID=
ALIYUN_SMS_ACCESS_KEY=
ALIYUN_SMS_SIGN_NAME=

View File

@ -53,6 +53,16 @@ class SmsCode extends Model
self::TYPE_RESET_PASSWORD, self::TYPE_RESET_PASSWORD,
]; ];
/**
* 阿里云短信模板
*
* @var array
*/
public static $aliyunTemplates = [
// self::TYPE_REGISTER => 'SMS_231444585',
// self::TYPE_RESET_PASSWORD => 'SMS_231444585',
];
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
@ -75,6 +85,16 @@ class SmsCode extends Model
return $this->is_use || ($this->expires_at && $this->expires_at->lte(now())); 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 * @param \Illuminate\Notifications\Notification $notification
* @return string * @return string

View File

@ -40,6 +40,16 @@ class SmsCodeCreated extends Notification implements ShouldQueue
*/ */
public function toSms($notifiable): SmsMessage 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,
];
});
} }
} }

View File

@ -12,7 +12,7 @@ class SmsCodeService
/** /**
* @var int * @var int
*/ */
protected $expires = 180; protected $expires = 300;
/** /**
* @param \Illuminate\Contracts\Cache\Repository $cache * @param \Illuminate\Contracts\Cache\Repository $cache

View File

@ -11,7 +11,7 @@ return [
// 默认可用的发送网关 // 默认可用的发送网关
'gateways' => [ 'gateways' => [
// 'aliyun',
], ],
], ],
// 可用的网关配置 // 可用的网关配置
@ -19,5 +19,11 @@ return [
'errorlog' => [ 'errorlog' => [
'file' => storage_path('logs/easysms.log'), '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'),
],
], ],
]; ];