海南旅游SAAS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
1.7 KiB

4 years ago
4 years ago
  1. <?php
  2. namespace App\Jobs;
  3. use App\Models\Agent;
  4. use App\Models\MiniProgramTemplate;
  5. use App\Models\MiniProgramUploadLog;
  6. use App\Service\UploadMiniProgram;
  7. use Exception;
  8. use GuzzleHttp\Exception\GuzzleException;
  9. use Illuminate\Bus\Queueable;
  10. use Illuminate\Contracts\Queue\ShouldQueue;
  11. use Illuminate\Foundation\Bus\Dispatchable;
  12. use Illuminate\Queue\InteractsWithQueue;
  13. use Illuminate\Queue\SerializesModels;
  14. class UploadMiniProgramQueue implements ShouldQueue
  15. {
  16. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  17. private int $template_id; //小程序模板ID
  18. private int $agent_id;
  19. /**
  20. * 任务可尝试的次数
  21. * @var int
  22. */
  23. public $tries = 3;
  24. /**
  25. * 任务失败前允许的最大异常数
  26. * @var int
  27. */
  28. public $maxExceptions = 3;
  29. /**
  30. * Create a new job instance.
  31. *
  32. * @param int $template_id 小程序模板ID
  33. * @param int $agent_id 代理商ID
  34. */
  35. public function __construct(int $template_id, int $agent_id)
  36. {
  37. $this->template_id = $template_id;
  38. $this->agent_id = $agent_id;
  39. }
  40. /**
  41. * Execute the job.
  42. *
  43. * @return void
  44. * @throws Exception
  45. */
  46. public function handle()
  47. {
  48. $agent = Agent::find($this->agent_id);
  49. $template = MiniProgramTemplate::find($this->template_id);
  50. if (!$agent || !$template) return;
  51. try {
  52. # 如果已经有上传成功的版本则不再上传
  53. $not_uploaded = MiniProgramUploadLog::where([
  54. 'is_success' => 0,
  55. 'agent_id' => $this->agent_id,
  56. 'template_id' => $this->template_id,
  57. ])->doesntExist();
  58. if ($not_uploaded) {
  59. new UploadMiniProgram($agent, $template);
  60. }
  61. } catch (GuzzleException | \Exception $e) {
  62. throw new Exception($agent->id . '-' . $agent->appid . $e->getMessage());
  63. }
  64. }
  65. }