Просмотр исходного кода

微信会话内容存储获取优化,下载暂时关闭

anderx месяцев назад: 6
Родитель
Сommit
d3cbca9659

+ 24 - 4
src/main/java/com/kede/common/task/ConversationContentTask.java

@@ -33,6 +33,7 @@ public class ConversationContentTask {
 //    @Scheduled(cron = "0 0/5 * * * ?")
     @RequestMapping(value ="/open/pushMsg", method = RequestMethod.GET)
     public void pushMsg() {
+        long startTime = System.currentTimeMillis();
         Integer pageSeq = 0;
         Integer pageLimit = 100;
         Integer count = 0;
@@ -50,7 +51,15 @@ public class ConversationContentTask {
             }
             pageSeq= pageSeq + pageLimit+1;
         }
-        System.out.println("=========================================获取数据完成==============================");
+        long endTime = System.currentTimeMillis();
+        //输出耗时计算出耗时分钟
+        long time = (endTime - startTime)/1000;
+        //换算成分钟
+        long f = time / 60;
+        long m = time % 60;
+
+
+        System.out.println("=========================================获取数据完成=====耗时"+f+"分"+m+"秒=========================");
     }
 
     /**
@@ -58,7 +67,7 @@ public class ConversationContentTask {
      */
     @RequestMapping(value ="/open/test", method = RequestMethod.GET)
     public void pushTest() {
-        ChatMsg chatMsg = chatMsgMapper.selectById(146);
+        ChatMsg chatMsg = chatMsgMapper.selectById(476);
         StringJoiner stringJoiner = new StringJoiner(",");
         if (StringUtils.isNotEmpty(chatMsg.getTolist())){
             //用逗号分割
@@ -97,7 +106,13 @@ public class ConversationContentTask {
             //获取发送这名称
             System.out.println("====>"+chatMsg);
             Map<String, Object> map = conversationContentService.pushGetChatName(from);
-            String chatName = map.get("name").toString();
+            int code = (int) map.get("code");
+            String chatName;
+            if (code==0){
+                chatName = map.get("name").toString();
+            }else {
+                chatName = "名称查询失败";
+            }
             String tolist = chatMsg.getTolist();
             String roomName=null;
             //判定是否有群编号,判定是单聊还是群聊,群聊获取群名称
@@ -117,8 +132,13 @@ public class ConversationContentTask {
                     //x为获取成功时
                     if (x!=84061){
                         Map<String, Object> maps = conversationContentService.pushGetChatName(tolistName);
-                        String name = maps.get("name").toString();
                         x= (int) maps.get("code");
+                        String name;
+                        if (x==0){
+                            name = maps.get("name").toString();
+                        }else {
+                            name = "名称查询失败";
+                        }
                         if (name != null){
                             stringJoiner.add(name);
                         }

+ 58 - 23
src/main/java/com/kede/wechat/service/impl/ConversationContentServiceImpl.java

@@ -1,5 +1,6 @@
 package com.kede.wechat.service.impl;
 
+import com.kede.common.dao.ChatMsgMapper;
 import com.kede.common.model.ChatMsg;
 import com.kede.common.model.ChatMsgUser;
 import com.kede.common.utils.DateUtils;
@@ -32,6 +33,8 @@ public class ConversationContentServiceImpl implements ConversationContentServic
     private RedisTemplate redisTemplate;
     @Resource
     private ChatMsgUserMapper chatMsgUserMapper;
+    @Resource
+    private ChatMsgMapper chatMsgMapper;
 
 
     @Value(value = "${conversationContent.corpid}")
@@ -139,15 +142,25 @@ public class ConversationContentServiceImpl implements ConversationContentServic
                     }
                     String str = String.valueOf(Finance.GetContentFromSlice(msg));
                     com.alibaba.fastjson.JSONObject jsonObject = com.alibaba.fastjson.JSONObject.parseObject(str);
-                System.out.println("获取slice内容======="+jsonObject);
                     String msgtype = jsonObject.getString("msgtype");
                     InputChatMsg chatMsg = new InputChatMsg(jsonObject);
-                    if(msgtype.equals("file")||msgtype.equals("image")){
-                        String url=pushFileUrl(msgtype,jsonObject);
-                        chatMsg.setContent(url);
+                    ChatMsg chatMsg1 = chatMsgMapper.selectByMsgId(chatMsg.getMsgid());
+                    if (chatMsg1 == null){
+                        if(!msgtype.equals("text")){
+                            if (msgtype.equals("mixed")){
+                                System.out.println(jsonObject);
+                                for (int j = 0; j < jsonObject.getJSONArray("mixed").size(); j++) {
+                                    String url=pushFileUrl(sdk,msgtype,jsonObject);
+                                }
+
+                            }else {
+                                String url=pushFileUrl(sdk,msgtype,jsonObject);
+                                chatMsg.setContent(url);
+                            }
+
+                        }
+                        list.add(chatMsg);
                     }
-                    list.add(chatMsg);
-
                 } catch (Exception e) {
                     e.printStackTrace();
                 }
@@ -162,13 +175,12 @@ public class ConversationContentServiceImpl implements ConversationContentServic
     return map;
     }
 
-    private String pushFileUrl( String msgtype, com.alibaba.fastjson.JSONObject jsonObject) {
+    private String pushFileUrl(long sdk, String msgtype, com.alibaba.fastjson.JSONObject jsonObject) {
         String url = uploadPath;
         String sdkfileid=null;
 
         if (msgtype.equals("file")){
             com.alibaba.fastjson.JSONObject file = jsonObject.getJSONObject("file");
-
             try {
                 String filename = file.getString("filename");
                 sdkfileid = file.getString("sdkfileid");
@@ -185,9 +197,17 @@ public class ConversationContentServiceImpl implements ConversationContentServic
             url = url +"/wechat/"+ System.currentTimeMillis()+".jpg";
             com.alibaba.fastjson.JSONObject image = jsonObject.getJSONObject("image");
             sdkfileid = image.getString("sdkfileid");
+        }else if (msgtype.equals("voice")){
+            url = url +"/wechat/"+ System.currentTimeMillis()+".mp3";
+            com.alibaba.fastjson.JSONObject image = jsonObject.getJSONObject("voice");
+            sdkfileid = image.getString("sdkfileid");
+        }else if (msgtype.equals("video")){
+            url = url +"/wechat/"+ System.currentTimeMillis()+".mp4";
+            com.alibaba.fastjson.JSONObject image = jsonObject.getJSONObject("video");
         }
-        //下载文件
-//        downloadFile(sdk, sdkfileid, url);
+
+//        下载文件
+        downloadFile(sdk, sdkfileid, url);
         return url;
     }
 
@@ -331,7 +351,6 @@ public class ConversationContentServiceImpl implements ConversationContentServic
     public Map<String,Object> pushGetChatName(String userId) {
         Map<String,Object> map = new HashMap<>();
         Integer i =  0;
-
         ChatMsgUser chatMsgUser = chatMsgUserMapper.selectByUserId(userId);
         if (chatMsgUser != null){
             map.put("name",chatMsgUser.getName());
@@ -352,7 +371,11 @@ public class ConversationContentServiceImpl implements ConversationContentServic
                 type=2;
             }
             Map<String, Object> maps = getUserName(url, type);
-            String userName = maps.get("name").toString();
+            Object o = maps.get("name");
+            String userName = null;
+            if(o!=null){
+                userName = o.toString();
+            }
             i= (Integer) maps.get("code");
             System.out.println(userId+"="+userName);
             if (StringUtils.isNotBlank(userName)){
@@ -394,7 +417,13 @@ public class ConversationContentServiceImpl implements ConversationContentServic
                 map.put("chat_id",roomid);
                 map.put("cursor",0);
                 com.alibaba.fastjson.JSONObject jsonObject = HttpUtils.httpPost(url, map);
-                System.out.println(jsonObject.toJSONString());
+                String roomname = jsonObject.getString("roomname");
+                if (StringUtils.isBlank(roomname)){
+                    com.alibaba.fastjson.JSONObject groupChat = jsonObject.getJSONObject("group_chat");
+                    if (groupChat!=null){
+                        name = groupChat.getString("name");
+                    }
+                }
                 if (jsonObject.getInteger("errcode")==0){
                 }else {
                     System.out.println("获取外部群名称失败");
@@ -408,7 +437,11 @@ public class ConversationContentServiceImpl implements ConversationContentServic
                 map.put("roomid",roomid);
                 com.alibaba.fastjson.JSONObject jsonObject = HttpUtils.httpPost(url, map);
                 System.out.println(jsonObject.toJSONString());
-                name = jsonObject.getString("roomname");
+                String roomname = jsonObject.getString("roomname");
+                if (StringUtils.isBlank(roomname)){
+                    com.alibaba.fastjson.JSONObject groupChat = jsonObject.getJSONObject("group_chat");
+                    name = groupChat.getString("name");
+                }
                 if (jsonObject.getInteger("errcode")==0){
                 }else {
                     System.out.println("获取内部群名称失败");
@@ -434,28 +467,30 @@ public class ConversationContentServiceImpl implements ConversationContentServic
                 com.alibaba.fastjson.JSONObject result = HttpUtils.httpGet(url);
                 if (result.getInteger("errcode")==0) {
                     System.out.println(result.toJSONString());
+                    com.alibaba.fastjson.JSONObject externalContact = result.getJSONObject("external_contact");
+                    code = result.getInteger("errcode");
+                    name=externalContact.getString("name");
                 }else {
+                    code = result.getInteger("errcode");
                     System.out.println(result.toJSONString());
                     System.out.println("获取外部联系人名称失败");
                 }
-                com.alibaba.fastjson.JSONObject externalContact = result.getJSONObject("external_contact");
-                code = result.getInteger("errcode");
-                name=externalContact.getString("name");
         }else if (type==2){
                 com.alibaba.fastjson.JSONObject result = HttpUtils.httpGet(url);
                 if (result.getInteger("errcode")==0) {
                     System.out.println(result.toJSONString());
-
+                    com.alibaba.fastjson.JSONObject externalContact = result.getJSONObject("external_contact");
+                    code = result.getInteger("errcode");
+                    name = result.getString("name");
                 }else {
                     System.out.println(result.toJSONString());
                     System.out.println("获取内部联系人名称失败");
+                    code = result.getInteger("errcode");
                 }
-                com.alibaba.fastjson.JSONObject externalContact = result.getJSONObject("external_contact");
-                code = result.getInteger("errcode");
-                name = result.getString("name");
+
             }
-        map.put("name",name);
-        map.put("code",code);
+        if(name!=null)map.put("name",name);
+        if (code!=null)map.put("code",code);
         return map;
     }