小说推广数据系统

YWWxAccount.php 3.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Log;
  4. use App\Models\Account;
  5. use App\Models\UserAuthorization;
  6. use App\Services\HttpService;
  7. use App\Services\YWService;
  8. use Illuminate\Console\Command;
  9. class YWWxAccount extends Command
  10. {
  11. protected $signature = 'YWWxAccount {label}';
  12. protected $description = '阅文平台微信账号获取';
  13. protected $page = 1;
  14. protected $perPage = 100;
  15. protected $label; // 区分阅文平台账号
  16. public function handle()
  17. {
  18. \DB::connection()->disableQueryLog();
  19. $this->label = $this->argument('label') ? $this->argument('label') : 1;
  20. $this->getAccountData();
  21. }
  22. public function getAccountData()
  23. {
  24. do {
  25. $params = [
  26. 'coop_type' => 1,
  27. 'start_time' => strtotime(date('Y-m-d', strtotime('-3 years')) . ' 00:00:00'),
  28. 'end_time' => strtotime(date('Y-m-d', strtotime('-1 days')) . ' 23:59:59'),
  29. 'page' => $this->page
  30. ];
  31. # 签名
  32. $params = YWService::sign($params, $this->label);
  33. $requestUri = YWService::API_BASE_URL . YWService::CHANNEL_LIST . '?' . http_build_query($params);
  34. # 获取列表
  35. $response = HttpService::httpGet($requestUri);
  36. if($response === false)
  37. Log::logError('阅文公众账号数据获取失败' . $requestUri, $params, 'YWAccountData');
  38. $responseData = json_decode($response, true);
  39. $total = 0;
  40. if(isset($responseData['data']['total_count']) && $responseData['data']['total_count'] > 0) {
  41. $total = $responseData['data']['total_count'];
  42. $list = $responseData['data']['list'];
  43. $platformId = YWService::PLATFORM_ID;
  44. foreach ($list as $item) {
  45. if(!isset($item['app_name']) || empty($item['app_name']))
  46. continue;
  47. # 获取appId
  48. $accountInfo = UserAuthorization::select(['wechat_account_id'])
  49. ->where('account_name', $item['app_name'])
  50. ->first();
  51. $insertData = [
  52. 'platform_id' => $platformId,
  53. 'channel_id' => $item['appflag'],
  54. 'site_domain' => null,
  55. 'username' => null,
  56. 'name' => $item['app_name'],
  57. 'app_id' => isset($accountInfo->wechat_account_id) ? $accountInfo->wechat_account_id : null,
  58. 'raw_id' => null,
  59. 'nickname' => $item['app_name'],
  60. 'platform_created_at' => null
  61. ];
  62. # 创建或更新
  63. Account::updateOrCreate(
  64. ['platform_id'=>$platformId, 'name'=>$item['app_name']], $insertData
  65. );
  66. }
  67. $this->page++;
  68. } else {
  69. Log::logError('阅文账号信息获取失败', (array)$responseData, 'YWAccountData');
  70. }
  71. $this->info('本次共同步账号'.$total.'个');
  72. } while($total == $this->perPage);
  73. }
  74. }