| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- //
- // CustomElemCmd.m
- // TIMChat
- //
- // Created by wilderliao on 16/6/22.
- // Copyright © 2016年 AlexiChen. All rights reserved.
- //
- #import "CustomElemCmd.h"
- @implementation CustomElemCmd
- - (instancetype)initWith:(NSInteger)command
- {
- if (self = [super init])
- {
- _userAction = command;
- }
- return self;
- }
- - (instancetype)initWith:(NSInteger)command param:(NSString *)param
- {
- if (self = [super init])
- {
- _userAction = command;
- _actionParam = param;
- }
- return self;
- }
- + (instancetype)parseCustom:(TIMCustomElem *)elem
- {
- NSData *data = elem.data;
- if (data)
- {
- NSString *dataStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
- CustomElemCmd *parse = [NSObject parse:[CustomElemCmd class] jsonString:dataStr];
- if (parse.msgType == EIMAMSG_InputStatus || parse.msgType == EIMAMSG_SaftyTip)
- {
- parse.customInfo = [parse.actionParam objectFromJSONString];
-
- return parse;
- }
- }
-
- DebugLog(@"自定义消息不是CustomElemCmd类型");
- return nil;
-
- }
- - (NSData *)packToSendData
- {
- NSMutableDictionary *post = [NSMutableDictionary dictionary];
- [post setObject:@(_userAction) forKey:@"userAction"];
-
- if (_actionParam && _actionParam.length > 0)
- {
- [post setObject:_actionParam forKey:@"actionParam"];
- }
-
- if ([NSJSONSerialization isValidJSONObject:post])
- {
- NSError *error = nil;
- NSData *data = [NSJSONSerialization dataWithJSONObject:post options:NSJSONWritingPrettyPrinted error:&error];
- if(error)
- {
- DebugLog(@"[%@] Post Json Error: %@", [self class], post);
- return nil;
- }
-
- DebugLog(@"CustomElemCmd content is %@", post);
- return data;
- }
- else
- {
- DebugLog(@"[%@] CustomElemCmd is not valid: %@", [self class], post);
- return nil;
- }
- }
- - (void)prepareForRender
- {
- // 因不用于显示,作空实现
- // do nothing
- }
- - (NSInteger)msgType
- {
- return _userAction;
- }
- @end
|