123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- <?php
- namespace App\Console\Commands;
- use App\Log;
- use App\Models\VpAccount;
- use App\RedisModel;
- use App\Services\HttpService;
- use App\Services\YouZiService;
- use Illuminate\Console\Command;
- class YouZiWxAccount extends Command
- {
- protected $signature = 'YouZiWxAccount {accountLabel?}';
- protected $description = '柚子分销端绑定公众账号获取';
- protected $accountLabel;
- protected $platformId;
- public function handle()
- {
- \DB::connection()->disableQueryLog();
- $this->accountLabel = $this->argument('accountLabel') ? $this->argument('accountLabel') : 0;
- $this->info('本次查询的账号对应键值为:' . $this->accountLabel);
- $this->getAccountData();
- }
- private function getAccountData($retry = 0)
- {
- $requestUrl = YouZiService::BASE_URI . YouZiService::ACCOUNT_LIST_URI;
- $accessToken = YouZiService::getAccessToken($this->accountLabel);
- if(empty($accessToken)) return false;
- $header = [
- 'accesstoken: ' . $accessToken
- ];
- $response = HttpService::httpGet($requestUrl, $header);
- $this->info($response);
- $responseData = json_decode($response, True);
- if(isset($responseData['code']) && $responseData['code'] == -10001 && $retry < 5) {
- # 清除已缓存的AccessToken
- RedisModel::del(YouZiService::ACCESS_TOKEN_RDS_KEY . '_' .$this->accountLabel);
- $retry++;
- $this->getAccountData($retry);
- }
- # 是否存在合法数据
- $data = isset($responseData['data']['mpList']) ? $responseData['data']['mpList'] : null;
- if(empty($data)) {
- Log::logError('柚子分销平台已绑定账号抓取失败', [
- 'response' => $responseData,
- 'accessToken' => $accessToken,
- 'retry' => $retry
- ]);
- return false;
- }
- if($this->accountLabel == 1) {
- $this->platformId = 3;
- } else {
- $this->platformId = 1;
- }
- $platformId = $this->platformId;
- # 检出数据
- foreach ($data as $datum) {
- if(!isset($datum['serviceType']) || !in_array($datum['serviceType'], YouZiService::ACCOUNT_SERVICE_TYPE)) continue;
- # 公众账号AppId获取
- $appId = isset($datum['mpAppId']) ? $datum['mpAppId'] : '';
- if(empty($appId)) {
- Log::logError('公众号AppId获取失败', $datum, 'YouZiWxAccount');
- continue;
- }
- $maAppId = isset($datum['maAppId']) ? $datum['maAppId'] : null;
- $enable = 1;
- $isDeleted = isset($datum['isDeleted']) ? $datum['isDeleted'] : 0;
- if($isDeleted) $enable = -1;
- $insertData = [
- 'created_id' => isset($datum['createdId']) ? $datum['createdId'] : null,
- 'platform_created_at' => isset($datum['createdTs']) ? date('Y-m-d H:i:s', round($datum['createdTs'] / 1000)) : null,
- 'updated_id' => isset($datum['updatedId']) ? $datum['updatedId'] : null,
- 'platform_updated_at' => isset($datum['updatedTs']) ? date('Y-m-d H:i:s', round($datum['updatedTs'] / 1000)) : null,
- 'enable' => $enable,
- 'nickname' => isset($datum['nickName']) ? $datum['nickName'] : null,
- 'raw_id' => isset($datum['userName']) ? $datum['userName'] : null,
- 'head_img' => isset($datum['headImg']) ? $datum['headImg'] : null,
- 'admin_id' => isset($datum['adminId']) ? $datum['adminId'] : null,
- 'ma_app_id' => $maAppId,
- 'service_type' => isset($datum['serviceType']) ? $datum['serviceType'] : null,
- 'principal_name' => isset($datum['principalName']) ? $datum['principalName'] : null,
- 'alias' => isset($datum['alias']) ? $datum['alias'] : null,
- 'qrcode_url' => isset($datum['qrcodeUrl']) ? $datum['qrcodeUrl'] : null,
- 'business_info' => isset($datum['business_info']) ? json_encode($datum['business_info']) : null,
- 'verify_type' => isset($datum['verifyType']) ? $datum['verifyType'] : null,
- 'subscribe_url' => isset($datum['subscribeUrl']) ? $datum['subscribeUrl'] : null,
- 'is_advertiser' => isset($datum['is_advertiser']) ? $datum['is_advertiser'] : null,
- 'action_set_id' => isset($datum['actionSetId']) ? $datum['actionSetId'] : null,
- 'suite_id' => isset($datum['suiteId']) ? $datum['suiteId'] : null,
- 'permanent_code' => isset($datum['permanentCode']) ? $datum['permanentCode'] : null,
- 'agent_id' => isset($datum['agentId']) ? $datum['agentId'] : null,
- 'auth_mode' => isset($datum['authMode']) ? $datum['authMode'] : null,
- 'allow_parties' => isset($datum['allowParties']) ? json_encode($datum['allowParties']) : null,
- 'allow_users' => isset($datum['allowUsers']) ? json_encode($datum['allowUsers']) : null,
- 'allow_tags' => isset($datum['allowTags']) ? json_encode($datum['allowTags']) : null,
- 'original_corp_id' => isset($datum['originalCorpId']) ? $datum['originalCorpId'] : null,
- ];
- # 创建 / 更新
- VpAccount::updateOrCreate(
- ['platform_id'=>$platformId, 'app_id'=>$appId, 'ma_app_id' => $maAppId], $insertData
- );
- }
- }
- }
|