小说推广数据系统

FansDataCube.php 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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\ZZYService;
  7. use App\Services\FansDataService;
  8. use Illuminate\Console\Command;
  9. class FansDataCube extends Command
  10. {
  11. protected $signature = 'FansDataCube {year?}';
  12. protected $description = '账号粉丝变化日统计';
  13. protected $year;
  14. protected $month;
  15. protected $dateNow;
  16. protected $limit = 50;
  17. protected $time;
  18. public function handle()
  19. {
  20. \DB::connection()->disableQueryLog();
  21. $this->year = $this->argument('year') ? $this->argument('year') : null;
  22. if(is_null($this->year)) {
  23. $month = null;
  24. $this->getData($month);
  25. } else {
  26. for($month=1; $month<=12;$month++) {
  27. $result = $this->getData($month);
  28. if($result === false) break;
  29. }
  30. }
  31. $this->info(date('m-d H:i:s') . ' 开始整理');
  32. }
  33. /*
  34. * 获取公众号粉丝每日数据
  35. * */
  36. private function getData($month)
  37. {
  38. if(is_null($month)) {
  39. $date = date('Y-m-d', strtotime('-1 days'));
  40. $this->getFansData($date);
  41. } else {
  42. $days = getDaysByMonth($this->year, $month);
  43. foreach ($days as $day) {
  44. if($day > date('Y-m-d')) return false;
  45. $this->getFansData($day);
  46. }
  47. }
  48. return true;
  49. }
  50. public function getFansData($date)
  51. {
  52. $this->info('日期:'.$date);
  53. try {
  54. $page = 1;
  55. do {
  56. // 获取公众号信息
  57. $accountList = Account::accountList(ZZYService::PLATFORM_ID, $page, $this->limit);
  58. $count = count($accountList);
  59. $this->info(date('m-d H:i:s') . ' 此次共有' . $count . '个账号待处理');
  60. $this->info(date('m-d H:i:s') . ' 页码数' . $page . '页');
  61. $insertData = [];
  62. foreach ($accountList as $item) {
  63. // 判断是否存在
  64. $isSave = FansData::where('app_id', $item->app_id)->where('ref_date', $date)->exists();
  65. if (!$isSave) {
  66. // 获取数据
  67. $data = FansDataService::fansDataCube(
  68. $item->app_id, $date, $date, $item->channel_id
  69. );
  70. if (empty($data) || !isset($data[0]) || empty($data[0])) {
  71. Log::logError('微信未返回粉丝数据,时间:' . $date . ',公众号Id:' . $item->app_id, (array)$data, 'FansDataCube');
  72. continue;
  73. }
  74. array_push($insertData, $data[0]);
  75. $this->info(date('m-d H:i:s') . $item->app_id . ' 的粉丝数据获取完毕');
  76. }
  77. }
  78. // 存储数据
  79. if (!empty($insertData)) {
  80. $result = FansData::insert($insertData);
  81. if (!$result) {
  82. Log::logError('粉丝数据写入失败', $insertData, 'FansDataCube');
  83. }
  84. }
  85. $page++;
  86. # 返回此次同步数量
  87. $this->info(date('H:i:s') . ' 已同步 ' . $count . ' 条粉丝信息,占用内存' . round(memory_get_usage() / 1024 / 1024) . 'M');
  88. } while ($count == $this->limit);
  89. } catch (\Exception $e) {
  90. Log::logError('粉丝数据获取失败'.$e->getMessage(), [], 'AccountData');
  91. }
  92. }
  93. }