|
|
@@ -0,0 +1,148 @@
|
|
|
+<?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);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|