Açıklama Yok

ChatSendHelper.m 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /************************************************************
  2. * * EaseMob CONFIDENTIAL
  3. * __________________
  4. * Copyright (C) 2013-2014 EaseMob Technologies. All rights reserved.
  5. *
  6. * NOTICE: All information contained herein is, and remains
  7. * the property of EaseMob Technologies.
  8. * Dissemination of this information or reproduction of this material
  9. * is strictly forbidden unless prior written permission is obtained
  10. * from EaseMob Technologies.
  11. */
  12. #import "ChatSendHelper.h"
  13. #import "ConvertToCommonEmoticonsHelper.h"
  14. @implementation ChatSendHelper
  15. +(EMMessage *)sendTextMessageWithString:(NSString *)str
  16. toUsername:(NSString *)username
  17. isChatGroup:(BOOL)isChatGroup
  18. requireEncryption:(BOOL)requireEncryption
  19. {
  20. NSString *willSendText = [ConvertToCommonEmoticonsHelper convertToCommonEmoticons:str];
  21. EMTextMessageBody *body = [[EMTextMessageBody alloc] initWithText:willSendText];
  22. return [self sendMessage:username messageBody:body isChatGroup:isChatGroup requireEncryption:requireEncryption];
  23. }
  24. +(EMMessage *)sendTextMessageWithString:(NSString *)str
  25. toUsername:(NSString *)username
  26. messageType:(BOOL)isGroup
  27. requireEncryption:(BOOL)requireEncryption
  28. ext:(NSDictionary *)ext
  29. {
  30. NSString *willSendText = [ConvertToCommonEmoticonsHelper convertToCommonEmoticons:str];
  31. EMTextMessageBody *body = [[EMTextMessageBody alloc] initWithText:willSendText];
  32. return [self sendMessage:username messageBody:body messageType:isGroup requireEncryption:requireEncryption ext:ext];
  33. }
  34. +(EMMessage *)sendImageMessageWithImage:(UIImage *)image
  35. toUsername:(NSString *)username
  36. isChatGroup:(BOOL)isChatGroup
  37. requireEncryption:(BOOL)requireEncryption
  38. {
  39. NSData *data = UIImagePNGRepresentation(image);
  40. if (!data) {
  41. data = UIImageJPEGRepresentation(image, 0.6f);
  42. }
  43. EMImageMessageBody *body = [[EMImageMessageBody alloc] initWithData:data displayName:@"image.jpg"];
  44. return [self sendMessage:username messageBody:body isChatGroup:isChatGroup requireEncryption:requireEncryption];
  45. }
  46. +(EMMessage *)sendLocationLatitude:(double)latitude
  47. longitude:(double)longitude
  48. address:(NSString *)address
  49. toUsername:(NSString *)username
  50. isChatGroup:(BOOL)isChatGroup
  51. requireEncryption:(BOOL)requireEncryption
  52. {
  53. return nil;
  54. }
  55. // 发送消息
  56. +(EMMessage *)sendMessage:(NSString *)username
  57. messageBody:(EMMessageBody *)body
  58. isChatGroup:(BOOL)isChatGroup
  59. requireEncryption:(BOOL)requireEncryption
  60. {
  61. EMMessage *retureMsg = [[EMMessage alloc] initWithConversationID:username
  62. from:[[EMClient sharedClient] currentUsername]
  63. to:username
  64. body:body
  65. ext:nil];
  66. retureMsg.chatType = (isChatGroup ? EMChatTypeGroupChat : EMChatTypeChat);
  67. NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
  68. [mutableDict setValue:[ChatSendHelper commonSenderExt]
  69. forKey:@"sender_info"];
  70. retureMsg.ext = mutableDict;
  71. [[EMClient sharedClient].chatManager sendMessage:retureMsg
  72. progress:^(int progress) {
  73. retureMsg.status = EMMessageStatusDelivering;
  74. } completion:^(EMMessage *message, EMError *error) {
  75. retureMsg.status = EMMessageStatusSuccessed;
  76. }];
  77. return retureMsg;
  78. }
  79. // 发送消息
  80. +(EMMessage *)sendMessage:(NSString *)username
  81. messageBody:(EMMessageBody *)body
  82. messageType:(BOOL)isGroup
  83. requireEncryption:(BOOL)requireEncryption
  84. ext:(NSDictionary *)ext
  85. {
  86. EMMessage *retureMsg = [[EMMessage alloc] initWithConversationID:username
  87. from:[[EMClient sharedClient] currentUsername]
  88. to:username
  89. body:body
  90. ext:ext];
  91. retureMsg.chatType = (isGroup ? EMChatTypeGroupChat : EMChatTypeChat);
  92. NSMutableDictionary *mutableDict = (ext ? [NSMutableDictionary dictionaryWithDictionary:ext] : [NSMutableDictionary dictionary]);
  93. [mutableDict setValue:[ChatSendHelper commonSenderExt] forKey:@"sender_info"];
  94. NSString *goodID = ext[EXT_MSG_GOODS_ID_V2];
  95. if (goodID.length > 0) {
  96. [mutableDict setValue:goodID forKey:EXT_MSG_GOODS_ID_V2];
  97. }
  98. NSMutableDictionary *weichatDict = [NSMutableDictionary dictionary];
  99. NSDictionary *oldKeys = ext[@"weichat"];
  100. for (NSString *key in oldKeys) {
  101. [weichatDict setValue:ext[@"weichat"][key] forKey:key];
  102. }
  103. User *user = [FKUserManager sharedManager].user;
  104. if (user.nickName.length > 0) {
  105. NSMutableDictionary *visitorDict = [NSMutableDictionary dictionary];
  106. NSString *nick = [NSString stringWithFormat:@"%@(%@)", [FLStringHelper replaceNilWithEmpty:user.nickName], user.userID];
  107. [visitorDict setValue:nick forKey:@"userNickname"];
  108. if ([user isVIP]) {
  109. [visitorDict setValue:@[@"vip1"] forKey:@"tags"];
  110. }
  111. [weichatDict setValue:visitorDict forKey:@"visitor"];
  112. }
  113. NSString *queueName = ext[EXT_QUEUE_NAME];
  114. if (queueName) {
  115. [weichatDict setValue:queueName forKey:@"queueName"];
  116. }
  117. [mutableDict setValue:weichatDict forKey:@"weichat"];
  118. retureMsg.ext = mutableDict;
  119. [[EMClient sharedClient].chatManager sendMessage:retureMsg
  120. progress:^(int progress) {
  121. retureMsg.status = EMMessageStatusDelivering;
  122. } completion:^(EMMessage *message, EMError *error) {
  123. retureMsg.status = EMMessageStatusSuccessed;
  124. }];
  125. return retureMsg;
  126. }
  127. + (NSDictionary *)commonSenderExt {
  128. User *user = [FKUserManager sharedManager].user;
  129. NSDictionary *dict = @{@"user_id": [FLStringHelper replaceNilWithEmpty:user.userID],
  130. @"nickname": [FLStringHelper replaceNilWithEmpty:user.nickName],
  131. @"head_url": [FLStringHelper replaceNilWithEmpty:user.headurl]};
  132. return dict;
  133. }
  134. @end