Bläddra i källkod

定时任务 刷新token

yumeng 5 år sedan
förälder
incheckning
dd75a013fb

+ 2 - 31
jeecg-boot-module-system/src/main/java/org/jeecg/modules/ctop/controller/AuthController.java

@@ -83,36 +83,7 @@ public class AuthController {
     @GetMapping("bytedance/refreshtoken")
     @ResponseBody
     public Map<String, Object> refreshToken(String accountId) {
-        Map<String, Object> resultMap = new HashMap<>();
-        CTopOauthToken token = tokenService.getOAuthTokenByAccountId(accountId);
-        if (null == token) {
-            resultMap.put("message", "token信息不存在");
-            resultMap.put("code", -1);
-            return resultMap;
-        }
-        String url = PropertiesUtils.getValue("bytedance_config", "bytedance_api_url") + BytedanceInterfaceConstant.REFRESH_TOKEN;
-        Map<String, Object> param = new HashMap<String, Object>();
-        param.put("app_id", PropertiesUtils.getValue("bytedance_config", "bytedance_appid"));
-        param.put("secret", PropertiesUtils.getValue("bytedance_config", "bytedance_secret"));
-        param.put("grant_type", "refresh_token");
-        param.put("refresh_token", token.getRefreshToken());
-
-        String result = HttpUtils.httpPostRequest(url, param, new HashMap<>());
-        JSONObject resultObject = JSONObject.parseObject(result);
-        Integer code = resultObject.getInteger("status");
-        if (null == code || code != 0) {
-            logger.info("刷新token异常;accountId:{}", accountId);
-            resultMap.put("message", "刷新token异常");
-            resultMap.put("code", -1);
-            return resultMap;
-        }
-        JSONObject data = resultObject.getJSONObject("data");
-        CTopOauthToken refreshToken = new CTopOauthToken(token, data);
-        cTopOauthTokenMapper.deleteById(token.getId());
-        cTopOauthTokenMapper.insert(refreshToken);
-        resultMap.put("message", "token刷新成功");
-        resultMap.put("code", 0);
-        return resultMap;
+        return null;
     }
 
     @Autowired
@@ -145,7 +116,7 @@ public class AuthController {
                 cTopOauthTokenMapper.insert(token);
                 bindAccountService.addBindAccount(token.getAccountId(), KuaishouInterfaceConstant.TYPE_AUTHORIZATION, state, KuaishouInterfaceConstant.LOGIN_TYPE_BYTEDANCE); //账号绑定
             }
-        }catch (Exception e){
+        } catch (Exception e) {
             e.printStackTrace();
         }
         return result;

+ 1 - 1
jeecg-boot-module-system/src/main/java/org/jeecg/modules/ctop/entity/CTopOauthToken.java

