Nav apraksta

FKLaunchRequest.m 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //
  2. // LaunchRequest.m
  3. // FirstLink
  4. //
  5. // Created by ascii on 15/8/21.
  6. // Copyright (c) 2015年 FirstLink. All rights reserved.
  7. //
  8. #import "FKLaunchRequest.h"
  9. #import "FKLaunchStore.h"
  10. #import "UserDefaultManager.h"
  11. #import "FKRegisterTipView.h"
  12. #import "FKWindowViewManager.h"
  13. #import "FKDynamicMessageItem.h"
  14. #import "FKRecommendReform.h"
  15. #import "FLControllerHelper.h"
  16. #import "SchemaManager.h"
  17. @implementation FKLaunchRequest
  18. + (void)requestLaunchUpdateWithInterval:(NSTimeInterval)interval {
  19. [NSTimer scheduledTimerWithTimeInterval:interval
  20. target:[FKLaunchRequest class]
  21. selector:@selector(requestLaunchUpdate)
  22. userInfo:nil
  23. repeats:NO];
  24. }
  25. + (void)requestLaunchUpdate {
  26. NSString *remoteLaunchURL = [FKLaunchStore remoteLaunchImageURL];
  27. NSString *encodeLaunchURL = @"";
  28. if (remoteLaunchURL) {
  29. encodeLaunchURL = [remoteLaunchURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  30. }
  31. NSString *URLString = [NSString stringWithFormat:@"%@/link-site/api/initpic.json?pic_url=%@"
  32. ,[[FKServerUtil sharedInstance] apiServer]
  33. ,encodeLaunchURL];
  34. FLDataCenter *dataCenter = [FLDataCenter sharedDataCenter];
  35. [dataCenter POST:URLString
  36. parameters:[FLRequestHelper commonParamaterForLogin]
  37. success:^(MSGHeader *header, id responseObject)
  38. {
  39. if (header.code.intValue == RESPONSE_MSG_NORMAL) {
  40. [FKLaunchRequest parseLaunchResponse:responseObject];
  41. }
  42. } failure:^(MSGHeader *header, NSError *error) {
  43. }];
  44. }
  45. + (void)parseLaunchResponse:(NSDictionary *)json {
  46. NSString *imageURL = [FKLaunchRequest parseLaunchImageURL:json];
  47. NSString *localRemoteURL = [FKLaunchStore remoteLaunchImageURL];
  48. NSString *targetURL = [FKLaunchRequest parseLaunchTargetURL:json];
  49. if (!imageURL || imageURL.length == 0) {
  50. [FKLaunchStore removeNetLaunchImage];
  51. [[UserDefaultManager sharedManager] removeUserDefaultKey:LAUNCH_TARGET_URL_KEY];
  52. return;
  53. }
  54. if (imageURL && ![imageURL isEqualToString:localRemoteURL]) {
  55. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  56. if (imageURL.length > 0) {
  57. NSURL *url = [NSURL URLWithString:imageURL];
  58. UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
  59. if (image && imageURL) {
  60. [FKLaunchStore removeNetLaunchImage];
  61. [FKLaunchStore saveNetLaunchImage:image name:imageURL];
  62. [[UserDefaultManager sharedManager] setUserDefaultObject:targetURL key:LAUNCH_TARGET_URL_KEY];
  63. } else {
  64. [FKLaunchStore removeNetLaunchImage];
  65. [[UserDefaultManager sharedManager] removeUserDefaultKey:LAUNCH_TARGET_URL_KEY];
  66. }
  67. }
  68. });
  69. } else {
  70. [[UserDefaultManager sharedManager] setUserDefaultObject:targetURL key:LAUNCH_TARGET_URL_KEY];
  71. }
  72. }
  73. + (NSString *)parseLaunchImageURL:(NSDictionary *)json {
  74. if (json[@"data"] != [NSNull null]) {
  75. if (IS_IPHONE_4 && json[@"data"][@"pic"][@"img_1"] != [NSNull null]) {
  76. return [NSString stringWithFormat:@"%@", json[@"data"][@"pic"][@"img_1"]];
  77. } else if (json[@"data"][@"pic"][@"img_2"] != [NSNull null]) {
  78. return [NSString stringWithFormat:@"%@", json[@"data"][@"pic"][@"img_2"]];
  79. }
  80. }
  81. return nil;
  82. }
  83. + (NSString *)parseLaunchTargetURL:(NSDictionary *)json {
  84. if (json[@"data"][@"pic"][@"target_url"] != [NSNull null]) {
  85. return [NSString stringWithFormat:@"%@", json[@"data"][@"pic"][@"target_url"]];
  86. }
  87. return nil;
  88. }
  89. + (void)requestOperationAlertInfo {
  90. NSString *URLString = [NSString stringWithFormat:@"%@/link-site/api/cash_coupon/find_messages.json", [[FKServerUtil sharedInstance] apiServer]];
  91. [[FLDataCenter sharedDataCenter] POST:URLString parameters:[FLRequestHelper commonParamater] success:^(MSGHeader *header, id responseObject) {
  92. if (header.code.intValue == RESPONSE_MSG_NORMAL) {
  93. FKDynamicMessageItem *item = [FKRecommendReform parserDynamicItemWithDict:responseObject];
  94. if (item.freshPic.length) {
  95. [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:item.freshPic] options:0 progress:nil completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
  96. if (image && [[FKWindowViewManager sharedManager] isNeedShowRegisterTip]) {
  97. dispatch_async(dispatch_get_main_queue(), ^{
  98. CGFloat width = 250, height = 350;
  99. FKRegisterTipView *view = [[FKRegisterTipView alloc] initWithBgImg:image];
  100. view.frame = CGRectMake((UISCREENWIDTH-width)/2, (UISCREENHEIGH-height)/2, width, height);
  101. view.completeAction = ^() {
  102. [[FKWindowViewManager sharedManager] completeShowRegisterTip];
  103. [[FKWindowViewManager sharedManager] closeFloatView];
  104. if (item.targetURL.length > 0) {
  105. [[SchemaManager sharedManager] parserURL:[NSURL URLWithString:item.targetURL] shouldCache:YES];
  106. } else {
  107. [FLControllerHelper presentLoginViewController];
  108. }
  109. };
  110. [[FKWindowViewManager sharedManager] appendFloatView:view];
  111. });
  112. }
  113. }];
  114. }
  115. }
  116. } failure:^(MSGHeader *header, NSError *error) {
  117. }];
  118. }
  119. @end