SmsBatchJob.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace App\Jobs;
  3. use App\Models\Company;
  4. use App\Models\Member;
  5. use App\Models\SmsBatch;
  6. use App\Models\SmsRule;
  7. use App\Validators\Rules\MobileRule;
  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 SmsBatchJob implements ShouldQueue
  14. {
  15. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  16. /**
  17. * @var SmsBatch
  18. */
  19. private $smsBatch;
  20. /**
  21. * Create a new job instance.
  22. *
  23. * @param SmsBatch $smsBatch
  24. */
  25. public function __construct(SmsBatch $smsBatch)
  26. {
  27. $this->smsBatch = $smsBatch;
  28. }
  29. /**
  30. * Execute the job.
  31. *
  32. * @return void
  33. */
  34. public function handle()
  35. {
  36. $status = $this->getRuleStatus($this->smsBatch->template->alias);
  37. if ($status) {
  38. if ($this->smsBatch->mobiles) {
  39. $mobiles=explode(",", $this->smsBatch->mobiles);
  40. foreach ($mobiles as $mobile) {
  41. if (validator_check($mobile, new MobileRule())) {
  42. $this->dispatchSms($mobile);
  43. $this->smsBatch->target_num++;
  44. }
  45. $this->smsBatch->save();
  46. }
  47. } else {
  48. $mobiles=$this->getMobiles($this->smsBatch->accept_member, $this->smsBatch->accept_type, $this->smsBatch->accept_time);
  49. if (!empty($mobiles)) {
  50. foreach ($mobiles as $mobile) {
  51. if (validator_check($mobile, new MobileRule())) {
  52. $this->dispatchSms($mobile);
  53. $this->smsBatch->target_num++;
  54. }
  55. $this->smsBatch->save();
  56. }
  57. }
  58. }
  59. }
  60. if ($this->smsBatch->target_num == 0) {
  61. $this->smsBatch->status=1;
  62. $this->smsBatch->save();
  63. }
  64. }
  65. protected function getMobiles($accept_member, $accept_type, $accetp_time)
  66. {
  67. $where=[];
  68. if ($accept_type) {
  69. $where[]=[$accept_type>2?"mobile_audit":"email_audit",$accept_type%2];
  70. }
  71. if ($accetp_time) {
  72. $where[]=["last_login_time",'<',time()-($accetp_time==1?7:30)*24*60*60];
  73. }
  74. $memberMobiles=Member::where($where)->pluck('mobile');
  75. $companyMobiles=Company::where($where)->pluck('mobile');
  76. if ($accept_member == 0) {
  77. return $memberMobiles->concat($companyMobiles);
  78. } else {
  79. return $accept_member==1?$companyMobiles:$memberMobiles;
  80. }
  81. }
  82. protected function dispatchSms($mobile)
  83. {
  84. $smsJob=new SmsJob($mobile, $this->smsBatch->template->alias, []);
  85. $smsJob->setBatchId($this->smsBatch->id);
  86. dispatch($smsJob);
  87. }
  88. protected function getRuleStatus($alias)
  89. {
  90. $result = SmsRule::where(['alias'=>$alias])->first();
  91. if ($result) {
  92. if ($result->status) {
  93. return true;
  94. }
  95. return false;
  96. } else {
  97. return false;
  98. }
  99. }
  100. }