SmsJob.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. dd($this->isAuthSms);
  82. if ($this->isAuthSms) {
  83. $code = $smsInstance->sendAuthSms($this->mobile, $template);
  84. } else {
  85. if($this->isVariable){
  86. $code = $smsInstance->sendSms($this->mobile, $template, $this->params);
  87. }else{
  88. $code = $smsInstance->sendSms($this->mobile, $template, $this->params, false, $this->content);
  89. }
  90. }
  91. $smsQueueRepository->saveRecord($smsQueue->id, 1, $code);
  92. $smsBatchRepository->updateBatch($this->batch_id);
  93. } catch (\Exception $e) {
  94. $smsQueueRepository->saveRecord($smsQueue->id, 3, $e->getMessage());
  95. $smsBatchRepository->updateBatch($this->batch_id, false);
  96. //throw $e;
  97. }
  98. if (!empty($this->callback)) {
  99. $obj=app($this->callback[0][0]);
  100. call_user_func_array([$obj, $this->callback[0][1]], $this->callback[1]);
  101. }
  102. }
  103. /**
  104. * @param $callback
  105. */
  106. public function setCallback($callback): void
  107. {
  108. $this->callback = $callback;
  109. }
  110. /**
  111. * 设置发送者
  112. * @param $utype: 0系统,1企业,2用户
  113. * @param $id: 对应 utype id
  114. */
  115. public function setSender($utype, $id)
  116. {
  117. $this->utype=$utype;
  118. $this->sender_id=$id;
  119. }
  120. /**
  121. * @param mixed $batch_id
  122. */
  123. public function setBatchId($batch_id): void
  124. {
  125. $this->batch_id = $batch_id;
  126. }
  127. }