HttpUtils.m 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //
  2. // HttpUtils.m
  3. // UUTUtils
  4. //
  5. // Created by zhuge on 2017/12/23.
  6. // Copyright © 2017年 zhuge. All rights reserved.
  7. //
  8. #import "HttpUtils.h"
  9. #import "ProjectInfo_Server.h"
  10. #import "ZGWebUtils.h"
  11. #import "ZGJsonUtils.h"
  12. #import "ZGNetCircularProgressView.h"
  13. #import "UrlCrptyUtils.h"
  14. #import "AppDelegate.h"
  15. @implementation HttpUtils
  16. - (void)requestAdConfigs:(ProjectInfo_Server *)server atWindow:(NSWindow *)window {
  17. // if(true){
  18. // return;
  19. // }
  20. NSLog(@"获取网络广告配置中...");
  21. // 创建progress view
  22. NSRect winRect = window.frame;
  23. float x = 0;
  24. float y = 0;
  25. float w = winRect.size.width;
  26. float h = winRect.size.height;
  27. ZGNetCircularProgressView *progressView = [[ZGNetCircularProgressView alloc] initWithFrame:NSMakeRect(x, y, w, h)];
  28. [window.contentView addSubview:progressView];
  29. NSString *package = server.package;
  30. NSString *uuid = @"";
  31. NSDictionary *dic = @{
  32. @"sdkVersion": @(SdkVersion),
  33. @"adVersion": @(0),
  34. @"packname": package,
  35. @"uuid": uuid,
  36. };
  37. NSString *dataParams = [ZGJsonUtils dictionaryToString:dic];
  38. dataParams = [HttpUtils urlEncode:dataParams];
  39. NSString *url = [NSString stringWithFormat:@"http://rdwxtool.com/adsaccount/www/gateway.php?act=104&data=%@", dataParams];
  40. NSLog(@"url:%@",url);
  41. [ZGWebUtils getForUrl:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
  42. NSString *ret = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  43. NSLog(@"%@", ret);
  44. NSDictionary *dic = [ZGJsonUtils stringToObject:ret];
  45. NSLog(@"%@", dic[@"data"]);
  46. NSString *dataStr = dic[@"data"][@"adJson"];
  47. if (dataStr != nil && ![dataStr isKindOfClass:[NSNull class]] && ![NSNull isEqual:dataStr] && ![@"" isEqual:dataStr]) {
  48. dataStr = [UrlCrptyUtils decrypt:dataStr];
  49. dic = [ZGJsonUtils stringToObject:dataStr];
  50. } else {
  51. dic = nil;
  52. }
  53. // dic = nil;
  54. // 从网络获取然后进行初始化
  55. [server performSelectorOnMainThread:@selector(initWithDic:) withObject:dic waitUntilDone:YES];
  56. [self performSelectorOnMainThread:@selector(onRequestFinished:) withObject:progressView waitUntilDone:YES];
  57. }];
  58. }
  59. - (void)saveAdConfigs:(NSString *)cfg forPackage:(NSString *)package atWindow:(NSWindow *)window {
  60. // 创建progress view
  61. NSRect winRect = window.frame;
  62. float x = 0;
  63. float y = 0;
  64. float w = winRect.size.width;
  65. float h = winRect.size.height;
  66. ZGNetCircularProgressView *progressView = [[ZGNetCircularProgressView alloc] initWithFrame:NSMakeRect(x, y, w, h)];
  67. [window.contentView addSubview:progressView];
  68. NSMutableDictionary *md = [NSMutableDictionary dictionaryWithCapacity:0];
  69. // cfg = @"zhuge test";
  70. md[@"packname"] = package;
  71. md[@"sdkVersion"] = @(SdkVersion);
  72. cfg = [UrlCrptyUtils encrypt:cfg];
  73. md[@"adJson"] = cfg;
  74. NSString *url = [NSString stringWithFormat:@"http://rdwxtool.com/adsaccount/www/gateway.php?act=204"];
  75. NSString *post = [NSString stringWithFormat:@"data=%@", [ZGJsonUtils dictionaryToString:md]];
  76. NSLog(@"%@&%@", url, post);
  77. NSData *data = [post dataUsingEncoding:NSUTF8StringEncoding];
  78. NSString *ret = [ZGWebUtils stringOfURLByPost:url postData:data];
  79. NSLog(@"%@", ret);
  80. // [ZGWebUtils getForUrl:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
  81. // NSString *ret = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  82. // NSLog(@"%@", ret);
  83. // [self performSelectorOnMainThread:@selector(onRequestFinished:) withObject:progressView waitUntilDone:YES];
  84. // }];
  85. [self onRequestFinished:progressView];
  86. }
  87. + (NSString *)urlEncode:(NSString *)str {
  88. NSString *encoded = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(
  89. NULL,
  90. (CFStringRef)str,
  91. NULL,
  92. (CFStringRef)@"!*'\"();:@&=+$,/?%#[]% ",
  93. kCFStringEncodingUTF8 ));
  94. // NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@"!*'\"();:@&=+$,/?%#[]% "];
  95. // NSString *encoded = [str stringByAddingPercentEncodingWithAllowedCharacters:set];
  96. return encoded;
  97. }
  98. - (void)onRequestFinished:(NSView *)view {
  99. [view removeFromSuperview];
  100. }
  101. @end