CustomElemCmd.m 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //
  2. // CustomElemCmd.m
  3. // TIMChat
  4. //
  5. // Created by wilderliao on 16/6/22.
  6. // Copyright © 2016年 AlexiChen. All rights reserved.
  7. //
  8. #import "CustomElemCmd.h"
  9. @implementation CustomElemCmd
  10. - (instancetype)initWith:(NSInteger)command
  11. {
  12. if (self = [super init])
  13. {
  14. _userAction = command;
  15. }
  16. return self;
  17. }
  18. - (instancetype)initWith:(NSInteger)command param:(NSString *)param
  19. {
  20. if (self = [super init])
  21. {
  22. _userAction = command;
  23. _actionParam = param;
  24. }
  25. return self;
  26. }
  27. + (instancetype)parseCustom:(TIMCustomElem *)elem
  28. {
  29. NSData *data = elem.data;
  30. if (data)
  31. {
  32. NSString *dataStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  33. CustomElemCmd *parse = [NSObject parse:[CustomElemCmd class] jsonString:dataStr];
  34. if (parse.msgType == EIMAMSG_InputStatus || parse.msgType == EIMAMSG_SaftyTip)
  35. {
  36. parse.customInfo = [parse.actionParam objectFromJSONString];
  37. return parse;
  38. }
  39. }
  40. DebugLog(@"自定义消息不是CustomElemCmd类型");
  41. return nil;
  42. }
  43. - (NSData *)packToSendData
  44. {
  45. NSMutableDictionary *post = [NSMutableDictionary dictionary];
  46. [post setObject:@(_userAction) forKey:@"userAction"];
  47. if (_actionParam && _actionParam.length > 0)
  48. {
  49. [post setObject:_actionParam forKey:@"actionParam"];
  50. }
  51. if ([NSJSONSerialization isValidJSONObject:post])
  52. {
  53. NSError *error = nil;
  54. NSData *data = [NSJSONSerialization dataWithJSONObject:post options:NSJSONWritingPrettyPrinted error:&error];
  55. if(error)
  56. {
  57. DebugLog(@"[%@] Post Json Error: %@", [self class], post);
  58. return nil;
  59. }
  60. DebugLog(@"CustomElemCmd content is %@", post);
  61. return data;
  62. }
  63. else
  64. {
  65. DebugLog(@"[%@] CustomElemCmd is not valid: %@", [self class], post);
  66. return nil;
  67. }
  68. }
  69. - (void)prepareForRender
  70. {
  71. // 因不用于显示,作空实现
  72. // do nothing
  73. }
  74. - (NSInteger)msgType
  75. {
  76. return _userAction;
  77. }
  78. @end