SyncOfficialAccounts.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: z-yang
  5. * Date: 2021/2/22
  6. * Time: 11:34
  7. */
  8. namespace App\Console\Commands\Report;
  9. use App\Modules\OfficialAccount\Models\OfficialAccount;
  10. use GuzzleHttp\Client;
  11. use Illuminate\Console\Command;
  12. class SyncOfficialAccounts extends Command
  13. {
  14. /**
  15. * The name and signature of the console command.
  16. *
  17. * @var string
  18. */
  19. protected $signature = 'sync:official:accounts';
  20. /**
  21. * The console command description.
  22. *
  23. * @var string
  24. */
  25. protected $description = '同步公众号到投放后台';
  26. /**
  27. * Execute the console command.
  28. *
  29. * @return mixed
  30. */
  31. public function handle()
  32. {
  33. // 获取所有公众号 'id','nickname','appid','distribution_channel_id' 只获取这4个
  34. $accounts = OfficialAccount::getAllOfficialAccountDB();
  35. $accounts = $accounts ? $accounts->toArray() : [];
  36. if (empty($accounts)) {
  37. dd('查询不到公众号');
  38. }
  39. $client = new Client(['timeout' => 10, 'verify' => false]);
  40. $accountsArr = array_chunk($accounts, 50);
  41. foreach ($accountsArr as $accountList) {
  42. $reportData = [
  43. 'platform' => 'wangduyun',
  44. 'list' => []
  45. ];
  46. foreach ($accountList as $account) {
  47. $reportData['list'][] = [
  48. 'id' => getProp($account, 'id'),
  49. 'name' => getProp($account, 'nickname'),
  50. 'app_id' => getProp($account, 'appid'),
  51. 'channel_id' => getProp($account, 'distribution_channel_id'),
  52. ];
  53. }
  54. // 执行上报
  55. $url = 'https://firetrack.zhuishuyun.com/api/syncOfficialAccounts';
  56. $client->post($url, [
  57. 'headers' => [
  58. 'x-code' => 'Mvnx1Yr3O8i!TS5u'
  59. ],
  60. 'json' => $reportData
  61. ]);
  62. }
  63. }
  64. }