123456789101112131415161718192021222324252627282930313233343536373839404142 |
- <?php
- namespace App\Helpers\Utils;
- use Log;
- use Overtrue\EasySms\EasySms;
- /**
- * 不涉及数据库模型的通用工具类
- * 阿里云短信发送
- */
- class Sms
- {
- protected $easySms;
- //定义短信模版常量
- const LOGIN = 'SMS_205123614'; //登录/注册验证码
- public function __construct() {
- $this->easySms = new EasySms(config('easysms'));
- }
- public function send($phone, $template = 'LOGIN', $data = []) {
- try {
- if(empty($data)){
- $data = [
- 'code' => str_pad(random_int(1, 9999), 4, 0, STR_PAD_LEFT) // 验证码
- ];
- }
- $this->easySms->send($phone, [
- 'template' => constant('self::' . $template), //阿里云模版
- 'data' => $data
- ]);
- } catch (\Overtrue\EasySms\Exceptions\NoGatewayAvailableException $exception) {
- $message = $exception->getException('aliyun')->getMessage();
- Log::info($message);
- return ['status'=> false, 'data'=> $data];
- //return $this->response->errorInternal($message ?? '短信发送异常');
- }
- return ['status'=> true, 'data'=> $data];
- }
- }
|