1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- <?php
- namespace App\Console\Commands;
- use App\Log;
- use App\Models\Account;
- use App\Models\UserAuthorization;
- use App\Services\HttpService;
- use App\Services\YWService;
- use Illuminate\Console\Command;
- class YWWxAccount extends Command
- {
- protected $signature = 'YWWxAccount {label}';
- protected $description = '阅文平台微信账号获取';
- protected $page = 1;
- protected $perPage = 100;
- protected $label; // 区分阅文平台账号
- public function handle()
- {
- \DB::connection()->disableQueryLog();
- $this->label = $this->argument('label') ? $this->argument('label') : 1;
- $this->getAccountData();
- }
- public function getAccountData()
- {
- do {
- $params = [
- 'coop_type' => 1,
- 'start_time' => strtotime(date('Y-m-d', strtotime('-3 years')) . ' 00:00:00'),
- 'end_time' => strtotime(date('Y-m-d', strtotime('-1 days')) . ' 23:59:59'),
- 'page' => $this->page
- ];
- # 签名
- $params = YWService::sign($params, $this->label);
- $requestUri = YWService::API_BASE_URL . YWService::CHANNEL_LIST . '?' . http_build_query($params);
- # 获取列表
- $response = HttpService::httpGet($requestUri);
- if($response === false)
- Log::logError('阅文公众账号数据获取失败' . $requestUri, $params, 'YWAccountData');
- $responseData = json_decode($response, true);
- $total = 0;
- if(isset($responseData['data']['total_count']) && $responseData['data']['total_count'] > 0) {
- $total = $responseData['data']['total_count'];
- $list = $responseData['data']['list'];
- $platformId = YWService::PLATFORM_ID;
- foreach ($list as $item) {
- if(!isset($item['app_name']) || empty($item['app_name']))
- continue;
- # 获取appId
- $accountInfo = UserAuthorization::select(['wechat_account_id'])
- ->where('account_name', $item['app_name'])
- ->first();
- $insertData = [
- 'platform_id' => $platformId,
- 'channel_id' => $item['appflag'],
- 'site_domain' => null,
- 'username' => null,
- 'name' => $item['app_name'],
- 'app_id' => isset($accountInfo->wechat_account_id) ? $accountInfo->wechat_account_id : null,
- 'raw_id' => null,
- 'nickname' => $item['app_name'],
- 'platform_created_at' => null
- ];
- # 创建或更新
- Account::updateOrCreate(
- ['platform_id'=>$platformId, 'name'=>$item['app_name']], $insertData
- );
- }
- $this->page++;
- } else {
- Log::logError('阅文账号信息获取失败', (array)$responseData, 'YWAccountData');
- }
- $this->info('本次共同步账号'.$total.'个');
- } while($total == $this->perPage);
- }
- }
|