Bladeren bron

增加推送

xueyiming 7 maanden geleden
bovenliggende
commit
217445fda6

+ 25 - 5
we-com-server/src/main/java/com/tzld/piaoquan/wecom/job/WeComMessageDataJob.java

@@ -1,5 +1,6 @@
 package com.tzld.piaoquan.wecom.job;
 
+import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import com.aliyun.odps.data.Record;
@@ -12,6 +13,7 @@ import com.tzld.piaoquan.wecom.model.po.MessageAttachment;
 import com.tzld.piaoquan.wecom.model.po.MessageAttachmentExample;
 import com.tzld.piaoquan.wecom.model.po.User;
 import com.tzld.piaoquan.wecom.model.po.UserExample;
+import com.tzld.piaoquan.wecom.service.MessageService;
 import com.tzld.piaoquan.wecom.utils.DateUtil;
 import com.tzld.piaoquan.wecom.utils.OdpsUtil;
 import com.tzld.piaoquan.wecom.utils.page.Page;
@@ -35,6 +37,9 @@ public class WeComMessageDataJob {
     @Autowired
     private RedisTemplate<String, Object> redisTemplate;
 
+    @Autowired
+    private MessageService messageService;
+
     private static final int sum = 3;
 
     List<PushMessage> goodList = new ArrayList<>();
@@ -82,6 +87,7 @@ public class WeComMessageDataJob {
         init();
         Map<String, List<String>> res = new HashMap<>();
         UserExample example = new UserExample();
+        example.createCriteria().andExternalUserId3rdPartyEqualTo("wmgizUDQAAYdFWt1hts2fU6P2h84fPvQ");
         long count = userMapper.countByExample(example);
         int page = 1;
         int pageSize = 1000;
@@ -143,9 +149,13 @@ public class WeComMessageDataJob {
 
     public void pushMessage(Map<String, List<String>> map) {
         List<JSONObject> pushList = new ArrayList<>();
+        List<String> staffExtIdList = new ArrayList<>();
+        staffExtIdList.add("wogizUDQAAk5Z_5nLRD-XTAPU9poaRCw");
+        staffExtIdList.add("wogizUDQAAzM5HbqIyA4k3KR6D8vKp3w");
         for (Map.Entry<String, List<String>> entry : map.entrySet()) {
             JSONObject jsonObject = new JSONObject();
-            jsonObject.put("name", "群发名称");
+            jsonObject.put("name", "日常push-0926-推荐官");
+            jsonObject.put("text", "- 晚上好!愿这宁静的夜晚带给你心灵的放松与安宁,以下是大家都在看的爆款视频!");
             JSONArray attachments = new JSONArray();
             List<Long> videoIdList = Arrays.stream(entry.getKey().split("_")).map(Long::parseLong).collect(Collectors.toList());
             for (Long videoId : videoIdList) {
@@ -169,15 +179,25 @@ public class WeComMessageDataJob {
             jsonObject.put("attachments", attachments);
             List<List<String>> lists = Lists.partition(entry.getValue(), 10000);
             for (List<String> list : lists) {
+                List<JSONObject> staffEuList = new ArrayList<>();
                 JSONObject newJSONObject = new JSONObject();
                 newJSONObject.putAll(jsonObject);
-                JSONObject staff_eu_list = new JSONObject();
-                staff_eu_list.put("staff_ext_id", "员工id");
-                staff_eu_list.put("eu_ext_ids", list);
-                newJSONObject.put("staff_eu_list", staff_eu_list);
+                for (String staffExtId : staffExtIdList) {
+                    JSONObject staff_eu = new JSONObject();
+                    staff_eu.put("staff_ext_id", staffExtId);
+                    staff_eu.put("eu_ext_ids", list);
+                    staffEuList.add(staff_eu);
+                }
+                newJSONObject.put("staff_eu_list", staffEuList);
                 pushList.add(newJSONObject);
             }
         }
+        if (CollectionUtils.isEmpty(pushList)) {
+            throw new RuntimeException("推送视频生成失败");
+        }
+        for (JSONObject jsonObject : pushList) {
+            messageService.pushMessage(jsonObject);
+        }
         System.out.println(pushList.get(0));
     }
 }

+ 18 - 7
we-com-server/src/main/java/com/tzld/piaoquan/wecom/job/WeComUserDataJob.java

@@ -3,12 +3,17 @@ package com.tzld.piaoquan.wecom.job;
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import com.tzld.piaoquan.wecom.dao.mapper.UserMapper;
+import com.tzld.piaoquan.wecom.model.po.HistoryMessage;
+import com.tzld.piaoquan.wecom.model.po.HistoryMessageExample;
 import com.tzld.piaoquan.wecom.model.po.User;
 import com.tzld.piaoquan.wecom.model.po.UserExample;
 import com.tzld.piaoquan.wecom.service.AccessTokenService;
+import com.tzld.piaoquan.wecom.service.MessageService;
 import com.tzld.piaoquan.wecom.utils.HttpClientUtil;
 import com.tzld.piaoquan.wecom.utils.HttpPoolClient;
+import com.tzld.piaoquan.wecom.utils.page.Page;
 import org.apache.commons.lang3.ObjectUtils;
+import org.checkerframework.checker.units.qual.A;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 import org.springframework.util.CollectionUtils;
@@ -90,8 +95,12 @@ public class WeComUserDataJob {
 
     public void updateUser() {
         try {
+            UserExample userExample = new UserExample();
+            userExample.setOrderByClause("create_time desc");
+            userExample.setPage(new Page<>(1, 1));
+            List<User> historyMessages = userMapper.selectByExample(userExample);
+            long startTime = historyMessages.get(0).getCreateTime().getTime() / 1000;
             Long endTime = System.currentTimeMillis() / 1000;
-            Long startTime = 1726822290L;
             Integer total = getUpdateUserTotal(startTime, endTime);
             if (total == null || total == 0) {
                 return;
@@ -122,13 +131,15 @@ public class WeComUserDataJob {
                         userMapper.updateByPrimaryKeySelective(user);
                     }
                     sum++;
-                    if (jsonArray.size() < size) {
-                        if (total > sum) {
-                            //TODO 输出异常  插入数量不足
-                            System.out.println("插入数量不足");
-                        }
-                        break;
+                }
+                if (jsonArray.size() < size) {
+                    if (total > sum) {
+                        //TODO 输出异常  插入数量不足
+                        System.out.println(total);
+                        System.out.println(sum);
+                        System.out.println("插入数量不足");
                     }
+                    break;
                 }
             }
         } catch (IOException e) {

+ 3 - 1
we-com-server/src/main/java/com/tzld/piaoquan/wecom/service/Impl/AccessTokenServiceImpl.java

@@ -5,6 +5,8 @@ import com.tzld.piaoquan.wecom.service.AccessTokenService;
 import com.tzld.piaoquan.wecom.utils.HttpClientUtil;
 import com.tzld.piaoquan.wecom.utils.HttpPoolClient;
 import org.apache.commons.lang3.StringUtils;
+import org.apache.http.client.HttpClient;
+import org.apache.http.params.HttpParams;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.stereotype.Service;
@@ -37,7 +39,7 @@ public class AccessTokenServiceImpl implements AccessTokenService {
         param.put("corp_id", CROP_ID);
         param.put("secret", SECRET);
         try {
-            String res = httpPoolClientDefault.postJson(POST_ACCESS_TOKEN_URL, param.toJSONString());
+            String res = httpPoolClientDefault.post(POST_ACCESS_TOKEN_URL, param.toJSONString());
             JSONObject jsonObject = JSONObject.parseObject(res);
             Long expiresIn = jsonObject.getLong("expires_in");
             String newAccessToken = jsonObject.getString("access_token");

+ 9 - 3
we-com-server/src/main/java/com/tzld/piaoquan/wecom/service/Impl/MessageServiceImpl.java

@@ -5,6 +5,8 @@ import com.tzld.piaoquan.wecom.service.AccessTokenService;
 import com.tzld.piaoquan.wecom.service.MessageService;
 import com.tzld.piaoquan.wecom.utils.HttpClientUtil;
 import com.tzld.piaoquan.wecom.utils.HttpPoolClient;
+import org.apache.http.params.BasicHttpParams;
+import org.apache.http.params.HttpParams;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
@@ -23,9 +25,13 @@ public class MessageServiceImpl implements MessageService {
     public void pushMessage(JSONObject jsonObject) {
         try {
             String accessToken = accessTokenService.getAccessToken();
-            jsonObject.put("access_token", accessToken);
-            httpPoolClientDefault.postJson(POST_MESSAGE_PUSH_URL, jsonObject.toJSONString());
-        }catch (IOException e){
+            String url = POST_MESSAGE_PUSH_URL
+                    + "?access_token=" + accessToken;
+//            HttpParams params = new BasicHttpParams();
+//            params.setParameter("access_token", accessToken);
+            String s = httpPoolClientDefault.post(url, jsonObject.toJSONString());
+            System.out.println(s);
+        } catch (IOException e) {
             e.printStackTrace();
         }
     }

+ 7 - 3
we-com-server/src/main/java/com/tzld/piaoquan/wecom/utils/HttpPoolClient.java

@@ -1,8 +1,10 @@
 package com.tzld.piaoquan.wecom.utils;
 
+import com.alibaba.fastjson.JSONObject;
 import com.google.common.collect.Lists;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.commons.lang3.concurrent.BasicThreadFactory;
+import org.apache.http.Consts;
 import org.apache.http.HttpEntity;
 import org.apache.http.HttpRequestInterceptor;
 import org.apache.http.HttpStatus;
@@ -20,6 +22,7 @@ import org.apache.http.impl.client.CloseableHttpClient;
 import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
 import org.apache.http.impl.client.HttpClientBuilder;
 import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
+import org.apache.http.params.HttpParams;
 import org.apache.http.ssl.SSLContexts;
 import org.apache.http.util.EntityUtils;
 import org.slf4j.Logger;
@@ -30,11 +33,12 @@ import javax.net.ssl.SSLContext;
 import java.io.IOException;
 import java.net.SocketTimeoutException;
 import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
 import java.security.KeyManagementException;
 import java.security.KeyStoreException;
 import java.security.NoSuchAlgorithmException;
 import java.util.List;
-import java.util.Optional;
+import java.util.Map;
 import java.util.concurrent.ScheduledExecutorService;
 import java.util.concurrent.ScheduledThreadPoolExecutor;
 import java.util.concurrent.TimeUnit;
@@ -93,12 +97,12 @@ public class HttpPoolClient {
     }
 
 
-    public String postJson(String url, String json) throws IOException {
+    public String post(String url, String json) throws IOException {
         HttpPost httpPost = new HttpPost(url);
         if (StringUtils.isBlank(json)) {
             return request(httpPost);
         }
-        StringEntity entity = new StringEntity(json, Charset.forName("UTF-8"));
+        StringEntity entity = new StringEntity(json, StandardCharsets.UTF_8);
         entity.setContentEncoding("UTF-8");
         entity.setContentType("application/json");
         httpPost.setEntity(entity);