ソースを参照

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

anderx 6 ヶ月 前
コミット
ba99003106

+ 42 - 41
src/main/java/com/kede/common/task/ConversationContentTask.java

@@ -106,13 +106,7 @@ public class ConversationContentTask {
             //获取发送这名称
             System.out.println("====>"+chatMsg);
             Map<String, Object> map = conversationContentService.pushGetChatName(from);
-            int code = (int) map.get("code");
-            String chatName;
-            if (code==0){
-                chatName = map.get("name").toString();
-            }else {
-                chatName = "名称查询失败";
-            }
+            String chatName=pushMapGetName(map);
             String tolist = chatMsg.getTolist();
             String roomName=null;
             //判定是否有群编号,判定是单聊还是群聊,群聊获取群名称
@@ -133,49 +127,56 @@ public class ConversationContentTask {
                     if (x!=84061){
                         Map<String, Object> maps = conversationContentService.pushGetChatName(tolistName);
                         x= (int) maps.get("code");
-                        String name;
-                        if (x==0){
-                            name = maps.get("name").toString();
-                        }else {
-                            name = "名称查询失败";
-                        }
+                        String name=pushMapGetName(maps);
                         if (name != null){
                             stringJoiner.add(name);
                         }
                     }
                 }
             }
-
-            ChatMsg chatMsg1 = chatMsgMapper.selectByMsgId(chatMsg.getMsgid());
-            if (chatMsg1 == null){
-                ChatMsg in= new ChatMsg();
-                in.setMsgid(chatMsg.getMsgid());
-                in.setActionType(chatMsg.getAction());
-                in.setFromId(chatMsg.getFrom());
-                in.setFromName(chatName);
-                in.setTolist(tolist);
-                in.setTolistName(stringJoiner.toString());
-                in.setRoomid(chatMsg.getRoomid());
-                in.setRoomName(roomName);
-                if (StringUtils.isNotBlank(chatMsg.getRoomid())){
-                    ChatMsgUser chatMsgUser = chatMsgUserMapper.selectByUserId(chatMsg.getRoomid());
-                    if (chatMsgUser == null){
-                        ChatMsgUser cmu = new ChatMsgUser();
-                        cmu.setUserId(chatMsg.getRoomid());
-                        cmu.setName(roomName);
-                        cmu.setType(3);
-                        cmu.setCreateTime(new Date());
-                        chatMsgUserMapper.insert(cmu);
-                    }
+            ChatMsg in= new ChatMsg();
+            in.setMsgid(chatMsg.getMsgid());
+            in.setActionType(chatMsg.getAction());
+            in.setFromId(chatMsg.getFrom());
+            in.setFromName(chatName);
+            in.setTolist(tolist);
+            in.setTolistName(stringJoiner.toString());
+            in.setRoomid(chatMsg.getRoomid());
+            if (StringUtils.isNotBlank(in.getRoomid())){
+                in.setType(1);
+            }else{
+                in.setType(0);
+            }
+            in.setRoomName(roomName);
+            if (StringUtils.isNotBlank(chatMsg.getRoomid())){
+                ChatMsgUser chatMsgUser = chatMsgUserMapper.selectByUserId(chatMsg.getRoomid());
+                if (chatMsgUser == null&&roomName!=null){
+                    ChatMsgUser cmu = new ChatMsgUser();
+                    cmu.setUserId(chatMsg.getRoomid());
+                    cmu.setName(roomName);
+                    cmu.setType(3);
+                    cmu.setCreateTime(new Date());
+                    chatMsgUserMapper.insert(cmu);
                 }
-                in.setMsgtime(chatMsg.getMsgtime());
-                in.setMsgtype(chatMsg.getMsgtype());
-                in.setMsgurl(chatMsg.getMsgurl());
-                in.setContent(chatMsg.getContent());
-                in.setCreateTime(new Date());
-                chatMsgMapper.insert(in);
             }
+            in.setMsgtime(chatMsg.getMsgtime());
+            in.setMsgtype(chatMsg.getMsgtype());
+            in.setMsgurl(chatMsg.getMsgurl());
+            in.setContent(chatMsg.getContent());
+            in.setCreateTime(new Date());
+            chatMsgMapper.insert(in);
+        }
+    }
+
+    private String pushMapGetName(Map<String, Object> map) {
+        Integer code = (Integer) map.get("code");
+        String chatName;
+        if (code==0){
+            chatName = map.get("name").toString();
+        }else {
+            chatName = "名称查询失败("+code+")";
         }
+        return chatName;
     }
 
 

+ 2 - 16
src/main/java/com/kede/wechat/bo/InputChatMsg.java

@@ -51,28 +51,14 @@ public class InputChatMsg {
             this.tolist =tolist.toString();
         }
         this.roomid = jsonObject.getString("roomid");
+        this.msgtype = jsonObject.getString("msgtype");
         Long msgtime1 = jsonObject.getLong("msgtime");
         if (msgtime1!=null){
         Date times=new Date();
         times.setTime(jsonObject.getLong("msgtime"));
         this.msgtime = times;
         }
-        this.msgtype = jsonObject.getString("msgtype");
-        JSONObject text = jsonObject.getJSONObject("text");
-        if (text!=null){
-            String content1 = text.getString("content");
-            try {
-                String osname = System.getProperties().getProperty("os.name");
-                if (osname.contains("Windows")) {
-                    String result = new String(content1.getBytes("GBK"),"UTF-8");
-                    this.content =result;
-                }else{
-                    this.content = content1;
-                }
-            } catch (UnsupportedEncodingException e) {
-                throw new RuntimeException(e);
-            }
-        }
+
 
     }
 

