TMPrivateChatGiftMessage.m 3.0 KB

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