ASNetTools.m 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  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) MKNetworkEngine *msgEngine;
  13. @property (nonatomic, strong) NSMutableDictionary *defuatParam;
  14. @end
  15. @implementation ASNetTools
  16. + (instancetype)shared {
  17. static id sharedInstance = nil;
  18. static dispatch_once_t onceToken;
  19. dispatch_once(&onceToken, ^{
  20. sharedInstance = [[self alloc] init];
  21. });
  22. return sharedInstance;
  23. }
  24. - (instancetype)init
  25. {
  26. self = [super init];
  27. if (self) {
  28. [self updateEngine];
  29. self.defuatParam = [NSMutableDictionary dictionary];
  30. }
  31. return self;
  32. }
  33. - (void)setDefualtParam:(NSDictionary *)defualt {
  34. self.defuatParam = [[NSMutableDictionary alloc] initWithDictionary:defualt];
  35. }
  36. - (void)updateDefualtParam:(NSDictionary *)newData {
  37. for (NSString *key in newData.allKeys) {
  38. self.defuatParam[key] = newData[key];
  39. }
  40. }
  41. - (void)updateEngine {
  42. NSString *token = [DataUtil loginToken];
  43. if (!token) {
  44. token = @"";
  45. }
  46. self.currentToken = token;
  47. self.engine = [[MKNetworkEngine alloc] initWithHostName:HostPath customHeaderFields:@{
  48. @"Authorization":token,
  49. @"powerby": @"longyitec",
  50. @"Content-Type": @"application/json;charset=utf-8"
  51. }];
  52. self.msgEngine = [[MKNetworkEngine alloc] initWithHostName:MsgHostPath customHeaderFields:@{
  53. @"Authorization":token,
  54. @"powerby": @"longyitec",
  55. @"Content-Type": @"application/json;charset=utf-8"
  56. }];
  57. }
  58. - (void)checkToken {
  59. NSString *token = [DataUtil loginToken];
  60. if (!token) {
  61. token = @"";
  62. }
  63. if (![token isEqualToString:self.currentToken]) {
  64. [self updateEngine];
  65. }
  66. }
  67. // MARK: - message Api
  68. // post请求
  69. - (void)postMsgWithPath:(NSString *)path param:(NSDictionary *)param success:(void(^)(id))success faild:(void(^)(NSString *code, NSString *msg))faild {
  70. BOOL isSSL = true;
  71. [self checkToken];
  72. NSMutableDictionary *tempDic = [[NSMutableDictionary alloc] initWithDictionary:self.defuatParam];
  73. [tempDic addEntriesFromDictionary:param];
  74. MKNetworkOperation *op = [self.msgEngine operationWithPath:path params:tempDic httpMethod:@"POST" ssl:isSSL];
  75. [op setPostDataEncoding:MKNKPostDataEncodingTypeJSON];
  76. __weak typeof(self) weakSelf = self;
  77. [op onCompletion:^(MKNetworkOperation *completedOperation) {
  78. dispatch_async(dispatch_get_main_queue(), ^{
  79. NSString *json = [op responseJSON];
  80. id temp = [json mj_JSONObject];
  81. NSLog(@"---Get-----url:\n%@\n-----data:\n%@\n--",path, temp);
  82. if ([temp isKindOfClass:[NSDictionary class]]) {
  83. NSDictionary *result = (NSDictionary *)temp;
  84. NSString *code = [NSString stringWithFormat:@"%@", result[@"code"]];
  85. id data = result[@"data"];
  86. if ([code isEqualToString:@"200"]) {
  87. success(data);
  88. } else {
  89. NSString *msg = result[@"msg"];
  90. faild(code, msg);
  91. }
  92. }
  93. });
  94. } onError:^(NSError *error) {
  95. dispatch_async(dispatch_get_main_queue(), ^{
  96. NSString *json = [op responseJSON];
  97. id temp = [json mj_JSONObject];
  98. if ([temp isKindOfClass:[NSDictionary class]]) {
  99. NSDictionary *result = (NSDictionary *)temp;
  100. NSString *status = result[@"code"];
  101. NSString *msg = result[@"msg"];
  102. if (error.code == 401) {
  103. [DataUtil setLoginToken:@""];
  104. [weakSelf updateEngine];
  105. faild(status, msg);
  106. return;
  107. }
  108. faild(status, msg);
  109. return;
  110. }
  111. if (error.code == 401) {
  112. [DataUtil setLoginToken:@""];
  113. [weakSelf updateEngine];
  114. faild(@"-1", @"Plase Login");
  115. return;
  116. }
  117. faild([NSString stringWithFormat:@"%ld", error.code], error.localizedDescription);
  118. });
  119. }];
  120. // dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), ^{
  121. [self.msgEngine enqueueOperation:op];
  122. // });
  123. }
  124. // formData的post请求
  125. - (void)formDataMsg_postWithPath:(NSString *)path param:(NSDictionary *)param success:(void(^)(id))success faild:(void(^)(NSString *code, NSString *msg))faild {
  126. BOOL isSSL = true;
  127. [self checkToken];
  128. NSMutableDictionary *tempDic = [[NSMutableDictionary alloc] initWithDictionary:self.defuatParam];
  129. [tempDic addEntriesFromDictionary:param];
  130. MKNetworkOperation *op = [self.msgEngine operationWithPath:path params:tempDic httpMethod:@"POST" ssl:isSSL];
  131. ///form-data 的请求格式
  132. NSData *data = [NSJSONSerialization dataWithJSONObject:param options:NSJSONWritingPrettyPrinted error:nil];
  133. for (NSString *key in param.allKeys) {
  134. // 循环拿到所有参数进行拼接
  135. // NSString * searchStr = [param[key] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
  136. NSString * searchStr = param[key];
  137. NSData *data =[searchStr dataUsingEncoding:NSUTF8StringEncoding];
  138. [op addData:data forKey:key];
  139. }
  140. __weak typeof(self) weakSelf = self;
  141. [op onCompletion:^(MKNetworkOperation *completedOperation) {
  142. dispatch_async(dispatch_get_main_queue(), ^{
  143. NSString *json = [op responseJSON];
  144. id temp = [json mj_JSONObject];
  145. NSLog(@"---Get-----url:\n%@\n-----data:\n%@\n--",path, temp);
  146. if ([temp isKindOfClass:[NSDictionary class]]) {
  147. NSDictionary *result = (NSDictionary *)temp;
  148. NSString *code = [NSString stringWithFormat:@"%@", result[@"code"]];
  149. id data = result[@"data"];
  150. if ([code isEqualToString:@"200"]) {
  151. success(data);
  152. } else {
  153. NSString *msg = result[@"msg"];
  154. faild(code, msg);
  155. }
  156. }
  157. });
  158. } onError:^(NSError *error) {
  159. dispatch_async(dispatch_get_main_queue(), ^{
  160. NSString *json = [op responseJSON];
  161. id temp = [json mj_JSONObject];
  162. if ([temp isKindOfClass:[NSDictionary class]]) {
  163. NSDictionary *result = (NSDictionary *)temp;
  164. NSString *status = result[@"code"];
  165. NSString *msg = result[@"msg"];
  166. if (error.code == 401) {
  167. [DataUtil setLoginToken:@""];
  168. [weakSelf updateEngine];
  169. faild(status, msg);
  170. return;
  171. }
  172. faild(status, msg);
  173. return;
  174. }
  175. if (error.code == 401) {
  176. [DataUtil setLoginToken:@""];
  177. [weakSelf updateEngine];
  178. faild(@"-1", @"Plase Login");
  179. return;
  180. }
  181. faild([NSString stringWithFormat:@"%ld", error.code], error.localizedDescription);
  182. });
  183. }];
  184. // dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), ^{
  185. [self.msgEngine enqueueOperation:op];
  186. // });
  187. }
  188. // get 请求
  189. - (void)getMsgWithPath:(NSString *)path param:(NSDictionary *)param success:(void(^)(id))success faild:(void(^)(NSString *code, NSString *msg))faild {
  190. BOOL isSSL = true;
  191. [self checkToken];
  192. NSMutableDictionary *tempDic = [[NSMutableDictionary alloc] initWithDictionary:self.defuatParam];
  193. [tempDic addEntriesFromDictionary:param];
  194. MKNetworkOperation *op = [self.msgEngine operationWithPath:path params:tempDic httpMethod:@"GET" ssl:isSSL];
  195. __weak typeof(self) weakSelf = self;
  196. [op onCompletion:^(MKNetworkOperation *completedOperation) {
  197. dispatch_async(dispatch_get_main_queue(), ^{
  198. NSString *json = [op responseJSON];
  199. id temp = [json mj_JSONObject];
  200. NSLog(@"---Get-----url:\n%@\n-----data:\n%@\n--",path, temp);
  201. if ([temp isKindOfClass:[NSDictionary class]]) {
  202. NSDictionary *result = (NSDictionary *)temp;
  203. NSString *code = [NSString stringWithFormat:@"%@", result[@"code"]];
  204. id data = result[@"data"];
  205. if ([code isEqualToString:@"200"]) {
  206. success(data);
  207. } else {
  208. NSString *msg = result[@"msg"];
  209. faild(code, msg);
  210. }
  211. }
  212. });
  213. } onError:^(NSError *error) {
  214. dispatch_async(dispatch_get_main_queue(), ^{
  215. NSString *json = [op responseJSON];
  216. id temp = [json mj_JSONObject];
  217. if ([temp isKindOfClass:[NSDictionary class]]) {
  218. NSDictionary *result = (NSDictionary *)temp;
  219. NSString *status = result[@"code"];
  220. NSString *msg = result[@"msg"];
  221. if (error.code == 401) {
  222. [DataUtil setLoginToken:@""];
  223. [weakSelf updateEngine];
  224. faild(status, msg);
  225. return;
  226. }
  227. faild(status, msg);
  228. return;
  229. }
  230. if (error.code == 401) {
  231. [DataUtil setLoginToken:@""];
  232. [weakSelf updateEngine];
  233. faild(@"-1", @"Plase Login");
  234. return;
  235. }
  236. faild([NSString stringWithFormat:@"%ld", error.code], error.localizedDescription);
  237. });
  238. }];
  239. [self.msgEngine enqueueOperation:op];
  240. // dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), ^{
  241. // });
  242. }
  243. // put 请求
  244. - (void)putMsgWithPath:(NSString *)path param:(NSDictionary *)param success:(void(^)(id))success faild:(void(^)(NSString *code, NSString *msg))faild {
  245. BOOL isSSL = true;
  246. //#if (DEBUG)
  247. // isSSL = false;
  248. //#endif
  249. [self checkToken];
  250. NSMutableDictionary *tempDic = [[NSMutableDictionary alloc] initWithDictionary:self.defuatParam];
  251. [tempDic addEntriesFromDictionary:param];
  252. MKNetworkOperation *op = [self.msgEngine operationWithPath:path params:tempDic httpMethod:@"PUT" ssl:isSSL];
  253. [op setPostDataEncoding:MKNKPostDataEncodingTypeJSON];
  254. __weak typeof(self) weakSelf = self;
  255. [op onCompletion:^(MKNetworkOperation *completedOperation) {
  256. dispatch_async(dispatch_get_main_queue(), ^{
  257. NSString *json = [op responseJSON];
  258. id temp = [json mj_JSONObject];
  259. NSLog(@"---Get-----url:\n%@\n-----data:\n%@\n--",path, temp);
  260. if ([temp isKindOfClass:[NSDictionary class]]) {
  261. NSDictionary *result = (NSDictionary *)temp;
  262. NSString *code = [NSString stringWithFormat:@"%@", result[@"code"]];
  263. id data = result[@"data"];
  264. if ([code isEqualToString:@"200"]) {
  265. success(data);
  266. } else {
  267. NSString *msg = result[@"msg"];
  268. faild(code, msg);
  269. }
  270. }
  271. });
  272. } onError:^(NSError *error) {
  273. dispatch_async(dispatch_get_main_queue(), ^{
  274. NSString *json = [op responseJSON];
  275. id temp = [json mj_JSONObject];
  276. if ([temp isKindOfClass:[NSDictionary class]]) {
  277. NSDictionary *result = (NSDictionary *)temp;
  278. NSString *status = result[@"code"];
  279. NSString *msg = result[@"msg"];
  280. if (error.code == 401) {
  281. [DataUtil setLoginToken:@""];
  282. [weakSelf updateEngine];
  283. faild(status, msg);
  284. return;
  285. }
  286. faild(status, msg);
  287. return;
  288. }
  289. if (error.code == 401) {
  290. [DataUtil setLoginToken:@""];
  291. [weakSelf updateEngine];
  292. faild(@"-1", @"Plase Login");
  293. return;
  294. }
  295. faild([NSString stringWithFormat:@"%ld", error.code], error.localizedDescription);
  296. });
  297. }];
  298. // dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), ^{
  299. [self.msgEngine enqueueOperation:op];
  300. // });
  301. }
  302. // del 请求
  303. - (void)delMsgWithPath:(NSString *)path param:(NSDictionary *)param success:(void(^)(id))success faild:(void(^)(NSString *code, NSString *msg))faild {
  304. BOOL isSSL = true;
  305. //#if (DEBUG)
  306. // isSSL = false;
  307. //#endif
  308. [self checkToken];
  309. NSMutableDictionary *tempDic = [[NSMutableDictionary alloc] initWithDictionary:self.defuatParam];
  310. [tempDic addEntriesFromDictionary:param];
  311. MKNetworkOperation *op = [self.msgEngine operationWithPath:path params:tempDic httpMethod:@"DELETE" ssl:isSSL];
  312. [op setPostDataEncoding:MKNKPostDataEncodingTypeJSON];
  313. __weak typeof(self) weakSelf = self;
  314. [op onCompletion:^(MKNetworkOperation *completedOperation) {
  315. dispatch_async(dispatch_get_main_queue(), ^{
  316. NSString *json = [op responseJSON];
  317. id temp = [json mj_JSONObject];
  318. NSLog(@"---Get-----url:\n%@\n-----data:\n%@\n--",path, temp);
  319. if ([temp isKindOfClass:[NSDictionary class]]) {
  320. NSDictionary *result = (NSDictionary *)temp;
  321. NSString *code = [NSString stringWithFormat:@"%@", result[@"code"]];
  322. id data = result[@"data"];
  323. if ([code isEqualToString:@"200"]) {
  324. success(data);
  325. } else {
  326. NSString *msg = result[@"msg"];
  327. faild(code, msg);
  328. }
  329. }
  330. });
  331. } onError:^(NSError *error) {
  332. dispatch_async(dispatch_get_main_queue(), ^{
  333. NSString *json = [op responseJSON];
  334. id temp = [json mj_JSONObject];
  335. if ([temp isKindOfClass:[NSDictionary class]]) {
  336. NSDictionary *result = (NSDictionary *)temp;
  337. NSString *status = result[@"code"];
  338. NSString *msg = result[@"msg"];
  339. if (error.code == 401) {
  340. [DataUtil setLoginToken:@""];
  341. [weakSelf updateEngine];
  342. faild(status, msg);
  343. return;
  344. }
  345. faild(status, msg);
  346. return;
  347. }
  348. if (error.code == 401) {
  349. [DataUtil setLoginToken:@""];
  350. [weakSelf updateEngine];
  351. faild(@"-1", @"Plase Login");
  352. return;
  353. }
  354. faild([NSString stringWithFormat:@"%ld", error.code], error.localizedDescription);
  355. });
  356. }];
  357. // dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), ^{
  358. [self.msgEngine enqueueOperation:op];
  359. // });
  360. }
  361. // MARK: - api V1
  362. // post请求
  363. - (void)postWithPath:(NSString *)path param:(NSDictionary *)param success:(void(^)(id))success faild:(void(^)(NSString *code, NSString *msg))faild {
  364. BOOL isSSL = true;
  365. [self checkToken];
  366. NSMutableDictionary *tempDic = [[NSMutableDictionary alloc] initWithDictionary:self.defuatParam];
  367. [tempDic addEntriesFromDictionary:param];
  368. MKNetworkOperation *op = [self.engine operationWithPath:path params:tempDic httpMethod:@"POST" ssl:isSSL];
  369. [op setPostDataEncoding:MKNKPostDataEncodingTypeJSON];
  370. __weak typeof(self) weakSelf = self;
  371. [op onCompletion:^(MKNetworkOperation *completedOperation) {
  372. dispatch_async(dispatch_get_main_queue(), ^{
  373. NSString *json = [op responseJSON];
  374. id temp = [json mj_JSONObject];
  375. NSLog(@"---Post-----url:\n%@\n-----param:\n%@\n-----data:\n%@\n--",path, param, temp);
  376. if ([temp isKindOfClass:[NSDictionary class]]) {
  377. NSDictionary *result = (NSDictionary *)temp;
  378. NSString *status = [NSString stringWithFormat:@"%@", result[@"status"]];
  379. id data = result[@"data"];
  380. if ([status isEqualToString:@"1"]) {
  381. success(data);
  382. } else {
  383. NSString *msg = result[@"msg"];
  384. faild(status, msg);
  385. }
  386. }
  387. });
  388. } onError:^(NSError *error) {
  389. dispatch_async(dispatch_get_main_queue(), ^{
  390. NSString *json = [op responseJSON];
  391. id temp = [json mj_JSONObject];
  392. if ([temp isKindOfClass:[NSDictionary class]]) {
  393. NSDictionary *result = (NSDictionary *)temp;
  394. NSString *status = result[@"status"];
  395. NSString *msg = result[@"msg"];
  396. if (error.code == 401) {
  397. [DataUtil setLoginToken:@""];
  398. [weakSelf updateEngine];
  399. faild(status, msg);
  400. return;
  401. }
  402. faild(status, msg);
  403. return;
  404. }
  405. if (error.code == 401) {
  406. [DataUtil setLoginToken:@""];
  407. [weakSelf updateEngine];
  408. faild(@"-1", @"Plase Login");
  409. return;
  410. }
  411. faild([NSString stringWithFormat:@"%ld", error.code], error.localizedDescription);
  412. });
  413. }];
  414. // dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), ^{
  415. [self.engine enqueueOperation:op];
  416. // });d
  417. }
  418. // formData的post请求
  419. - (void)formData_postWithPath:(NSString *)path param:(NSDictionary *)param success:(void(^)(id))success faild:(void(^)(NSString *code, NSString *msg))faild {
  420. BOOL isSSL = true;
  421. [self checkToken];
  422. NSMutableDictionary *tempDic = [[NSMutableDictionary alloc] initWithDictionary:self.defuatParam];
  423. [tempDic addEntriesFromDictionary:param];
  424. MKNetworkOperation *op = [self.engine operationWithPath:path params:tempDic httpMethod:@"POST" ssl:isSSL];
  425. ///form-data 的请求格式
  426. NSData *data = [NSJSONSerialization dataWithJSONObject:param options:NSJSONWritingPrettyPrinted error:nil];
  427. for (NSString *key in param.allKeys) {
  428. // 循环拿到所有参数进行拼接
  429. // NSString * searchStr = [param[key] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
  430. NSString * searchStr = param[key];
  431. NSData *data =[searchStr dataUsingEncoding:NSUTF8StringEncoding];
  432. [op addData:data forKey:key];
  433. }
  434. __weak typeof(self) weakSelf = self;
  435. [op onCompletion:^(MKNetworkOperation *completedOperation) {
  436. dispatch_async(dispatch_get_main_queue(), ^{
  437. NSString *json = [op responseJSON];
  438. id temp = [json mj_JSONObject];
  439. NSLog(@"---post-----url:\n%@\n-----data:\n%@\n--",path, temp);
  440. if ([temp isKindOfClass:[NSDictionary class]]) {
  441. NSDictionary *result = (NSDictionary *)temp;
  442. NSString *status = [NSString stringWithFormat:@"%@", result[@"status"]];
  443. id data = result[@"data"];
  444. if ([status isEqualToString:@"1"]) {
  445. success(data);
  446. } else {
  447. NSString *msg = result[@"msg"];
  448. faild(status, msg);
  449. }
  450. }
  451. });
  452. } onError:^(NSError *error) {
  453. dispatch_async(dispatch_get_main_queue(), ^{
  454. NSString *json = [op responseJSON];
  455. id temp = [json mj_JSONObject];
  456. if ([temp isKindOfClass:[NSDictionary class]]) {
  457. NSDictionary *result = (NSDictionary *)temp;
  458. NSString *status = result[@"status"];
  459. NSString *msg = result[@"msg"];
  460. if (error.code == 401) {
  461. [DataUtil setLoginToken:@""];
  462. [weakSelf updateEngine];
  463. faild(status, msg);
  464. return;
  465. }
  466. faild(status, msg);
  467. return;
  468. }
  469. if (error.code == 401) {
  470. [DataUtil setLoginToken:@""];
  471. [weakSelf updateEngine];
  472. faild(@"-1", @"Plase Login");
  473. return;
  474. }
  475. faild([NSString stringWithFormat:@"%ld", error.code], error.localizedDescription);
  476. });
  477. }];
  478. dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), ^{
  479. [self.engine enqueueOperation:op];
  480. });
  481. }
  482. // get 请求
  483. - (void)getWithPath:(NSString *)path param:(NSDictionary *)param success:(void(^)(id))success faild:(void(^)(NSString *code, NSString *msg))faild {
  484. BOOL isSSL = true;
  485. [self checkToken];
  486. NSMutableDictionary *tempDic = [[NSMutableDictionary alloc] initWithDictionary:self.defuatParam];
  487. [tempDic addEntriesFromDictionary:param];
  488. MKNetworkOperation *op = [self.engine operationWithPath:path params:tempDic httpMethod:@"GET" ssl:isSSL];
  489. __weak typeof(self) weakSelf = self;
  490. [op onCompletion:^(MKNetworkOperation *completedOperation) {
  491. dispatch_async(dispatch_get_main_queue(), ^{
  492. NSString *json = [op responseJSON];
  493. id temp = [json mj_JSONObject];
  494. NSLog(@"---Get-----url:\n%@\n-----data:\n%@\n--",path, temp);
  495. if ([temp isKindOfClass:[NSDictionary class]]) {
  496. NSDictionary *result = (NSDictionary *)temp;
  497. NSString *status = [NSString stringWithFormat:@"%@", result[@"status"]];
  498. id data = result[@"data"];
  499. if ([status isEqualToString:@"1"]) {
  500. success(data);
  501. } else {
  502. NSString *msg = result[@"msg"];
  503. faild(status, msg);
  504. }
  505. } else {
  506. NSString *msg = @"failed";
  507. faild(0, msg);
  508. }
  509. });
  510. } onError:^(NSError *error) {
  511. dispatch_async(dispatch_get_main_queue(), ^{
  512. NSString *json = [op responseJSON];
  513. id temp = [json mj_JSONObject];
  514. if ([temp isKindOfClass:[NSDictionary class]]) {
  515. NSDictionary *result = (NSDictionary *)temp;
  516. NSString *status = result[@"status"];
  517. NSString *msg = result[@"msg"];
  518. if (error.code == 401) {
  519. [DataUtil setLoginToken:@""];
  520. [weakSelf updateEngine];
  521. faild(status, msg);
  522. return;
  523. }
  524. faild(status, msg);
  525. return;
  526. }
  527. if (error.code == 401) {
  528. [DataUtil setLoginToken:@""];
  529. [weakSelf updateEngine];
  530. faild(@"-1", @"Plase Login");
  531. return;
  532. }
  533. faild([NSString stringWithFormat:@"%ld", error.code], error.localizedDescription);
  534. });
  535. }];
  536. [self.engine enqueueOperation:op];
  537. // dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), ^{
  538. // });
  539. }
  540. // put 请求
  541. - (void)putWithPath:(NSString *)path param:(NSDictionary *)param success:(void(^)(id))success faild:(void(^)(NSString *code, NSString *msg))faild {
  542. BOOL isSSL = true;
  543. //#if (DEBUG)
  544. // isSSL = false;
  545. //#endif
  546. [self checkToken];
  547. NSMutableDictionary *tempDic = [[NSMutableDictionary alloc] initWithDictionary:self.defuatParam];
  548. [tempDic addEntriesFromDictionary:param];
  549. MKNetworkOperation *op = [self.engine operationWithPath:path params:tempDic httpMethod:@"PUT" ssl:isSSL];
  550. [op setPostDataEncoding:MKNKPostDataEncodingTypeJSON];
  551. __weak typeof(self) weakSelf = self;
  552. [op onCompletion:^(MKNetworkOperation *completedOperation) {
  553. dispatch_async(dispatch_get_main_queue(), ^{
  554. NSString *json = [op responseJSON];
  555. id temp = [json mj_JSONObject];
  556. NSLog(@"---Put-----url:\n%@\n-----data:\n%@\n--",path, temp);
  557. if ([temp isKindOfClass:[NSDictionary class]]) {
  558. NSDictionary *result = (NSDictionary *)temp;
  559. NSString *status = [NSString stringWithFormat:@"%@", result[@"status"]];
  560. id data = result[@"data"];
  561. if ([status isEqualToString:@"1"]) {
  562. success(data);
  563. } else {
  564. NSString *msg = result[@"msg"];
  565. faild(status, msg);
  566. }
  567. }
  568. });
  569. } onError:^(NSError *error) {
  570. dispatch_async(dispatch_get_main_queue(), ^{
  571. NSString *json = [op responseJSON];
  572. id temp = [json mj_JSONObject];
  573. if ([temp isKindOfClass:[NSDictionary class]]) {
  574. NSDictionary *result = (NSDictionary *)temp;
  575. NSString *status = [NSString stringWithFormat:@"%@", result[@"status"]];
  576. NSString *msg = result[@"msg"];
  577. if (error.code == 401) {
  578. [DataUtil setLoginToken:@""];
  579. [weakSelf updateEngine];
  580. faild(status, msg);
  581. return;
  582. }
  583. faild(status, msg);
  584. return;
  585. }
  586. if (error.code == 401) {
  587. [DataUtil setLoginToken:@""];
  588. [weakSelf updateEngine];
  589. faild(@"-1", @"Plase Login");
  590. return;
  591. }
  592. faild([NSString stringWithFormat:@"%ld", error.code], error.localizedDescription);
  593. });
  594. }];
  595. // dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), ^{
  596. [self.engine enqueueOperation:op];
  597. // });
  598. }
  599. // del 请求
  600. - (void)delWithPath:(NSString *)path param:(NSDictionary *)param success:(void(^)(id))success faild:(void(^)(NSString *code, NSString *msg))faild {
  601. BOOL isSSL = true;
  602. //#if (DEBUG)
  603. // isSSL = false;
  604. //#endif
  605. [self checkToken];
  606. NSMutableDictionary *tempDic = [[NSMutableDictionary alloc] initWithDictionary:self.defuatParam];
  607. [tempDic addEntriesFromDictionary:param];
  608. MKNetworkOperation *op = [self.engine operationWithPath:path params:tempDic httpMethod:@"DELETE" ssl:isSSL];
  609. [op setPostDataEncoding:MKNKPostDataEncodingTypeJSON];
  610. __weak typeof(self) weakSelf = self;
  611. [op onCompletion:^(MKNetworkOperation *completedOperation) {
  612. dispatch_async(dispatch_get_main_queue(), ^{
  613. NSString *json = [op responseJSON];
  614. id temp = [json mj_JSONObject];
  615. if ([temp isKindOfClass:[NSDictionary class]]) {
  616. NSDictionary *result = (NSDictionary *)temp;
  617. NSString *status = [NSString stringWithFormat:@"%@", result[@"status"]];
  618. id data = result[@"data"];
  619. if ([status isEqualToString:@"1"]) {
  620. success(data);
  621. } else {
  622. NSString *msg = result[@"msg"];
  623. faild(status, msg);
  624. }
  625. }
  626. });
  627. } onError:^(NSError *error) {
  628. dispatch_async(dispatch_get_main_queue(), ^{
  629. NSString *json = [op responseJSON];
  630. id temp = [json mj_JSONObject];
  631. if ([temp isKindOfClass:[NSDictionary class]]) {
  632. NSDictionary *result = (NSDictionary *)temp;
  633. NSString *status = result[@"status"];
  634. NSString *msg = result[@"msg"];
  635. if (error.code == 401) {
  636. [DataUtil setLoginToken:@""];
  637. [weakSelf updateEngine];
  638. faild(status, msg);
  639. return;
  640. }
  641. faild(status, msg);
  642. return;
  643. }
  644. if (error.code == 401) {
  645. [DataUtil setLoginToken:@""];
  646. [weakSelf updateEngine];
  647. faild(@"-1", @"Plase Login");
  648. return;
  649. }
  650. faild([NSString stringWithFormat:@"%ld", error.code], error.localizedDescription);
  651. });
  652. }];
  653. // dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), ^{
  654. [self.engine enqueueOperation:op];
  655. // });
  656. }
  657. // MARK: - debug
  658. + (void)debugRegist {
  659. NSDictionary *param = @{
  660. @"customer": @{
  661. @"email": @"abc123456789@qq.com",
  662. @"firstname": @"ujrbcf",
  663. @"lastname": @"serwt",
  664. @"dob": @"2023/10/20"
  665. },
  666. @"password": @"A123456a",
  667. @"extension_attributes" : @{
  668. @"is_subscribed": @(true)
  669. }
  670. };
  671. [ASNetTools.shared postWithPath:registerUrl param:param success:^(id _Nonnull result) {
  672. NSLog(@"----url:%@-----result:%@------", registerUrl, result);
  673. } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) {
  674. NSLog(@"----url:%@-----code:%@----msg:%@--", registerUrl, code, msg);
  675. }];
  676. }
  677. + (void)login {
  678. NSDictionary *param = @{
  679. @"username" :@"abc123456789@qq.com",//@"Britneyngwafa@gmail.com",//
  680. @"password":@"A123456a",//@"1qazXSW@123",//
  681. };
  682. [ASNetTools.shared postWithPath:loginUrl param:param success:^(id _Nonnull result) {
  683. NSString *token = [NSString stringWithFormat:@"Bearer %@", result];
  684. [DataUtil setLoginToken:token];
  685. [ASNetTools.shared updateEngine];
  686. [ASUserInfoManager.shared getInfo];
  687. NSLog(@"----url:%@-----result:%@------", loginUrl, result);
  688. } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) {
  689. NSLog(@"----url:%@-----code:%@----msg:%@--", loginUrl, code, msg);
  690. }];
  691. }
  692. +(void)xxx_loginWithParam:(NSDictionary *)param
  693. success:(void(^)(id))success
  694. faild:(void(^)(NSString *code, NSString *msg))faild {
  695. [ASNetTools.shared postWithPath:loginUrl param:param success:^(id _Nonnull result) {
  696. NSString *token = [NSString stringWithFormat:@"Bearer %@", result];
  697. [DataUtil setLoginToken:token];
  698. [ASNetTools.shared updateEngine];
  699. [ASUserInfoManager.shared getInfo];
  700. success(result);
  701. NSLog(@"----url:%@-----result:%@------", loginUrl, result);
  702. } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) {
  703. NSLog(@"----url:%@-----code:%@----msg:%@--", loginUrl, code, msg);
  704. faild(code,msg);
  705. }];
  706. }
  707. ///获取广告位优惠卷列表
  708. +(void)reqNet_getAdvCoupons{
  709. [ASNetTools.shared getWithPath:BaseRequestrUrl(@"advCoupons") param:@{} success:^(id _Nonnull result) {
  710. [ASNetTools shared].xxx_couponAry = result;
  711. } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) {
  712. }];
  713. }
  714. -(NSMutableArray *)xxx_couponAry{
  715. if(!_xxx_couponAry){
  716. _xxx_couponAry = [[NSMutableArray alloc]init];
  717. }
  718. return _xxx_couponAry;
  719. }
  720. @end