123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- <?php
- namespace App\Console\Commands;
- use App\Log;
- use App\Models\Account;
- use App\Models\FansData;
- use App\Services\ZZYService;
- use App\Services\FansDataService;
- use Illuminate\Console\Command;
- class FansDataCube extends Command
- {
- protected $signature = 'FansDataCube {year?}';
- protected $description = '账号粉丝变化日统计';
- protected $year;
- protected $month;
- protected $dateNow;
- protected $limit = 50;
- protected $time;
- public function handle()
- {
- \DB::connection()->disableQueryLog();
- $this->year = $this->argument('year') ? $this->argument('year') : null;
- if(is_null($this->year)) {
- $month = null;
- $this->getData($month);
- } else {
- for($month=1; $month<=12;$month++) {
- $result = $this->getData($month);
- if($result === false) break;
- }
- }
- $this->info(date('m-d H:i:s') . ' 开始整理');
- }
- /*
- * 获取公众号粉丝每日数据
- * */
- private function getData($month)
- {
- if(is_null($month)) {
- $date = date('Y-m-d', strtotime('-1 days'));
- $this->getFansData($date);
- } else {
- $days = getDaysByMonth($this->year, $month);
- foreach ($days as $day) {
- if($day > date('Y-m-d')) return false;
- $this->getFansData($day);
- }
- }
- return true;
- }
- public function getFansData($date)
- {
- $this->info('日期:'.$date);
- try {
- $page = 1;
- do {
- // 获取公众号信息
- $accountList = Account::accountList(ZZYService::PLATFORM_ID, $page, $this->limit);
- $count = count($accountList);
- $this->info(date('m-d H:i:s') . ' 此次共有' . $count . '个账号待处理');
- $this->info(date('m-d H:i:s') . ' 页码数' . $page . '页');
- $insertData = [];
- foreach ($accountList as $item) {
- // 判断是否存在
- $isSave = FansData::where('app_id', $item->app_id)->where('ref_date', $date)->exists();
- if (!$isSave) {
- // 获取数据
- $data = FansDataService::fansDataCube(
- $item->app_id, $date, $date, $item->channel_id
- );
- if (empty($data) || !isset($data[0]) || empty($data[0])) {
- Log::logError('微信未返回粉丝数据,时间:' . $date . ',公众号Id:' . $item->app_id, (array)$data, 'FansDataCube');
- continue;
- }
- array_push($insertData, $data[0]);
- $this->info(date('m-d H:i:s') . $item->app_id . ' 的粉丝数据获取完毕');
- }
- }
- // 存储数据
- if (!empty($insertData)) {
- $result = FansData::insert($insertData);
- if (!$result) {
- Log::logError('粉丝数据写入失败', $insertData, 'FansDataCube');
- }
- }
- $page++;
- # 返回此次同步数量
- $this->info(date('H:i:s') . ' 已同步 ' . $count . ' 条粉丝信息,占用内存' . round(memory_get_usage() / 1024 / 1024) . 'M');
- } while ($count == $this->limit);
- } catch (\Exception $e) {
- Log::logError('粉丝数据获取失败'.$e->getMessage(), [], 'AccountData');
- }
- }
- }
|