|
|
@@ -13,18 +13,17 @@ 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.fs.newAdv.vo.AccessTokenByAuthCodeVo;
|
|
|
+import com.fs.newAdv.vo.AccessTokenVo;
|
|
|
import com.google.gson.Gson;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
-
|
|
|
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;
|
|
|
@@ -136,14 +135,14 @@ public class BaiduApiClient extends AbstractApiClient implements IAccessTokenCli
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public SiteStatistics getDataReport(PromotionAccount account, Site site) {
|
|
|
+ public SiteStatistics getDataReport(PromotionAccount account, String startDate, String endDate) {
|
|
|
// 构建请求参数
|
|
|
- Map<String,Object> map = new HashMap<>();
|
|
|
- Map<String,Object> header = new HashMap<>();
|
|
|
+ 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<>();
|
|
|
+ header.put("userName", "BDCC-yyt19");
|
|
|
+ map.put("header", header);
|
|
|
+ Map<String, Object> body = new HashMap<>();
|
|
|
// 基础信息
|
|
|
//报告名称: 信息流整体账户报告
|
|
|
//reportType: 2172649
|
|
|
@@ -152,15 +151,15 @@ public class BaiduApiClient extends AbstractApiClient implements IAccessTokenCli
|
|
|
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("startDate", startDate);
|
|
|
+ body.put("endDate", endDate);
|
|
|
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("columns", Arrays.asList("impression", "click", "cost", "ctr", "cpc", "cpm", "phoneButtonClicks"));
|
|
|
body.put("startRow", 0);
|
|
|
body.put("rowCount", 1000);
|
|
|
- map.put("body",body);
|
|
|
+ map.put("body", body);
|
|
|
|
|
|
HttpResponse execute = HttpRequest.post(REPORT_DATA_API_URL)
|
|
|
.header("Content-Type", "application/json")
|
|
|
@@ -168,13 +167,13 @@ public class BaiduApiClient extends AbstractApiClient implements IAccessTokenCli
|
|
|
.timeout(SystemConstant.API_TIMEOUT)
|
|
|
.execute();
|
|
|
JSONObject jsonObject = JSONUtil.parseObj(execute.body());
|
|
|
+ log.info("百度数据返回结果:{}", 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.setImpressionCount(Integer.valueOf(jsonObject1.getStr("impression")));
|
|
|
+ siteStatistics.setClickCount(Integer.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")));
|
|
|
@@ -183,48 +182,15 @@ public class BaiduApiClient extends AbstractApiClient implements IAccessTokenCli
|
|
|
}
|
|
|
|
|
|
|
|
|
-
|
|
|
@Override
|
|
|
public Map<String, String> refreshAccessToken(String appId, String appSecret, String refreshToken) {
|
|
|
return Collections.emptyMap();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public String getAccessTokenByAuthCode(HttpServletRequest request) {
|
|
|
-
|
|
|
- // 获取请求参数
|
|
|
- Map<String, String> params = new HashMap<>();
|
|
|
- this.fillParams(params, request);
|
|
|
- log.info("callback: params = {}", org.json.JSONObject.valueToString(params));
|
|
|
- int userIdInt = 0;
|
|
|
- try {
|
|
|
- userIdInt = Integer.parseInt(params.get(USERID));
|
|
|
- } catch (Exception e) {
|
|
|
- return this.getResponseJson(600011, "参数错误", new Object());
|
|
|
- }
|
|
|
- // 对签名内容进行判空
|
|
|
- if (StrUtil.isBlank(params.get(SIGNATURE))) {
|
|
|
- return this.getResponseJson(600011, "参数错误", new Object());
|
|
|
- }
|
|
|
-
|
|
|
- // 获取应用对应的密钥
|
|
|
- PromotionAccount app = promotionAccountService.getAppByAppId(params.get(APPID), AdvertiserTypeEnum.BAIDU);
|
|
|
- String sk = app.getAppSecret();
|
|
|
- // 开发者进行验签
|
|
|
- // 检查状态码
|
|
|
- if (!this.checkState(params.get(APPID), Long.valueOf(app.getAdAccountId()), params.get(STATE))) {
|
|
|
- log.info("callback: state check fail");
|
|
|
- return this.getResponseJson(600011, "状态码错误", new Object());
|
|
|
- }
|
|
|
-
|
|
|
- // 签名验证
|
|
|
- if (!this.checkSignature(params, sk)) {
|
|
|
- log.info("callback: signature check fail");
|
|
|
- return this.getResponseJson(600011, "签名错误", new Object());
|
|
|
- }
|
|
|
-
|
|
|
+ public AccessTokenVo getAccessTokenByAuthCode(AccessTokenByAuthCodeVo codeVo) {
|
|
|
// 调用接口换取授权令牌
|
|
|
- Map<String, Object> response = getBaiDuAccessToken(params, sk, userIdInt);
|
|
|
+ Map<String, Object> response = getBaiDuAccessToken(codeVo);
|
|
|
log.info("callback:getAccesstoken, response={}", response);
|
|
|
String accessToken = null;
|
|
|
String refreshToken = null;
|
|
|
@@ -236,16 +202,13 @@ public class BaiduApiClient extends AbstractApiClient implements IAccessTokenCli
|
|
|
JSONObject data = res.getJSONObject("data");
|
|
|
accessToken = (String) data.get("accessToken");
|
|
|
refreshToken = (String) data.get("refreshToken");
|
|
|
- app.setAccessToken(accessToken);
|
|
|
- app.setRefreshToken(refreshToken);
|
|
|
- promotionAccountService.updateById(app);
|
|
|
- } else {
|
|
|
- return this.getResponseJson(600011, "未获取到 access_token", new Object());
|
|
|
+ return AccessTokenVo.builder()
|
|
|
+ .accessToken(accessToken)
|
|
|
+ .refreshToken(refreshToken)
|
|
|
+ .build();
|
|
|
}
|
|
|
}
|
|
|
- Map<String, String> data = new HashMap<>();
|
|
|
- data.put("accessToken", accessToken);
|
|
|
- return this.getResponseJson(0, "success", data);
|
|
|
+ return null;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -254,13 +217,10 @@ public class BaiduApiClient extends AbstractApiClient implements IAccessTokenCli
|
|
|
* @param params
|
|
|
* @param request
|
|
|
*/
|
|
|
- private void fillParams(Map<String, String> params, HttpServletRequest request) {
|
|
|
- params.put(APPID, request.getParameter(APPID));
|
|
|
- params.put(AUTH_CODE, request.getParameter(AUTH_CODE));
|
|
|
- params.put(USERID, request.getParameter(USERID));
|
|
|
- params.put(TIMESTAMP, request.getParameter(TIMESTAMP));
|
|
|
- params.put(SIGNATURE, request.getParameter(SIGNATURE));
|
|
|
- params.put(STATE, request.getParameter(STATE));
|
|
|
+ private void fillParams(Map<String, String> params, AccessTokenByAuthCodeVo request) {
|
|
|
+ params.put(APPID, request.getAppId());
|
|
|
+ params.put(AUTH_CODE, request.getAuthCode());
|
|
|
+ params.put(USERID, request.getUserId());
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -337,19 +297,16 @@ public class BaiduApiClient extends AbstractApiClient implements IAccessTokenCli
|
|
|
/**
|
|
|
* 换取授权令牌
|
|
|
*
|
|
|
- * @param params
|
|
|
- * @param sk
|
|
|
- * @param userIdInt 授权账户ID
|
|
|
* @return
|
|
|
*/
|
|
|
- private Map<String, Object> getBaiDuAccessToken(Map<String, String> params, String sk, int userIdInt) {
|
|
|
+ private Map<String, Object> getBaiDuAccessToken(AccessTokenByAuthCodeVo codeVo) {
|
|
|
Map<String, Object> requestMap = new HashMap<>();
|
|
|
- requestMap.put(APPID, params.get(APPID));
|
|
|
- requestMap.put("secretKey", sk);
|
|
|
- requestMap.put(AUTH_CODE, params.get(AUTH_CODE));
|
|
|
+ requestMap.put(APPID, codeVo.getAppId());
|
|
|
+ requestMap.put("secretKey", codeVo.getAppSecret());
|
|
|
+ requestMap.put(AUTH_CODE, codeVo.getAuthCode());
|
|
|
requestMap.put("grantType", "access_token");
|
|
|
- requestMap.put("userId", userIdInt);
|
|
|
- String paramsJson = org.json.JSONObject.valueToString(requestMap);
|
|
|
+ requestMap.put("userId", codeVo.getUserId());
|
|
|
+ String paramsJson = JSONUtil.toJsonStr(requestMap);
|
|
|
|
|
|
HttpResponse execute = HttpRequest.post(ACCESSTOKEN_URL)
|
|
|
.header("Content-Type", "application/json")
|