WeChatFansJob.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace App\Jobs;
  3. use App\Services\WeChat\WechatFactory;
  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 WeChatFansJob implements ShouldQueue
  10. {
  11. use Dispatchable;
  12. use InteractsWithQueue;
  13. use Queueable;
  14. use SerializesModels;
  15. /**
  16. * Create a new job instance.
  17. *
  18. * @return void
  19. */
  20. public function __construct($publicId, $openid_list)
  21. {
  22. $this->publicId = $publicId;
  23. $this->openid_list = $openid_list;
  24. }
  25. private $publicId;
  26. private $openid_list = [];
  27. /**
  28. * Execute the job.
  29. *
  30. * @return void
  31. */
  32. public function handle()
  33. {
  34. //
  35. $user_info_list = WechatFactory::Fans($this->publicId)->batchGet($this->openid_list);
  36. foreach ($user_info_list as $user_info) {
  37. try {
  38. if ($user_info->subscribe) {
  39. $data = [
  40. 'nickname' => trim($user_info->nickname) ?: "",
  41. 'sex' => $user_info->sex,
  42. 'language' => $user_info->language ?: "",
  43. 'city' => $user_info->city ?: "",
  44. 'province' => $user_info->province ?: "",
  45. 'country' => $user_info->country ?: "",
  46. 'headimgurl' => $user_info->headimgurl ?: "",
  47. 'unionid' => isset($user_info->unionid) ? $user_info->unionid : "",
  48. 'subscribe_time' => $user_info->subscribe_time,
  49. 'remark' => $user_info->remark ?: "",
  50. 'groupid' => $user_info->groupid,
  51. 'tagid_list' => json_encode($user_info->tagid_list),
  52. 'subscribe_scene' => $user_info->subscribe_scene ?: "",
  53. 'qr_scene' => $user_info->qr_scene ?: $user_info->qr_scene_str
  54. ];
  55. \DB::table("kddx_user_openid")
  56. ->where('openid', $user_info->openid)
  57. ->update($data);
  58. \DB::table("pocket.kdgx_fans_" . substr($this->publicId, -1))
  59. ->where('openid', $user_info->openid)
  60. ->update($data);
  61. } else {
  62. \DB::table('kddx_user_openid')->where('openid', $user_info->openid)
  63. ->update([
  64. 'subscribe' => 0
  65. ]);
  66. \DB::table("pocket.kdgx_fans_" . substr($this->publicId, -1))
  67. ->where('openid', $user_info->openid)
  68. ->update([
  69. 'subscribe' => 0
  70. ]);
  71. }
  72. } catch (\Exception $e) {
  73. dump($user_info);
  74. dump($e);
  75. }
  76. }
  77. }
  78. }