Ocean.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace app\controller\common;
  3. use app\extra\basic\Base;
  4. use app\extra\ocean\AccessToken;
  5. use app\model\cpa\CpaAccountGroup;
  6. use LinFly\Annotation\Attributes\Route\Controller;
  7. use LinFly\Annotation\Attributes\Route\GetMapping;
  8. use support\Request;
  9. use support\Response;
  10. use support\think\Cache;
  11. use Webman\RedisQueue\Redis;
  12. #[Controller(prefix: "/ocean")]
  13. class Ocean extends Base
  14. {
  15. /**
  16. * AccessToken前缀
  17. * @var string
  18. */
  19. protected string $tokenPrefix = "ocean_[appid]_access_token";
  20. /**
  21. * RefreshToken前缀
  22. * @var string
  23. */
  24. protected string $refreshPrefix = "ocean_[appid]_refresh_token";
  25. /**
  26. * 应用授权Token
  27. * @param Request $request
  28. * @return Response
  29. */
  30. #[GetMapping("oauth")]
  31. public function getAdTokenOauth(Request $request): Response
  32. {
  33. try {
  34. $param = $this->_valid([
  35. "auth_code.require" => "参数错误",
  36. "app_id.require" => "参数错误",
  37. "state.require" => "参数错误"
  38. ]);
  39. if (!is_array($param)) return error($param);
  40. $config = $this->getConfig($param['app_id']);
  41. $accessToken = (new AccessToken)->config($config)->getAccessTokenCode($param['auth_code'],$param['state']);
  42. if (isset($accessToken['access_token'])) {
  43. Cache::set($this->getTokenPrefix($param['app_id']),$accessToken['access_token'],$accessToken['expires_in']);
  44. Cache::set($this->getRefreshPrefix($param['app_id']),$accessToken['refresh_token'],$accessToken['refresh_token_expires_in']);
  45. $group = (new AccessToken)->config($config)->getOauthAccount();
  46. if (!empty($group['list'])) {
  47. foreach ($group['list'] as $val) {
  48. $groupModel = (new CpaAccountGroup)->where("advertiser_id",$val['advertiser_id'])->findOrEmpty();
  49. if ($groupModel->isEmpty()) {
  50. $groupModel->strict(false)->insertGetId($val);
  51. }
  52. }
  53. }
  54. return success("换取成功");
  55. }
  56. return error("换取Token失败");
  57. } catch (\Throwable $throwable) {
  58. return error($throwable->getMessage());
  59. }
  60. }
  61. #[GetMapping("account")]
  62. public function getAccount(): Response
  63. {
  64. try {
  65. $config = $this->getConfig();
  66. $accessToken = (new AccessToken)->config($config)->getOauthAccount();
  67. return success("换取成功",$accessToken);
  68. } catch (\Throwable $throwable) {
  69. return error($throwable->getMessage());
  70. }
  71. }
  72. #[GetMapping("token")]
  73. public function getAccountToken(): Response
  74. {
  75. try {
  76. $config = $this->getConfig();
  77. $accessToken = (new AccessToken)->config($config)->getAccessToken();
  78. return success("换取成功",['data'=> $accessToken]);
  79. } catch (\Throwable $throwable) {
  80. return error($throwable->getMessage());
  81. }
  82. }
  83. #[GetMapping("sync")]
  84. public function syncAccount(Request $request): Response
  85. {
  86. try {
  87. $param = $this->_valid([
  88. "account.require" => "参数错误"
  89. ]);
  90. if (!is_array($param)) return error($param);
  91. Redis::send("sync-account",["account_id" => $param['account']]);
  92. return success("发起同步成功");
  93. } catch (\Throwable $throwable) {
  94. return error($throwable->getMessage());
  95. }
  96. }
  97. /**
  98. * 配置信息
  99. * @param string $type
  100. * @return array
  101. */
  102. protected function getConfig(string $type = ""): array
  103. {
  104. return [
  105. "appid" => "1874672050081403",
  106. "secret" => "584e48eab19f6ec69118686b40998e2082eb77ee",
  107. "prefix" => "ocean",
  108. ];
  109. }
  110. /**
  111. * @param string $type
  112. * @return string
  113. */
  114. protected function getTokenPrefix(string $type = ""): string
  115. {
  116. $config = $this->getConfig($type);
  117. return str_replace("[appid]",$config['appid'],$this->tokenPrefix);
  118. }
  119. /**
  120. * @param string $type
  121. * @return string
  122. */
  123. protected function getRefreshPrefix(string $type = ""): string
  124. {
  125. $config = $this->getConfig($type);
  126. return str_replace("[appid]",$config['appid'],$this->refreshPrefix);
  127. }
  128. }