|
|
@@ -3,6 +3,8 @@ package com.fs.app.integration.client.advertiser;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
import cn.hutool.http.HttpRequest;
|
|
|
import cn.hutool.http.HttpResponse;
|
|
|
+import cn.hutool.json.JSONArray;
|
|
|
+import cn.hutool.json.JSONObject;
|
|
|
import cn.hutool.json.JSONUtil;
|
|
|
import com.fs.ad.yk.utils.Md5Util;
|
|
|
import com.fs.app.integration.client.AbstractApiClient;
|
|
|
@@ -11,17 +13,21 @@ import com.fs.baidu.utils.AESUtils;
|
|
|
import com.fs.common.constant.SystemConstant;
|
|
|
import com.fs.common.exception.ThirdPartyException;
|
|
|
import com.fs.newAdv.domain.PromotionAccount;
|
|
|
+import com.fs.newAdv.domain.Site;
|
|
|
+import com.fs.newAdv.domain.SiteStatistics;
|
|
|
import com.fs.newAdv.enums.AdvertiserTypeEnum;
|
|
|
import com.fs.newAdv.service.IPromotionAccountService;
|
|
|
import com.google.gson.Gson;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.json.JSONObject;
|
|
|
+
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import javax.crypto.SecretKey;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
+import java.math.BigDecimal;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
+import java.time.LocalDateTime;
|
|
|
import java.util.*;
|
|
|
|
|
|
/**
|
|
|
@@ -40,6 +46,7 @@ public class BaiduApiClient extends AbstractApiClient implements IAccessTokenCli
|
|
|
* 百度OCPC转化回传API地址
|
|
|
*/
|
|
|
private static final String CONVERSION_API_URL = "https://ocpc.baidu.com/ocpcapi/api/uploadConvertData";
|
|
|
+ private static final String REPORT_DATA_API_URL = "https://api.baidu.com/json/sms/service/OpenApiReportService/getReportData";
|
|
|
|
|
|
|
|
|
private static final String APPID = "appId";
|
|
|
@@ -129,10 +136,54 @@ public class BaiduApiClient extends AbstractApiClient implements IAccessTokenCli
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public Map<String, String> getAccessToken(Integer code, String appId, String appSecret, String callbackUrl) {
|
|
|
- return Collections.emptyMap();
|
|
|
+ public SiteStatistics getDataReport(PromotionAccount account, Site site) {
|
|
|
+ // 构建请求参数
|
|
|
+ Map<String,Object> map = new HashMap<>();
|
|
|
+ Map<String,Object> header = new HashMap<>();
|
|
|
+ header.put("accessToken", account.getAccessToken());
|
|
|
+ header.put("userName", site.getPromotionAccountName());
|
|
|
+ map.put("header",header);
|
|
|
+ Map<String,Object> body = new HashMap<>();
|
|
|
+ // 基础信息
|
|
|
+ //报告名称: 信息流整体账户报告
|
|
|
+ //reportType: 2172649
|
|
|
+ //支持的时间单位: HOUR DAY WEEK MONTH SUMMARY
|
|
|
+ //支持的最大时间区间: 824天
|
|
|
+ body.put("reportType", 2172649);
|
|
|
+ // 查询当天的数据
|
|
|
+ LocalDateTime yesterday = LocalDateTime.now().minusDays(1);
|
|
|
+ body.put("startDate", cn.hutool.core.date.DateUtil.format(yesterday, "yyyy-MM-dd"));
|
|
|
+ body.put("endDate", cn.hutool.core.date.DateUtil.format(LocalDateTime.now(), "yyyy-MM-dd"));
|
|
|
+ body.put("timeUnit", "DAY");
|
|
|
+ body.put("userIds", Collections.singletonList(account.getAdAccountId()));
|
|
|
+ // 基础指标
|
|
|
+ body.put("columns", Arrays.asList("impression", "click", "cost", "ctr", "cpc","cpm","phoneButtonClicks"));
|
|
|
+ body.put("startRow", 0);
|
|
|
+ body.put("rowCount", 1000);
|
|
|
+ map.put("body",body);
|
|
|
+
|
|
|
+ HttpResponse execute = HttpRequest.post(REPORT_DATA_API_URL)
|
|
|
+ .header("Content-Type", "application/json")
|
|
|
+ .body(JSONUtil.toJsonStr(map))
|
|
|
+ .timeout(SystemConstant.API_TIMEOUT)
|
|
|
+ .execute();
|
|
|
+ JSONObject jsonObject = JSONUtil.parseObj(execute.body());
|
|
|
+ JSONObject data = jsonObject.getJSONObject("body").getJSONObject("data");
|
|
|
+ JSONArray rows = data.getJSONArray("rows");
|
|
|
+ JSONObject jsonObject1 = rows.getJSONObject(0);
|
|
|
+ SiteStatistics siteStatistics = new SiteStatistics();
|
|
|
+ siteStatistics.setSiteId(site.getId());
|
|
|
+ siteStatistics.setImpressionCount(Long.valueOf(jsonObject1.getStr("impression")));
|
|
|
+ siteStatistics.setClickCount(Long.valueOf(jsonObject1.getStr("click")));
|
|
|
+ siteStatistics.setActualCost(new BigDecimal(jsonObject1.getStr("cost")));
|
|
|
+ siteStatistics.setAccountCost(new BigDecimal(jsonObject1.getStr("cost")));
|
|
|
+ siteStatistics.setClickRate(new BigDecimal(jsonObject1.getStr("ctr")));
|
|
|
+ siteStatistics.setAvgClickPrice(new BigDecimal(jsonObject1.getStr("cpc")));
|
|
|
+ return siteStatistics;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+
|
|
|
@Override
|
|
|
public Map<String, String> refreshAccessToken(String appId, String appSecret, String refreshToken) {
|
|
|
return Collections.emptyMap();
|
|
|
@@ -144,7 +195,7 @@ public class BaiduApiClient extends AbstractApiClient implements IAccessTokenCli
|
|
|
// 获取请求参数
|
|
|
Map<String, String> params = new HashMap<>();
|
|
|
this.fillParams(params, request);
|
|
|
- log.info("callback: params = {}", JSONObject.valueToString(params));
|
|
|
+ log.info("callback: params = {}", org.json.JSONObject.valueToString(params));
|
|
|
int userIdInt = 0;
|
|
|
try {
|
|
|
userIdInt = Integer.parseInt(params.get(USERID));
|
|
|
@@ -240,7 +291,7 @@ public class BaiduApiClient extends AbstractApiClient implements IAccessTokenCli
|
|
|
responseMap.put("code", code);
|
|
|
responseMap.put("message", message);
|
|
|
responseMap.put("data", data);
|
|
|
- return JSONObject.valueToString(responseMap);
|
|
|
+ return org.json.JSONObject.valueToString(responseMap);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -298,7 +349,7 @@ public class BaiduApiClient extends AbstractApiClient implements IAccessTokenCli
|
|
|
requestMap.put(AUTH_CODE, params.get(AUTH_CODE));
|
|
|
requestMap.put("grantType", "access_token");
|
|
|
requestMap.put("userId", userIdInt);
|
|
|
- String paramsJson = JSONObject.valueToString(requestMap);
|
|
|
+ String paramsJson = org.json.JSONObject.valueToString(requestMap);
|
|
|
|
|
|
HttpResponse execute = HttpRequest.post(ACCESSTOKEN_URL)
|
|
|
.header("Content-Type", "application/json")
|