SyncAccount.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace app\queue\redis\fast;
  3. use app\extra\ocean\Account;
  4. use app\model\cpa\CpaAccount;
  5. use Webman\RedisQueue\Consumer;
  6. class SyncAccount implements Consumer
  7. {
  8. /**
  9. * 消费队列名
  10. * 核销队列
  11. * @var string
  12. */
  13. public string $queue = "sync-account";
  14. /**
  15. * 连接配置
  16. * @var string
  17. */
  18. public string $connection = "default";
  19. public function consume($data): bool
  20. {
  21. try {
  22. if (empty($data['account_id'])) return true;
  23. echo("开始同步====>" . getDateFull());
  24. $nextPage = 1;
  25. while (is_numeric($nextPage)) {
  26. $resp = (new Account)->config($this->getConfig())->token()->getAccountList($data['account_id'],"AD",$nextPage);
  27. if (empty($resp['account_list'])) {
  28. echo getDateFull()."==没有数据了==\n";
  29. $nextPage = null;
  30. return true;
  31. }
  32. foreach ($resp['account_list'] as $item) {
  33. $account = (new CpaAccount)->where("advertiser_id",$item['account_id'])->findOrEmpty();
  34. if ($account->isEmpty()) {
  35. $account->strict(false)->insertGetId([
  36. "advertiser_id" => $item['account_id'],
  37. "group_id" => $data['account_id'],
  38. "advertiser_name" => $item['account_name']
  39. ]);
  40. echo getDateFull()."==新账户入库=={$item['account_id']}====\n";
  41. } else {
  42. $account->save([
  43. "advertiser_id" => $item['account_id'],
  44. "advertiser_name" => $item['account_name']
  45. ]);
  46. echo getDateFull()."==更新账户=={$item['account_id']}====\n";
  47. }
  48. }
  49. $nextPage = $nextPage+1;
  50. echo("进入下一页=={$nextPage}==".getDateFull());
  51. }
  52. } catch (\Throwable $throwable) {
  53. echo getDateFull()."===同步账户列表报错\n";
  54. echo $throwable->getFile()."\n";
  55. echo $throwable->getLine()."\n";
  56. echo $throwable->getMessage()."\n";
  57. return true;
  58. }
  59. return true;
  60. }
  61. /**
  62. * 配置信息
  63. * @param string $type
  64. * @return array
  65. */
  66. protected function getConfig(string $type = ""): array
  67. {
  68. return [
  69. "appid" => "1874672050081403",
  70. "secret" => "584e48eab19f6ec69118686b40998e2082eb77ee",
  71. "prefix" => "ocean",
  72. ];
  73. }
  74. }