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

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

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

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

@@ -167,45 +167,87 @@ public class ConversationContentServiceImpl implements ConversationContentServic
     }
 
     private void pushSmgType(String msgtype, com.alibaba.fastjson.JSONObject jsonObject, long sdk, InputChatMsg chatMsg) {
-        if (msgtype.equals("text")) {
+        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);
-                }
+                chatMsg.setContent(pushCharset(content1));
             }
-        }else if (msgtype.equals("mixed")){
+        }else if (msgtype.equals("link"))
+        {
+            //链接
+            com.alibaba.fastjson.JSONObject link = jsonObject.getJSONObject("link");
+            chatMsg.setMsgurl(link.getString("link_url"));
+            chatMsg.setContent(pushCharset(link.getString("title")));
+        }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);
+            com.alibaba.fastjson.JSONObject mixed = jsonObject.getJSONObject("mixed");
+            com.alibaba.fastjson.JSONArray item = mixed.getJSONArray("item");
+            for (int j = 0; j < item.size(); j++) {
+                com.alibaba.fastjson.JSONObject e = item.getJSONObject(j);
+                String type = e.getString("type");
+                if (type.equals("text")){
+                    com.alibaba.fastjson.JSONObject contentJson = e.getJSONObject("content");
+                    String content = contentJson.getString("content");
+                    chatMsg.setContent(pushCharset(content));
                 }else {
-                    String url = pushFileUrl(sdk,e.getString("msgtype"),e);
+                    String url =uploadPath;
+                    com.alibaba.fastjson.JSONObject content = e.getJSONObject("content");
+                    String sdkfileid=content.getString("sdkfileid");
+                    if (type.equals("file")){
+                        try {
+                            String filename = content.getString("filename");
+                            String osname = System.getProperties().getProperty("os.name");
+                            if (osname.contains("Windows")) {
+                                filename = new String(filename.getBytes("GBK"),"UTF-8");
+                                String safeName=filename.replaceAll("[<>:\"/\\\\|?*,、]", "_");
+                                url = url +"/wechat/"+ safeName;
+                            }
+                        } catch (UnsupportedEncodingException ex) {
+                            throw new RuntimeException(ex);
+                        }
+                    }else if (type.equals("image")) {
+                        url = url + "/wechat/" + System.currentTimeMillis() + ".jpg";
+                        com.alibaba.fastjson.JSONObject image = jsonObject.getJSONObject("image");
+                    }else {
+                        chatMsg.setContent(chatMsg.getContent()+"[附件="+e+"]");
+                    }
+                    downloadFile(sdk, sdkfileid, url);
                     chatMsg.setMsgurl(url);
                 }
             }
-        } else if (msgtype.equals("sphfeed")) {
+        } else if (msgtype.equals("sphfeed"))
+        {
             chatMsg.setContent(jsonObject.getString("sphfeed"));
-        } else if (msgtype.equals("file")||msgtype.equals("image")||msgtype.equals("video")){
+        } else if (msgtype.equals("file")||msgtype.equals("image")||msgtype.equals("video")||msgtype.equals("voice"))
+        {
             String url=pushFileUrl(sdk, msgtype, jsonObject);
             chatMsg.setMsgurl(url);
         } else {
-            chatMsg.setContent(jsonObject.toJSONString());
+            chatMsg.setContent(pushCharset(jsonObject.toJSONString()));
         }
     }
 
+    private String pushCharset(String content1) {
+        String str ;
+        try {
+            String osname = System.getProperties().getProperty("os.name");
+            if (osname.contains("Windows")) {
+                String result = new String(content1.getBytes("GBK"),"UTF-8");
+                str=result;
+            }else{
+                str=content1;
+            }
+        } catch (UnsupportedEncodingException e) {
+            throw new RuntimeException(e);
+        }
+        return str;
+    }
+
     private String pushFileUrl(long sdk, String msgtype, com.alibaba.fastjson.JSONObject jsonObject) {
         String url = uploadPath;
         String sdkfileid=null;
@@ -236,7 +278,6 @@ public class ConversationContentServiceImpl implements ConversationContentServic
             url = url +"/wechat/"+ System.currentTimeMillis()+".mp4";
             com.alibaba.fastjson.JSONObject image = jsonObject.getJSONObject("video");
         }
-
 //        下载文件
         downloadFile(sdk, sdkfileid, url);
         return url;