1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- <?php
- namespace App\Console\Commands;
- use App\Log;
- use App\Models\Account;
- use App\Models\FansData;
- use App\Services\FansDataService;
- use App\Services\ZZYService;
- use Illuminate\Console\Command;
- class FansDataOfDay extends Command
- {
- protected $signature = 'FansDataOfDay {time_line?}';
- protected $description = '按日获取粉丝数据';
- protected $time;
- protected $limit = 50;
- public function __construct()
- {
- parent::__construct();
- }
- public function handle()
- {
- \DB::connection()->disableQueryLog();
- $this->time = $this->argument('time_line') ? $this->argument('time_line') : date('Y-m-d', strtotime('-1 days'));
- $this->info(date('m-d H:i:s') . ' 开始整理');
- $this->getData($this->time);
- }
- public function getData($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');
- }
- }
- }
|