ASNetTools.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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, strong) MKNetworkEngine *engine;
  11. @property (nonatomic, strong) NSMutableDictionary *defuatParam;
  12. @end
  13. @implementation ASNetTools
  14. + (instancetype)shared {
  15. static id sharedInstance = nil;
  16. static dispatch_once_t onceToken;
  17. dispatch_once(&onceToken, ^{
  18. sharedInstance = [[self alloc] init];
  19. });
  20. return sharedInstance;
  21. }
  22. - (instancetype)init
  23. {
  24. self = [super init];
  25. if (self) {
  26. [self updateEngine];
  27. self.defuatParam = [NSMutableDictionary dictionary];
  28. }
  29. return self;
  30. }
  31. - (void)setDefualtParam:(NSDictionary *)defualt {
  32. self.defuatParam = [[NSMutableDictionary alloc] initWithDictionary:defualt];
  33. }
  34. - (void)updateDefualtParam:(NSDictionary *)newData {
  35. for (NSString *key in newData.allKeys) {
  36. self.defuatParam[key] = newData[key];
  37. }
  38. }
  39. - (void)updateEngine {
  40. NSString *token = [NSUserDefaults.standardUserDefaults stringForKey:TokenKey];
  41. if (!token) {
  42. token = @"";
  43. }
  44. self.engine = [[MKNetworkEngine alloc] initWithHostName:HostPath customHeaderFields:@{
  45. @"Authorization":token,
  46. @"powerby": @"longyitec",
  47. @"Content-Type": @"application/json;charset=utf-8"
  48. }];
  49. }
  50. // post请求
  51. - (void)postWithPath:(NSString *)path param:(NSDictionary *)param success:(void(^)(id))success faild:(void(^)(NSString *code, NSString *msg))faild {
  52. BOOL isSSL = true;
  53. //#if (DEBUG)
  54. // isSSL = false;
  55. //#endif
  56. NSMutableDictionary *tempDic = [[NSMutableDictionary alloc] initWithDictionary:self.defuatParam];
  57. [tempDic addEntriesFromDictionary:param];
  58. MKNetworkOperation *op = [self.engine operationWithPath:path params:tempDic httpMethod:@"POST" ssl:isSSL];
  59. [op setPostDataEncoding:MKNKPostDataEncodingTypeJSON];
  60. __weak typeof(self) weakSelf = self;
  61. [op onCompletion:^(MKNetworkOperation *completedOperation) {
  62. dispatch_async(dispatch_get_main_queue(), ^{
  63. NSString *json = [op responseJSON];
  64. id temp = [json mj_JSONObject];
  65. if ([temp isKindOfClass:[NSDictionary class]]) {
  66. NSDictionary *result = (NSDictionary *)temp;
  67. NSString *status = [NSString stringWithFormat:@"%@", result[@"status"]];
  68. id data = result[@"data"];
  69. if ([status isEqualToString:@"1"]) {
  70. success(data);
  71. } else {
  72. NSString *msg = result[@"msg"];
  73. faild(status, msg);
  74. }
  75. }
  76. });
  77. } onError:^(NSError *error) {
  78. dispatch_async(dispatch_get_main_queue(), ^{
  79. NSString *json = [op responseJSON];
  80. id temp = [json mj_JSONObject];
  81. if ([temp isKindOfClass:[NSDictionary class]]) {
  82. NSDictionary *result = (NSDictionary *)temp;
  83. NSString *status = result[@"status"];
  84. NSString *msg = result[@"msg"];
  85. if (error.code == 401) {
  86. [NSUserDefaults.standardUserDefaults setValue:@"" forKey:TokenKey];
  87. [weakSelf updateEngine];
  88. faild(status, msg);
  89. return;
  90. }
  91. faild(status, msg);
  92. return;
  93. }
  94. if (error.code == 401) {
  95. [NSUserDefaults.standardUserDefaults setValue:@"" forKey:TokenKey];
  96. [weakSelf updateEngine];
  97. faild(@"-1", @"Plase Login");
  98. return;
  99. }
  100. faild([NSString stringWithFormat:@"%ld", error.code], error.localizedDescription);
  101. });
  102. }];
  103. dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), ^{
  104. [self.engine enqueueOperation:op];
  105. });
  106. }
  107. // get 请求
  108. - (void)getWithPath:(NSString *)path param:(NSDictionary *)param success:(void(^)(id))success faild:(void(^)(NSString *code, NSString *msg))faild {
  109. BOOL isSSL = true;
  110. //#if (DEBUG)
  111. // isSSL = false;
  112. //#endif
  113. NSMutableDictionary *tempDic = [[NSMutableDictionary alloc] initWithDictionary:self.defuatParam];
  114. [tempDic addEntriesFromDictionary:param];
  115. MKNetworkOperation *op = [self.engine operationWithPath:path params:tempDic httpMethod:@"GET" ssl:isSSL];
  116. __weak typeof(self) weakSelf = self;
  117. [op onCompletion:^(MKNetworkOperation *completedOperation) {
  118. dispatch_async(dispatch_get_main_queue(), ^{
  119. NSString *json = [op responseJSON];
  120. id temp = [json mj_JSONObject];
  121. if ([temp isKindOfClass:[NSDictionary class]]) {
  122. NSDictionary *result = (NSDictionary *)temp;
  123. NSString *status = [NSString stringWithFormat:@"%@", result[@"status"]];
  124. id data = result[@"data"];
  125. if ([status isEqualToString:@"1"]) {
  126. success(data);
  127. } else {
  128. NSString *msg = result[@"msg"];
  129. faild(status, msg);
  130. }
  131. }
  132. });
  133. } onError:^(NSError *error) {
  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 = result[@"status"];
  140. NSString *msg = result[@"msg"];
  141. if (error.code == 401) {
  142. [NSUserDefaults.standardUserDefaults setValue:@"" forKey:TokenKey];
  143. [weakSelf updateEngine];
  144. faild(status, msg);
  145. return;
  146. }
  147. faild(status, msg);
  148. return;
  149. }
  150. if (error.code == 401) {
  151. [NSUserDefaults.standardUserDefaults setValue:@"" forKey:TokenKey];
  152. [weakSelf updateEngine];
  153. faild(@"-1", @"Plase Login");
  154. return;
  155. }
  156. faild([NSString stringWithFormat:@"%ld", error.code], error.localizedDescription);
  157. });
  158. }];
  159. dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), ^{
  160. [self.engine enqueueOperation:op];
  161. });
  162. }
  163. // put 请求
  164. - (void)putWithPath:(NSString *)path param:(NSDictionary *)param success:(void(^)(id))success faild:(void(^)(NSString *code, NSString *msg))faild {
  165. BOOL isSSL = true;
  166. //#if (DEBUG)
  167. // isSSL = false;
  168. //#endif
  169. NSMutableDictionary *tempDic = [[NSMutableDictionary alloc] initWithDictionary:self.defuatParam];
  170. [tempDic addEntriesFromDictionary:param];
  171. MKNetworkOperation *op = [self.engine operationWithPath:path params:tempDic httpMethod:@"PUT" ssl:isSSL];
  172. [op setPostDataEncoding:MKNKPostDataEncodingTypeJSON];
  173. __weak typeof(self) weakSelf = self;
  174. [op onCompletion:^(MKNetworkOperation *completedOperation) {
  175. dispatch_async(dispatch_get_main_queue(), ^{
  176. NSString *json = [op responseJSON];
  177. id temp = [json mj_JSONObject];
  178. if ([temp isKindOfClass:[NSDictionary class]]) {
  179. NSDictionary *result = (NSDictionary *)temp;
  180. NSString *status = [NSString stringWithFormat:@"%@", result[@"status"]];
  181. id data = result[@"data"];
  182. if ([status isEqualToString:@"1"]) {
  183. success(data);
  184. } else {
  185. NSString *msg = result[@"msg"];
  186. faild(status, msg);
  187. }
  188. }
  189. });
  190. } onError:^(NSError *error) {
  191. dispatch_async(dispatch_get_main_queue(), ^{
  192. NSString *json = [op responseJSON];
  193. id temp = [json mj_JSONObject];
  194. if ([temp isKindOfClass:[NSDictionary class]]) {
  195. NSDictionary *result = (NSDictionary *)temp;
  196. NSString *status = result[@"status"];
  197. NSString *msg = result[@"msg"];
  198. if (error.code == 401) {
  199. [NSUserDefaults.standardUserDefaults setValue:@"" forKey:TokenKey];
  200. [weakSelf updateEngine];
  201. faild(status, msg);
  202. return;
  203. }
  204. faild(status, msg);
  205. return;
  206. }
  207. if (error.code == 401) {
  208. [NSUserDefaults.standardUserDefaults setValue:@"" forKey:TokenKey];
  209. [weakSelf updateEngine];
  210. faild(@"-1", @"Plase Login");
  211. return;
  212. }
  213. faild([NSString stringWithFormat:@"%ld", error.code], error.localizedDescription);
  214. });
  215. }];
  216. dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), ^{
  217. [self.engine enqueueOperation:op];
  218. });
  219. }
  220. // del 请求
  221. - (void)delWithPath:(NSString *)path param:(NSDictionary *)param success:(void(^)(id))success faild:(void(^)(NSString *code, NSString *msg))faild {
  222. BOOL isSSL = true;
  223. //#if (DEBUG)
  224. // isSSL = false;
  225. //#endif
  226. NSMutableDictionary *tempDic = [[NSMutableDictionary alloc] initWithDictionary:self.defuatParam];
  227. [tempDic addEntriesFromDictionary:param];
  228. MKNetworkOperation *op = [self.engine operationWithPath:path params:tempDic httpMethod:@"DELETE" ssl:isSSL];
  229. [op setPostDataEncoding:MKNKPostDataEncodingTypeJSON];
  230. __weak typeof(self) weakSelf = self;
  231. [op onCompletion:^(MKNetworkOperation *completedOperation) {
  232. dispatch_async(dispatch_get_main_queue(), ^{
  233. NSString *json = [op responseJSON];
  234. id temp = [json mj_JSONObject];
  235. if ([temp isKindOfClass:[NSDictionary class]]) {
  236. NSDictionary *result = (NSDictionary *)temp;
  237. NSString *status = [NSString stringWithFormat:@"%@", result[@"status"]];
  238. id data = result[@"data"];
  239. if ([status isEqualToString:@"1"]) {
  240. success(data);
  241. } else {
  242. NSString *msg = result[@"msg"];
  243. faild(status, msg);
  244. }
  245. }
  246. });
  247. } onError:^(NSError *error) {
  248. dispatch_async(dispatch_get_main_queue(), ^{
  249. NSString *json = [op responseJSON];
  250. id temp = [json mj_JSONObject];
  251. if ([temp isKindOfClass:[NSDictionary class]]) {
  252. NSDictionary *result = (NSDictionary *)temp;
  253. NSString *status = result[@"status"];
  254. NSString *msg = result[@"msg"];
  255. if (error.code == 401) {
  256. [NSUserDefaults.standardUserDefaults setValue:@"" forKey:TokenKey];
  257. [weakSelf updateEngine];
  258. faild(status, msg);
  259. return;
  260. }
  261. faild(status, msg);
  262. return;
  263. }
  264. if (error.code == 401) {
  265. [NSUserDefaults.standardUserDefaults setValue:@"" forKey:TokenKey];
  266. [weakSelf updateEngine];
  267. faild(@"-1", @"Plase Login");
  268. return;
  269. }
  270. faild([NSString stringWithFormat:@"%ld", error.code], error.localizedDescription);
  271. });
  272. }];
  273. dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), ^{
  274. [self.engine enqueueOperation:op];
  275. });
  276. }
  277. + (void)debugRegist {
  278. NSDictionary *param = @{
  279. @"customer": @{
  280. @"email": @"abc123456@qq.com",
  281. @"firstname": @"ujrbcf",
  282. @"lastname": @"serwt",
  283. @"dob": @"2023/10/20"
  284. },
  285. @"password": @"a123456A",
  286. @"extension_attributes" : @{
  287. @"is_subscribed": @(true)
  288. }
  289. };
  290. [ASNetTools.shared postWithPath:registerUrl param:param success:^(id _Nonnull result) {
  291. NSLog(@"----url:%@-----result:%@------", registerUrl, result);
  292. } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) {
  293. NSLog(@"----url:%@-----code:%@----msg:%@--", registerUrl, code, msg);
  294. }];
  295. }
  296. + (void)login {
  297. NSDictionary *param = @{
  298. @"username" :@"abc123456@qq.com",
  299. @"password":@"a123456A"
  300. };
  301. [ASNetTools.shared postWithPath:loginUrl param:param success:^(id _Nonnull result) {
  302. NSString *token = [NSString stringWithFormat:@"Bearer %@", result];
  303. [NSUserDefaults.standardUserDefaults setValue:token forKey:TokenKey];
  304. [ASNetTools.shared updateEngine];
  305. [ASUserInfoManager.shared getInfo];
  306. NSLog(@"----url:%@-----result:%@------", loginUrl, result);
  307. } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) {
  308. NSLog(@"----url:%@-----code:%@----msg:%@--", loginUrl, code, msg);
  309. }];
  310. }
  311. @end