Преглед изворни кода

抖音小程序定时更新

ZHAOXIAN пре 1 година
родитељ
комит
4a3ec7e31f

+ 21 - 0
job-bytedance/src/main/java/cn/com/ctop/job/bytedance/data/mapper/BytedanceAppletTokenRefreshMapper.java

@@ -0,0 +1,21 @@
+package cn.com.ctop.job.bytedance.data.mapper;
+
+
+import com.alibaba.fastjson.JSONObject;
+import org.apache.ibatis.annotations.Param;
+
+import java.util.List;
+
+/**
+ * 头条项目列Mapper接口
+ *
+ * @author ruoyi
+ * @date 2025-04-23
+ */
+public interface BytedanceAppletTokenRefreshMapper {
+
+    List<JSONObject> queryAppletList();
+
+    void refreshAppletTokens(@Param("list") List<JSONObject> list);
+
+}

+ 33 - 0
job-bytedance/src/main/java/cn/com/ctop/job/bytedance/data/mapper/xml/BytedanceAppletTokenRefreshMapper.xml

@@ -0,0 +1,33 @@
+<?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="cn.com.ctop.job.bytedance.data.mapper.BytedanceAppletTokenRefreshMapper">
+
+    <select id="queryAppletList" resultType="com.alibaba.fastjson.JSONObject">
+        SELECT t1.id,
+               t1.name,
+               t1.app_id AS 'appId', t1.app_secret AS 'appSecret', t2.id as 'officialAccountId'
+        FROM thirtpart.ly_applet t1
+                 LEFT JOIN thirtpart.ly_official_accounts t2 ON t1.id = t2.applet_id
+        WHERE t1.source != 2
+	AND t1.type =2
+    </select>
+
+    <insert id="refreshAppletTokens">
+        replace into thirtpart.applet_tokens (
+        app_id,
+        official_account_id,
+        applet_id,
+        token
+        )
+        values
+        <foreach collection="list" item="item" separator=",">
+            (
+            #{item.appId},
+            #{item.officialAccountId},
+            #{item.id},
+            #{item.token}
+            )
+        </foreach>
+    </insert>
+
+</mapper>

+ 10 - 0
job-bytedance/src/main/java/cn/com/ctop/job/bytedance/data/service/BytedanceAppletTokenRefreshService.java

@@ -0,0 +1,10 @@
+package cn.com.ctop.job.bytedance.data.service;
+
+import com.alibaba.fastjson.JSONObject;
+
+import java.util.List;
+
+public interface BytedanceAppletTokenRefreshService {
+
+    void buildAppletTokens();
+}

+ 5 - 0
job-bytedance/src/main/java/cn/com/ctop/job/bytedance/data/service/IBytedanceRefreshTokenService.java

@@ -1,9 +1,14 @@
 package cn.com.ctop.job.bytedance.data.service;
 
+import redis.clients.jedis.Jedis;
+
 public interface IBytedanceRefreshTokenService {
+
     void bytedanceRefreshToken();
 
     void refreshBaiduYunToken();
 
     void refreshAppletTokens();
+
+    Jedis getJedisToken();
 }

+ 67 - 0
job-bytedance/src/main/java/cn/com/ctop/job/bytedance/data/service/impl/BytedanceAppletTokenRefreshServiceImpl.java

@@ -0,0 +1,67 @@
+package cn.com.ctop.job.bytedance.data.service.impl;
+
+import cn.com.ctop.job.bytedance.data.mapper.BytedanceAppletTokenRefreshMapper;
+import cn.com.ctop.job.bytedance.data.service.BytedanceAppletTokenRefreshService;
+import cn.com.ctop.job.bytedance.data.service.IBytedanceRefreshTokenService;
+import cn.com.ctop.job.bytedance.data.utils.Check;
+import cn.com.ctop.job.bytedance.data.utils.HttpUtils;
+import com.alibaba.fastjson.JSONObject;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+import redis.clients.jedis.Jedis;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+@Service
+@Slf4j
+public class BytedanceAppletTokenRefreshServiceImpl implements BytedanceAppletTokenRefreshService {
+
+    @Autowired
+    private BytedanceAppletTokenRefreshMapper tokenRefreshMapper;
+
+    @Autowired
+    private IBytedanceRefreshTokenService refreshTokenService;
+
+    @Override
+    public void buildAppletTokens() {
+        Jedis jedis = refreshTokenService.getJedisToken();
+        List<JSONObject> list = tokenRefreshMapper.queryAppletList();
+        for (JSONObject applet : list) {
+            buildAppletToken(applet, jedis);
+        }
+        tokenRefreshMapper.refreshAppletTokens(list);
+        jedis.close();
+    }
+
+    //构建小程序token
+    private void buildAppletToken(JSONObject applet, Jedis jedis) {
+        String name = applet.getString("name");
+        try {
+            Map<String, Object> param = new HashMap<>();
+            String appId = applet.getString("appId");
+            param.put("client_key", appId);
+            param.put("client_secret", applet.getString("appSecret"));
+            param.put("grant_type", "client_credential");
+            String result = HttpUtils.httpPostRequest("https://open.douyin.com/oauth/client_token/", param, null);
+            JSONObject tokenInfo = JSONObject.parseObject(result);
+            if (!Check.isNull(tokenInfo)) {
+                JSONObject data = tokenInfo.getJSONObject("data");
+                if (!Check.isNull(data) && data.getInteger("error_code") == 0) {
+                    String accessToken = data.getString("access_token");
+                    jedis.setex("client-token-key_" + appId, 3600, accessToken);
+                    log.info("小程序:{},获取token完成", name);
+                    applet.put("token", accessToken);
+                } else {
+                    log.error("小程序:{},获取token失败!结果:{}", name, tokenInfo);
+                }
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+            log.error("小程序:{},获取token异常!", name);
+        }
+    }
+
+}

+ 2 - 1
job-bytedance/src/main/java/cn/com/ctop/job/bytedance/data/service/impl/BytedanceRefreshTokenServiceImpl.java

@@ -104,7 +104,8 @@ public class BytedanceRefreshTokenServiceImpl implements IBytedanceRefreshTokenS
     }
 
     //获取token
-    private Jedis getJedisToken() {
+    @Override
+    public Jedis getJedisToken() {
         Jedis jedis = null;
         try {
             String ip = "r-2zelacjrljt205nsb1pd.redis.rds.aliyuncs.com";

+ 32 - 0
job-bytedance/src/main/java/cn/com/ctop/job/bytedance/handler/BytedanceAppletTokenRefreshJob.java

@@ -0,0 +1,32 @@
+package cn.com.ctop.job.bytedance.handler;
+
+import cn.com.ctop.job.bytedance.data.service.BytedanceAppletTokenRefreshService;
+import com.xxl.job.core.context.XxlJobHelper;
+import com.xxl.job.core.handler.annotation.XxlJob;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+/**
+ * 抖音小程序access_token刷新
+ */
+@Slf4j
+@Component
+public class BytedanceAppletTokenRefreshJob {
+
+    @Autowired
+    private BytedanceAppletTokenRefreshService tokenRefreshService;
+
+    @XxlJob("bytedanceAppletTokenRefreshJob")
+    public void bytedanceAppletTokenRefreshJob() {
+        log.info("[bytedanceAppletTokenRefreshJob] 任务开始");
+        try {
+            // 生成小程序token
+            tokenRefreshService.buildAppletTokens();
+        } catch (Exception e) {
+            XxlJobHelper.handleFail("[bytedanceAppletTokenRefreshJob] 异常 + " + e.getMessage());
+        }
+    }
+
+
+}