SmsService.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace App\Services\Common;
  3. use App\Exceptions\ResponseException;
  4. use App\Jobs\SmsJob;
  5. use App\Repositories\SmsTemplateRepository;
  6. use Illuminate\Support\Facades\Cache;
  7. /**
  8. * 短信服务.
  9. * Class SmsService
  10. * @package App\Services\Common
  11. */
  12. class SmsService
  13. {
  14. protected $utype;
  15. protected $sender_id;
  16. /**
  17. * @var array
  18. */
  19. protected $callback;
  20. /**
  21. * SmsService constructor.
  22. */
  23. public function __construct()
  24. {
  25. $this->utype = 0;
  26. $this->sender_id = 0;
  27. $this->batch_id = 0;
  28. $this->callback = [];
  29. }
  30. /**
  31. * 发送基本短信
  32. * @param string $mobile
  33. * @param string $template_alias
  34. * @param array $params
  35. */
  36. public function sendSms(string $mobile, string $template_alias, array $params = [], $isAuthSms = false, $isVariable = true, $content = '')
  37. {
  38. $smsJob=new SmsJob($mobile, $template_alias, $params, $isAuthSms, $isVariable, $content);
  39. $smsJob->setSender($this->utype, $this->sender_id);
  40. $smsJob->setCallback($this->callback);
  41. dispatch($smsJob);
  42. }
  43. /**
  44. * 发送基本短信
  45. * @param string $mobile
  46. * @param string $template_alias
  47. * @param array $params
  48. */
  49. public function sendSmsStatic(string $mobile, string $template_alias, array $params = [])
  50. {
  51. $smsJob=new SmsJob($mobile, $template_alias, $params, false);
  52. $smsJob->setSender($this->utype, $this->sender_id);
  53. $smsJob->setCallback($this->callback);
  54. dispatch($smsJob);
  55. }
  56. /**
  57. * 发送确认短信,需要缓存验证码code
  58. * @param string $mobile
  59. * @param string $template_alias
  60. * @throws ResponseException
  61. */
  62. public function sendAuthSms(string $mobile, string $template_alias)
  63. {
  64. $is_send=Cache::has($template_alias.'_'.$mobile.'_send');
  65. if ($is_send) {
  66. throw new ResponseException("请稍后再发送短信");
  67. }
  68. $smsJob=new SmsJob($mobile, $template_alias, [], true);
  69. dispatch($smsJob);
  70. Cache::put($template_alias.'_'.$mobile.'_send', 1, 1);
  71. }
  72. /**
  73. * 检查验证短信的验证码是否准确
  74. * @param string $mobile
  75. * @param string $template_alias
  76. * @param string $code
  77. * @return bool
  78. */
  79. public function checkAuthSms(string $mobile, string $template_alias, string $code): bool
  80. {
  81. $cacheCode=Cache::get($template_alias.'_'.$mobile);
  82. if ($code!=$cacheCode) {
  83. return false;
  84. }
  85. Cache::forget($template_alias.'_'.$mobile);
  86. return true;
  87. }
  88. /**
  89. * 设置回调函数
  90. * @param string $obj 需实例对象的完整命名空间
  91. * @param string $method 调用obj对应的方法
  92. * @param array $args 方法里的参数(按回调方法里的参数顺序)
  93. * @return SmsService
  94. */
  95. public function setCallback(string $obj, string $method, array $args)
  96. {
  97. $this->callback[]=[$obj, $method];
  98. $this->callback[]=$args;
  99. return $this;
  100. }
  101. /**
  102. * 设置发送者
  103. * @param $utype 0系统,1企业,2用户
  104. * @param $id: 对应 utype id
  105. * @return SmsService
  106. */
  107. public function setSender($utype, $id)
  108. {
  109. $this->utype=$utype;
  110. $this->sender_id=$id;
  111. return $this;
  112. }
  113. }