1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- <?php
- namespace App\Console\Commands;
- use App\Log;
- use App\Models\Account;
- use App\Models\UserAuthorization;
- use App\Services\HttpService;
- use App\Services\ZZYService;
- use Illuminate\Console\Command;
- class ZZYWxAccount extends Command
- {
- protected $signature = 'ZZYWxAccount';
- protected $description = '掌中云平台公众账号数据获取';
- protected $page = 1;
- protected $perPage = 50;
- public function handle()
- {
- $this->getAccountData();
- }
- public function getAccountData()
- {
- do {
- $param = [
- 'key' => ZZYService::API_KEY,
- 'page' => $this->page,
- 'per_page' => $this->perPage
- ];
- # 获取签名
- $param['sign'] = ZZYService::sign($param);
- $requestUri = ZZYService::API_BASE_URL . ZZYService::CHANNEL_LIST . '?' . http_build_query($param);
- # 获取列表
- $response = HttpService::httpGet($requestUri);
- if($response === false)
- Log::logError('掌中云公众账号数据获取失败' . $requestUri, $param, 'AccountDataZZY');
- $responseData = json_decode($response, true);
- $total = 0;
- if(isset($responseData['data']['count']) && $responseData['data']['count'] > 0) {
- $total = $responseData['data']['count'];
- $list = $responseData['data']['items'];
- $platformId = ZZYService::PLATFORM_ID;
- foreach ($list as $item) {
- $wxInfo = $item['mp'];
- $appId = isset($wxInfo['app_id']) ? $wxInfo['app_id'] : '';
- if(empty($appId)) {
- $appId = UserAuthorization::where('account_name', $wxInfo['nickname'])->value('wechat_account_id');
- }
- $insertData = [
- 'platform_id' => $platformId,
- 'channel_id' => $item['id'],
- 'site_domain' => $item['site_domain'],
- 'username' => $item['username'],
- 'name' => $item['nickname'],
- 'app_id' => $appId,
- 'raw_id' => $wxInfo['raw_id'],
- 'nickname' => $wxInfo['nickname'],
- 'platform_created_at' => date('Y-m-d H:i:s', strtotime($item['created_at']))
- ];
- # 创建或更新
- Account::updateOrCreate(
- ['platform_id'=>$platformId, 'name'=>$item['nickname']], $insertData
- );
- }
- }
- } while($total == $this->perPage);
- }
- }
|