ASNetTools.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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. });
  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. if ([temp isKindOfClass:[NSDictionary class]]) {
  138. NSDictionary *result = (NSDictionary *)temp;
  139. NSString *status = [NSString stringWithFormat:@"%@", result[@"status"]];
  140. id data = result[@"data"];
  141. if ([status isEqualToString:@"1"]) {
  142. success(data);
  143. } else {
  144. NSString *msg = result[@"msg"];
  145. faild(status, msg);
  146. }
  147. }
  148. });
  149. } onError:^(NSError *error) {
  150. dispatch_async(dispatch_get_main_queue(), ^{
  151. NSString *json = [op responseJSON];
  152. id temp = [json mj_JSONObject];
  153. if ([temp isKindOfClass:[NSDictionary class]]) {
  154. NSDictionary *result = (NSDictionary *)temp;
  155. NSString *status = result[@"status"];
  156. NSString *msg = result[@"msg"];
  157. if (error.code == 401) {
  158. [DataUtil setLoginToken:@""];
  159. [weakSelf updateEngine];
  160. faild(status, msg);
  161. return;
  162. }
  163. faild(status, msg);
  164. return;
  165. }
  166. if (error.code == 401) {
  167. [DataUtil setLoginToken:@""];
  168. [weakSelf updateEngine];
  169. faild(@"-1", @"Plase Login");
  170. return;
  171. }
  172. faild([NSString stringWithFormat:@"%ld", error.code], error.localizedDescription);
  173. });
  174. }];
  175. dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), ^{
  176. [self.engine enqueueOperation:op];
  177. });
  178. }
  179. // get 请求
  180. - (void)getWithPath:(NSString *)path param:(NSDictionary *)param success:(void(^)(id))success faild:(void(^)(NSString *code, NSString *msg))faild {
  181. BOOL isSSL = true;
  182. [self checkToken];
  183. NSMutableDictionary *tempDic = [[NSMutableDictionary alloc] initWithDictionary:self.defuatParam];
  184. [tempDic addEntriesFromDictionary:param];
  185. MKNetworkOperation *op = [self.engine operationWithPath:path params:tempDic httpMethod:@"GET" ssl:isSSL];
  186. __weak typeof(self) weakSelf = self;
  187. [op onCompletion:^(MKNetworkOperation *completedOperation) {
  188. dispatch_async(dispatch_get_main_queue(), ^{
  189. NSString *json = [op responseJSON];
  190. id temp = [json mj_JSONObject];
  191. if ([temp isKindOfClass:[NSDictionary class]]) {
  192. NSDictionary *result = (NSDictionary *)temp;
  193. NSString *status = [NSString stringWithFormat:@"%@", result[@"status"]];
  194. id data = result[@"data"];
  195. if ([status isEqualToString:@"1"]) {
  196. success(data);
  197. } else {
  198. NSString *msg = result[@"msg"];
  199. faild(status, msg);
  200. }
  201. }
  202. });
  203. } onError:^(NSError *error) {
  204. dispatch_async(dispatch_get_main_queue(), ^{
  205. NSString *json = [op responseJSON];
  206. id temp = [json mj_JSONObject];
  207. if ([temp isKindOfClass:[NSDictionary class]]) {
  208. NSDictionary *result = (NSDictionary *)temp;
  209. NSString *status = result[@"status"];
  210. NSString *msg = result[@"msg"];
  211. if (error.code == 401) {
  212. [DataUtil setLoginToken:@""];
  213. [weakSelf updateEngine];
  214. faild(status, msg);
  215. return;
  216. }
  217. faild(status, msg);
  218. return;
  219. }
  220. if (error.code == 401) {
  221. [DataUtil setLoginToken:@""];
  222. [weakSelf updateEngine];
  223. faild(@"-1", @"Plase Login");
  224. return;
  225. }
  226. faild([NSString stringWithFormat:@"%ld", error.code], error.localizedDescription);
  227. });
  228. }];
  229. dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), ^{
  230. [self.engine enqueueOperation:op];
  231. });
  232. }
  233. // put 请求
  234. - (void)putWithPath:(NSString *)path param:(NSDictionary *)param success:(void(^)(id))success faild:(void(^)(NSString *code, NSString *msg))faild {
  235. BOOL isSSL = true;
  236. //#if (DEBUG)
  237. // isSSL = false;
  238. //#endif
  239. [self checkToken];
  240. NSMutableDictionary *tempDic = [[NSMutableDictionary alloc] initWithDictionary:self.defuatParam];
  241. [tempDic addEntriesFromDictionary:param];
  242. MKNetworkOperation *op = [self.engine operationWithPath:path params:tempDic httpMethod:@"PUT" ssl:isSSL];
  243. [op setPostDataEncoding:MKNKPostDataEncodingTypeJSON];
  244. __weak typeof(self) weakSelf = self;
  245. [op onCompletion:^(MKNetworkOperation *completedOperation) {
  246. dispatch_async(dispatch_get_main_queue(), ^{
  247. NSString *json = [op responseJSON];
  248. id temp = [json mj_JSONObject];
  249. if ([temp isKindOfClass:[NSDictionary class]]) {
  250. NSDictionary *result = (NSDictionary *)temp;
  251. NSString *status = [NSString stringWithFormat:@"%@", result[@"status"]];
  252. id data = result[@"data"];
  253. if ([status isEqualToString:@"1"]) {
  254. success(data);
  255. } else {
  256. NSString *msg = result[@"msg"];
  257. faild(status, msg);
  258. }
  259. }
  260. });
  261. } onError:^(NSError *error) {
  262. dispatch_async(dispatch_get_main_queue(), ^{
  263. NSString *json = [op responseJSON];
  264. id temp = [json mj_JSONObject];
  265. if ([temp isKindOfClass:[NSDictionary class]]) {
  266. NSDictionary *result = (NSDictionary *)temp;
  267. NSString *status = result[@"status"];
  268. NSString *msg = result[@"msg"];
  269. if (error.code == 401) {
  270. [DataUtil setLoginToken:@""];
  271. [weakSelf updateEngine];
  272. faild(status, msg);
  273. return;
  274. }
  275. faild(status, msg);
  276. return;
  277. }
  278. if (error.code == 401) {
  279. [DataUtil setLoginToken:@""];
  280. [weakSelf updateEngine];
  281. faild(@"-1", @"Plase Login");
  282. return;
  283. }
  284. faild([NSString stringWithFormat:@"%ld", error.code], error.localizedDescription);
  285. });
  286. }];
  287. dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), ^{
  288. [self.engine enqueueOperation:op];
  289. });
  290. }
  291. // del 请求
  292. - (void)delWithPath:(NSString *)path param:(NSDictionary *)param success:(void(^)(id))success faild:(void(^)(NSString *code, NSString *msg))faild {
  293. BOOL isSSL = true;
  294. //#if (DEBUG)
  295. // isSSL = false;
  296. //#endif
  297. [self checkToken];
  298. NSMutableDictionary *tempDic = [[NSMutableDictionary alloc] initWithDictionary:self.defuatParam];
  299. [tempDic addEntriesFromDictionary:param];
  300. MKNetworkOperation *op = [self.engine operationWithPath:path params:tempDic httpMethod:@"DELETE" ssl:isSSL];
  301. [op setPostDataEncoding:MKNKPostDataEncodingTypeJSON];
  302. __weak typeof(self) weakSelf = self;
  303. [op onCompletion:^(MKNetworkOperation *completedOperation) {
  304. dispatch_async(dispatch_get_main_queue(), ^{
  305. NSString *json = [op responseJSON];
  306. id temp = [json mj_JSONObject];
  307. if ([temp isKindOfClass:[NSDictionary class]]) {
  308. NSDictionary *result = (NSDictionary *)temp;
  309. NSString *status = [NSString stringWithFormat:@"%@", result[@"status"]];
  310. id data = result[@"data"];
  311. if ([status isEqualToString:@"1"]) {
  312. success(data);
  313. } else {
  314. NSString *msg = result[@"msg"];
  315. faild(status, msg);
  316. }
  317. }
  318. });
  319. } onError:^(NSError *error) {
  320. dispatch_async(dispatch_get_main_queue(), ^{
  321. NSString *json = [op responseJSON];
  322. id temp = [json mj_JSONObject];
  323. if ([temp isKindOfClass:[NSDictionary class]]) {
  324. NSDictionary *result = (NSDictionary *)temp;
  325. NSString *status = result[@"status"];
  326. NSString *msg = result[@"msg"];
  327. if (error.code == 401) {
  328. [DataUtil setLoginToken:@""];
  329. [weakSelf updateEngine];
  330. faild(status, msg);
  331. return;
  332. }
  333. faild(status, msg);
  334. return;
  335. }
  336. if (error.code == 401) {
  337. [DataUtil setLoginToken:@""];
  338. [weakSelf updateEngine];
  339. faild(@"-1", @"Plase Login");
  340. return;
  341. }
  342. faild([NSString stringWithFormat:@"%ld", error.code], error.localizedDescription);
  343. });
  344. }];
  345. dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), ^{
  346. [self.engine enqueueOperation:op];
  347. });
  348. }
  349. + (void)debugRegist {
  350. NSDictionary *param = @{
  351. @"customer": @{
  352. @"email": @"abc123456789@qq.com",
  353. @"firstname": @"ujrbcf",
  354. @"lastname": @"serwt",
  355. @"dob": @"2023/10/20"
  356. },
  357. @"password": @"A123456a",
  358. @"extension_attributes" : @{
  359. @"is_subscribed": @(true)
  360. }
  361. };
  362. [ASNetTools.shared postWithPath:registerUrl param:param success:^(id _Nonnull result) {
  363. NSLog(@"----url:%@-----result:%@------", registerUrl, result);
  364. } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) {
  365. NSLog(@"----url:%@-----code:%@----msg:%@--", registerUrl, code, msg);
  366. }];
  367. }
  368. + (void)login {
  369. NSDictionary *param = @{
  370. @"username" :@"Britneyngwafa@gmail.com",//@"abc123456789@qq.com",
  371. @"password":@"1qazXSW@123"//@"A123456a"
  372. };
  373. [ASNetTools.shared postWithPath:loginUrl param:param success:^(id _Nonnull result) {
  374. NSString *token = [NSString stringWithFormat:@"Bearer %@", result];
  375. [DataUtil setLoginToken:token];
  376. [ASNetTools.shared updateEngine];
  377. [ASUserInfoManager.shared getInfo];
  378. NSLog(@"----url:%@-----result:%@------", loginUrl, result);
  379. } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) {
  380. NSLog(@"----url:%@-----code:%@----msg:%@--", loginUrl, code, msg);
  381. }];
  382. }
  383. +(void)xxx_loginWithParam:(NSDictionary *)param
  384. success:(void(^)(id))success
  385. faild:(void(^)(NSString *code, NSString *msg))faild {
  386. [ASNetTools.shared postWithPath:loginUrl param:param success:^(id _Nonnull result) {
  387. NSString *token = [NSString stringWithFormat:@"Bearer %@", result];
  388. [DataUtil setLoginToken:token];
  389. [ASNetTools.shared updateEngine];
  390. [ASUserInfoManager.shared getInfo];
  391. success(result);
  392. NSLog(@"----url:%@-----result:%@------", loginUrl, result);
  393. } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) {
  394. NSLog(@"----url:%@-----code:%@----msg:%@--", loginUrl, code, msg);
  395. faild(code,msg);
  396. }];
  397. }
  398. @end