Pārlūkot izejas kodu

头条代理商 token

zhaoxian 1 gadu atpakaļ
vecāks
revīzija
13781fa6a7

+ 65 - 0
module-common/src/main/java/cn/com/ctop/common/module/entity/BytedanceAgentToken.java

@@ -0,0 +1,65 @@
+package cn.com.ctop.common.module.entity;
+
+import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.annotation.TableName;
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+import lombok.EqualsAndHashCode;
+import lombok.experimental.Accessors;
+
+import java.util.Date;
+
+/**
+ * 代理商token
+ *
+ * @author jeecg-boot
+ * @version V1.0
+ * @date 2020-08-18
+ */
+@Data
+public class BytedanceAgentToken {
+
+    /**
+     * 主键ID
+     */
+    @TableId(type = IdType.UUID)
+    @ApiModelProperty(value = "主键ID")
+    private String id;
+
+    private String agentId;
+    private String agentName;
+    /**
+     * 访问令牌
+     */
+    private String accessToken;
+    /**
+     * 访问令牌有效期
+     */
+    private Date accessTokenExpiresIn;
+    /**
+     * 刷新令牌
+     */
+    private String refreshToken;
+    /**
+     * 刷新令牌有效期
+     */
+    private Date refreshTokenExpiresIn;
+    /**
+     * 代理商类型 0:三方代理商 1:本代理商
+     */
+    private Integer agentType;
+
+
+    private Long appId;
+
+    /**
+     * 创建时间
+     */
+    private Date createTime;
+    /**
+     * 修改时间
+     */
+    private Date updateTime;
+}

+ 8 - 0
module-common/src/main/java/cn/com/ctop/common/module/mapper/CtopOauthTokenMapper.java

@@ -1,6 +1,8 @@
 package cn.com.ctop.common.module.mapper;
 
+import cn.com.ctop.common.module.entity.BytedanceAgentToken;
 import cn.com.ctop.common.module.entity.CtopOauthToken;
+import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 import org.apache.ibatis.annotations.Param;
 
@@ -61,4 +63,10 @@ public interface CtopOauthTokenMapper extends BaseMapper<CtopOauthToken> {
     List<CtopOauthToken> queryTtValidRuleAccountsToken();
 
     List<CtopOauthToken> selectTokensByProjectId(Long projectId);
+
+    List<JSONObject> queryBytedanceAgentTokens();
+
+    JSONObject queryBytedanceAgentTokenByAccountId(@Param("agentId") Long agentId);
+
+    void replace(@Param("data") BytedanceAgentToken data);
 }

+ 79 - 46
module-common/src/main/java/cn/com/ctop/common/module/mapper/xml/CtopOauthTokenMapper.xml

@@ -18,8 +18,9 @@
     </resultMap>
 
     <sql id="Base_Column_List">
-        id,media_id,advertiser_id,account_id,access_token,refresh_token,access_token_expires_in,refresh_token_expires_in,agent_type,app_id,create_time,update_time
-   </sql>
+        id
+        ,media_id,advertiser_id,account_id,access_token,refresh_token,access_token_expires_in,refresh_token_expires_in,agent_type,app_id,create_time,update_time
+    </sql>
 
     <select id="selectByteDanceTokens" resultMap="BaseResultMap">
         select
@@ -89,73 +90,66 @@
 
 
     <select id="selectKuaiShouToken" resultType="cn.com.ctop.common.module.entity.CtopOauthToken">
