TMCustomMessage.m 2.7 KB

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