小说推广数据系统

ZZYWxAccount.php 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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\ZZYService;
  8. use Illuminate\Console\Command;
  9. class ZZYWxAccount extends Command
  10. {
  11. protected $signature = 'ZZYWxAccount';
  12. protected $description = '掌中云平台公众账号数据获取';
  13. protected $page = 1;
  14. protected $perPage = 50;
  15. public function handle()
  16. {
  17. $this->getAccountData();
  18. }
  19. public function getAccountData()
  20. {
  21. do {
  22. $param = [
  23. 'key' => ZZYService::API_KEY,
  24. 'page' => $this->page,
  25. 'per_page' => $this->perPage
  26. ];
  27. # 获取签名
  28. $param['sign'] = ZZYService::sign($param);
  29. $requestUri = ZZYService::API_BASE_URL . ZZYService::CHANNEL_LIST . '?' . http_build_query($param);
  30. # 获取列表
  31. $response = HttpService::httpGet($requestUri);
  32. if($response === false)
  33. Log::logError('掌中云公众账号数据获取失败' . $requestUri, $param, 'AccountDataZZY');
  34. $responseData = json_decode($response, true);
  35. $total = 0;
  36. if(isset($responseData['data']['count']) && $responseData['data']['count'] > 0) {
  37. $total = $responseData['data']['count'];
  38. $list = $responseData['data']['items'];
  39. $platformId = ZZYService::PLATFORM_ID;
  40. foreach ($list as $item) {
  41. $wxInfo = $item['mp'];
  42. $appId = isset($wxInfo['app_id']) ? $wxInfo['app_id'] : '';
  43. if(empty($appId)) {
  44. $appId = UserAuthorization::where('account_name', $wxInfo['nickname'])->value('wechat_account_id');
  45. }
  46. $insertData = [
  47. 'platform_id' => $platformId,
  48. 'channel_id' => $item['id'],
  49. 'site_domain' => $item['site_domain'],
  50. 'username' => $item['username'],
  51. 'name' => $item['nickname'],
  52. 'app_id' => $appId,
  53. 'raw_id' => $wxInfo['raw_id'],
  54. 'nickname' => $wxInfo['nickname'],
  55. 'platform_created_at' => date('Y-m-d H:i:s', strtotime($item['created_at']))
  56. ];
  57. # 创建或更新
  58. Account::updateOrCreate(
  59. ['platform_id'=>$platformId, 'name'=>$item['nickname']], $insertData
  60. );
  61. }
  62. }
  63. } while($total == $this->perPage);
  64. }
  65. }