SmsJob.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace App\Jobs;
  3. use Aix\Sms\Contracts\Smser;
  4. use App\Repositories\SmsBatchRepository;
  5. use App\Repositories\SmsQueueRepository;
  6. use App\Repositories\SmsRepository;
  7. use App\Repositories\SmsTemplateRepository;
  8. use Illuminate\Bus\Queueable;
  9. use Illuminate\Queue\SerializesModels;
  10. use Illuminate\Queue\InteractsWithQueue;
  11. use Illuminate\Contracts\Queue\ShouldQueue;
  12. use Illuminate\Foundation\Bus\Dispatchable;
  13. class SmsJob implements ShouldQueue
  14. {
  15. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  16. /**
  17. * @var array
  18. */
  19. protected $callback;
  20. protected $mobile;
  21. protected $sms_template_alias;
  22. protected $params;
  23. protected $isAuthSms;
  24. protected $utype;
  25. protected $sender_id;
  26. protected $batch_id;
  27. protected $isVariable;//是否是变量短信
  28. protected $content;
  29. /**
  30. * SmsJob constructor.
  31. * @param $mobile
  32. * @param $sms_template_alias
  33. * @param $params
  34. * @param $isAuthSms
  35. */
  36. public function __construct($mobile, $sms_template_alias, $params, $isAuthSms = false, $isVariable = true, $content = '')
  37. {
  38. $this->queue="sms";//指定队列
  39. $this->mobile = $mobile;
  40. $this->sms_template_alias = $sms_template_alias;
  41. $this->params = $params;
  42. $this->isAuthSms = $isAuthSms;
  43. $this->utype = 0;
  44. $this->sender_id = 0;
  45. $this->batch_id = 0;
  46. $this->isVariable = $isVariable;
  47. $this->content = $content;
  48. }
  49. /**
  50. * Execute the job.
  51. *
  52. * @param SmsRepository $smsRepository
  53. * @param SmsTemplateRepository $smsTemplateRepository
  54. * @param SmsQueueRepository $smsQueueRepository
  55. * @param SmsBatchRepository $smsBatchRepository
  56. * @return void
  57. * @throws \Prettus\Validator\Exceptions\ValidatorException
  58. */
  59. public function handle(
  60. SmsRepository $smsRepository,
  61. SmsTemplateRepository $smsTemplateRepository,
  62. SmsQueueRepository $smsQueueRepository,
  63. SmsBatchRepository $smsBatchRepository
  64. ) {
  65. $sms=$smsRepository->getUsefulSms();
  66. $class="Aix\\Sms\\".$sms['alias'];
  67. /** @var Smser $smsInstance */
  68. $smsInstance=new $class($sms->app_key, $sms->secret_key, $sms->signature_key);
  69. if($sms['alias'] == 'ChuanglanSms'){
  70. $template = $smsTemplateRepository->getTemplateByCL($this->sms_template_alias, $sms['alias']);
  71. }else{
  72. $template=$smsTemplateRepository->getTemplate($this->sms_template_alias, $sms['alias'], $this->params);
  73. }
  74. $smsQueue=$smsQueueRepository->createRecord($template, $this->mobile, $this->utype, $this->sender_id, $this->batch_id);
  75. if (!isset($template->role) || $template->role->status == 0) {
  76. $smsQueueRepository->saveRecord($smsQueue->id, 3, "短信规则不存在或者短信规则没有开启");
  77. $smsBatchRepository->updateBatch($this->batch_id, false);
  78. return;
  79. }
  80. try {
  81. if ($this->isAuthSms) {
  82. $code = $smsInstance->sendAuthSms($this->mobile, $template);
  83. } else {
  84. if($this->isVariable){
  85. $code = $smsInstance->sendSms($this->mobile, $template, $this->params);
  86. }else{
  87. $code = $smsInstance->sendSms($this->mobile, $template, $this->params, false, $this->content);
  88. }
  89. }
  90. $smsQueueRepository->saveRecord($smsQueue->id, 1, $code);
  91. $smsBatchRepository->updateBatch($this->batch_id);
  92. } catch (\Exception $e) {
  93. $smsQueueRepository->saveRecord($smsQueue->id, 3, $e->getMessage());
  94. $smsBatchRepository->updateBatch($this->batch_id, false);
  95. //throw $e;
  96. }
  97. if (!empty($this->callback)) {
  98. $obj=app($this->callback[0][0]);
  99. call_user_func_array([$obj, $this->callback[0][1]], $this->callback[1]);
  100. }
  101. }
  102. /**
  103. * @param $callback
  104. */
  105. public function setCallback($callback): void
  106. {
  107. $this->callback = $callback;
  108. }
  109. /**
  110. * 设置发送者
  111. * @param $utype: 0系统,1企业,2用户
  112. * @param $id: 对应 utype id
  113. */
  114. public function setSender($utype, $id)
  115. {
  116. $this->utype=$utype;
  117. $this->sender_id=$id;
  118. }
  119. /**
  120. * @param mixed $batch_id
  121. */
  122. public function setBatchId($batch_id): void
  123. {
  124. $this->batch_id = $batch_id;
  125. }
  126. }