/// <summary> /// 下载消息文件 /// </summary> /// <param name="mMessage">消息</param> /// <param name="progress">进度回调</param> /// <param name="complite">完成回调</param> public void downloadFileAttchment(EMMessage mMessage, DownloadMessageAttchmentProgress progress, DownloadMessageAttchmentComplite complite) { new Thread(new ThreadStart(() => { if (mMessage.bodies().Length < 1) { return; } if (mMessage.bodies()[0].type == EMMessageBodyType.IMAGE) { downloadImageMainAttchment(mMessage, progress, complite); } else if (mMessage.bodies()[0].type == EMMessageBodyType.VIDEO) { downloadVideoMainAttchment(mMessage, progress, complite); } else if (mMessage.bodies()[0].type == EMMessageBodyType.FILE) { downloadFileMainAttchment(mMessage, progress, complite); } })).Start(); }
private void downloadImageMainAttchment(EMMessage mMessage, DownloadMessageAttchmentProgress progress, DownloadMessageAttchmentComplite complite) { EMImageMessageBody body = mMessage.bodies()[0] as EMImageMessageBody; body.setDownloadStatus(EMDownloadStatus.DOWNLOADING); mMessage.clearBodies(); mMessage.addBody(body); var conversation = EaseHelper.shard.client.getChatManager().conversationWithType(mMessage.conversationId(), DCUtilTool.GetMConversationType(mMessage.chatType()), true); conversation.updateMessage(mMessage); string dir = System.Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "\\Changliao" + "\\ChangLiao\\" + SettingMenager.shard.idCard; if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } string path = dir + "\\" + DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".jpg"; HttpUitls.Instance.DownloadFile(body.remotePath(), path, (p) => { if (progress != null) { progress(p); } }, (b) => { if (b) { var fi = new FileInfo(path); body.setFileLength(fi.Length); body.setLocalPath(path); Image image = Image.FromFile(path); body.setSize(new EMImageMessageBody.Size((double)image.Width, (double)image.Height)); body.setDownloadStatus(EMDownloadStatus.SUCCESSED); conversation.updateMessage(mMessage); complite(mMessage); } else { body.setDownloadStatus(EMDownloadStatus.FAILED); conversation.updateMessage(mMessage); complite(mMessage); } }); }
/// <summary> /// 显示消息预览文字 /// </summary> /// <param name="message">消息</param> /// <returns></returns> public static string getMessageShowTest(EMMessage message) { if (message == null) { return(string.Empty); } var bodys = message.bodies(); if (bodys.Length < 1) { return(""); } EMMessageBody body = bodys[0]; string text = ""; if (body.type == EMMessageBodyType.TEXT) { EMTextMessageBody textMessageBody = (EMTextMessageBody)body; if (textMessageBody.text().EndsWith("_encode")) { var arr = textMessageBody.text().Split('_'); text += DCEncrypt.Decrypt(arr[0], DCEncrypt.key); } else { text += textMessageBody.text(); } } if (body.type == EMMessageBodyType.VOICE) { text += "[语音]"; } if (body.type == EMMessageBodyType.VIDEO) { text += "[视频]"; } if (body.type == EMMessageBodyType.IMAGE) { text += "[图片]"; } if (body.type == EMMessageBodyType.LOCATION) { text += "[位置]"; } if (body.type == EMMessageBodyType.FILE) { text += "[文件]"; } if (message.from() == SettingMenager.shard.userID) { return(text); } if (message.chatType() == EMChatType.SINGLE) { var friend = DBHelper.Instance.getFriend(message.from()); if (friend == null) { var stronger = DBHelper.Instance.GetStronger(message.from()); if (stronger != null) { text = stronger.nickName + ":" + text; } } else { text = string.IsNullOrEmpty(friend.target_user_nickname) ? friend.friend_self_name : friend.target_user_nickname + ":" + text; } } else { var groupUser = DBHelper.Instance.GetGroupUser(message.from(), message.conversationId()); if (groupUser == null) { if (DBHelper.Instance.checkFriend(message.from())) { var friend = DBHelper.Instance.getFriend(message.from()); if (!string.IsNullOrEmpty(friend.target_user_nickname)) { text = friend.target_user_nickname + ":" + text; } else { text = friend.friend_self_name + ":" + text; } } else { var stronger = DBHelper.Instance.GetStronger(message.from()); if (stronger != null) { text = stronger.nickName + ":" + text; } } } else { if (DBHelper.Instance.checkFriend(message.from())) { text = groupUser.friend_name + ":" + text; } else if (!string.IsNullOrEmpty(groupUser.group_user_nickname)) { text = groupUser.group_user_nickname + ":" + text; } else { text = groupUser.user_name + ":" + text; } } } return(text); }
public MessageModel(EMMessage mMessage) { this.message = mMessage; if (message.bodies().Length < 1) { return; } bodyType = message.bodies()[0].type; chatType = message.chatType(); messageId = message.msgId(); firstbody = message.bodies()[0]; isRead = message.isRead(); if (message.msgDirection() == EMMessageDirection.SEND) { isSender = true; } else { isSender = false; } nickName = message.from(); if (bodyType == EMMessageBodyType.TEXT) { if (message.getAttribute("jpzim_is_big_expression", out isGifFace)) { if (isGifFace) { message.getAttribute("jpzim_big_expression_path", out gitFaceURL); message.getAttribute("faceH", out faceH); message.getAttribute("faceW", out faceW); if (!string.IsNullOrEmpty(gitFaceURL)) { DCWebImageMaanager.shard.downloadImageAsync(gitFaceURL, (image, b) => { this.image = image; if (faceH < 1 || faceW < 1) { faceW = image.Width; faceH = image.Height; } }); } text = "[动画表情]"; isRead = message.isRead(); setupUserInfo(); return; } } string typ; if (message.getAttribute("type", out typ)) { if (typ.Equals("person")) { isIDCard = true; isRead = message.isRead(); message.getAttribute("id", out IDCardID); return; } } EMTextMessageBody body = (EMTextMessageBody)message.bodies()[0]; text = body.text(); isRead = message.isRead(); if (text.EndsWith("_encode")) { var arr = text.Split('_'); text = DCEncrypt.Decrypt(arr[0], DCEncrypt.key); if (string.IsNullOrEmpty(text)) { text = body.text(); } } } else if (bodyType == EMMessageBodyType.IMAGE) { EMImageMessageBody body = (EMImageMessageBody)message.bodies()[0]; var file = body.localPath(); if (!string.IsNullOrEmpty(body.localPath()) && File.Exists(body.localPath())) { image = new Bitmap(body.localPath()); } fileLocalPath = body.localPath(); thumbnailFileLocalPath = body.thumbnailLocalPath(); fileURLPath = body.remotePath(); thumbnailFileURLPath = body.thumbnailRemotePath(); fileSize = body.fileLength(); imageSize = new Size(Convert.ToInt32(body.size().mWidth), Convert.ToInt32(body.size().mHeight)); thumbnailImageSize = new Size(Convert.ToInt32(body.thumbnailSize().mWidth), Convert.ToInt32(body.thumbnailSize().mWidth)); } else if (bodyType == EMMessageBodyType.VOICE) { EMVoiceMessageBody body = (EMVoiceMessageBody)message.bodies()[0]; fileLocalPath = body.localPath(); fileURLPath = body.remotePath(); fileSize = body.fileLength(); mediaDuration = body.duration(); } else if (bodyType == EMMessageBodyType.VIDEO) { EMVideoMessageBody body = (EMVideoMessageBody)message.bodies()[0]; fileLocalPath = body.localPath(); thumbnailFileLocalPath = body.thumbnailLocalPath(); fileURLPath = body.remotePath(); thumbnailFileURLPath = body.thumbnailRemotePath(); fileSize = body.fileLength(); if (string.IsNullOrEmpty(thumbnailFileLocalPath) && File.Exists(body.thumbnailLocalPath())) { thumbnailImage = Image.FromFile(thumbnailFileLocalPath); } thumbnailImageSize = new Size(Convert.ToInt32(body.size().mWidth), Convert.ToInt32(body.size().mHeight)); } else { if (bodyType == EMMessageBodyType.COMMAND) { return; } if (bodyType == EMMessageBodyType.LOCATION) { EMLocationMessageBody body1 = firstbody as EMLocationMessageBody; latitude = body1.latitude(); longitude = body1.longitude(); address = body1.address(); return; } EMFileMessageBody body = (EMFileMessageBody)message.bodies()[0]; fileLocalPath = body.localPath(); fileURLPath = body.remotePath(); fileSize = body.fileLength(); displayName = body.displayName(); fileSizeDes = getFileSize(); } isRead = message.isRead(); setupUserInfo(); }
private void downloadFileMainAttchment(EMMessage mMessage, DownloadMessageAttchmentProgress progress, DownloadMessageAttchmentComplite complite) { EMFileMessageBody body = mMessage.bodies()[0] as EMFileMessageBody; body.setDownloadStatus(EMDownloadStatus.DOWNLOADING); mMessage.clearBodies(); mMessage.addBody(body); var conversation = EaseHelper.shard.client.getChatManager().conversationWithType(mMessage.conversationId(), DCUtilTool.GetMConversationType(mMessage.chatType()), true); conversation.updateMessage(mMessage); string dir = System.Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "\\Changliao" + "\\ChangLiao\\" + SettingMenager.shard.idCard; if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } string path = dir + "\\" + body.displayName(); var p = path; int i = 0; while (File.Exists(p)) { i += 1; var arr = path.Split('.'); if (arr.Length > 2) { p = ""; for (int j = 0; j < arr.Length - 1; j++) { p += arr[j]; } p = p + "(" + i + ")." + arr[arr.Length - 1]; } else if (arr.Length == 2) { p = arr[0] + "(" + i + ")." + arr[1]; } else { p = path + "(" + i + ")"; } } HttpUitls.Instance.DownloadFile(body.remotePath(), p, (p1) => { if (progress != null) { progress(p1); } }, (b) => { if (b) { var fi = new FileInfo(path); body.setFileLength(fi.Length); body.setLocalPath(path); body.setDownloadStatus(EMDownloadStatus.SUCCESSED); conversation.updateMessage(mMessage); complite(mMessage); } else { body.setDownloadStatus(EMDownloadStatus.FAILED); conversation.updateMessage(mMessage); complite(mMessage); } }); }