+ 47 - 24
src/main/java/com/kede/wechat/service/impl/ConversationContentServiceImpl.java

@@ -148,26 +148,8 @@ public class ConversationContentServiceImpl implements ConversationContentServic
                     InputChatMsg chatMsg = new InputChatMsg(jsonObject);
                     ChatMsg chatMsg1 = chatMsgMapper.selectByMsgId(chatMsg.getMsgid());
                     if (chatMsg1 == null){
-                        if(!msgtype.equals("text")){
-                            if (msgtype.equals("mixed")){
-                                System.out.println(jsonObject);
-                                com.alibaba.fastjson.JSONArray mixed = jsonObject.getJSONArray("mixed");
-                                for (int j = 0; j < mixed.size(); j++) {
-                                    com.alibaba.fastjson.JSONObject e = mixed.getJSONObject(j);
-                                    if (e.getString("msgtype").equals("text")){
-                                        String content = pushFileUrl(sdk,e.getString("msgtype"),e);
-                                        chatMsg.setContent(content);
-                                    }else {
-                                        String url = pushFileUrl(sdk,e.getString("msgtype"),e);
-                                        chatMsg.setMsgurl(url);
-                                    }
-                                }
-                            }else {
-                                String url=pushFileUrl(sdk,msgtype,jsonObject);
-                                chatMsg.setMsgurl(url);
-                            }
-
-                        }
+                        //处理数据
+                        pushSmgType(msgtype, jsonObject, sdk, chatMsg);
                         list.add(chatMsg);
                     }
                 } catch (Exception e) {
@@ -184,6 +166,46 @@ public class ConversationContentServiceImpl implements ConversationContentServic
     return map;
     }
 
+    private void pushSmgType(String msgtype, com.alibaba.fastjson.JSONObject jsonObject, long sdk, InputChatMsg chatMsg) {
+        if (msgtype.equals("text")) {
+            com.alibaba.fastjson.JSONObject text = jsonObject.getJSONObject("text");
+            if (text!=null){
+                String content1 = text.getString("content");
+                try {
+                    String osname = System.getProperties().getProperty("os.name");
+                    if (osname.contains("Windows")) {
+                        String result = new String(content1.getBytes("GBK"),"UTF-8");
+                        chatMsg.setContent(result);
+                    }else{
+                        chatMsg.setContent(content1);
+                    }
+                } catch (UnsupportedEncodingException e) {
+                    throw new RuntimeException(e);
+                }
+            }
+        }else if (msgtype.equals("mixed")){
+            System.out.println(jsonObject);
+            com.alibaba.fastjson.JSONArray mixed = jsonObject.getJSONArray("mixed");
+            for (int j = 0; j < mixed.size(); j++) {
+                com.alibaba.fastjson.JSONObject e = mixed.getJSONObject(j);
+                if (e.getString("msgtype").equals("text")){
+                    String content = pushFileUrl(sdk,e.getString("msgtype"),e);
+                    chatMsg.setContent(content);
+                }else {
+                    String url = pushFileUrl(sdk,e.getString("msgtype"),e);
+                    chatMsg.setMsgurl(url);
+                }
+            }
+        } else if (msgtype.equals("sphfeed")) {
+            chatMsg.setContent(jsonObject.getString("sphfeed"));
+        } else if (msgtype.equals("file")||msgtype.equals("image")||msgtype.equals("video")){
+            String url=pushFileUrl(sdk, msgtype, jsonObject);
+            chatMsg.setMsgurl(url);
+        } else {
+            chatMsg.setContent(jsonObject.toJSONString());
+        }
+    }
+
     private String pushFileUrl(long sdk, String msgtype, com.alibaba.fastjson.JSONObject jsonObject) {
         String url = uploadPath;
         String sdkfileid=null;
@@ -368,9 +390,10 @@ public class ConversationContentServiceImpl implements ConversationContentServic
         }else {
             String msgIdSub = userId.substring(0, 2);
             String url=null;
-            Integer type=0;
+            Integer type;
             if (msgIdSub.equals("wb")){
-                url=String.format("https://qyapi.weixin.qq.com/cgi-bin/msgaudit/get_robot_info??access_token=%s&robot_id=%s",getAppAccessToken(),userId);
+                type=0;
+//                url=String.format("https://qyapi.weixin.qq.com/cgi-bin/msgaudit/get_robot_info??access_token=%s&robot_id=%s",getAppAccessToken(),userId);
             }else if (msgIdSub.equals("wo")||msgIdSub.equals("wm")){
                             //     https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get
                 url=String.format("https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get?access_token=%s&external_userid=%s",getAppAccessToken(),userId);
@@ -435,7 +458,7 @@ public class ConversationContentServiceImpl implements ConversationContentServic
                 }
                 if (jsonObject.getInteger("errcode")==0){
                 }else {
-                    System.out.println("获取外部群名称失败");
+                    System.out.println("获取外部群名称失败"+jsonObject.getString("errcode"));
                 }
             }
             else
@@ -453,7 +476,7 @@ public class ConversationContentServiceImpl implements ConversationContentServic
                 }
                 if (jsonObject.getInteger("errcode")==0){
                 }else {
-                    System.out.println("获取内部群名称失败");
+                    System.out.println("获取内部群名称失败"+jsonObject.getString("errcode"));
                 }
 
             }