You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
291 lines
12 KiB
291 lines
12 KiB
// |
|
// ChatPresenter.m |
|
// myWatch |
|
// |
|
// Created by xTT on 16/7/5. |
|
// Copyright © 2016年 xTT. All rights reserved. |
|
// |
|
|
|
#import "ChatPresenter.h" |
|
#import "ChatViewController.h" |
|
|
|
|
|
#import "CJFileUtility.h" |
|
#import "VoiceConverter.h" |
|
|
|
|
|
#import "MyMQTT.h" |
|
|
|
|
|
#define group_user_leaveTag 100 |
|
|
|
|
|
|
|
@interface ChatPresenter() |
|
|
|
@end |
|
|
|
@implementation ChatPresenter |
|
|
|
|
|
- (ChatViewController *)myVC{ |
|
|
|
|
|
return (ChatViewController *)self.vc; |
|
} |
|
|
|
- (void)P_getFamilyMessageRecv:(NSString *)messageid |
|
block:(void(^)(NSMutableArray *messageArr))block |
|
{ |
|
NSMutableDictionary *parameters = [NSMutableDictionary dictionary]; |
|
|
|
if (messageid) { |
|
NSDictionary *tmp = @{@"id":@{@"lt":messageid}}; |
|
[parameters setObject:tmp.mj_JSONString forKey:@"query"]; |
|
[parameters setObject:@"id" forKey:@"sortKey"]; |
|
} |
|
|
|
WEAKSELF |
|
[self.myVC.infoCircle getCircleMessageWithParameters:parameters |
|
success:^(id responseObject) |
|
{ |
|
NSMutableArray *tmp = [XHMessage mj_objectArrayWithKeyValuesArray:responseObject[@"messages"]]; |
|
NSMutableArray *arr = [NSMutableArray arrayWithArray:[[tmp reverseObjectEnumerator] allObjects]];//倒序操作 |
|
[arr enumerateObjectsUsingBlock:^(XHMessage *obj, NSUInteger idx, BOOL * _Nonnull stop) { |
|
[weakSelf.myVC setLastShowTimetampWithMessage:obj]; |
|
baseModel *model = [weakSelf.myVC.infoCircle getObjWithID:obj.senderID]; |
|
//MARK: 读取阅读记录的配置 |
|
if(obj.messageMediaType == 3 && obj.bubbleMessageType == XHBubbleMessageTypeReceiving){ |
|
NSArray * pathArr = [UserDefaults objectForKey:kReadRecordArrKey]; |
|
if(pathArr){ |
|
if([pathArr containsObject:obj.content]){//存在,那就是已读 |
|
obj.isRead = YES; |
|
}else{ //不存在就是未读 |
|
obj.isRead = NO; |
|
} |
|
} |
|
}else{ |
|
obj.isRead = YES; |
|
} |
|
|
|
|
|
obj.avatar = model.image; |
|
obj.avator = model.avator; |
|
obj.senderName = model.name; |
|
// obj.shouldShowUserName = YES; |
|
// if ([obj.type isEqualToNumber:@(10005)]) { |
|
// Device *device = [[User currentUser] getDeviceWithImei:obj.senderID]; |
|
// if ([obj.online isEqualToString:@"0"]) { |
|
// obj.content = [NSString stringWithFormat:@"%@下线了",device.name]; |
|
// }else{ |
|
// obj.content = [NSString stringWithFormat:@"%@上线了",device.name]; |
|
// } |
|
// } |
|
}]; |
|
block(arr); |
|
} failure:^{ |
|
block(nil); |
|
}]; |
|
|
|
} |
|
|
|
- (void)P_getSingleFamilyMessageRecv:(NSString *)imei |
|
page:(NSInteger)page |
|
block:(void(^)(NSMutableArray *messageArr))block |
|
{ |
|
NSMutableDictionary *parameters = [NSMutableDictionary dictionary]; |
|
[parameters setValue:@(page) forKey:@"page"]; |
|
[parameters setValue:cUser.openid forKey:@"openid"]; |
|
[parameters setValue:cUser.accesstoken forKey:@"token"]; |
|
[parameters setValue:@(page) forKey:@"page"]; |
|
[parameters setValue:@(1) forKey:@"sort"]; |
|
|
|
WEAKSELF |
|
[self.myVC.infoCircle getCircleSingleMessageWithImei:imei Parameters:parameters |
|
success:^(id responseObject) |
|
{ |
|
NSMutableArray *arr = [XHMessage mj_objectArrayWithKeyValuesArray:responseObject[@"messages"]]; |
|
//NSMutableArray *arr = [NSMutableArray arrayWithArray:[[tmp reverseObjectEnumerator] allObjects]];//倒序操作 |
|
[arr enumerateObjectsUsingBlock:^(XHMessage *obj, NSUInteger idx, BOOL * _Nonnull stop) { |
|
[weakSelf.myVC setLastShowTimetampWithMessage:obj]; |
|
baseModel *model = [weakSelf.myVC.infoCircle getObjWithID:obj.senderID]; |
|
//MARK: 读取阅读记录的配置 |
|
if(obj.messageMediaType == 3 && obj.bubbleMessageType == XHBubbleMessageTypeReceiving){ |
|
NSArray * pathArr = [UserDefaults objectForKey:kReadRecordArrKey]; |
|
if(pathArr){ |
|
if([pathArr containsObject:obj.content]){//存在,那就是已读 |
|
obj.isRead = YES; |
|
}else{ //不存在就是未读 |
|
obj.isRead = NO; |
|
} |
|
} |
|
}else{ |
|
obj.isRead = YES; |
|
} |
|
|
|
obj.avatar = obj.bubbleMessageType == XHBubbleMessageTypeSending ? cUser.image : model.image; |
|
obj.avator = obj.bubbleMessageType == XHBubbleMessageTypeSending ? cUser.avator : model.avator; |
|
obj.senderName = obj.bubbleMessageType == XHBubbleMessageTypeSending ? cUser.name : model.name; |
|
}]; |
|
block(arr); |
|
} failure:^{ |
|
block(nil); |
|
}]; |
|
|
|
} |
|
|
|
- (void)P_getFamilyMessageLastMsg:(NSString *)lastMsgid |
|
block:(void(^)(NSMutableArray *messageArr))block{ |
|
NSMutableDictionary *parameters = [NSMutableDictionary dictionary]; |
|
if (lastMsgid) { |
|
NSDictionary *tmp = @{@"id":@{@"gt":lastMsgid}}; |
|
[parameters setObject:tmp.mj_JSONString forKey:@"query"]; |
|
[parameters setObject:@"id" forKey:@"sortKey"]; |
|
} |
|
|
|
WEAKSELF |
|
[self.myVC.infoCircle getCircleMessageWithParameters:parameters |
|
success:^(id responseObject) |
|
{ |
|
NSMutableArray *tmp = [XHMessage mj_objectArrayWithKeyValuesArray:responseObject[@"messages"]]; |
|
NSMutableArray *arr = [NSMutableArray arrayWithArray:[[tmp reverseObjectEnumerator] allObjects]];//倒序操作 |
|
[arr enumerateObjectsUsingBlock:^(XHMessage *obj, NSUInteger idx, BOOL * _Nonnull stop) { |
|
[weakSelf.myVC setLastShowTimetampWithMessage:obj]; |
|
baseModel *model = [weakSelf.myVC.infoCircle getObjWithID:obj.senderID]; |
|
//MARK: 读取阅读记录的配置 |
|
if(obj.messageMediaType == 3 && obj.bubbleMessageType == XHBubbleMessageTypeReceiving){ |
|
NSArray * pathArr = [UserDefaults objectForKey:kReadRecordArrKey]; |
|
if(pathArr){ |
|
if([pathArr containsObject:obj.content]){//存在,那就是已读 |
|
obj.isRead = YES; |
|
}else{ //不存在就是未读 |
|
obj.isRead = NO; |
|
} |
|
} |
|
}else{ |
|
obj.isRead = YES; |
|
} |
|
|
|
obj.avatar = model.image; |
|
obj.avator = model.avator; |
|
obj.senderName = model.name; |
|
|
|
}]; |
|
block(arr); |
|
} failure:^{ |
|
block(nil); |
|
}]; |
|
} |
|
|
|
|
|
#pragma mark 初始化数据 |
|
- (void)P_initData:(void(^)())block{ |
|
self.isSended = YES; |
|
[[NSNotificationCenter defaultCenter] addObserver:self |
|
selector:@selector(reloadTableView) |
|
name:@"reloadCircleDataView" |
|
object:nil]; |
|
|
|
[[NSNotificationCenter defaultCenter] addObserverForName:PUSH_type_chatGroupEnter |
|
object:nil |
|
queue:[NSOperationQueue mainQueue] |
|
usingBlock:^(NSNotification * _Nonnull note){ |
|
[self.myVC.infoCircle getCircleInfoSuccess:^{} |
|
failure:^{}]; |
|
}]; |
|
[[NSNotificationCenter defaultCenter] addObserverForName:PUSH_type_chatGroupMessage |
|
object:nil |
|
queue:[NSOperationQueue mainQueue] |
|
usingBlock:^(NSNotification * _Nonnull note) |
|
{ |
|
dispatch_async(dispatch_get_global_queue(0, 0), ^{ |
|
if ([note.object[@"groupid"] isEqualToString:self.myVC.infoCircle.groupid]) { |
|
while (!self.isSended) { |
|
[NSThread sleepForTimeInterval:0.3]; |
|
} |
|
dispatch_async(dispatch_get_main_queue(), ^(void) { |
|
NSArray *arr = [[self.myVC.messages reverseObjectEnumerator] allObjects]; |
|
__block BOOL hasMSG = NO; |
|
[arr enumerateObjectsUsingBlock:^(XHMessage *obj, NSUInteger idx, BOOL * _Nonnull stop) { |
|
if ([note.object[@"message"][@"id"] isEqualToString:obj.id]) { |
|
hasMSG = YES; |
|
*stop = YES; |
|
} |
|
}]; |
|
if (!hasMSG) { |
|
XHMessage *messgae = [XHMessage mj_objectWithKeyValues:note.object[@"message"]]; |
|
baseModel *model = [self.myVC.infoCircle getObjWithID:messgae.senderID]; |
|
messgae.isRead = NO; |
|
messgae.avatar = model.image; |
|
messgae.avator = model.avator; |
|
messgae.senderName = model.name; |
|
// messgae.shouldShowUserName = YES; |
|
[self.myVC addMessage:messgae]; //添加数据 |
|
} |
|
}); |
|
|
|
} |
|
}); |
|
|
|
}]; |
|
|
|
// [[NSNotificationCenter defaultCenter] addObserver:self |
|
// selector:@selector(oldTalkStatusWithAPNS:) |
|
// name:xAPNS_talk_status |
|
// object:nil]; |
|
block(); |
|
} |
|
|
|
- (void)reloadTableView{ |
|
self.blockRefreshUIData(); |
|
} |
|
|
|
|
|
#pragma 播放语音 |
|
|
|
- (void)P_playVoiceWithMessage:(XHMessage *)message block:(void(^)(NSString *wavPath))block{ |
|
NSString *path = [CJFileUtility documentsPathSubDir:@"msg_audio" andFile:message.id]; |
|
NSString *wavPath = [path stringByAppendingPathExtension:@"wav"]; |
|
NSString *amrPath = [path stringByAppendingPathExtension:@"amr"]; |
|
|
|
if ([CJFileUtility fileExists:wavPath] || [CJFileUtility fileExists:amrPath]){ |
|
if (![CJFileUtility fileExists:wavPath] && [CJFileUtility fileExists:amrPath]) { |
|
[self amrToWav:amrPath]; |
|
} |
|
block (wavPath); |
|
}else{ |
|
WEAKSELF |
|
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:message.content]]; |
|
[[xMyHttp downloadTaskWithRequest:request |
|
progress:nil |
|
destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) { |
|
return [NSURL fileURLWithPath:amrPath]; |
|
} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) { |
|
if (![CJFileUtility fileExists:wavPath] && [CJFileUtility fileExists:amrPath]) { |
|
[weakSelf amrToWav:amrPath]; |
|
} |
|
block(wavPath); |
|
}] resume]; |
|
} |
|
} |
|
|
|
- (void)wavToAmr:(NSString *)path { |
|
//转格式 |
|
if ([CJFileUtility fileExists:path]) { |
|
NSString *armPath = [CJFileUtility changeFileExt:path ext:@"amr"]; |
|
[VoiceConverter wavToAmr:path amrSavePath:armPath]; |
|
} |
|
} |
|
|
|
//转换后删掉amr文件 |
|
- (void)amrToWav:(NSString *)path { |
|
//转格式 |
|
if ([CJFileUtility fileExists:path]) { |
|
NSString *wavPath = [CJFileUtility changeFileExt:path ext:@"wav"]; |
|
[VoiceConverter amrToWav:path wavSavePath:wavPath]; |
|
[CJFileUtility deleteFile:path]; |
|
} |
|
} |
|
|
|
|
|
@end
|
|
|