| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- <?php
- namespace app\controller\common;
- use app\extra\basic\Base;
- use app\extra\ocean\AccessToken;
- use app\model\cpa\CpaAccountGroup;
- use LinFly\Annotation\Attributes\Route\Controller;
- use LinFly\Annotation\Attributes\Route\GetMapping;
- use support\Request;
- use support\Response;
- use support\think\Cache;
- use Webman\RedisQueue\Redis;
- #[Controller(prefix: "/ocean")]
- class Ocean extends Base
- {
- /**
- * AccessToken前缀
- * @var string
- */
- protected string $tokenPrefix = "ocean_[appid]_access_token";
- /**
- * RefreshToken前缀
- * @var string
- */
- protected string $refreshPrefix = "ocean_[appid]_refresh_token";
- /**
- * 应用授权Token
- * @param Request $request
- * @return Response
- */
- #[GetMapping("oauth")]
- public function getAdTokenOauth(Request $request): Response
- {
- try {
- $param = $this->_valid([
- "auth_code.require" => "参数错误",
- "app_id.require" => "参数错误",
- "state.require" => "参数错误"
- ]);
- if (!is_array($param)) return error($param);
- $config = $this->getConfig($param['app_id']);
- $accessToken = (new AccessToken)->config($config)->getAccessTokenCode($param['auth_code'],$param['state']);
- if (isset($accessToken['access_token'])) {
- Cache::set($this->getTokenPrefix($param['app_id']),$accessToken['access_token'],$accessToken['expires_in']);
- Cache::set($this->getRefreshPrefix($param['app_id']),$accessToken['refresh_token'],$accessToken['refresh_token_expires_in']);
- $group = (new AccessToken)->config($config)->getOauthAccount();
- if (!empty($group['list'])) {
- foreach ($group['list'] as $val) {
- $groupModel = (new CpaAccountGroup)->where("advertiser_id",$val['advertiser_id'])->findOrEmpty();
- if ($groupModel->isEmpty()) {
- $groupModel->strict(false)->insertGetId($val);
- }
- }
- }
- return success("换取成功");
- }
- return error("换取Token失败");
- } catch (\Throwable $throwable) {
- return error($throwable->getMessage());
- }
- }
- #[GetMapping("account")]
- public function getAccount(): Response
- {
- try {
- $config = $this->getConfig();
- $accessToken = (new AccessToken)->config($config)->getOauthAccount();
- return success("换取成功",$accessToken);
- } catch (\Throwable $throwable) {
- return error($throwable->getMessage());
- }
- }
- #[GetMapping("token")]
- public function getAccountToken(): Response
- {
- try {
- $config = $this->getConfig();
- $accessToken = (new AccessToken)->config($config)->getAccessToken();
- return success("换取成功",['data'=> $accessToken]);
- } catch (\Throwable $throwable) {
- return error($throwable->getMessage());
- }
- }
- #[GetMapping("sync")]
- public function syncAccount(Request $request): Response
- {
- try {
- $param = $this->_valid([
- "account.require" => "参数错误"
- ]);
- if (!is_array($param)) return error($param);
- Redis::send("sync-account",["account_id" => $param['account']]);
- return success("发起同步成功");
- } catch (\Throwable $throwable) {
- return error($throwable->getMessage());
- }
- }
- /**
- * 配置信息
- * @param string $type
- * @return array
- */
- protected function getConfig(string $type = ""): array
- {
- return [
- "appid" => "1874672050081403",
- "secret" => "584e48eab19f6ec69118686b40998e2082eb77ee",
- "prefix" => "ocean",
- ];
- }
- /**
- * @param string $type
- * @return string
- */
- protected function getTokenPrefix(string $type = ""): string
- {
- $config = $this->getConfig($type);
- return str_replace("[appid]",$config['appid'],$this->tokenPrefix);
- }
- /**
- * @param string $type
- * @return string
- */
- protected function getRefreshPrefix(string $type = ""): string
- {
- $config = $this->getConfig($type);
- return str_replace("[appid]",$config['appid'],$this->refreshPrefix);
- }
- }
|