Brak opisu

MessageViewModel.m 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. //
  2. // MessageViewModel.m
  3. // FirstLink
  4. //
  5. // Created by unicode on 14-11-6.
  6. // Copyright (c) 2014年 FirstLink. All rights reserved.
  7. //
  8. #import "MessageViewModel.h"
  9. #import "FKAppMessage.h"
  10. #import "FKMessageManager.h"
  11. #import "FKPersonOrderReform.h"
  12. #import "FKHomeReform.h"
  13. @interface MessageViewModel ()
  14. @property (nonatomic, strong) NSMutableArray *todayMessages;
  15. @property (nonatomic, strong) NSMutableArray *last7dayMessages;
  16. @property (nonatomic, strong) NSMutableArray *last30dayMessages;
  17. @property (nonatomic, strong) NSMutableArray *earilerMessages;
  18. @property (nonatomic, strong) NSMutableDictionary *sectionTypeDict;
  19. @end
  20. @implementation MessageViewModel
  21. + (void)requestUsers:(NSArray *)userIDs success:(void (^)(MSGHeader *, id))success failure:(void (^)(MSGHeader *, NSError *))failure {
  22. if (userIDs.count == 0) {
  23. return;
  24. }
  25. NSString *stringID = [MessageViewModel stringIDs:userIDs];
  26. NSString *URLString = [NSString stringWithFormat:@"%@/link-site/api/find_em_users.json?ids=%@", [[FKServerUtil sharedInstance] apiServer], stringID];
  27. FLDataCenter *dataCenter = [FLDataCenter sharedDataCenter];
  28. [dataCenter POST:URLString parameters:[FLRequestHelper commonParamater] success:^(MSGHeader *header, id responseObject) {
  29. if ([header.code intValue] == RESPONSE_MSG_NORMAL) {
  30. success(header, [MessageViewModel parserUser:responseObject]);
  31. } else {
  32. success(header, responseObject);
  33. }
  34. } failure:^(MSGHeader *header, NSError *error) {
  35. failure(header, error);
  36. }];
  37. }
  38. + (NSString*)stringIDs:(NSArray*)userIDs {
  39. if (userIDs.count == 0) {
  40. return @"";
  41. }
  42. NSMutableString *mutableString = [NSMutableString string];
  43. for (NSString *stringID in userIDs) {
  44. [mutableString appendFormat:@"%@,", stringID];
  45. }
  46. return [mutableString substringToIndex:(mutableString.length - 1)];
  47. }
  48. + (NSMutableDictionary*)parserUser:(NSDictionary*)json {
  49. if (json[@"data"] != [NSNull null]) {
  50. NSMutableDictionary *mutableDictionary = [NSMutableDictionary dictionary];
  51. NSDictionary *tempDict = nil;
  52. for (NSDictionary *dict in json[@"data"][@"users"]) {
  53. if (dict[@"id"] != [NSNull null]) {
  54. tempDict = @{@"headURL": dict[@"head_pic"],
  55. @"nickname": dict[@"nickname"]};
  56. [mutableDictionary setValue:tempDict forKey:[NSString stringWithFormat:@"%@", dict[@"id"]]];
  57. }
  58. }
  59. return mutableDictionary;
  60. }
  61. return nil;
  62. }
  63. #pragma mark - Request MSG
  64. - (void)requestMessageWithIndex:(NSString *)index
  65. AnchorID:(NSString *)anchorID
  66. startRow:(NSString *)startRow
  67. pageSize:(NSString *)pageSize
  68. type:(NSString *)type
  69. success:(void (^)(MSGHeader *, id, NSString *))success
  70. failure:(void (^)(MSGHeader *, NSError *))failure {
  71. NSString *itemJSONString;
  72. if (startRow && pageSize ) {
  73. itemJSONString = [self itemJSONStringWithIndex:index
  74. AnchorID:anchorID
  75. startRow:startRow
  76. pageSize:pageSize
  77. type:type];
  78. }
  79. NSMutableDictionary *para = [NSMutableDictionary dictionary];
  80. [para setValue:itemJSONString forKey:@"user_notify_message_json"];
  81. [para addEntriesFromDictionary:[FLRequestHelper commonParamater]];
  82. NSString *URLString = [NSString stringWithFormat:@"%@/link-site/api/message/find_user_notify_messages.json?", [[FKServerUtil sharedInstance] apiServer]];
  83. WeakSelf(weakSelf);
  84. FLDataCenter *dataCenter = [FLDataCenter sharedDataCenter];
  85. [dataCenter POST:URLString
  86. parameters:para
  87. success:^(MSGHeader *header, id responseObject)
  88. {
  89. if ([header.code intValue] == RESPONSE_MSG_NORMAL) {
  90. weakSelf.pageHeader = [PageHeader parserHeader:responseObject];
  91. success(header, [weakSelf parseMessages:responseObject], [MessageViewModel parseServerTime:responseObject]);
  92. } else {
  93. success(header, responseObject, nil);
  94. }
  95. } failure:^(MSGHeader *header, NSError *error) {
  96. failure(header, error);
  97. }];
  98. }
  99. - (NSString*)itemJSONStringWithIndex:(NSString*)index
  100. AnchorID:(NSString*)anchorID
  101. startRow:(NSString*)startRow
  102. pageSize:(NSString*)pageSize
  103. type:(NSString*)type {
  104. NSDictionary *itemDict = [NSMutableDictionary dictionary];
  105. if (index) {
  106. [itemDict setValue:index forKey:@"index"];
  107. }
  108. if (anchorID) {
  109. [itemDict setValue:anchorID forKey:@"anchor_id"];
  110. }
  111. if (startRow) {
  112. [itemDict setValue:startRow forKey:@"start_row"];
  113. }
  114. if (pageSize) {
  115. [itemDict setValue:pageSize forKey:@"page_size"];
  116. }
  117. if (type) {
  118. [itemDict setValue:type forKey:@"message_type"];
  119. }
  120. NSError *error;
  121. NSData *jsonData = [NSJSONSerialization dataWithJSONObject:itemDict
  122. options:NSJSONWritingPrettyPrinted
  123. error:&error];
  124. if (!jsonData) {
  125. NSLog(@"itemJSONString error:%@", error.localizedDescription);
  126. return nil;
  127. }
  128. return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
  129. }
  130. - (NSMutableArray*)parseMessages:(NSDictionary *)json {
  131. if (json[@"data"] != [NSNull null]) {
  132. NSMutableArray *msgArray = [NSMutableArray array];
  133. for (NSDictionary *dict in json[@"data"][@"list"]) {
  134. FKAppMessage *msg = [[FKAppMessage alloc] init];
  135. [msg mts_setValuesForKeysWithDictionary:dict];
  136. if (msg) {
  137. [msgArray addObject:msg];
  138. }
  139. }
  140. return msgArray;
  141. }
  142. return nil;
  143. }
  144. + (NSString *)parseServerTime:(NSDictionary *)json {
  145. if ([json[@"data"][@"server_time"] isKindOfClass:[NSString class]]) {
  146. return json[@"data"][@"server_time"];
  147. }
  148. return nil;
  149. }
  150. #pragma mark -
  151. + (void)requestBadgeSuccess:(void (^)(MSGHeader *header, FKBadgeMessage *badge, FKAppMessage *comment))success
  152. failure:(void (^)(MSGHeader *, NSError *))failure {
  153. NSString *URLString = [NSString stringWithFormat:@"%@/link-site/api/message/find_unread_msg_num.json", [[FKServerUtil sharedInstance] apiServer]];
  154. FLDataCenter *dataCenter = [FLDataCenter sharedDataCenter];
  155. [dataCenter POST:URLString
  156. parameters:[FLRequestHelper commonParamater]
  157. success:^(MSGHeader *header, id responseObject)
  158. {
  159. if ([header.code intValue] == RESPONSE_MSG_NORMAL) {
  160. success(header, [MessageViewModel parseBadge:responseObject], [MessageViewModel parseComment:responseObject]);
  161. } else {
  162. success(header, nil, nil);
  163. }
  164. } failure:^(MSGHeader *header, NSError *error) {
  165. failure(header, error);
  166. }];
  167. }
  168. + (void)requestOrderBadgeSuccess:(void (^)(MSGHeader *, FKPersonOrderBadgeItem *))success
  169. failure:(void (^)(MSGHeader *, NSError *))failure {
  170. NSString *URLString = [NSString stringWithFormat:@"%@/link-site/api/payment_order/find_payment_order_count.json", [[FKServerUtil sharedInstance] apiServer]];
  171. FLDataCenter *dataCenter = [FLDataCenter sharedDataCenter];
  172. [dataCenter POST:URLString
  173. parameters:[FLRequestHelper commonParamater]
  174. success:^(MSGHeader *header, id responseObject)
  175. {
  176. if ([header.code intValue] == RESPONSE_MSG_NORMAL) {
  177. success(header, [FKPersonOrderReform parseOrderBadge:responseObject]);
  178. } else {
  179. success(header, nil);
  180. }
  181. } failure:^(MSGHeader *header, NSError *error) {
  182. failure(header, error);
  183. }];
  184. }
  185. + (void)requestExpiredCouponSuccess:(void (^)(MSGHeader *, NSInteger))success
  186. failure:(void (^)(MSGHeader *, NSError *))failure {
  187. if (![FKUserManager isUserLogin]) {
  188. return;
  189. }
  190. NSString *URLString = [NSString stringWithFormat:@"%@/link-site/api/cash_coupon/get_expired_cash_coupon_count.json", [[FKServerUtil sharedInstance] apiServer]];
  191. FLDataCenter *dataCenter = [FLDataCenter sharedDataCenter];
  192. [dataCenter POST:URLString
  193. parameters:[FLRequestHelper commonParamater]
  194. success:^(MSGHeader *header, id responseObject)
  195. {
  196. if ([header.code intValue] == RESPONSE_MSG_NORMAL) {
  197. success(header, [FKHomeReform parseCouponCount:responseObject]);
  198. } else {
  199. success(header, 0);
  200. }
  201. } failure:^(MSGHeader *header, NSError *error) {
  202. failure(header, error);
  203. }];
  204. }
  205. + (FKBadgeMessage*)parseBadge:(NSDictionary *)json {
  206. if (json[@"data"] != [NSNull null]) {
  207. FKBadgeMessage *badge = [[FKBadgeMessage alloc] init];
  208. [badge mts_setValuesForKeysWithDictionary:json[@"data"]];
  209. return badge;
  210. }
  211. return nil;
  212. }
  213. + (FKAppMessage*)parseComment:(NSDictionary *)json {
  214. if (json[@"data"][@"latest_comment_msg"] != [NSNull null]) {
  215. FKAppMessage *item = [[FKAppMessage alloc] init];
  216. [item mts_setValuesForKeysWithDictionary:json[@"data"][@"latest_comment_msg"]];
  217. return item;
  218. }
  219. return nil;
  220. }
  221. #pragma mark -
  222. + (void)markMessageRead:(NSString *)msgID
  223. msgType:(NSString *)type
  224. success:(void (^)(MSGHeader *, id))success
  225. failure:(void (^)(MSGHeader *, NSError *))failure
  226. {
  227. NSString *itemJSONString;
  228. NSArray *keys;
  229. NSArray *values;
  230. if (msgID) {
  231. keys = @[@"id", @"message_type"];
  232. values = @[msgID, type];
  233. } else {
  234. keys = @[@"message_type"];
  235. values = @[type];
  236. }
  237. itemJSONString = [FLRequestHelper JSONStringWithKeys:keys
  238. values:values];
  239. NSMutableDictionary *para = [NSMutableDictionary dictionary];
  240. [para setValue:itemJSONString forKey:@"user_notify_message_json"];
  241. [para addEntriesFromDictionary:[FLRequestHelper commonParamater]];
  242. NSString *URLString = [NSString stringWithFormat:@"%@/link-site/api/message/read_msgs.json", [[FKServerUtil sharedInstance] apiServer]];
  243. FLDataCenter *dataCenter = [FLDataCenter sharedDataCenter];
  244. [dataCenter POST:URLString
  245. parameters:para
  246. success:^(MSGHeader *header, id responseObject)
  247. {
  248. if ([header.code intValue] == RESPONSE_MSG_NORMAL) {
  249. success(header, nil);
  250. } else {
  251. success(header, responseObject);
  252. }
  253. } failure:^(MSGHeader *header, NSError *error) {
  254. failure(header, error);
  255. }];
  256. }
  257. #pragma mark -
  258. - (NSMutableArray*)appMessages {
  259. if (!_appMessages) {
  260. _appMessages = [NSMutableArray array];
  261. }
  262. return _appMessages;
  263. }
  264. - (NSMutableArray*)todayMessages {
  265. if (!_todayMessages) {
  266. _todayMessages = [NSMutableArray array];
  267. }
  268. return _todayMessages;
  269. }
  270. - (NSMutableArray*)last7dayMessages {
  271. if (!_last7dayMessages) {
  272. _last7dayMessages = [NSMutableArray array];
  273. }
  274. return _last7dayMessages;
  275. }
  276. - (NSMutableArray*)last30dayMessages {
  277. if (!_last30dayMessages) {
  278. _last30dayMessages = [NSMutableArray array];
  279. }
  280. return _last30dayMessages;
  281. }
  282. - (NSMutableArray*)earilerMessages {
  283. if (!_earilerMessages) {
  284. _earilerMessages = [NSMutableArray array];
  285. }
  286. return _earilerMessages;
  287. }
  288. - (void)removeAllMessages {
  289. [self.appMessages removeAllObjects];
  290. }
  291. - (void)removeTimelineMessages {
  292. [self.todayMessages removeAllObjects];
  293. [self.last7dayMessages removeAllObjects];
  294. [self.last30dayMessages removeAllObjects];
  295. [self.earilerMessages removeAllObjects];
  296. }
  297. - (void)addMessages:(NSArray *)messages {
  298. if ([messages isKindOfClass:[NSArray class]]) {
  299. [self.appMessages addObjectsFromArray:messages];
  300. }
  301. }
  302. - (NSDateComponents *)convertToComponentsFormateFromString:(NSString *)timeString baseTime:(NSString *)baseTime{
  303. if (!timeString || timeString.length == 0) return nil;
  304. NSDate *nowDate = [NSDate date];
  305. if ([FLStringHelper isValidString:baseTime]) {
  306. nowDate = [FLStringHelper convertToDateFromString:baseTime];
  307. }
  308. NSRange range = [timeString rangeOfString:@"T"];
  309. if (range.length > 0) {
  310. timeString = [NSString stringWithFormat:@"%@T23:59:59", [timeString substringToIndex:range.location]];
  311. }
  312. NSDate *compareTime = [FLStringHelper convertToDateFromString:timeString];
  313. NSTimeInterval countTimeSecond = [compareTime timeIntervalSinceDate:nowDate];
  314. if (countTimeSecond <= 0) return nil;
  315. NSDateComponents *dateComponent = [FLStringHelper convertSecondToComponents:countTimeSecond baseTime:baseTime];
  316. return dateComponent;
  317. }
  318. - (void)addTimelineMessages:(NSArray *)messages serverTime:(NSString *)serverTime {
  319. NSDateComponents *compoment;
  320. for (FKAppMessage *message in messages) {
  321. compoment = [self convertToComponentsFormateFromString:serverTime baseTime:message.createTime];
  322. if (compoment.day <= 0) {
  323. [self.todayMessages addObject:message];
  324. } else if (compoment.day <= 6) {
  325. [self.last7dayMessages addObject:message];
  326. } else if (compoment.day <= 29) {
  327. [self.last30dayMessages addObject:message];
  328. } else if (compoment.day > 29) {
  329. [self.earilerMessages addObject:message];
  330. }
  331. }
  332. }
  333. - (NSMutableDictionary *)sectionTypeDict {
  334. if (!_sectionTypeDict) {
  335. _sectionTypeDict = [NSMutableDictionary dictionary];
  336. }
  337. return _sectionTypeDict;
  338. }
  339. - (kMessageSectionType)sectionTypeForSection:(NSInteger)section {
  340. NSNumber *value = self.sectionTypeDict[[NSString stringWithFormat:@"%lu", section]];
  341. if (value) {
  342. return (kMessageSectionType)(value.integerValue);
  343. }
  344. return kMessageSectionTypeNone;
  345. }
  346. - (NSInteger)numberOfTimelineSectionsInTableView {
  347. [self.sectionTypeDict removeAllObjects];
  348. NSUInteger count = 0;
  349. if (self.todayMessages.count > 0) {
  350. [self.sectionTypeDict setValue:[NSNumber numberWithInteger:kMessageSectionTypeToday] forKey:[NSString stringWithFormat:@"%lu", count++]];
  351. }
  352. if (self.last7dayMessages.count > 0) {
  353. [self.sectionTypeDict setValue:[NSNumber numberWithInteger:kMessageSectionTypeLast7Day] forKey:[NSString stringWithFormat:@"%lu", count++]];
  354. }
  355. if (self.last30dayMessages.count > 0) {
  356. [self.sectionTypeDict setValue:[NSNumber numberWithInteger:kMessageSectionTypeLast30Day] forKey:[NSString stringWithFormat:@"%lu", count++]];
  357. }
  358. if (self.earilerMessages.count > 0) {
  359. [self.sectionTypeDict setValue:[NSNumber numberWithInteger:kMessageSectionTypeEarlier] forKey:[NSString stringWithFormat:@"%lu", count++]];
  360. }
  361. return count;
  362. }
  363. - (NSInteger)numberOfRowsInTimelineSection:(NSInteger)section {
  364. kMessageSectionType sectionType = [self sectionTypeForSection:section];
  365. if (sectionType == kMessageSectionTypeToday) {
  366. return (self.todayMessages.count + 1);
  367. } else if (sectionType == kMessageSectionTypeLast7Day) {
  368. return (self.last7dayMessages.count + 1);
  369. } else if (sectionType == kMessageSectionTypeLast30Day){
  370. return (self.last30dayMessages.count + 1);
  371. } else if (sectionType == kMessageSectionTypeEarlier){
  372. return (self.earilerMessages.count + 1);
  373. }
  374. return 0;
  375. }
  376. - (FKAppMessage*)timelineItemAtIndex:(NSIndexPath *)indexPath {
  377. kMessageSectionType sectionType = [self sectionTypeForSection:indexPath.section];
  378. NSArray *array;
  379. if (sectionType == kMessageSectionTypeToday) {
  380. array = self.todayMessages;
  381. } else if (sectionType == kMessageSectionTypeLast7Day) {
  382. array = self.last7dayMessages;
  383. } else if (sectionType == kMessageSectionTypeLast30Day){
  384. array = self.last30dayMessages;
  385. } else if (sectionType == kMessageSectionTypeEarlier){
  386. array = self.earilerMessages;
  387. }
  388. if ((indexPath.row - 1) >= 0 && (indexPath.row - 1) < array.count) {
  389. return array[(indexPath.row - 1)];
  390. }
  391. return nil;
  392. }
  393. - (NSString *)timelineHeaderMessageAtIndexPath:(NSIndexPath *)indexPath {
  394. kMessageSectionType sectionType = [self sectionTypeForSection:indexPath.section];
  395. if (sectionType == kMessageSectionTypeToday) {
  396. return @"今天";
  397. } else if (sectionType == kMessageSectionTypeLast7Day) {
  398. return @"最近7天";
  399. } else if (sectionType == kMessageSectionTypeLast30Day){
  400. return @"最近30天";
  401. } else if (sectionType == kMessageSectionTypeEarlier){
  402. return @"一个月以前";
  403. }
  404. return nil;
  405. }
  406. - (BOOL)hasTimelineMessage {
  407. return (self.todayMessages.count + self.last7dayMessages.count + self.last30dayMessages.count + self.earilerMessages.count);
  408. }
  409. @end