UserRententionJob.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace App\Jobs;
  3. use App\Consts\SysConsts;
  4. use App\Modules\User\Models\QappUserRententionLog;
  5. use App\Modules\User\Models\User;
  6. use GuzzleHttp\Client;
  7. use Illuminate\Bus\Queueable;
  8. use Illuminate\Queue\SerializesModels;
  9. use Illuminate\Queue\InteractsWithQueue;
  10. use Illuminate\Contracts\Queue\ShouldQueue;
  11. use Illuminate\Foundation\Bus\Dispatchable;
  12. /**
  13. * 用户留存
  14. */
  15. class UserRententionJob implements ShouldQueue
  16. {
  17. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  18. protected $uid;
  19. protected $read_time;
  20. protected $register_time;
  21. /**
  22. * Create a new job instance.
  23. *
  24. * @return void
  25. */
  26. public function __construct(int $uid, string $read_time, string $register_time)
  27. {
  28. $this->uid = $uid;
  29. $this->read_time = $read_time;
  30. $this->register_time = $register_time;
  31. }
  32. /**
  33. * Execute the job.
  34. *
  35. * @return void
  36. */
  37. public function handle()
  38. {
  39. $date = date('Y-m-d', strtotime($this->read_time));
  40. $exists = QappUserRententionLog::where('uid', $this->uid)->exists();
  41. if ($exists) {
  42. return;
  43. }
  44. $client = new Client();
  45. $params = [
  46. 'uid' => $this->uid,
  47. 'source' => 'zsy'
  48. ];
  49. $params['sign'] = _sign($params, SysConsts::TIKTOK_KEY);
  50. $url = 'https://newtrackapi.zhuishuyun.com/api/qappuser/rentention';
  51. $response = $client->post($url, ['form_params' => $params])->getBody()->getContents();
  52. myLog('new_user_rentention')->info($response);
  53. $result = json_decode($response);
  54. if ($result) {
  55. if ($result->code == 0) {
  56. QappUserRententionLog::create([
  57. 'date' => $date,
  58. 'uid' => $this->uid,
  59. 'register_time' => $this->register_time,
  60. 'read_time' => $this->read_time,
  61. ]);
  62. }
  63. }
  64. }
  65. }