UserRententionJob.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 $time;
  20. /**
  21. * Create a new job instance.
  22. *
  23. * @return void
  24. */
  25. public function __construct(int $uid, string $time)
  26. {
  27. $this->uid = $uid;
  28. $this->time = $time;
  29. }
  30. /**
  31. * Execute the job.
  32. *
  33. * @return void
  34. */
  35. public function handle()
  36. {
  37. $user = User::find($this->uid);
  38. $date = date('Y-m-d', strtotime($this->time));
  39. $is_next_day = date('Y-m-d', strtotime($user->created_at)) == date('Y-m-d', strtotime('-1 days', strtotime($this->time)));
  40. if ($is_next_day) {
  41. $exists = QappUserRententionLog::where('uid', $this->uid)->exists();
  42. if ($exists) {
  43. return;
  44. }
  45. $client = new Client();
  46. $params = [
  47. 'uid' => $this->uid,
  48. 'source' => 'zsy'
  49. ];
  50. $params['sign'] = _sign($params, SysConsts::TIKTOK_KEY);
  51. $url = 'https://newtrackapi.zhuishuyun.com/api/qappuser/rentention';
  52. $response = $client->post($url, ['form_params' => $params])->getBody()->getContents();
  53. myLog('new_user_rentention')->info($response);
  54. $result = json_decode($response);
  55. if ($result) {
  56. if ($result->code == 0) {
  57. QappUserRententionLog::create([
  58. 'date' => $date,
  59. 'uid' => $this->uid,
  60. 'register_time' => $user->created_at,
  61. 'read_time' => $this->time,
  62. ]);
  63. }
  64. }
  65. }
  66. }
  67. }