ReportUsers.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. namespace App\Console\Commands\Report;
  3. use GuzzleHttp\Client;
  4. use App\Modules\User\Models\User;
  5. use Illuminate\Console\Command;
  6. use Illuminate\Support\Facades\Redis;
  7. class ReportUsers extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'report:users';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = '上报注册用户';
  21. /**
  22. * Execute the console command.
  23. *
  24. * @return mixed
  25. */
  26. public function handle()
  27. {
  28. // 获取最新同步uid
  29. $key = 'report:delivery:last:uid';
  30. $uid = (int)Redis::get($key);
  31. $client = new Client(['timeout' => 10, 'verify' => false]);
  32. // 获取批量用户信息
  33. $users = User::getUsersByUid($uid);
  34. if ($users) {
  35. $reportData = [
  36. 'platform' => 'wangduyun',
  37. 'list' => []
  38. ];
  39. foreach ($users as $user) {
  40. $uid = getProp($user, 'id');
  41. $reportData['list'][] = [
  42. 'uid' => $uid,
  43. 'channel_id' => getProp($user, 'distribution_channel_id'),
  44. 'register_time' => $user->created_at->format('Y-m-d H:i:s')
  45. ];
  46. }
  47. // 执行上报
  48. $client->post(env('REPORT_URI') . '/api/reportRegisters', [
  49. 'headers' => [
  50. 'x-code' => 'Mvnx1Yr3O8i!TS5u'
  51. ],
  52. 'json' => $reportData
  53. ]);
  54. // 更新uid
  55. Redis::set($key, $uid);
  56. }
  57. }
  58. }