|
|
@@ -1,6 +1,5 @@
|
|
|
package com.fs.newAdv.integration.client.advertiser;
|
|
|
|
|
|
-import cn.hutool.core.util.StrUtil;
|
|
|
import cn.hutool.http.HttpRequest;
|
|
|
import cn.hutool.http.HttpResponse;
|
|
|
import cn.hutool.json.JSONArray;
|
|
|
@@ -9,7 +8,6 @@ import cn.hutool.json.JSONUtil;
|
|
|
import com.baidu.dev2.thirdparty.jackson.core.JsonProcessingException;
|
|
|
import com.baidu.dev2.thirdparty.jackson.databind.ObjectMapper;
|
|
|
import com.fs.common.constant.SystemConstant;
|
|
|
-import com.fs.common.exception.ThirdPartyException;
|
|
|
import com.fs.newAdv.domain.PromotionAccount;
|
|
|
import com.fs.newAdv.domain.SiteStatistics;
|
|
|
import com.fs.newAdv.enums.AdvertiserTypeEnum;
|
|
|
@@ -22,9 +20,11 @@ import org.apache.http.client.utils.URIBuilder;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
-import java.net.URISyntaxException;
|
|
|
-import java.net.URL;
|
|
|
-import java.util.*;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
|
|
|
/**
|
|
|
* 巨量引擎API客户端
|
|
|
@@ -87,65 +87,24 @@ public class OceanEngineApiClient extends AbstractApiClient implements IAccessTo
|
|
|
return params;
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 构建上下文信息
|
|
|
- *
|
|
|
- * @param conversionData 转化数据
|
|
|
- * @return 上下文Map
|
|
|
- */
|
|
|
- private Map<String, Object> buildContext(Map<String, Object> conversionData) {
|
|
|
- Map<String, Object> context = new HashMap<>();
|
|
|
-
|
|
|
- // 必填:点击ID
|
|
|
- context.put("ad", buildAdContext(conversionData));
|
|
|
-
|
|
|
- // 用户信息
|
|
|
- if (conversionData.containsKey("userId")) {
|
|
|
- Map<String, Object> user = new HashMap<>();
|
|
|
- user.put("user_id", conversionData.get("userId"));
|
|
|
- context.put("user", user);
|
|
|
- }
|
|
|
-
|
|
|
- return context;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 构建广告上下文
|
|
|
- *
|
|
|
- * @param conversionData 转化数据
|
|
|
- * @return 广告上下文
|
|
|
- */
|
|
|
- private Map<String, Object> buildAdContext(Map<String, Object> conversionData) {
|
|
|
- Map<String, Object> ad = new HashMap<>();
|
|
|
-
|
|
|
- // 点击ID(必填)
|
|
|
- String clickId = (String) conversionData.get("traceId");
|
|
|
- if (StrUtil.isBlank(clickId)) {
|
|
|
- throw new ThirdPartyException("点击ID不能为空");
|
|
|
- }
|
|
|
- ad.put("callback", clickId);
|
|
|
-
|
|
|
- return ad;
|
|
|
- }
|
|
|
-
|
|
|
@Override
|
|
|
public AdvertiserTypeEnum getAdvertiserType() {
|
|
|
return AdvertiserTypeEnum.OCEANENGINE;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public SiteStatistics getDataReport(PromotionAccount account,String ideaId, String startDate, String endDate) {
|
|
|
+ public SiteStatistics getDataReport(PromotionAccount account, String ideaId, String startDate, String endDate) {
|
|
|
// 构建请求参数
|
|
|
Map<String, Object> map = new HashMap<>();
|
|
|
map.put("advertiser_id", Long.valueOf(account.getAdAccountId()));
|
|
|
// 纬度--天
|
|
|
map.put("dimensions", Arrays.asList("stat_time_day"));
|
|
|
// 过滤条件
|
|
|
- Map<String,Object> filters = new HashMap<>();
|
|
|
- filters.put("field","cdp_promotion_id");
|
|
|
- filters.put("type",2);
|
|
|
- filters.put("operator",1);
|
|
|
- filters.put("values ",Arrays.asList(ideaId));
|
|
|
+ Map<String, Object> filters = new HashMap<>();
|
|
|
+ filters.put("field", "cdp_promotion_id");
|
|
|
+ filters.put("type", 2);
|
|
|
+ filters.put("operator", 1);
|
|
|
+ filters.put("values", Arrays.asList(ideaId));
|
|
|
map.put("filters", Arrays.asList(filters));
|
|
|
/**
|
|
|
* stat_cost 消耗(元)
|
|
|
@@ -180,20 +139,10 @@ public class OceanEngineApiClient extends AbstractApiClient implements IAccessTo
|
|
|
throw new RuntimeException(e);
|
|
|
}
|
|
|
HttpResponse execute = HttpRequest.get(url)
|
|
|
- .header("Access-Token", account.getAccessToken())
|
|
|
+ .header("Access-Token", getAccessToken(account.getId()))
|
|
|
.timeout(SystemConstant.API_TIMEOUT)
|
|
|
.execute();
|
|
|
JSONObject jsonObject = JSONUtil.parseObj(execute.body());
|
|
|
- if (jsonObject.getInt("code") != 0) {
|
|
|
- // 刷新token重新请求
|
|
|
- log.info("巨量刷新token重新请求");
|
|
|
- AccessTokenVo accessTokenVo = refreshAccessToken(account.getAppId(), account.getAppSecret(), account.getRefreshToken());
|
|
|
- execute = HttpRequest.get(url)
|
|
|
- .header("Access-Token", accessTokenVo.getAccessToken())
|
|
|
- .timeout(SystemConstant.API_TIMEOUT)
|
|
|
- .execute();
|
|
|
- jsonObject = JSONUtil.parseObj(execute.body());
|
|
|
- }
|
|
|
JSONObject data = jsonObject.getJSONObject("data");
|
|
|
JSONArray rows = data.getJSONArray("rows");
|
|
|
JSONObject jsonObject1 = rows.getJSONObject(0);
|
|
|
@@ -208,55 +157,48 @@ public class OceanEngineApiClient extends AbstractApiClient implements IAccessTo
|
|
|
return siteStatistics;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
@Override
|
|
|
- public AccessTokenVo refreshAccessToken(String appId, String appSecret, String refreshToken) {
|
|
|
- Map<String, Object> map = new HashMap<>();
|
|
|
- map.put("app_id", appId);
|
|
|
- map.put("secret", appSecret);
|
|
|
- map.put("refresh_token", refreshToken);
|
|
|
- HttpResponse response = HttpRequest.post(ACCESS_TOKEN_URL)
|
|
|
- .header("Content-Type", "application/json")
|
|
|
- .form(JSONUtil.toJsonStr(map))
|
|
|
- .timeout(SystemConstant.API_TIMEOUT)
|
|
|
- .execute();
|
|
|
- JSONObject res = new JSONObject(response.body());
|
|
|
- int code = (int) res.get("code");
|
|
|
- if (code == 0) {
|
|
|
- JSONObject data = res.getJSONObject("data");
|
|
|
- return AccessTokenVo.builder()
|
|
|
- .accessToken((String) data.get("access_token"))
|
|
|
- .refreshToken((String) data.get("refresh_token"))
|
|
|
- .build();
|
|
|
- }
|
|
|
- return null;
|
|
|
+ public AccessTokenVo refreshAccessToken(PromotionAccount promotionAccount) {
|
|
|
+ return executeToken(this::getAccessTokenVo, () -> {
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ map.put("app_id", promotionAccount.getAppId());
|
|
|
+ map.put("secret", promotionAccount.getAppSecret());
|
|
|
+ map.put("refresh_token", promotionAccount.getRefreshToken());
|
|
|
+ // 发送HTTP请求
|
|
|
+ return HttpRequest.post(REFRESH_TOKEN_URL)
|
|
|
+ .header("Content-Type", "application/json")
|
|
|
+ .body(JSONUtil.toJsonStr(map))
|
|
|
+ .timeout(SystemConstant.API_TIMEOUT)
|
|
|
+ .execute();
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
|
|
|
@Override
|
|
|
public AccessTokenVo getAccessTokenByAuthCode(AccessTokenByAuthCodeVo request) {
|
|
|
- Map<String, Object> map = new HashMap<>();
|
|
|
- map.put("app_id", request.getAppId());
|
|
|
- map.put("secret", request.getAppSecret());
|
|
|
- map.put("auth_code", request.getAuthCode());
|
|
|
- HttpResponse response = HttpRequest.post(ACCESS_TOKEN_URL)
|
|
|
- .header("Content-Type", "application/json")
|
|
|
- .body(JSONUtil.toJsonStr(map))
|
|
|
- .timeout(SystemConstant.API_TIMEOUT)
|
|
|
- .execute();
|
|
|
- String body = response.body();
|
|
|
- log.info("巨量获取token数据{}", body);
|
|
|
- JSONObject res = new JSONObject(response.body());
|
|
|
- int code = (int) res.get("code");
|
|
|
- if (code == 0) {
|
|
|
- JSONObject data = res.getJSONObject("data");
|
|
|
- return AccessTokenVo.builder()
|
|
|
- .accessToken((String) data.get("access_token"))
|
|
|
- .refreshToken((String) data.get("refresh_token"))
|
|
|
- .build();
|
|
|
- }
|
|
|
- return null;
|
|
|
+ return executeToken(this::getAccessTokenVo, () -> {
|
|
|
+ // 调用接口换取授权令牌
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ map.put("app_id", request.getAppId());
|
|
|
+ map.put("secret", request.getAppSecret());
|
|
|
+ map.put("auth_code", request.getAuthCode());
|
|
|
+ // 发送HTTP请求
|
|
|
+ return HttpRequest.post(ACCESS_TOKEN_URL)
|
|
|
+ .header("Content-Type", "application/json")
|
|
|
+ .body(JSONUtil.toJsonStr(map))
|
|
|
+ .timeout(SystemConstant.API_TIMEOUT)
|
|
|
+ .execute();
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+ public AccessTokenVo getAccessTokenVo(JSONObject res) {
|
|
|
+ JSONObject data = res.getJSONObject("data");
|
|
|
+ return AccessTokenVo.builder()
|
|
|
+ .accessToken((String) data.get("access_token"))
|
|
|
+ .refreshToken((String) data.get("refresh_token"))
|
|
|
+ .expireTime(LocalDateTime.now().plusSeconds(data.getLong("refresh_token_expires_in")))
|
|
|
+ .build();
|
|
|
+ }
|
|
|
}
|
|
|
|