Browse Source

修复冲突

chenlei1231 3 months ago
parent
commit
4e316afa32

BIN
conf/adweb_v3.sql.zip


+ 14 - 1
jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/adweb/api/controller/OpenAPIController.java

@@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import jakarta.annotation.Resource;
 import jakarta.servlet.http.HttpServletRequest;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
 import org.jeecg.common.api.vo.Result;
 import org.jeecg.config.shiro.IgnoreAuth;
 import org.jeecg.modules.adweb.api.vo.APIRequestVO;
@@ -11,6 +12,8 @@ import org.jeecg.modules.adweb.api.vo.ProductInfoVO;
 import org.jeecg.modules.adweb.common.mapper.CommonMapper;
 import org.jeecg.modules.adweb.site.entity.AdwebSite;
 import org.jeecg.modules.adweb.site.service.IAdwebSiteService;
+import org.springframework.http.HttpHeaders;
+import org.springframework.security.oauth2.server.resource.InvalidBearerTokenException;
 import org.springframework.web.bind.annotation.*;
 
 import java.util.*;
@@ -30,7 +33,10 @@ public class OpenAPIController {
 
     @PostMapping("/product/list")
     @ResponseBody
-    public List<ProductInfoVO> listProducts(@RequestParam APIRequestVO apiReques) {
+    public List<ProductInfoVO> listProducts(
+            @RequestHeader(value = HttpHeaders.AUTHORIZATION, required = true) String authToken,
+            @RequestParam APIRequestVO apiRequest) {
+        this.validateAuthToken(authToken);
 
         return Collections.EMPTY_LIST;
     }
@@ -67,4 +73,11 @@ public class OpenAPIController {
         result.put("wordpress_setting", adwebsite.getWordpressSetting()); // 指定wordpress 传输的产品类型是来自adwebpro还是woocommerce
         return Result.ok(result);
     }
+
+    private boolean validateAuthToken(String authToken) {
+        if (!StringUtils.equals(STATIC_BEARER_TOKEN, authToken)) {
+            throw new InvalidBearerTokenException("Bearer toke is invalid: " + authToken);
+        }
+        return true;
+    }
 }

+ 2 - 0
jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/adweb/marketing/googleads/dto/ReportRequestDTO.java

@@ -10,6 +10,8 @@ public class ReportRequestDTO {
 
     private String customerId;
 
+    private String refreshToken;
+
     private String startDate;
 
     private String endDate;

+ 45 - 50
jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/adweb/marketing/googleads/service/GoogleAdsReportService.java

@@ -72,16 +72,17 @@ public class GoogleAdsReportService {
             cacheNames =
                     "googleads:getCustomerStats"
                             + TTLCacheManager.TTL_SPLITTER
-                            + 60 * 10) // Redis TTL为10分钟
+                            + 60 * 5) // Redis TTL为5分钟
     public CustomerStatsVO getCustomerStats(String siteCode) {
-        String customerId = this.getCustomerId(siteCode);
-        if (StringUtils.isEmpty(customerId)) {
+        GoogleAds googleAds = this.getAdsAccount(siteCode);
+        if (StringUtils.isBlank(googleAds.getCustomerId())) {
             log.info("Google Ads帐号未配置, siteCode = {}", siteCode);
             return new CustomerStatsVO(); // @Cacheable方法不允许返回null,返回CustomerStatsVO默认值
         }
 
         // 1. 创建API request
-        OpenAPIRequest<String> openAPIRequest = this.buildAPIRequest(customerId);
+        OpenAPIRequest<ReportRequestDTO> openAPIRequest =
+                this.buildAPIRequest(googleAds, null, null, 1); // limit值无效
 
         // 2. 请求API接口
         OpenAPIResponse<CustomerStatsDTO> openAPIResponse =
@@ -101,10 +102,10 @@ public class GoogleAdsReportService {
             cacheNames =
                     "googleads:getDailyStats"
                             + TTLCacheManager.TTL_SPLITTER
-                            + 60 * 10) // Redis TTL为10分钟
+                            + 60 * 5) // Redis TTL为5分钟
     public List<DailyStatsVO> getDailyStats(String siteCode, Date start, Date end) {
-        String customerId = this.getCustomerId(siteCode);
-        if (StringUtils.isEmpty(customerId)) {
+        GoogleAds googleAds = this.getAdsAccount(siteCode);
+        if (StringUtils.isBlank(googleAds.getCustomerId())) {
             log.info("Google Ads帐号未配置, siteCode = {}", siteCode);
             return Collections.EMPTY_LIST;
         }
@@ -122,7 +123,7 @@ public class GoogleAdsReportService {
 
         // 1. 创建API request
         OpenAPIRequest<ReportRequestDTO> openAPIRequest =
-                this.buildAPIRequest(this.buildReportRequest(customerId, start, end, limit));
+                this.buildAPIRequest(googleAds, start, end, limit);
 
         // 2. 请求API接口
         OpenAPIResponse<Map<String, MetricsDTO>> openAPIResponse =
@@ -146,18 +147,17 @@ public class GoogleAdsReportService {
             cacheNames =
                     "googleads:getCampaignStats"
                             + TTLCacheManager.TTL_SPLITTER
-                            + 60 * 10) // Redis TTL为10分钟
+                            + 60 * 5) // Redis TTL为5分钟
     public List<CampaignStatsVO> getCampaignStats(String siteCode, Date start, Date end) {
-        String customerId = this.getCustomerId(siteCode);
-        if (StringUtils.isEmpty(customerId)) {
+        GoogleAds googleAds = this.getAdsAccount(siteCode);
+        if (StringUtils.isBlank(googleAds.getCustomerId())) {
             log.info("Google Ads帐号未配置, siteCode = {}", siteCode);
             return Collections.EMPTY_LIST;
         }
 
         // 1. 创建API request
         OpenAPIRequest<ReportRequestDTO> openAPIRequest =
-                this.buildAPIRequest(
-                        this.buildReportRequest(customerId, start, end, 50)); // 返回top 50
+                this.buildAPIRequest(googleAds, start, end, 50); // 返回top 50
 
         // 2. 请求API接口
         OpenAPIResponse<List<CampaignStatsDTO>> openAPIResponse =
@@ -181,18 +181,17 @@ public class GoogleAdsReportService {
             cacheNames =
                     "googleads:getKeywordStats"
                             + TTLCacheManager.TTL_SPLITTER
-                            + 60 * 10) // Redis TTL为10分钟
+                            + 60 * 5) // Redis TTL为5分钟
     public List<KeywordStatsVO> getKeywordStats(String siteCode, Date start, Date end) {
-        String customerId = this.getCustomerId(siteCode);
-        if (StringUtils.isEmpty(customerId)) {
+        GoogleAds googleAds = this.getAdsAccount(siteCode);
+        if (StringUtils.isBlank(googleAds.getCustomerId())) {
             log.info("Google Ads帐号未配置, siteCode = {}", siteCode);
             return Collections.EMPTY_LIST;
         }
 
         // 1. 创建API request
         OpenAPIRequest<ReportRequestDTO> openAPIRequest =
-                this.buildAPIRequest(
-                        this.buildReportRequest(customerId, start, end, 50)); // 返回top 50
+                this.buildAPIRequest(googleAds, start, end, 50); // 返回top 50
 
         // 2. 请求API接口
         OpenAPIResponse<Map<String, MetricsDTO>> openAPIResponse =
@@ -216,18 +215,17 @@ public class GoogleAdsReportService {
             cacheNames =
                     "googleads:getPlacementStats"
                             + TTLCacheManager.TTL_SPLITTER
-                            + 60 * 10) // Redis TTL为10分钟
+                            + 60 * 5) // Redis TTL为5分钟
     public List<PlacementStatsVO> getPlacementStats(String siteCode, Date start, Date end) {
-        String customerId = this.getCustomerId(siteCode);
-        if (StringUtils.isEmpty(customerId)) {
+        GoogleAds googleAds = this.getAdsAccount(siteCode);
+        if (StringUtils.isBlank(googleAds.getCustomerId())) {
             log.info("Google Ads帐号未配置, siteCode = {}", siteCode);
             return Collections.EMPTY_LIST;
         }
 
         // 1. 创建API request
         OpenAPIRequest<ReportRequestDTO> openAPIRequest =
-                this.buildAPIRequest(
-                        this.buildReportRequest(customerId, start, end, 50)); // 返回top 50
+                this.buildAPIRequest(googleAds, start, end, 50); // 返回top 50
 
         // 2. 请求API接口
         OpenAPIResponse<Map<String, MetricsDTO>> openAPIResponse =
@@ -251,18 +249,17 @@ public class GoogleAdsReportService {
             cacheNames =
                     "googleads:getCountryStats"
                             + TTLCacheManager.TTL_SPLITTER
-                            + 60 * 10) // Redis TTL为10分钟
+                            + 60 * 5) // Redis TTL为5分钟
     public List<CountryStatsVO> getCountryStats(String siteCode, Date start, Date end) {
-        String customerId = this.getCustomerId(siteCode);
-        if (StringUtils.isEmpty(customerId)) {
+        GoogleAds googleAds = this.getAdsAccount(siteCode);
+        if (StringUtils.isBlank(googleAds.getCustomerId())) {
             log.info("Google Ads帐号未配置, siteCode = {}", siteCode);
             return Collections.EMPTY_LIST;
         }
 
         // 1. 创建API request
         OpenAPIRequest<ReportRequestDTO> openAPIRequest =
-                this.buildAPIRequest(
-                        this.buildReportRequest(customerId, start, end, 50)); // 返回top 50
+                this.buildAPIRequest(googleAds, start, end, 50); // 返回top 50
 
         // 2. 请求API接口
         OpenAPIResponse<Map<String, MetricsDTO>> openAPIResponse =
@@ -282,41 +279,34 @@ public class GoogleAdsReportService {
     }
 
     /**
-     * 获取Google Ads customer ID - 放入Redis缓存,避免同一页面多个HTTP请求频繁DB查询
+     * 获取{@link GoogleAds}帐号信息 - 放入Redis缓存,避免同一页面多个HTTP请求频繁DB查询
      *
      * <p>使用AdwebRedisUtil操作 - @Cacheable注解无法嵌套使用
      */
-    private synchronized String getCustomerId(String siteCode) {
-        final String redisKey = String.format("%s:%s:%s", "googleads", "customerId", siteCode);
+    private synchronized GoogleAds getAdsAccount(String siteCode) {
+        final String redisKey = String.format("%s:account:%s", "googleads", siteCode);
 
-        String customerId = redisUtil.getString(redisKey);
-        if (StringUtils.isNotEmpty(customerId)) {
-            return customerId;
+        GoogleAds googleAds = (GoogleAds) redisUtil.get(redisKey);
+        if (Objects.nonNull(googleAds)) {
+            return googleAds;
         }
 
         // DB查询
-        GoogleAds googleAds =
+        googleAds =
                 googleAdsService.getOne(
                         new LambdaQueryWrapper<GoogleAds>().eq(GoogleAds::getSiteCode, siteCode),
                         false);
-        customerId = Objects.nonNull(googleAds) ? googleAds.getCustomerId() : "";
-        redisUtil.set(redisKey, customerId, 60); // Redis TTL为1分钟
-
-        return customerId;
-    }
+        googleAds = Objects.nonNull(googleAds) ? googleAds : new GoogleAds(); // Redis存储对象不允许为空
+        redisUtil.set(redisKey, googleAds, 30); // Redis TTL为30秒
 
-    private <T> OpenAPIRequest<T> buildAPIRequest(T data) {
-        OpenAPIRequest<T> apiRequest = new OpenAPIRequest<>();
-        apiRequest.setRequestServer(this.getClass().getSimpleName());
-        apiRequest.setRequestTime(System.currentTimeMillis());
-        apiRequest.setData(data);
-        return apiRequest;
+        return googleAds;
     }
 
-    private ReportRequestDTO buildReportRequest(
-            String customerId, Date start, Date end, int limit) {
+    private OpenAPIRequest<ReportRequestDTO> buildAPIRequest(
+            GoogleAds googleAds, Date start, Date end, int limit) {
         ReportRequestDTO reportRequest = new ReportRequestDTO();
-        reportRequest.setCustomerId(customerId);
+        reportRequest.setCustomerId(googleAds.getCustomerId());
+        reportRequest.setRefreshToken(googleAds.getRefreshToken());
         reportRequest.setStartDate(
                 DateUtil.formatDate(
                         Optional.ofNullable(start)
@@ -327,6 +317,11 @@ public class GoogleAdsReportService {
                 DateUtil.formatDate(
                         Optional.ofNullable(end).orElse(new Date()), DateUtil.DATE_FORMAT));
         reportRequest.setLimit(limit);
-        return reportRequest;
+
+        OpenAPIRequest<ReportRequestDTO> apiRequest = new OpenAPIRequest<>();
+        apiRequest.setRequestServer(this.getClass().getSimpleName());
+        apiRequest.setRequestTime(System.currentTimeMillis());
+        apiRequest.setData(reportRequest);
+        return apiRequest;
     }
 }