/************************************************************ * * EaseMob CONFIDENTIAL * __________________ * Copyright (C) 2013-2014 EaseMob Technologies. All rights reserved. * * NOTICE: All information contained herein is, and remains * the property of EaseMob Technologies. * Dissemination of this information or reproduction of this material * is strictly forbidden unless prior written permission is obtained * from EaseMob Technologies. */ #import "ChatSendHelper.h" #import "ConvertToCommonEmoticonsHelper.h" @implementation ChatSendHelper +(EMMessage *)sendTextMessageWithString:(NSString *)str toUsername:(NSString *)username isChatGroup:(BOOL)isChatGroup requireEncryption:(BOOL)requireEncryption { NSString *willSendText = [ConvertToCommonEmoticonsHelper convertToCommonEmoticons:str]; EMTextMessageBody *body = [[EMTextMessageBody alloc] initWithText:willSendText]; return [self sendMessage:username messageBody:body isChatGroup:isChatGroup requireEncryption:requireEncryption]; } +(EMMessage *)sendTextMessageWithString:(NSString *)str toUsername:(NSString *)username messageType:(BOOL)isGroup requireEncryption:(BOOL)requireEncryption ext:(NSDictionary *)ext { NSString *willSendText = [ConvertToCommonEmoticonsHelper convertToCommonEmoticons:str]; EMTextMessageBody *body = [[EMTextMessageBody alloc] initWithText:willSendText]; return [self sendMessage:username messageBody:body messageType:isGroup requireEncryption:requireEncryption ext:ext]; } +(EMMessage *)sendImageMessageWithImage:(UIImage *)image toUsername:(NSString *)username isChatGroup:(BOOL)isChatGroup requireEncryption:(BOOL)requireEncryption { NSData *data = UIImagePNGRepresentation(image); if (!data) { data = UIImageJPEGRepresentation(image, 0.6f); } EMImageMessageBody *body = [[EMImageMessageBody alloc] initWithData:data displayName:@"image.jpg"]; return [self sendMessage:username messageBody:body isChatGroup:isChatGroup requireEncryption:requireEncryption]; } +(EMMessage *)sendLocationLatitude:(double)latitude longitude:(double)longitude address:(NSString *)address toUsername:(NSString *)username isChatGroup:(BOOL)isChatGroup requireEncryption:(BOOL)requireEncryption { return nil; } // 发送消息 +(EMMessage *)sendMessage:(NSString *)username messageBody:(EMMessageBody *)body isChatGroup:(BOOL)isChatGroup requireEncryption:(BOOL)requireEncryption { EMMessage *retureMsg = [[EMMessage alloc] initWithConversationID:username from:[[EMClient sharedClient] currentUsername] to:username body:body ext:nil]; retureMsg.chatType = (isChatGroup ? EMChatTypeGroupChat : EMChatTypeChat); NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary]; [mutableDict setValue:[ChatSendHelper commonSenderExt] forKey:@"sender_info"]; retureMsg.ext = mutableDict; [[EMClient sharedClient].chatManager sendMessage:retureMsg progress:^(int progress) { retureMsg.status = EMMessageStatusDelivering; } completion:^(EMMessage *message, EMError *error) { retureMsg.status = EMMessageStatusSuccessed; }]; return retureMsg; } // 发送消息 +(EMMessage *)sendMessage:(NSString *)username messageBody:(EMMessageBody *)body messageType:(BOOL)isGroup requireEncryption:(BOOL)requireEncryption ext:(NSDictionary *)ext { EMMessage *retureMsg = [[EMMessage alloc] initWithConversationID:username from:[[EMClient sharedClient] currentUsername] to:username body:body ext:ext]; retureMsg.chatType = (isGroup ? EMChatTypeGroupChat : EMChatTypeChat); NSMutableDictionary *mutableDict = (ext ? [NSMutableDictionary dictionaryWithDictionary:ext] : [NSMutableDictionary dictionary]); [mutableDict setValue:[ChatSendHelper commonSenderExt] forKey:@"sender_info"]; NSString *goodID = ext[EXT_MSG_GOODS_ID_V2]; if (goodID.length > 0) { [mutableDict setValue:goodID forKey:EXT_MSG_GOODS_ID_V2]; } NSMutableDictionary *weichatDict = [NSMutableDictionary dictionary]; NSDictionary *oldKeys = ext[@"weichat"]; for (NSString *key in oldKeys) { [weichatDict setValue:ext[@"weichat"][key] forKey:key]; } User *user = [FKUserManager sharedManager].user; if (user.nickName.length > 0) { NSMutableDictionary *visitorDict = [NSMutableDictionary dictionary]; NSString *nick = [NSString stringWithFormat:@"%@(%@)", [FLStringHelper replaceNilWithEmpty:user.nickName], user.userID]; [visitorDict setValue:nick forKey:@"userNickname"]; if ([user isVIP]) { [visitorDict setValue:@[@"vip1"] forKey:@"tags"]; } [weichatDict setValue:visitorDict forKey:@"visitor"]; } NSString *queueName = ext[EXT_QUEUE_NAME]; if (queueName) { [weichatDict setValue:queueName forKey:@"queueName"]; } [mutableDict setValue:weichatDict forKey:@"weichat"]; retureMsg.ext = mutableDict; [[EMClient sharedClient].chatManager sendMessage:retureMsg progress:^(int progress) { retureMsg.status = EMMessageStatusDelivering; } completion:^(EMMessage *message, EMError *error) { retureMsg.status = EMMessageStatusSuccessed; }]; return retureMsg; } + (NSDictionary *)commonSenderExt { User *user = [FKUserManager sharedManager].user; NSDictionary *dict = @{@"user_id": [FLStringHelper replaceNilWithEmpty:user.userID], @"nickname": [FLStringHelper replaceNilWithEmpty:user.nickName], @"head_url": [FLStringHelper replaceNilWithEmpty:user.headurl]}; return dict; } @end