123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- <?php
- namespace App\Services\Common;
- use App\Exceptions\ResponseException;
- use App\Jobs\SmsJob;
- use App\Repositories\SmsTemplateRepository;
- use Illuminate\Support\Facades\Cache;
- class SmsService
- {
- protected $utype;
- protected $sender_id;
-
- protected $callback;
-
- public function __construct()
- {
- $this->utype = 0;
- $this->sender_id = 0;
- $this->batch_id = 0;
- $this->callback = [];
- }
-
- public function sendSms(string $mobile, string $template_alias, array $params = [], $isAuthSms = false, $isVariable = true, $content = '')
- {
- $smsJob=new SmsJob($mobile, $template_alias, $params, $isAuthSms, $isVariable, $content);
- $smsJob->setSender($this->utype, $this->sender_id);
- $smsJob->setCallback($this->callback);
- dispatch($smsJob);
- }
-
- public function sendSmsStatic(string $mobile, string $template_alias, array $params = [])
- {
- $smsJob=new SmsJob($mobile, $template_alias, $params, false);
- $smsJob->setSender($this->utype, $this->sender_id);
- $smsJob->setCallback($this->callback);
- dispatch($smsJob);
- }
-
- public function sendAuthSms(string $mobile, string $template_alias)
- {
- $is_send=Cache::has($template_alias.'_'.$mobile.'_send');
- if ($is_send) {
- throw new ResponseException("请稍后再发送短信");
- }
- $smsJob=new SmsJob($mobile, $template_alias, [], true);
- dispatch($smsJob);
- Cache::put($template_alias.'_'.$mobile.'_send', 1, 1);
- }
-
- public function checkAuthSms(string $mobile, string $template_alias, string $code): bool
- {
- $cacheCode=Cache::get($template_alias.'_'.$mobile);
- if ($code!=$cacheCode) {
- return false;
- }
- Cache::forget($template_alias.'_'.$mobile);
- return true;
- }
-
- public function setCallback(string $obj, string $method, array $args)
- {
- $this->callback[]=[$obj, $method];
- $this->callback[]=$args;
- return $this;
- }
-
- public function setSender($utype, $id)
- {
- $this->utype=$utype;
- $this->sender_id=$id;
- return $this;
- }
- }
|