-    SELECT
-	t1.id AS 'id',
-	t1.media_id AS 'mediaId',
-	t1.advertiser_id AS 'advertiser_id',
-	t1.account_id AS 'accountId',
-	( CASE WHEN t3.access_token IS NOT NULL THEN t3.access_token ELSE t1.access_token END ) AS 'accessToken',
-	( CASE WHEN t3.refresh_token IS NOT NULL THEN t3.refresh_token ELSE t1.refresh_token END ) AS 'refreshToken',
-	t1.agent_type AS 'agentType',
-	t1.app_id AS 'appId',
-	t1.access_token_expires_in as 'accessTokenExpiresIn',
-	t1.refresh_token_expires_in as 'refreshTokenExpiresIn',
-	t1.create_time as 'createTime',
-	t1.update_time as 'updateTime'
-	FROM
-	ctop_oauth_token t1
-	LEFT JOIN ctop_user_allocation t2 ON t1.account_id = t2.account_id
-	LEFT JOIN ctop_oauth_agent_token t3 ON t1.app_id = t3.app_id
-    WHERE
-	t2.account_status = 0
-	AND t1.media_id = 2;
+        SELECT t1.id AS 'id', t1.media_id AS 'mediaId', t1.advertiser_id AS 'advertiser_id', t1.account_id AS 'accountId', (CASE
+                                                                                                                                WHEN t3.access_token IS NOT NULL
+                                                                                                                                    THEN t3.access_token
+                                                                                                                                ELSE t1.access_token END) AS 'accessToken', (CASE
+                                                                                                                                                                                 WHEN t3.refresh_token IS NOT NULL
+                                                                                                                                                                                     THEN t3.refresh_token
+                                                                                                                                                                                 ELSE t1.refresh_token END) AS 'refreshToken', t1.agent_type AS 'agentType', t1.app_id AS 'appId', t1.access_token_expires_in as 'accessTokenExpiresIn', t1.refresh_token_expires_in as 'refreshTokenExpiresIn', t1.create_time as 'createTime', t1.update_time as 'updateTime'
+        FROM ctop_oauth_token t1
+                 LEFT JOIN ctop_user_allocation t2 ON t1.account_id = t2.account_id
+                 LEFT JOIN ctop_oauth_agent_token t3 ON t1.app_id = t3.app_id
+        WHERE t2.account_status = 0
+          AND t1.media_id = 2;
 
     </select>
 
     <select id="selectKuaiShouTokenBefore" resultType="cn.com.ctop.common.module.entity.CtopOauthToken">
-        select  t1.*
+        select t1.*
         from ctop_oauth_token t1
-        left join ctop_user_allocation t2
-        on t1.account_id = t2.account_id
+                 left join ctop_user_allocation t2
+                           on t1.account_id = t2.account_id
         where t1.media_id = 2
-        and date_format(t2.create_time, '%Y-%m-%d') = date_sub(curdate(),interval 1 day)
+          and date_format(t2.create_time, '%Y-%m-%d') = date_sub(curdate(), interval 1 day)
     </select>
 
     <select id="selectToutiaoToken" resultType="cn.com.ctop.common.module.entity.CtopOauthToken">
         select t1.*
         from ctop_oauth_token t1
-        left join ctop_user_allocation t2
-        on t1.account_id = t2.account_id
-        where t1.media_id in (1,3) and t2.account_status = 0
+                 left join ctop_user_allocation t2
+                           on t1.account_id = t2.account_id
+        where t1.media_id in (1, 3)
+          and t2.account_status = 0
         group by account_id
     </select>
 
     <select id="getAccessTokenByAccountIdAndMediaId" resultType="cn.com.ctop.common.module.entity.CtopOauthToken">
-        select
-        *
-        from
-        ctop_oauth_token
+        select *
+        from ctop_oauth_token
         where media_id = #{mediaId}
-        and account_id = #{accountId}
+          and account_id = #{accountId}
     </select>
 
     <!-- 根据mediaType 和 账户绑定助手时间createTime 获取accountId -->
     <select id="getToutiaoTokenByCreateTime" resultType="cn.com.ctop.common.module.entity.CtopOauthToken">
-            select t1.*
+        select t1.*
         from ctop_oauth_token t1
-        left join ctop_user_allocation t2
-        on t1.account_id = t2.account_id
+                 left join ctop_user_allocation t2
+                           on t1.account_id = t2.account_id
         where t2.account_status = 0