@@ -119,7 +119,7 @@ public class CTopOauthToken {
 
     public CTopOauthToken(CTopOauthToken token, JSONObject data) {
         this.advertiserId = token.getAdvertiserId();
-        this.mediaId = "toutiao";
+        this.mediaId = "1";
         this.accountId = token.getAccountId();
         String accessToken = data.getString("access_token");
         Integer refreshTokenExpiresIn = data.getInteger("refresh_token_expires_in");

+ 23 - 0
jeecg-boot-module-system/src/main/java/org/jeecg/modules/ctop/job/RefreshTokenJob.java

@@ -0,0 +1,23 @@
+package org.jeecg.modules.ctop.job;
+
+import org.jeecg.modules.ctop.service.IRefreshTokenService;
+import org.quartz.Job;
+import org.quartz.JobExecutionContext;
+import org.quartz.JobExecutionException;
+import org.springframework.beans.factory.annotation.Autowired;
+
+public class RefreshTokenJob implements Job {
+    @Autowired
+    private IRefreshTokenService refreshTokenService;
+
+    /**
+     * 刷新token
+     *
+     * @param jobExecutionContext
+     * @throws JobExecutionException
+     */
+    @Override
+    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
+        refreshTokenService.getRefresh();
+    }
+}

+ 7 - 0
jeecg-boot-module-system/src/main/java/org/jeecg/modules/ctop/mapper/CTopOauthTokenMapper.java

@@ -3,6 +3,8 @@ package org.jeecg.modules.ctop.mapper;
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 import org.jeecg.modules.ctop.entity.CTopOauthToken;
 
+import java.util.List;
+
 /**
  * @Description: oauth授权码
  * @Author: jeecg-boot
@@ -10,4 +12,9 @@ import org.jeecg.modules.ctop.entity.CTopOauthToken;
  * @Version: V1.0
  */
 public interface CTopOauthTokenMapper extends BaseMapper<CTopOauthToken> {
+    List<CTopOauthToken> selectAll();
+
+    int updateByPrimaryKeySelective(CTopOauthToken oauthToken);
+
+    CTopOauthToken selectByAccountId(Long accountId);
 }

+ 73 - 0
jeecg-boot-module-system/src/main/java/org/jeecg/modules/ctop/mapper/xml/CTopOauthTokenMapper.xml

@@ -1,4 +1,77 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 <mapper namespace="org.jeecg.modules.ctop.mapper.CTopOauthTokenMapper">
+
+    <resultMap id="BaseResultMap" type="org.jeecg.modules.ctop.entity.CTopOauthToken">
+        <result column="id" property="id" jdbcType="BIGINT"/>
+        <result column="media_id" property="mediaId" jdbcType="VARCHAR"/>
+        <result column="advertiser_id" property="advertiserId" jdbcType="VARCHAR"/>
+        <result column="account_id" property="accountId" jdbcType="BIGINT"/>
+        <result column="access_token" property="accessToken" jdbcType="VARCHAR"/>
+        <result column="refresh_token" property="refreshToken" jdbcType="VARCHAR"/>
+        <result column="access_token_expires_in" property="accessTokenExpiresIn" jdbcType="TIMESTAMP"/>
+        <result column="refresh_token_expires_in" property="refreshTokenExpiresIn" jdbcType="TIMESTAMP"/>
+        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
+        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
+    </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,create_time,update_time
+   </sql>
+
+    <select id="selectAll" resultMap="BaseResultMap">
+        select
+        <include refid="Base_Column_List"/>
+        from ctop_oauth_token
+    </select>
+
+
+    <update id="updateByPrimaryKeySelective" parameterType="org.jeecg.modules.ctop.entity.CTopOauthToken">
+        update ctop_oauth_token
+        <set>
+
+            <if test="mediaId != null">
+                media_id = #{mediaId},
+            </if>
+
+            <if test="advertiserId != null">
+                advertiser_id = #{advertiserId},
+            </if>
+
+            <if test="accessToken != null">
+                access_token = #{accessToken},
+            </if>
+
+            <if test="accessTokenExpiresIn != null">
+                access_token_expires_in = #{accessTokenExpiresIn},
+            </if>
+
+            <if test="refreshToken != null">
+                refresh_token = #{refreshToken,jdbcType=VARCHAR},
+            </if>
+
+            <if test="refreshTokenExpiresIn != null">
+                refresh_token_expires_in = #{refreshTokenExpiresIn},
+            </if>
+
+            <if test="createTime != null">
+                create_time = #{createTime},
+            </if>
+
+            <if test="updateTime != null">
+                update_time = #{updateTime},
+            </if>
+        </set>
+        where account_id = #{accountId}
+    </update>
+
+    <select id="selectByAccountId" resultMap="BaseResultMap">
+        select
+        <include refid="Base_Column_List"/>
+        from ctop_oauth_token
+        where account_id = #{accountId}
+        order by create_time desc
+        limit 1
+    </select>
+
 </mapper>

+ 7 - 1
jeecg-boot-module-system/src/main/java/org/jeecg/modules/ctop/service/ICTopOauthTokenService.java

@@ -3,13 +3,19 @@ package org.jeecg.modules.ctop.service;
 import org.jeecg.modules.ctop.entity.CTopOauthToken;
 import com.baomidou.mybatisplus.extension.service.IService;
 
+import java.util.Map;
+
 /**
  * @Description: oauth授权码
  * @Author: jeecg-boot
- * @Date:   2019-07-09
+ * @Date: 2019-07-09
  * @Version: V1.0
  */
 public interface ICTopOauthTokenService extends IService<CTopOauthToken> {
 
     CTopOauthToken getOAuthTokenByAccountId(String accountId);
+
+    Map<String, Object> getByteDanceAccessToken(String accountId);
+
+    Map<String,Object> getKuaiShouRefreshToken(Long accountId,String refreshToken);
 }

+ 9 - 0
jeecg-boot-module-system/src/main/java/org/jeecg/modules/ctop/service/IRefreshTokenService.java

@@ -0,0 +1,9 @@
+package org.jeecg.modules.ctop.service;
+
+import org.jeecg.modules.ctop.entity.CTopOauthToken;
+
+import java.util.List;
+
+public interface IRefreshTokenService {
+    List<CTopOauthToken> getRefresh();
+}

+ 112 - 3
jeecg-boot-module-system/src/main/java/org/jeecg/modules/ctop/service/impl/CTopOauthTokenServiceImpl.java

@@ -1,22 +1,36 @@
 package org.jeecg.modules.ctop.service.impl;
 
+import cn.com.ctop.common.utils.Check;
+import cn.com.ctop.common.utils.HttpUtils;
+import cn.com.ctop.common.utils.PropertiesUtils;
+import cn.com.ctop.kuaishou.modules.material.entity.KuaishouResultToken;
+import cn.com.ctop.toutiao.common.BytedanceInterfaceConstant;
+import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import constant.KuaishouInterfaceConstant;
 import org.jeecg.modules.ctop.entity.CTopOauthToken;
 import org.jeecg.modules.ctop.mapper.CTopOauthTokenMapper;
 import org.jeecg.modules.ctop.service.ICTopOauthTokenService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
-import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
 
 /**
  * @Description: oauth授权码
  * @Author: jeecg-boot
- * @Date:   2019-07-09
+ * @Date: 2019-07-09
  * @Version: V1.0
  */
 @Service
 public class CTopOauthTokenServiceImpl extends ServiceImpl<CTopOauthTokenMapper, CTopOauthToken> implements ICTopOauthTokenService {
+    private static final Logger logger = LoggerFactory.getLogger(CTopOauthTokenServiceImpl.class);
+
 
     @Autowired
     private CTopOauthTokenMapper cTopOauthTokenMapper;
@@ -24,8 +38,103 @@ public class CTopOauthTokenServiceImpl extends ServiceImpl<CTopOauthTokenMapper,
     @Override
     public CTopOauthToken getOAuthTokenByAccountId(String accountId) {
         QueryWrapper<CTopOauthToken> queryWrapper = new QueryWrapper<>();
-        queryWrapper.eq("advertiser_id", accountId).orderByDesc("create_time");
+        queryWrapper.eq("account_id", accountId).orderByDesc("create_time");
         CTopOauthToken cTopOauthToken = cTopOauthTokenMapper.selectOne(queryWrapper);
         return cTopOauthToken;
     }
+
+    /**
+     * 刷新token
+     *
+     * @param accountId
+     * @return
+     */
+    @Override
+    public Map<String, Object> getByteDanceAccessToken(String accountId) {
+        Map<String, Object> resultMap = new HashMap<>();
+        CTopOauthToken token = this.getOAuthTokenByAccountId(accountId);
+        if (null == token) {
+            resultMap.put("message", "token信息不存在");
+            resultMap.put("code", -1);
+            return resultMap;
+        }
+        String url = PropertiesUtils.getValue("bytedance_config", "bytedance_api_url") + BytedanceInterfaceConstant.REFRESH_TOKEN;
+        Map<String, Object> param = new HashMap<String, Object>();
+        param.put("app_id", PropertiesUtils.getValue("bytedance_config", "bytedance_appid"));
+        param.put("secret", PropertiesUtils.getValue("bytedance_config", "bytedance_secret"));
+        param.put("grant_type", "refresh_token");
+        param.put("refresh_token", token.getRefreshToken());
+        String result = HttpUtils.httpPostRequest(url, param, new HashMap<>());
+        JSONObject resultObject = JSONObject.parseObject(result);
+        Integer code = resultObject.getInteger("code");
+        if (null == code || code != 0) {
+            logger.info("刷新token异常;accountId:{}", accountId);
+            resultMap.put("message", "刷新token异常");
+            resultMap.put("code", -1);
+            return resultMap;
+        }
+        JSONObject data = resultObject.getJSONObject("data");
+        CTopOauthToken refreshToken = new CTopOauthToken(token, data);
+        cTopOauthTokenMapper.deleteById(token.getId());
+        cTopOauthTokenMapper.insert(refreshToken);
+        logger.info("头条刷新token成功:accountId:{}", accountId);
+        resultMap.put("message", "token刷新成功");
+        resultMap.put("code", 0);
+        return resultMap;
+    }
+
+    /**
+     * 快手 刷新token
+     *
+     * @param accountId
+     * @param refreshToken
+     * @return
+     */
+    @Override
+    public Map<String, Object> getKuaiShouRefreshToken(Long accountId, String refreshToken) {
+        String url = PropertiesUtils.getConfig("kuaishou_api_url") + KuaishouInterfaceConstant.REFRESH_TOKEN;
+        Map<String, String> headers = new HashMap<String, String>();
+        headers.put("Content-Type", " application/json");
+        Map<String, Object> param = new HashMap<String, Object>();
+        param.put("app_id", PropertiesUtils.getConfig("kuaishou_appid"));
+        param.put("secret", PropertiesUtils.getConfig("kuaishou_secret"));
+        param.put("refresh_token", refreshToken);
+        try {
+            String result = HttpUtils.httpPostRequest(url, param, headers);
+            JSONObject resultJson = JSONObject.parseObject(result);
+            if (!Check.isNull(resultJson)) {
+                Integer code = resultJson.getInteger("code");
+                if (code == 0) {
+                    CTopOauthToken oauthToken = cTopOauthTokenMapper.selectByAccountId(accountId);
+                    if (!Check.isNull(oauthToken)) {
+                        JSONObject dataJson = resultJson.getJSONObject("data");
+                        if (!Check.isNull(dataJson)) {
+                            oauthToken.setAccountId(accountId);
+                            oauthToken.setAccessToken(dataJson.getString("access_token"));
+                            long accessTokenExpireIn = dataJson.getLong("access_token_expires_in") * 1000L;
+                            long now = System.currentTimeMillis();
+                            Date accessTokenExpireInDate = new Date(now + accessTokenExpireIn);
+                            oauthToken.setAccessTokenExpiresIn(accessTokenExpireInDate);
+                            oauthToken.setRefreshToken(dataJson.getString("refresh_token"));
+                            long refreshTokenExpireIn = dataJson.getLong("refresh_token_expires_in") * 1000L;
+                            Date refreshTokenExpireInDate = new Date(now + refreshTokenExpireIn);
+                            oauthToken.setRefreshTokenExpiresIn(refreshTokenExpireInDate);
+                            oauthToken.setUpdateTime(new Date());
+                            cTopOauthTokenMapper.updateByPrimaryKeySelective(oauthToken);
+                        }
+                    }
+                } else {
+                    logger.error("快手刷新token失败,accountId:{},result:{}", accountId, resultJson);
+
+                }
+            } else {
+                logger.error("快手刷新token返回结果为空,accountId:{}", accountId);
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+
+        }
+
+        return null;
+    }
 }

+ 45 - 0
jeecg-boot-module-system/src/main/java/org/jeecg/modules/ctop/service/impl/RefreshTokenServiceImpl.java

@@ -0,0 +1,45 @@
+package org.jeecg.modules.ctop.service.impl;
+
+import cn.com.ctop.common.utils.Check;
+import org.jeecg.modules.ctop.entity.CTopOauthToken;
+import org.jeecg.modules.ctop.mapper.CTopOauthTokenMapper;
+import org.jeecg.modules.ctop.service.ICTopOauthTokenService;
+import org.jeecg.modules.ctop.service.IRefreshTokenService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.util.List;
+
+@Service
+public class RefreshTokenServiceImpl implements IRefreshTokenService {
+    @Autowired
+    private CTopOauthTokenMapper oauthTokenMapper;
+    @Autowired
+    private ICTopOauthTokenService oauthTokenService;
+
+    /**
+     * 获取所有token
+     *
+     * @return
+     */
+    @Override
+    public List<CTopOauthToken> getRefresh() {
+
+        List<CTopOauthToken> oauthTokens = oauthTokenMapper.selectAll();
+
+        if (!Check.isNull(oauthTokens)) {
+            for (CTopOauthToken oauthToken : oauthTokens) {
+                if ("1".equals(oauthToken.getMediaId().trim())) { //头条
+                    oauthTokenService.getByteDanceAccessToken(String.valueOf(oauthToken.getAccountId()));
+                }
+                if ("2".equals(oauthToken.getMediaId().trim())) { //快手
+                    oauthTokenService.getKuaiShouRefreshToken(oauthToken.getAccountId(), oauthToken.getRefreshToken());
+                }
+
+            }
+        }
+
+        System.err.println(oauthTokens);
+        return null;
+    }
+}

+ 14 - 9
jeecg-boot-module-system/src/main/java/org/jeecg/modules/kuaishou/controller/KuaiShouController.java

@@ -2,6 +2,8 @@ package org.jeecg.modules.kuaishou.controller;
 
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
+import org.jeecg.modules.ctop.service.ICTopOauthTokenService;
+import org.jeecg.modules.ctop.service.IRefreshTokenService;
 import org.jeecg.modules.kuaishou.service.IKuaishouInterfaceService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.RequestMapping;
@@ -114,15 +116,6 @@ public class KuaiShouController {
     }
 
 
-    @RequestMapping("/getToken")
-    public void getToken() {
-        String accessToken = "092955a5f1768f8dfdc19fd821ccfd9e";
-        kuaishouInterfaceService.getRefreshToken(accessToken);
-
-
-    }
-
-
     @RequestMapping(value = "/video/upload", consumes = "multipart/form-data;charset=utf-8")
     public void upload(@RequestParam("multipartFile") MultipartFile multipartFile, HttpServletRequest request) throws IOException {
 
@@ -234,4 +227,16 @@ public class KuaiShouController {
         kuaishouInterfaceService.appUpdate(advertiserId, accessToken, requestJson, multipartFile, request);
 
     }
+
+    @Autowired
+    private IRefreshTokenService refreshTokenService;
+    @Autowired
+    private ICTopOauthTokenService oauthTokenService;
+
+    @RequestMapping("/test")
+    public void test() throws IOException {
+        oauthTokenService.getKuaiShouRefreshToken(23212L, "b51ce96a098299b04fa6cb70617e653d");
+
+    }
+
 }

+ 0 - 8
jeecg-boot-module-system/src/main/java/org/jeecg/modules/kuaishou/service/IKuaishouInterfaceService.java

@@ -84,14 +84,6 @@ public interface IKuaishouInterfaceService {
      */
     KuaishouResultToken getAccessToken(String authCode);
 
-    /**
-     * 刷新token
-     *
-     * @param refreshToken
-     * @return
-     */
-    KuaishouResultToken getRefreshToken(String refreshToken);
-
 
     /**
      * 获取可选的深度转化类型

+ 0 - 35
jeecg-boot-module-system/src/main/java/org/jeecg/modules/kuaishou/service/impl/KuaishouInterfaceServiceImpl.java

@@ -733,41 +733,6 @@ public class KuaishouInterfaceServiceImpl implements IKuaishouInterfaceService {
     }
 
     /**
-     * 刷新token
-     *
-     * @param refreshToken
-     * @return
-     */
-    @Override
-    public KuaishouResultToken getRefreshToken(String refreshToken) {
-        String url = PropertiesUtils.getConfig("kuaishou_api_url") + KuaishouInterfaceConstant.REFRESH_TOKEN;
-        Map<String, String> headers = new HashMap<String, String>();
-        headers.put("Content-Type", " application/json");
-        Map<String, Object> param = new HashMap<String, Object>();
-        param.put("app_id", PropertiesUtils.getConfig("kuaishou_appid"));
-        param.put("secret", PropertiesUtils.getConfig("kuaishou_secret"));
-        param.put("refresh_token", refreshToken);
-        KuaishouResultToken kuaishouResult = null;
-        try {
-            String result = HttpUtils.httpPostRequest(url, param, headers);
-            System.err.println(result);
-            JSONObject tokenJson = new JSONObject();
-            if (!Check.isNullMap(tokenJson)) {
-
-
-            } else {
-                logger.error("重新获取token返回结果为空");
-            }
-
-
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
-        return kuaishouResult;
-    }
-
-
-    /**
      * 获取可选的深度转化类型
      *
      * @param advertiserId