ASNetTools.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. //
  2. // ASNetTools.m
  3. // Asteria
  4. //
  5. // Created by iOS on 2023/11/25.
  6. //
  7. #import "ASNetTools.h"
  8. #import <MKNetworkKit/MKNetworkKit.h>
  9. @interface ASNetTools ()
  10. @property (nonatomic, copy) NSString *currentToken;
  11. @property (nonatomic, strong) MKNetworkEngine *engine;
  12. @property (nonatomic, strong) NSMutableDictionary *defuatParam;
  13. @end
  14. @implementation ASNetTools
  15. + (instancetype)shared {
  16. static id sharedInstance = nil;
  17. static dispatch_once_t onceToken;
  18. dispatch_once(&onceToken, ^{
  19. sharedInstance = [[self alloc] init];
  20. });
  21. return sharedInstance;
  22. }
  23. - (instancetype)init
  24. {
  25. self = [super init];
  26. if (self) {
  27. [self updateEngine];
  28. self.defuatParam = [NSMutableDictionary dictionary];
  29. }
  30. return self;
  31. }
  32. - (void)setDefualtParam:(NSDictionary *)defualt {
  33. self.defuatParam = [[NSMutableDictionary alloc] initWithDictionary:defualt];
  34. }
  35. - (void)updateDefualtParam:(NSDictionary *)newData {
  36. for (NSString *key in newData.allKeys) {
  37. self.defuatParam[key] = newData[key];
  38. }
  39. }
  40. - (void)updateEngine {
  41. NSString *token = [DataUtil loginToken];
  42. if (!token) {
  43. token = @"";
  44. }
  45. self.currentToken = token;
  46. self.engine = [[MKNetworkEngine alloc] initWithHostName:HostPath customHeaderFields:@{
  47. @"Authorization":token,
  48. @"powerby": @"longyitec",
  49. @"Content-Type": @"application/json;charset=utf-8"
  50. }];
  51. }
  52. - (void)checkToken {
  53. NSString *token = [DataUtil loginToken];
  54. if (!token) {
  55. token = @"";
  56. }
  57. if (![token isEqualToString:self.currentToken]) {
  58. [self updateEngine];
  59. }
  60. }
  61. // post请求
  62. - (void)postWithPath:(NSString *)path param:(NSDictionary *)param success:(void(^)(id))success faild:(void(^)(NSString *code, NSString *msg))faild {
  63. BOOL isSSL = true;
  64. [self checkToken];
  65. NSMutableDictionary *tempDic = [[NSMutableDictionary alloc] initWithDictionary:self.defuatParam];
  66. [tempDic addEntriesFromDictionary:param];
  67. MKNetworkOperation *op = [self.engine operationWithPath:path params:tempDic httpMethod:@"POST" ssl:isSSL];
  68. [op setPostDataEncoding:MKNKPostDataEncodingTypeJSON];
  69. __weak typeof(self) weakSelf = self;
  70. [op onCompletion:^(MKNetworkOperation *completedOperation) {
  71. dispatch_async(dispatch_get_main_queue(), ^{
  72. NSString *json = [op responseJSON];
  73. id temp = [json mj_JSONObject];
  74. if ([temp isKindOfClass:[NSDictionary class]]) {
  75. NSDictionary *result = (NSDictionary *)temp;
  76. NSString *status = [NSString stringWithFormat:@"%@", result[@"status"]];
  77. id data = result[@"data"];
  78. if ([status isEqualToString:@"1"]) {
  79. success(data);
  80. } else {
  81. NSString *msg = result[@"msg"];
  82. faild(status, msg);
  83. }
  84. }
  85. });
  86. } onError:^(NSError *error) {
  87. dispatch_async(dispatch_get_main_queue(), ^{
  88. NSString *json = [op responseJSON];
  89. id temp = [json mj_JSONObject];
  90. if ([temp isKindOfClass:[NSDictionary class]]) {
  91. NSDictionary *result = (NSDictionary *)temp;
  92. NSString *status = result[@"status"];
  93. NSString *msg = result[@"msg"];
  94. if (error.code == 401) {
  95. [DataUtil setLoginToken:@""];
  96. [weakSelf updateEngine];
  97. faild(status, msg);
  98. return;
  99. }
  100. faild(status, msg);
  101. return;
  102. }
  103. if (error.code == 401) {
  104. [DataUtil setLoginToken:@""];
  105. [weakSelf updateEngine];
  106. faild(@"-1", @"Plase Login");
  107. return;
  108. }
  109. faild([NSString stringWithFormat:@"%ld", error.code], error.localizedDescription);
  110. });
  111. }];
  112. // dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), ^{
  113. [self.engine enqueueOperation:op];
  114. // });d
  115. }
  116. // formData的post请求
  117. - (void)formData_postWithPath:(NSString *)path param:(NSDictionary *)param success:(void(^)(id))success faild:(void(^)(NSString *code, NSString *msg))faild {
  118. BOOL isSSL = true;
  119. [self checkToken];
  120. NSMutableDictionary *tempDic = [[NSMutableDictionary alloc] initWithDictionary:self.defuatParam];
  121. [tempDic addEntriesFromDictionary:param];
  122. MKNetworkOperation *op = [self.engine operationWithPath:path params:tempDic httpMethod:@"POST" ssl:isSSL];
  123. ///form-data 的请求格式
  124. NSData *data = [NSJSONSerialization dataWithJSONObject:param options:NSJSONWritingPrettyPrinted error:nil];
  125. for (NSString *key in param.allKeys) {
  126. // 循环拿到所有参数进行拼接
  127. // NSString * searchStr = [param[key] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
  128. NSString * searchStr = param[key];
  129. NSData *data =[searchStr dataUsingEncoding:NSUTF8StringEncoding];
  130. [op addData:data forKey:key];
  131. }
  132. __weak typeof(self) weakSelf = self;
  133. [op onCompletion:^(MKNetworkOperation *completedOperation) {
  134. dispatch_async(dispatch_get_main_queue(), ^{
  135. NSString *json = [op responseJSON];
  136. id temp = [json mj_JSONObject];
  137. NSLog(@"---post-----url:\n%@\n-----data:\n%@\n--",path, temp);
  138. if ([temp isKindOfClass:[NSDictionary class]]) {
  139. NSDictionary *result = (NSDictionary *)temp;
  140. NSString *status = [NSString stringWithFormat:@"%@", result[@"status"]];
  141. id data = result[@"data"];
  142. if ([status isEqualToString:@"1"]) {
  143. success(data);
  144. } else {
  145. NSString *msg = result[@"msg"];
  146. faild(status, msg);
  147. }
  148. }
  149. });
  150. } onError:^(NSError *error) {
  151. dispatch_async(dispatch_get_main_queue(), ^{
  152. NSString *json = [op responseJSON];
  153. id temp = [json mj_JSONObject];
  154. if ([temp isKindOfClass:[NSDictionary class]]) {
  155. NSDictionary *result = (NSDictionary *)temp;
  156. NSString *status = result[@"status"];
  157. NSString *msg = result[@"msg"];
  158. if (error.code == 401) {
  159. [DataUtil setLoginToken:@""];
  160. [weakSelf updateEngine];
  161. faild(status, msg);
  162. return;
  163. }
  164. faild(status, msg);
  165. return;
  166. }
  167. if (error.code == 401) {
  168. [DataUtil setLoginToken:@""];
  169. [weakSelf updateEngine];
  170. faild(@"-1", @"Plase Login");
  171. return;
  172. }
  173. faild([NSString stringWithFormat:@"%ld", error.code], error.localizedDescription);
  174. });
  175. }];
  176. dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), ^{
  177. [self.engine enqueueOperation:op];
  178. });
  179. }
  180. // get 请求
  181. - (void)getWithPath:(NSString *)path param:(NSDictionary *)param success:(void(^)(id))success faild:(void(^)(NSString *code, NSString *msg))faild {
  182. BOOL isSSL = true;
  183. [self checkToken];
  184. NSMutableDictionary *tempDic = [[NSMutableDictionary alloc] initWithDictionary:self.defuatParam];
  185. [tempDic addEntriesFromDictionary:param];
  186. MKNetworkOperation *op = [self.engine operationWithPath:path params:tempDic httpMethod:@"GET" ssl:isSSL];
  187. __weak typeof(self) weakSelf = self;
  188. [op onCompletion:^(MKNetworkOperation *completedOperation) {
  189. dispatch_async(dispatch_get_main_queue(), ^{
  190. NSString *json = [op responseJSON];
  191. id temp = [json mj_JSONObject];
  192. NSLog(@"---Get-----url:\n%@\n-----data:\n%@\n--",path, temp);
  193. if ([temp isKindOfClass:[NSDictionary class]]) {
  194. NSDictionary *result = (NSDictionary *)temp;
  195. NSString *status = [NSString stringWithFormat:@"%@", result[@"status"]];
  196. id data = result[@"data"];
  197. if ([status isEqualToString:@"1"]) {
  198. success(data);
  199. } else {
  200. NSString *msg = result[@"msg"];
  201. faild(status, msg);
  202. }
  203. }
  204. });
  205. } onError:^(NSError *error) {
  206. dispatch_async(dispatch_get_main_queue(), ^{
  207. NSString *json = [op responseJSON];
  208. id temp = [json mj_JSONObject];
  209. if ([temp isKindOfClass:[NSDictionary class]]) {
  210. NSDictionary *result = (NSDictionary *)temp;
  211. NSString *status = result[@"status"];
  212. NSString *msg = result[@"msg"];
  213. if (error.code == 401) {
  214. [DataUtil setLoginToken:@""];
  215. [weakSelf updateEngine];
  216. faild(status, msg);
  217. return;
  218. }
  219. faild(status, msg);
  220. return;
  221. }
  222. if (error.code == 401) {
  223. [DataUtil setLoginToken:@""];
  224. [weakSelf updateEngine];
  225. faild(@"-1", @"Plase Login");
  226. return;
  227. }
  228. faild([NSString stringWithFormat:@"%ld", error.code], error.localizedDescription);
  229. });
  230. }];
  231. [self.engine enqueueOperation:op];
  232. // dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), ^{
  233. // });
  234. }
  235. // put 请求
  236. - (void)putWithPath:(NSString *)path param:(NSDictionary *)param success:(void(^)(id))success faild:(void(^)(NSString *code, NSString *msg))faild {
  237. BOOL isSSL = true;
  238. //#if (DEBUG)
  239. // isSSL = false;
  240. //#endif
  241. [self checkToken];
  242. NSMutableDictionary *tempDic = [[NSMutableDictionary alloc] initWithDictionary:self.defuatParam];
  243. [tempDic addEntriesFromDictionary:param];
  244. MKNetworkOperation *op = [self.engine operationWithPath:path params:tempDic httpMethod:@"PUT" ssl:isSSL];
  245. [op setPostDataEncoding:MKNKPostDataEncodingTypeJSON];
  246. __weak typeof(self) weakSelf = self;
  247. [op onCompletion:^(MKNetworkOperation *completedOperation) {
  248. dispatch_async(dispatch_get_main_queue(), ^{
  249. NSString *json = [op responseJSON];
  250. id temp = [json mj_JSONObject];
  251. NSLog(@"---Put-----url:\n%@\n-----data:\n%@\n--",path, temp);
  252. if ([temp isKindOfClass:[NSDictionary class]]) {
  253. NSDictionary *result = (NSDictionary *)temp;
  254. NSString *status = [NSString stringWithFormat:@"%@", result[@"status"]];
  255. id data = result[@"data"];
  256. if ([status isEqualToString:@"1"]) {
  257. success(data);
  258. } else {
  259. NSString *msg = result[@"msg"];
  260. faild(status, msg);
  261. }
  262. }
  263. });
  264. } onError:^(NSError *error) {
  265. dispatch_async(dispatch_get_main_queue(), ^{
  266. NSString *json = [op responseJSON];
  267. id temp = [json mj_JSONObject];
  268. if ([temp isKindOfClass:[NSDictionary class]]) {
  269. NSDictionary *result = (NSDictionary *)temp;
  270. NSString *status = result[@"status"];
  271. NSString *msg = result[@"msg"];
  272. if (error.code == 401) {
  273. [DataUtil setLoginToken:@""];
  274. [weakSelf updateEngine];
  275. faild(status, msg);
  276. return;
  277. }
  278. faild(status, msg);
  279. return;
  280. }
  281. if (error.code == 401) {
  282. [DataUtil setLoginToken:@""];
  283. [weakSelf updateEngine];
  284. faild(@"-1", @"Plase Login");
  285. return;
  286. }
  287. faild([NSString stringWithFormat:@"%ld", error.code], error.localizedDescription);
  288. });
  289. }];
  290. // dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), ^{
  291. [self.engine enqueueOperation:op];
  292. // });
  293. }
  294. // del 请求
  295. - (void)delWithPath:(NSString *)path param:(NSDictionary *)param success:(void(^)(id))success faild:(void(^)(NSString *code, NSString *msg))faild {
  296. BOOL isSSL = true;
  297. //#if (DEBUG)
  298. // isSSL = false;
  299. //#endif
  300. [self checkToken];
  301. NSMutableDictionary *tempDic = [[NSMutableDictionary alloc] initWithDictionary:self.defuatParam];
  302. [tempDic addEntriesFromDictionary:param];
  303. MKNetworkOperation *op = [self.engine operationWithPath:path params:tempDic httpMethod:@"DELETE" ssl:isSSL];
  304. [op setPostDataEncoding:MKNKPostDataEncodingTypeJSON];
  305. __weak typeof(self) weakSelf = self;
  306. [op onCompletion:^(MKNetworkOperation *completedOperation) {
  307. dispatch_async(dispatch_get_main_queue(), ^{
  308. NSString *json = [op responseJSON];
  309. id temp = [json mj_JSONObject];
  310. if ([temp isKindOfClass:[NSDictionary class]]) {
  311. NSDictionary *result = (NSDictionary *)temp;
  312. NSString *status = [NSString stringWithFormat:@"%@", result[@"status"]];
  313. id data = result[@"data"];
  314. if ([status isEqualToString:@"1"]) {
  315. success(data);
  316. } else {
  317. NSString *msg = result[@"msg"];
  318. faild(status, msg);
  319. }
  320. }
  321. });
  322. } onError:^(NSError *error) {
  323. dispatch_async(dispatch_get_main_queue(), ^{
  324. NSString *json = [op responseJSON];
  325. id temp = [json mj_JSONObject];
  326. if ([temp isKindOfClass:[NSDictionary class]]) {
  327. NSDictionary *result = (NSDictionary *)temp;
  328. NSString *status = result[@"status"];
  329. NSString *msg = result[@"msg"];
  330. if (error.code == 401) {
  331. [DataUtil setLoginToken:@""];
  332. [weakSelf updateEngine];
  333. faild(status, msg);
  334. return;
  335. }
  336. faild(status, msg);
  337. return;
  338. }
  339. if (error.code == 401) {
  340. [DataUtil setLoginToken:@""];
  341. [weakSelf updateEngine];
  342. faild(@"-1", @"Plase Login");
  343. return;
  344. }
  345. faild([NSString stringWithFormat:@"%ld", error.code], error.localizedDescription);
  346. });
  347. }];
  348. // dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), ^{
  349. [self.engine enqueueOperation:op];
  350. // });
  351. }
  352. + (void)debugRegist {
  353. NSDictionary *param = @{
  354. @"customer": @{
  355. @"email": @"abc123456789@qq.com",
  356. @"firstname": @"ujrbcf",
  357. @"lastname": @"serwt",
  358. @"dob": @"2023/10/20"
  359. },
  360. @"password": @"A123456a",
  361. @"extension_attributes" : @{
  362. @"is_subscribed": @(true)
  363. }
  364. };
  365. [ASNetTools.shared postWithPath:registerUrl param:param success:^(id _Nonnull result) {
  366. NSLog(@"----url:%@-----result:%@------", registerUrl, result);
  367. } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) {
  368. NSLog(@"----url:%@-----code:%@----msg:%@--", registerUrl, code, msg);
  369. }];
  370. }
  371. + (void)login {
  372. NSDictionary *param = @{
  373. @"username" :@"abc123456789@qq.com",//@"Britneyngwafa@gmail.com",//
  374. @"password":@"A123456a",//@"1qazXSW@123",//
  375. };
  376. [ASNetTools.shared postWithPath:loginUrl param:param success:^(id _Nonnull result) {
  377. NSString *token = [NSString stringWithFormat:@"Bearer %@", result];
  378. [DataUtil setLoginToken:token];
  379. [ASNetTools.shared updateEngine];
  380. [ASUserInfoManager.shared getInfo];
  381. NSLog(@"----url:%@-----result:%@------", loginUrl, result);
  382. } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) {
  383. NSLog(@"----url:%@-----code:%@----msg:%@--", loginUrl, code, msg);
  384. }];
  385. }
  386. +(void)xxx_loginWithParam:(NSDictionary *)param
  387. success:(void(^)(id))success
  388. faild:(void(^)(NSString *code, NSString *msg))faild {
  389. [ASNetTools.shared postWithPath:loginUrl param:param success:^(id _Nonnull result) {
  390. NSString *token = [NSString stringWithFormat:@"Bearer %@", result];
  391. [DataUtil setLoginToken:token];
  392. [ASNetTools.shared updateEngine];
  393. [ASUserInfoManager.shared getInfo];
  394. success(result);
  395. NSLog(@"----url:%@-----result:%@------", loginUrl, result);
  396. } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) {
  397. NSLog(@"----url:%@-----code:%@----msg:%@--", loginUrl, code, msg);
  398. faild(code,msg);
  399. }];
  400. }
  401. ///获取广告位优惠卷列表
  402. +(void)reqNet_getAdvCoupons{
  403. [ASNetTools.shared getWithPath:BaseRequestrUrl(@"advCoupons") param:@{} success:^(id _Nonnull result) {
  404. [ASNetTools shared].xxx_couponAry = result;
  405. } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) {
  406. }];
  407. }
  408. -(NSMutableArray *)xxx_couponAry{
  409. if(!_xxx_couponAry){
  410. _xxx_couponAry = [[NSMutableArray alloc]init];
  411. }
  412. return _xxx_couponAry;
  413. }
  414. @end