-        and t1.media_id in (1,3)
-        and date_format(t2.create_time, '%Y-%m-%d') = #{createTime}
+          and t1.media_id in (1, 3)
+          and date_format(t2.create_time, '%Y-%m-%d') = #{createTime}
         group by account_id
     </select>
 
     <select id="queryKsValidRuleAccountsToken" resultType="cn.com.ctop.common.module.entity.CtopOauthToken">
-        SELECT * FROM ctop_oauth_token
+        SELECT *
+        FROM ctop_oauth_token
         WHERE account_id IN
-              (SELECT t2.account_id FROM ctop_rule_template t1
-                                             INNER JOIN ctop_rule_account_template t2 ON t1.id = t2.template_id
+              (SELECT t2.account_id
+               FROM ctop_rule_template t1
+                        INNER JOIN ctop_rule_account_template t2 ON t1.id = t2.template_id
                WHERE t1.media_type = 2
                  AND t2.template_status = 0
                GROUP BY t2.account_id
@@ -163,11 +157,13 @@
     </select>
 
     <select id="queryTtValidRuleAccountsToken" resultType="cn.com.ctop.common.module.entity.CtopOauthToken">
-        SELECT * FROM ctop_oauth_token
+        SELECT *
+        FROM ctop_oauth_token
         WHERE account_id IN
-              (SELECT t2.account_id FROM ctop_rule_template t1
-                                             INNER JOIN ctop_rule_account_template t2 ON t1.id = t2.template_id
-               WHERE t1.media_type IN (1,3)
+              (SELECT t2.account_id
+               FROM ctop_rule_template t1
+                        INNER JOIN ctop_rule_account_template t2 ON t1.id = t2.template_id
+               WHERE t1.media_type IN (1, 3)
                  AND t2.template_status = 0
                GROUP BY t2.account_id
               )
@@ -183,4 +179,41 @@
         group by account_id
     </select>
 
+    <select id="queryBytedanceAgentTokens" resultType="com.alibaba.fastjson.JSONObject">
+        select *
+        FROM ctop_bytedance_agent_token
+    </select>
+
+    <select id="queryBytedanceAgentTokenByAccountId" resultType="com.alibaba.fastjson.JSONObject">
+        SELECT *
+        FROM ctop_bytedance_agent_token
+        WHERE agent_id = #{agentId}
+    </select>
+
+    <insert id="replace">
+        replace
+        into ctop_bytedance_agent_token(
+        id,
+        agent_id,
+        agent_name,
+        access_token,
+        access_token_expires_in,
+        refresh_token,
+        refresh_token_expires_in,
+        agent_type,
+        app_id )
+        values
+            (
+        #{data.id},
+        #{data.agentId},
+        #{data.agentName},
+        #{data.accessToken},
+        #{data.accessTokenExpiresIn},
+        #{data.refreshToken},
+        #{data.refreshTokenExpiresIn},
+        #{data.agentType},
+        #{data.appId}
+        )
+    </insert>
+
 </mapper>

+ 3 - 0
module-common/src/main/java/cn/com/ctop/common/module/service/ICtopOauthTokenService.java

@@ -1,6 +1,7 @@
 package cn.com.ctop.common.module.service;
 
 import cn.com.ctop.common.module.entity.CtopOauthToken;
+import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.extension.service.IService;
 
 import java.util.List;
@@ -51,4 +52,6 @@ public interface ICtopOauthTokenService extends IService<CtopOauthToken> {
     List<CtopOauthToken> queryTtValidRuleAccountsToken();
 
     void getLiveKuaiShouAgentRefreshToken(String id, String accessToken, String refreshToken, Long appId, String secret);
+
+    void refreshAgentBytedanceToken(JSONObject token);
 }

+ 2 - 0
module-common/src/main/java/cn/com/ctop/common/module/service/IRefreshTokenService.java

@@ -8,4 +8,6 @@ public interface IRefreshTokenService {
     void getKuaiShouAgentRefresh();
 
     void getLiveKuaiShouAgentRefresh();
+
+    void bytedanceRefreshAgentToken(String accountId);
 }

+ 47 - 2
module-common/src/main/java/cn/com/ctop/common/module/service/impl/CtopOauthTokenServiceImpl.java

@@ -1,5 +1,6 @@
 package cn.com.ctop.common.module.service.impl;
 
+import cn.com.ctop.common.module.entity.BytedanceAgentToken;
 import cn.com.ctop.common.module.entity.CtopOauthToken;
 import cn.com.ctop.common.module.entity.OauthAgentToken;
 import cn.com.ctop.common.module.mapper.CtopOauthTokenMapper;
@@ -18,7 +19,11 @@ import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 
 import javax.annotation.Resource;
-import java.util.*;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
 
 /**
  * @Description: oauth授权码
@@ -321,7 +326,7 @@ public class CtopOauthTokenServiceImpl extends ServiceImpl<CtopOauthTokenMapper,
 
 
     @Override
-    public void getLiveKuaiShouAgentRefreshToken(String id , String accessToken, String refreshToken, Long appId, String secret) {
+    public void getLiveKuaiShouAgentRefreshToken(String id, String accessToken, String refreshToken, Long appId, String secret) {
         log.info("快手刷新token入参,token:{},refreshToken:{}", accessToken, refreshToken);
         String url = kuaishouApiUrl + "/rest/openapi/oauth2/authorize/refresh_token";
         Map<String, String> headers = new HashMap<>();
@@ -376,6 +381,46 @@ public class CtopOauthTokenServiceImpl extends ServiceImpl<CtopOauthTokenMapper,
 
     }
 
+    /**
+     * 刷新代理商token
+     *
+     * @return
+     */
+    @Override
+    public void refreshAgentBytedanceToken(JSONObject token) {
+        Map<String, Object> resultMap = new HashMap<>();
+        String url = bytedanceUrlApiPrefix + bytedanceUrlRefreshToken;
+        Map<String, Object> param = new HashMap<>();
+        param.put("app_id", bytedanceAppId);
+        param.put("secret", bytedanceSecret);
+        param.put("grant_type", "refresh_token");
+        param.put("refresh_token", token.getString("refresh_token"));
+        String result = HttpUtils.httpPostRequest(url, param, new HashMap<>());
+        if (!Check.isNull(result)) {
+            JSONObject resultObject = JSONObject.parseObject(result);
+            Integer code = resultObject.getInteger("code");
+            String message = resultObject.getString("message");
+            if (code == 0) {
+                JSONObject data = resultObject.getJSONObject("data");
+                long accessTokenExpireIn = data.getLong("expires_in") * 1000L;
+                long now = System.currentTimeMillis();
+                Date accessTokenExpireInDate = new Date(now + accessTokenExpireIn);
+                data.put("access_token_expires_in", accessTokenExpireInDate);
+                long refreshTokenExpireIn = data.getLong("refresh_token_expires_in") * 1000L;
+                Date refreshTokenExpireInDate = new Date(now + refreshTokenExpireIn);
+                data.put("refresh_token_expires_in", accessTokenExpireInDate);
+                data.put("id", token.getString("id"));
+                data.put("agent_id", token.getString("agent_id"));
+                data.put("agent_name", token.getString("agent_name"));
+                data.put("agent_type", token.getString("agent_type"));
+                data.put("app_id", token.getString("app_id"));
+                BytedanceAgentToken tokenInfo = JSONObject.parseObject(data.toJSONString(), BytedanceAgentToken.class);
+                ctopOauthTokenMapper.replace(tokenInfo);
+
+            }
+        }
+    }
+
     @Override
     public List<CtopOauthToken> queryKsValidRuleAccountsToken() {
         return ctopOauthTokenMapper.queryKsValidRuleAccountsToken();

+ 23 - 2
module-common/src/main/java/cn/com/ctop/common/module/service/impl/RefreshTokenServiceImpl.java

@@ -9,6 +9,7 @@ import cn.com.ctop.common.module.service.IOauthConfigService;
 import cn.com.ctop.common.module.service.IRefreshTokenService;
 import cn.com.ctop.common.module.utils.Check;
 import cn.com.ctop.common.module.utils.CtopAdConstant;
+import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.xxl.job.core.context.XxlJobHelper;
 import lombok.extern.slf4j.Slf4j;
@@ -81,6 +82,26 @@ public class RefreshTokenServiceImpl implements IRefreshTokenService {
         }
     }
 
+
+    @Override
+    public void bytedanceRefreshAgentToken(String agentId) {
+        if (Check.isNull(agentId)) {
+            List<JSONObject> tokens = oauthTokenMapper.queryBytedanceAgentTokens();
+            if (!Check.isNull(tokens)) {
+                for (JSONObject token : tokens) {
+                    oauthTokenService.refreshAgentBytedanceToken(token);
+                }
+            }
+        } else {
+            JSONObject token = oauthTokenMapper.queryBytedanceAgentTokenByAccountId(Long.parseLong(agentId));
+            if (token == null) {
+                XxlJobHelper.log("未获取到代理商账户数据,accountId:{}", agentId);
+                return;
+            }
+            oauthTokenService.refreshAgentBytedanceToken(token);
+        }
+    }
+
     @Override
     public void getKuaiShouAgentRefresh() {
         QueryWrapper<OauthAgentToken> agentTokenQueryWrapper = new QueryWrapper<>();
@@ -88,7 +109,7 @@ public class RefreshTokenServiceImpl implements IRefreshTokenService {
         List<OauthAgentToken> list = agentTokenService.list(agentTokenQueryWrapper);
         if (!Check.isNull(list)) {
             for (OauthAgentToken agentToken : list) {
-                oauthTokenService.getKuaiShouAgentRefreshToken(agentToken.getId(),agentToken.getAgentId(), agentToken.getAccessToken(), agentToken.getRefreshToken(),agentToken.getAppId(),agentToken.getSecret());
+                oauthTokenService.getKuaiShouAgentRefreshToken(agentToken.getId(), agentToken.getAgentId(), agentToken.getAccessToken(), agentToken.getRefreshToken(), agentToken.getAppId(), agentToken.getSecret());
             }
         }
     }
@@ -104,7 +125,7 @@ public class RefreshTokenServiceImpl implements IRefreshTokenService {
         List<OauthAgentToken> list = agentTokenService.getLiveKuaiShouAgentList(2);
         if (!Check.isNull(list)) {
             for (OauthAgentToken agentToken : list) {
-                oauthTokenService.getLiveKuaiShouAgentRefreshToken(agentToken.getId(), agentToken.getAccessToken(), agentToken.getRefreshToken(),agentToken.getAppId(),agentToken.getSecret());
+                oauthTokenService.getLiveKuaiShouAgentRefreshToken(agentToken.getId(), agentToken.getAccessToken(), agentToken.getRefreshToken(), agentToken.getAppId(), agentToken.getSecret());
             }
         }
     }

+ 10 - 0
module-job-bytedance/src/main/java/cn/com/ctop/job/bytedance/handler/ByteDanceRefreshTokenJob.java

@@ -26,6 +26,16 @@ public class ByteDanceRefreshTokenJob {
         refreshTokenService.byteDanceRefresh(null);
     }
 
+
+    /**
+     * 刷新代理商token
+     * @throws Exception
+     */
+    @XxlJob("bytedanceRefreshAgentTokenJob")
+    public void bytedanceRefreshAgentTokenJob() throws Exception {
+        refreshTokenService.bytedanceRefreshAgentToken(null);
+    }
+
     @Autowired
     private IUserAllocationService userAllocationService;