TMCustomMessage.m 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //
  2. // TMCustomMessage.m
  3. // Timi
  4. //
  5. // Created by 翟玉磊 on 2021/5/11.
  6. //
  7. #import "TMCustomMessage.h"
  8. #import "NSString+YYAdd.h"
  9. #import "NSData+YYAdd.h"
  10. #import "Macro.h"
  11. static NSString * const RTC_CONVENE_GIFT_NOTICE = @"private:chat:gift:settle";
  12. @implementation TMCustomMessage
  13. ///消息是否存储,是否计入未读数
  14. + (RCMessagePersistent)persistentFlag {
  15. return MessagePersistent_NONE;
  16. }
  17. /// NSCoding
  18. - (instancetype)initWithCoder:(NSCoder *)aDecoder {
  19. self = [super init];
  20. if (self) {
  21. self.data = [aDecoder decodeObjectForKey:@"data"];
  22. }
  23. return self;
  24. }
  25. /// NSCoding
  26. - (void)encodeWithCoder:(NSCoder *)aCoder {
  27. [aCoder encodeObject:self.data forKey:@"data"];
  28. }
  29. ///将消息内容编码成json
  30. - (NSData *)encode {
  31. NSMutableDictionary *dataDict = [NSMutableDictionary dictionary];
  32. [dataDict setObject:self.data forKey:@"data"];
  33. if (self.senderUserInfo) {
  34. [dataDict setObject:[self encodeUserInfo:self.senderUserInfo] forKey:@"user"];
  35. }
  36. NSData *data = [NSJSONSerialization dataWithJSONObject:dataDict options:kNilOptions error:nil];
  37. return data;
  38. }
  39. ///将json解码生成消息内容
  40. - (void)decodeWithData:(NSData *)data {
  41. if (data) {
  42. __autoreleasing NSError *error = nil;
  43. NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
  44. if (dictionary) {
  45. self.data = dictionary[@"data"];
  46. NSDictionary *userinfoDic = dictionary[@"user"];
  47. [self decodeUserInfo:userinfoDic];
  48. }
  49. }
  50. }
  51. /// 会话列表中显示的摘要
  52. - (NSString *)conversationDigest {
  53. NSString *str = @"";
  54. NSDictionary *param = [self.data jsonValueDecoded];
  55. if (param != nil) {
  56. //自定义TIMCustomElem首页默认显示
  57. NSString *type = [@"12" toString:param[@"type"]];
  58. if ([type isEqualToString:RTC_CONVENE_GIFT_NOTICE]) {
  59. NSString *senderUserName = [@"12" toString:param[@"senderUserName"]];
  60. NSString *targetUserName = [@"12" toString:param[@"targetUserName"]];
  61. NSString *giftName = [@"12" toString:param[@"giftName"]];
  62. NSInteger quantity= [param[@"quantity"] integerValue];
  63. str = [NSString stringWithFormat:@"%@ 送给 %@ %@ x%ld", senderUserName, targetUserName, giftName, quantity];
  64. }else{
  65. NSString *msg = [@"12" toString:param[@"msg"]];
  66. NSString *msgContent = [@"12" toString:param[@"msgContent"]];
  67. if (StringIsEmpty(msg)) {
  68. str = msgContent;
  69. }else {
  70. str = msg;
  71. }
  72. }
  73. }
  74. if (StringIsNotEmpty(str)) {
  75. return str;
  76. }
  77. return @"";
  78. }
  79. ///消息的类型名
  80. + (NSString *)getObjectName {
  81. return TMCustomMessageIdentifier;
  82. }
  83. @end