WxkfUserInfoJob.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace App\Jobs;
  3. use App\Models\Wxkf\UserInfoModel;
  4. use Illuminate\Bus\Queueable;
  5. use Illuminate\Queue\SerializesModels;
  6. use Illuminate\Queue\InteractsWithQueue;
  7. use Illuminate\Contracts\Queue\ShouldQueue;
  8. use Illuminate\Foundation\Bus\Dispatchable;
  9. class WxkfUserInfoJob implements ShouldQueue
  10. {
  11. use Dispatchable;
  12. use InteractsWithQueue;
  13. use Queueable;
  14. use SerializesModels;
  15. private $userinfos;
  16. private $wxid;
  17. /**
  18. * Create a new job instance.
  19. *
  20. * @param string $wxid
  21. * @param array $userinfos
  22. */
  23. public function __construct(string $wxid, array $userinfos)
  24. {
  25. $this->wxid = $wxid;
  26. $this->userinfos = $userinfos;
  27. }
  28. /**
  29. * Execute the job.
  30. *
  31. * @return void
  32. */
  33. public function handle()
  34. {
  35. sleep(5);
  36. foreach ($this->userinfos as $data) {
  37. try {
  38. $create = array(
  39. 'wxkf_wxid' => $this->wxid,
  40. 'wxid' => $data['wxid'],
  41. 'sex' => isset($data['sex']) ? $data['sex'] : 0
  42. );
  43. if (!empty($data['nickname'])) {
  44. $create['nickname'] = $data['nickname'];
  45. }
  46. if (!empty($data['head_img'])) {
  47. $create['head_img'] = $data['head_img'];
  48. }
  49. if (!empty($data['wx_alias'])) {
  50. $create['wx_alias'] = $data['wx_alias'];
  51. }
  52. UserInfoModel::updateOrCreate(
  53. array(
  54. 'wxkf_wxid' => $this->wxid,
  55. 'wxid' => $data['wxid']
  56. ),
  57. $create
  58. );
  59. } catch (\Exception $exception) {
  60. app('sentry')->captureException($exception);
  61. }
  62. }
  63. }
  64. /**
  65. * 要处理的失败任务
  66. * @param Exception $exception
  67. */
  68. public function failed(Exception $exception)
  69. {
  70. }
  71. }