小说推广数据系统

YouZiWxAccount.php 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Log;
  4. use App\Models\VpAccount;
  5. use App\RedisModel;
  6. use App\Services\HttpService;
  7. use App\Services\YouZiService;
  8. use Illuminate\Console\Command;
  9. class YouZiWxAccount extends Command
  10. {
  11. protected $signature = 'YouZiWxAccount {accountLabel?}';
  12. protected $description = '柚子分销端绑定公众账号获取';
  13. protected $accountLabel;
  14. protected $platformId;
  15. public function handle()
  16. {
  17. \DB::connection()->disableQueryLog();
  18. $this->accountLabel = $this->argument('accountLabel') ? $this->argument('accountLabel') : 0;
  19. $this->info('本次查询的账号对应键值为:' . $this->accountLabel);
  20. $this->getAccountData();
  21. }
  22. private function getAccountData($retry = 0)
  23. {
  24. $requestUrl = YouZiService::BASE_URI . YouZiService::ACCOUNT_LIST_URI;
  25. $accessToken = YouZiService::getAccessToken($this->accountLabel);
  26. if(empty($accessToken)) return false;
  27. $header = [
  28. 'accesstoken: ' . $accessToken
  29. ];
  30. $response = HttpService::httpGet($requestUrl, $header);
  31. $this->info($response);
  32. $responseData = json_decode($response, True);
  33. if(isset($responseData['code']) && $responseData['code'] == -10001 && $retry < 5) {
  34. # 清除已缓存的AccessToken
  35. RedisModel::del(YouZiService::ACCESS_TOKEN_RDS_KEY . '_' .$this->accountLabel);
  36. $retry++;
  37. $this->getAccountData($retry);
  38. }
  39. # 是否存在合法数据
  40. $data = isset($responseData['data']['mpList']) ? $responseData['data']['mpList'] : null;
  41. if(empty($data)) {
  42. Log::logError('柚子分销平台已绑定账号抓取失败', [
  43. 'response' => $responseData,
  44. 'accessToken' => $accessToken,
  45. 'retry' => $retry
  46. ]);
  47. return false;
  48. }
  49. if($this->accountLabel == 1) {
  50. $this->platformId = 3;
  51. } else {
  52. $this->platformId = 1;
  53. }
  54. $platformId = $this->platformId;
  55. # 检出数据
  56. foreach ($data as $datum) {
  57. if(!isset($datum['serviceType']) || !in_array($datum['serviceType'], YouZiService::ACCOUNT_SERVICE_TYPE)) continue;
  58. # 公众账号AppId获取
  59. $appId = isset($datum['mpAppId']) ? $datum['mpAppId'] : '';
  60. if(empty($appId)) {
  61. Log::logError('公众号AppId获取失败', $datum, 'YouZiWxAccount');
  62. continue;
  63. }
  64. $maAppId = isset($datum['maAppId']) ? $datum['maAppId'] : null;
  65. $enable = 1;
  66. $isDeleted = isset($datum['isDeleted']) ? $datum['isDeleted'] : 0;
  67. if($isDeleted) $enable = -1;
  68. $insertData = [
  69. 'created_id' => isset($datum['createdId']) ? $datum['createdId'] : null,
  70. 'platform_created_at' => isset($datum['createdTs']) ? date('Y-m-d H:i:s', round($datum['createdTs'] / 1000)) : null,
  71. 'updated_id' => isset($datum['updatedId']) ? $datum['updatedId'] : null,
  72. 'platform_updated_at' => isset($datum['updatedTs']) ? date('Y-m-d H:i:s', round($datum['updatedTs'] / 1000)) : null,
  73. 'enable' => $enable,
  74. 'nickname' => isset($datum['nickName']) ? $datum['nickName'] : null,
  75. 'raw_id' => isset($datum['userName']) ? $datum['userName'] : null,
  76. 'head_img' => isset($datum['headImg']) ? $datum['headImg'] : null,
  77. 'admin_id' => isset($datum['adminId']) ? $datum['adminId'] : null,
  78. 'ma_app_id' => $maAppId,
  79. 'service_type' => isset($datum['serviceType']) ? $datum['serviceType'] : null,
  80. 'principal_name' => isset($datum['principalName']) ? $datum['principalName'] : null,
  81. 'alias' => isset($datum['alias']) ? $datum['alias'] : null,
  82. 'qrcode_url' => isset($datum['qrcodeUrl']) ? $datum['qrcodeUrl'] : null,
  83. 'business_info' => isset($datum['business_info']) ? json_encode($datum['business_info']) : null,
  84. 'verify_type' => isset($datum['verifyType']) ? $datum['verifyType'] : null,
  85. 'subscribe_url' => isset($datum['subscribeUrl']) ? $datum['subscribeUrl'] : null,
  86. 'is_advertiser' => isset($datum['is_advertiser']) ? $datum['is_advertiser'] : null,
  87. 'action_set_id' => isset($datum['actionSetId']) ? $datum['actionSetId'] : null,
  88. 'suite_id' => isset($datum['suiteId']) ? $datum['suiteId'] : null,
  89. 'permanent_code' => isset($datum['permanentCode']) ? $datum['permanentCode'] : null,
  90. 'agent_id' => isset($datum['agentId']) ? $datum['agentId'] : null,
  91. 'auth_mode' => isset($datum['authMode']) ? $datum['authMode'] : null,
  92. 'allow_parties' => isset($datum['allowParties']) ? json_encode($datum['allowParties']) : null,
  93. 'allow_users' => isset($datum['allowUsers']) ? json_encode($datum['allowUsers']) : null,
  94. 'allow_tags' => isset($datum['allowTags']) ? json_encode($datum['allowTags']) : null,
  95. 'original_corp_id' => isset($datum['originalCorpId']) ? $datum['originalCorpId'] : null,
  96. ];
  97. # 创建 / 更新
  98. VpAccount::updateOrCreate(
  99. ['platform_id'=>$platformId, 'app_id'=>$appId, 'ma_app_id' => $maAppId], $insertData
  100. );
  101. }
  102. }
  103. }