小说推广数据系统

FansDataOfDay.php 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Log;
  4. use App\Models\Account;
  5. use App\Models\FansData;
  6. use App\Services\FansDataService;
  7. use App\Services\ZZYService;
  8. use Illuminate\Console\Command;
  9. class FansDataOfDay extends Command
  10. {
  11. protected $signature = 'FansDataOfDay {time_line?}';
  12. protected $description = '按日获取粉丝数据';
  13. protected $time;
  14. protected $limit = 50;
  15. public function __construct()
  16. {
  17. parent::__construct();
  18. }
  19. public function handle()
  20. {
  21. \DB::connection()->disableQueryLog();
  22. $this->time = $this->argument('time_line') ? $this->argument('time_line') : date('Y-m-d', strtotime('-1 days'));
  23. $this->info(date('m-d H:i:s') . ' 开始整理');
  24. $this->getData($this->time);
  25. }
  26. public function getData($date)
  27. {
  28. try {
  29. $page = 1;
  30. do {
  31. // 获取公众号信息
  32. $accountList = Account::accountList(ZZYService::PLATFORM_ID, $page, $this->limit);
  33. $count = count($accountList);
  34. $this->info(date('m-d H:i:s') . ' 此次共有' . $count . '个账号待处理');
  35. $this->info(date('m-d H:i:s') . ' 页码数' . $page . '页');
  36. $insertData = [];
  37. foreach ($accountList as $item) {
  38. // 判断是否存在
  39. $isSave = FansData::where('app_id', $item->app_id)->where('ref_date', $date)->exists();
  40. if (!$isSave) {
  41. // 获取数据
  42. $data = FansDataService::fansDataCube(
  43. $item->app_id, $date, $date, $item->channel_id
  44. );
  45. if (empty($data) || !isset($data[0]) || empty($data[0])) {
  46. Log::logError('微信未返回粉丝数据,时间:' . $date . ',公众号Id:' . $item->app_id, (array)$data, 'FansDataCube');
  47. continue;
  48. }
  49. array_push($insertData, $data[0]);
  50. $this->info(date('m-d H:i:s') . $item->app_id . ' 的粉丝数据获取完毕');
  51. }
  52. }
  53. // 存储数据
  54. if (!empty($insertData)) {
  55. $result = FansData::insert($insertData);
  56. if (!$result) {
  57. Log::logError('粉丝数据写入失败', $insertData, 'FansDataCube');
  58. }
  59. }
  60. $page++;
  61. # 返回此次同步数量
  62. $this->info(date('H:i:s') . ' 已同步 ' . $count . ' 条粉丝信息,占用内存' . round(memory_get_usage() / 1024 / 1024) . 'M');
  63. } while ($count == $this->limit);
  64. } catch (\Exception $e) {
  65. Log::logError('粉丝数据获取失败'.$e->getMessage(), [], 'AccountData');
  66. }
  67. }
  68. }