1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
//
// KWMAPIManager.m
// iXiaoWeiOA
//
// Created by kevin on 3/14/15.
// Copyright (c) 2015 kollway. All rights reserved.
//
#import "KWMAPIManager.h"
#import "KWMStringUtil.h"
#import "KWMRequestListResult.h"
#import "KWMBaseModel.h"
#import "KWMUser.h"
#import "NSString+SAMAdditions.h"
#import "KWMAPIManager.h"
#import <UIImageView+WebCache.h>
#import "KWMUserDao.h"
#import "KWMCustomer.h"
#import "KWMCustomerResult.h"
#import "KWMBrandsTypeResult.h"
#import "KWMBrandsTypeModel.h"
#import "KWMDictionaryResult.h"
#import "KWMOrderListResult.h"
#import <RegexKitLite/RegexKitLite.h>
#import <SAMKeychain/SAMKeychain.h>
#import "KWMUserModel.h"
#import "KWMHttpUtil.h"
#import "KWMHomeDataResult.h"
/** 使用API缓存(isNeedCache为True)的请求的链接必须是完整的,因为PPNetworkHelper里的Sessionmanage是一开始就初始化了, BaseUrl无法设置 **/
typedef NS_ENUM(NSInteger, KWMHTTPMethod) {
KWMHTTPMethodGET = 0,
KWMHTTPMethodPOST,
KWMHTTPMethodPUT,
KWMHTTPMethodDELETE
};
@implementation KWMAPIManager
+ (KWMAPIManager *)sharedManager {
static KWMAPIManager *_sharedManager = nil;
static dispatch_once_t oncePredicate;
dispatch_once(&oncePredicate, ^{
_sharedManager = [[self alloc] init];
});
return _sharedManager;
}
- (NSString *)toAbsoluteUrl:(NSString *)relativeUrl {
if([KWMStringUtil isEmpty:relativeUrl]){
return self.baseUrl;
}
if([relativeUrl rangeOfString:@"http"].location == 0){
return relativeUrl;
}
return [[NSString alloc] initWithFormat:@"%@%@", self.baseUrl, relativeUrl];
}
- (NSString *)baseUrl {
if(! _baseUrl){
if (API_HOST_PORT != 8088) {
_baseUrl = [[NSString alloc] initWithFormat:@"https://%@:%d", API_HOST, API_HOST_PORT];
}else{
_baseUrl = [[NSString alloc] initWithFormat:@"https://%@", API_HOST];
}
}
return _baseUrl;
}
-(void(^)(NSError *error))buildNewFaliedCallback:(void (^)(NSURLSessionDataTask *task, NSError *error))failure{
void(^failedCallback)(NSError *error) =
^(NSError *error){
failure(nil, error);
};
return failedCallback;
}
- (void(^)(id responseObject))buildNewCacheCallbackWithResultCalss:
(Class)requestResultClass
modelClass:(Class)modelClass
callback:(void (^)(id responseObject))callback
failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure{
if(!callback){
return nil;
}
void(^cacheCallback)(id responseObject) =
^(id responseObject){
NSError *error;
if(!responseObject){
callback(nil);
}else{
id result = nil;
@try {
result = [[requestResultClass alloc] initWithDictionary:responseObject modelClass:modelClass error:&error];
}
@catch (NSException *e){
NSLog(@"%@", [e description]);
}
@finally{
[self checkRequestResult:result];
if(error){
failure(nil, error);
}else{
callback(result);
}
}
}
};
return cacheCallback;
}
- (void(^)(id responseObject))buildNewSuccessCallbackWithResultCalss:
(Class)requestResultClass
modelClass:(Class)modelClass
success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure{
void(^successCallback)(id responseObject) =
^(id responseObject){
NSError *error;
if(!responseObject){
success(nil, nil);
}else{
id result = nil;
@try {
result = [[requestResultClass alloc] initWithDictionary:responseObject modelClass:modelClass error:&error];
}
@catch (NSException *e){
NSLog(@"%@", [e description]);
}
@finally{
[self checkRequestResult:result];
if(error){
failure(nil, error);
}else{
success(nil, result);
}
}
}
};
return successCallback;
}
/**
* 根据API返回类型,模型类型创建成功callback
*/
- (void(^)(NSURLSessionDataTask *, id ))buildSuccessCallbackWithResultClass:(Class)requestResultClass
modelClass:(Class)modelClass
success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure{
void(^successCallback)(NSURLSessionDataTask *, id ) =
^(NSURLSessionDataTask *task, id jsonDict){
NSError *error;
id result = nil;
@try {
//#ifdef DEBUG
// NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDict options:NSJSONWritingPrettyPrinted error:&error];
// NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
// NSLog(@"%@", jsonString);
//#endif
result = [[requestResultClass alloc] initWithDictionary:jsonDict modelClass:modelClass error:&error];
}
@catch (NSException *e){
NSLog(@"%@", [e description]);
}
@finally{
[self checkRequestResult:result];
if(error){
failure(task, error);
}else{
success(task, result);
}
}
};
return successCallback;
}
/**
* 检测API返回错误号
*/
- (void)checkRequestResult:(id)requestResult {
if(! requestResult) {
return;
}
NSString *message = nil;
NSInteger responseCode = 0;
if([requestResult isKindOfClass:[KWMRequestResult class]]){
responseCode = ((KWMRequestResult *)requestResult).code;
message = ((KWMRequestResult *)requestResult).message;
}else if([requestResult isKindOfClass:[KWMRequestListResult class]]){
responseCode = ((KWMRequestListResult *)requestResult).code;
message = ((KWMRequestListResult *)requestResult).message;
}else{
return;
}
switch (responseCode){
case 1: {
[[NSNotificationCenter defaultCenter] postNotificationName:KWMReceiverNoLogin object:nil userInfo: nil];
break;
}
default:break;
}
}
/**
* 处理列表API的请求参数
*/
- (NSDictionary *)processListAPIParameters:(NSDictionary *)parameters {
static NSString *KEY_PAGE = @"page";
static NSString *KEY_PAGE_SIZE = @"pagesize";
NSMutableDictionary *mutableParameters = [[NSMutableDictionary alloc] initWithDictionary:parameters];
NSAssert((mutableParameters && mutableParameters[KEY_PAGE]), @"required parameter: page");
if(!mutableParameters[KEY_PAGE_SIZE]){
mutableParameters[KEY_PAGE_SIZE] = @(KWM_PAGE_SIZE).stringValue;
}
return [[NSDictionary alloc] initWithDictionary:mutableParameters];
}
//账号 密码
//正式
static NSString *const userName = @"868f335328bf6ea61fc94448ed314ace";
static NSString *const password = @"ae6dc70d26e9315f370ebade3e86d21b";
//测试
static NSString *const userNameTest = @"55275437287d63252b80dc5a784b3b8f";
static NSString *const passwordTest = @"9e84aae218c57cdf0762763c4cf5a651";
- (AFHTTPSessionManager *)buildSessionManager{
return [self buildSessionManager:NO afSessionManage:nil isUseClientAuth:NO];
}
- (AFHTTPSessionManager *)buildSessionManager:(BOOL)isJsonParameter afSessionManage:(AFHTTPSessionManager *)afSessionManage isUseClientAuth:(BOOL)isUseClientAuth{
NSURL *url = [NSURL URLWithString:self.baseUrl];
AFHTTPSessionManager *sessionManager;
if(afSessionManage){
sessionManager = afSessionManage;
}else{
sessionManager = [[AFHTTPSessionManager alloc] initWithBaseURL:url];
}
//设置超时时间为10秒
// [sessionManager.requestSerializer willChangeValueForKey:@"timeoutInterval"];
sessionManager.requestSerializer.timeoutInterval = 10.f;
// [sessionManager.requestSerializer didChangeValueForKey:@"timeoutInterval"];
if (isJsonParameter) {
sessionManager.requestSerializer = [AFJSONRequestSerializer serializerWithWritingOptions:0];
}
// 使用系统cookie管理 2017年5月18日 因为改动cart需要使用cookie
// [[KWMUserDao shareDao] checkSession];
sessionManager.requestSerializer.HTTPShouldHandleCookies = YES;
sessionManager.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
//测试
if([TestModel isEqualToString:@"YES"]){
[sessionManager.requestSerializer setAuthorizationHeaderFieldWithUsername:userNameTest password:passwordTest];
}
//正式
else{
[sessionManager.requestSerializer setAuthorizationHeaderFieldWithUsername:userName password:password];
}
id tokenDict = [[NSUserDefaults standardUserDefaults] objectForKey:@"token"];
if(tokenDict){
BUYCustomerToken *token = [BUYCustomerToken customerTokenWithJSONDictionary:tokenDict];
if(token){
NSString *accessToken = token.accessToken ?: @"";
[sessionManager.requestSerializer setValue:accessToken forHTTPHeaderField:@"X-Shopify-Customer-Access-Token"];
}
}
if(isUseClientAuth){
[sessionManager.requestSerializer setValue:Order_API_Auth forHTTPHeaderField:@"Authorization"];
}
return sessionManager;
}
-(BOOL)isUserClientAuth:(Class)result{
return result == [KWMOrderListResult class];
}
- (NSURLSessionDataTask *)startSessionTask:(KWMHTTPMethod)httpMethod
apiPath:(NSString *)apiPath
parameters:(NSDictionary *)parameters
filePaths:(NSDictionary *)filePaths
timeout:(NSTimeInterval) timeout
images:(NSArray *)images
jsonParameter:(BOOL)isJsonParameter
result:(Class)result
model:(Class)model
success:(void (^)(NSURLSessionDataTask *task, id result))success
failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure
cacheCallback:(void (^)( id result))cacheCallback
isNeedCache:(Boolean)isNeedCache{
NSAssert((result == [KWMRequestResult class] || result == [KWMRequestListResult class]
|| result == [KWMCheckoutPayResult class]
|| result == [KWMCemaroseResult class] || [result isSubclassOfClass:[KWMCemaroseResult class]]),
@"result class must either be KWMRequestResult or KWMRequestListResult!");
// NSAssert((!model || (model && [model isSubclassOfClass:[JSONModel class]])),
// @"model class must be sub class of JSONModel");
BOOL isUseClientAuth = [self isUserClientAuth:result];
//使用sessionManager请求时用到的callback
void(^successCallback)(NSURLSessionDataTask *, id ) = [self buildSuccessCallbackWithResultClass:result
modelClass:model
success:success
failure:failure];
//使用PPNetworkHelper请求时用到的callback
void(^newSuccessCallback)(id responseObject) = [self buildNewSuccessCallbackWithResultCalss:result
modelClass:model
success:success
failure:failure];
//使用PPNetworkHelper请求时用到的缓存callback
void(^newCacheCallback)(id responseObject) = [self buildNewCacheCallbackWithResultCalss:result
modelClass:model
callback:cacheCallback
failure:failure];
//使用PPNetworkHelper请求失败时用到的callback
void(^newFailedCallback)(NSError *error) = [self buildNewFaliedCallback:failure];
AFHTTPSessionManager *sessionManager = [self buildSessionManager:isJsonParameter afSessionManage:nil isUseClientAuth:isUseClientAuth];
if (timeout) {
[sessionManager.requestSerializer setTimeoutInterval:timeout];
}
//app version code
// static NSString *KEY_VERSION_CODE = @"version_code";
// NSString *build = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
NSMutableDictionary *mutableParameters = [[NSMutableDictionary alloc] initWithDictionary:parameters];
// mutableParameters[KEY_VERSION_CODE] = build;
//os_version统一添加
// static NSString *KEY_OS_VERSION = @"os_version";
// mutableParameters[KEY_OS_VERSION] = [NSString stringWithFormat:@"ios%@", [UIDevice currentDevice].systemVersion];
// NSLog(@"os_version:%@=====", mutableParameters[KEY_OS_VERSION]);
//初始化PPNetworkHelper里的sessionManager
[PPNetworkHelper setAFHTTPSessionManagerProperty:^(AFHTTPSessionManager *sessionManager) {
if (timeout) {
[sessionManager.requestSerializer setTimeoutInterval:timeout];
}
[self buildSessionManager:isJsonParameter afSessionManage:sessionManager isUseClientAuth:isUseClientAuth];
}];
if (filePaths != nil && filePaths.count > 0) {
return [sessionManager POST:apiPath parameters:mutableParameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSEnumerator *e = [filePaths keyEnumerator];
for (NSString *key in e) {
NSString *filePath = filePaths[key];
if (![KWMStringUtil isEmpty:filePath]) {
if ([fileManager fileExistsAtPath:filePath]) {
[formData appendPartWithFileURL:[NSURL fileURLWithPath:filePath]
name:key
error:nil];
}else if([filePath sam_containsString:@"http"]){//网络图片
NSURL *url = [[NSURL alloc] initWithString:filePath];
[formData appendPartWithFileURL:url
name:key
error:nil];
}
}
}
} success:successCallback failure:failure];
}
if (images != nil && images.count > 0) {
return [sessionManager POST:apiPath parameters:mutableParameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
NSFileManager *fileManager = [NSFileManager defaultManager];
for (NSInteger i = 0; i < images.count; i++) {
NSString *filePath = images[i];
if (![KWMStringUtil isEmpty:filePath]
&& [fileManager fileExistsAtPath:filePath]) {
if (images.count == 1) {
[formData appendPartWithFileURL:[NSURL fileURLWithPath:filePath]
name:@"image"
error:nil];
}else{
[formData appendPartWithFileURL:[NSURL fileURLWithPath:filePath]
name:[NSString stringWithFormat:@"image%@", @(i+1)]
error:nil];
}
}
}
} success:successCallback failure:failure];
}
if(parameters.allKeys==nil || parameters.allKeys.count==0){
parameters = nil;
}
switch (httpMethod) {
case KWMHTTPMethodGET:{
if(isNeedCache && newCacheCallback){
id task = [PPNetworkHelper GET:apiPath parameters:parameters responseCache:newCacheCallback success:newSuccessCallback failure:newFailedCallback];
return [task isKindOfClass:[NSURLSessionDataTask class]] ? task : nil;
}else{
return [sessionManager GET:apiPath parameters:parameters success:successCallback failure:failure];
}
}
case KWMHTTPMethodPOST:{
if(isNeedCache && newCacheCallback){
id task = [PPNetworkHelper POST:apiPath parameters:parameters responseCache:newCacheCallback success:newSuccessCallback failure:newFailedCallback];
return [task isKindOfClass:[NSURLSessionDataTask class]] ? task : nil;
}else{
return [sessionManager POST:apiPath parameters:parameters success:successCallback failure:failure];
}
}
case KWMHTTPMethodPUT:{
return [sessionManager PUT:apiPath parameters:parameters success:successCallback failure:failure];
}
case KWMHTTPMethodDELETE:{
return [sessionManager DELETE:apiPath parameters:parameters success:successCallback failure:failure];
}
default:{
NSString *reason = [[NSString alloc] initWithFormat:@"Don't use this way to build your NSURLSessionDataTask!"];
NSAssert(NO, reason);
return nil;
}
}
}
- (NSURLSessionDataTask *)startSessionTask:(KWMHTTPMethod)httpMethod
apiPath:(NSString *)apiPath
parameters:(NSDictionary *)parameters
filePaths:(NSDictionary *)filePaths
jsonParameter:(BOOL)isJsonParameter
result:(Class)result
model:(Class)model
success:(void (^)(NSURLSessionDataTask *task, id result))success
failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure
cacheCallback:(void (^)( id result))cacheCallback
isNeedCache:(Boolean)isNeedCache{
return [self startSessionTask:httpMethod
apiPath:apiPath
parameters:parameters
filePaths:filePaths
timeout:0
images:nil
jsonParameter:isJsonParameter
result:result
model:model
success:success
failure:failure
cacheCallback:cacheCallback
isNeedCache:isNeedCache];
}
- (NSURLSessionDataTask *)makeSessionTask:(KWMHTTPMethod)httpMethod
apiPath:(NSString *)apiPath
parameters:(NSDictionary *)parameters
result:(Class)result
model:(Class)model
success:(void (^)(NSURLSessionDataTask *task, id result))success
failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure {
void(^successCallback)(NSURLSessionDataTask *, id ) = [self buildSuccessCallbackWithResultClass:result
modelClass:model
success:success
failure:failure];
AFHTTPSessionManager *sessionManager = [self buildSessionManager:NO afSessionManage:nil isUseClientAuth:NO];
NSString *method = httpMethod == KWMHTTPMethodPOST ? @"POST" : @"GET";
NSURLRequest *request = [sessionManager.requestSerializer requestWithMethod:method URLString:apiPath parameters:parameters error:nil];
__block id task = [sessionManager dataTaskWithRequest:request completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
if (error) {
failure(task,error);
}else{
successCallback(task,responseObject);
}
}];
return task;
}
- (NSURLSessionDataTask *)startSessionTask:(KWMHTTPMethod)httpMethod
apiPath:(NSString *)apiPath
parameters:(NSDictionary *)parameters
result:(Class)result
model:(Class)model
success:(void (^)(NSURLSessionDataTask *task, id result))success
failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure
cacheCallback:(void (^)( id result))cacheCallback
isNeedCache:(Boolean)isNeedCache{
return [self startSessionTask:httpMethod
apiPath:apiPath
parameters:parameters
filePaths:nil
jsonParameter:NO
result:result
model:model
success:success failure:failure
cacheCallback:cacheCallback
isNeedCache:isNeedCache];
}
//比上面的方法少isNeedCache和cacheCallback
- (NSURLSessionDataTask *)startSessionTask:(KWMHTTPMethod)httpMethod
apiPath:(NSString *)apiPath
parameters:(NSDictionary *)parameters
result:(Class)result
model:(Class)model
success:(void (^)(NSURLSessionDataTask *task, id result))success
failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure{
return [self startSessionTask:httpMethod
apiPath:apiPath
parameters:parameters
filePaths:nil
jsonParameter:NO
result:result
model:model
success:success failure:failure cacheCallback:nil isNeedCache:NO];
}
- (NSString *)toIds:(NSArray<__kindof NSString *> *)idsArray {
NSMutableString *tmp = [NSMutableString new];
for (NSString *s in idsArray) {
[tmp appendFormat:@"%@,", s];
}
if (tmp.length > 0) {
return [tmp substringToIndex:tmp.length - 1];
}else{
return @"";
}
}
- (NSURLSessionDataTask *)adminUserSuccess:(void (^)(NSURLSessionDataTask *, KWMDictionaryResult *))success failure:(void (^)(NSURLSessionDataTask *, NSError *))failure {
NSNumber *customerId = [[[KWMUserDao shareDao] customer] identifier];
if (!customerId) {
return nil;
}
NSString *apiPath = [[NSString alloc] initWithFormat:@"https://%@/admin/customers/%@.json",Shopify_SHOP_DOMAIN,customerId];
return [self startSessionTask:KWMHTTPMethodGET
apiPath:apiPath
parameters:nil
result:[KWMDictionaryResult class]
model:nil
success:success
failure:failure];
}
- (void)loginWebSuccess:(void (^)())success failure:(void (^)(NSError *))failure {
KWMUserModel *user= [KWMUserModel shareUser];
NSString *account = user.email;
NSString *password = user.password;
if (!password) {
password = [SAMKeychain passwordForService:@"cemarose.account" account:account];
}
// 没登陆过不做处理
if (!password) {
return;
}
NSLog(@"loginTask username:%@,password:%@",account,password);
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObject:@"text/html"]];
[manager setTaskWillPerformHTTPRedirectionBlock:^NSURLRequest * _Nonnull(NSURLSession * _Nonnull session, NSURLSessionTask * _Nonnull task, NSURLResponse * _Nonnull response, NSURLRequest * _Nonnull request) {
NSString *url = request.URL.absoluteString;
if ([url isEqualToString:[NSString stringWithFormat:@"https://%@/account",Shopify_SHOP_DOMAIN]]) {
if(success) success();
}else{
NSError *error = [[NSError alloc] initWithDomain:url code:401 userInfo:@{NSLocalizedDescriptionKey:@"email & password is invalid !"}];
if(failure) failure(error);
}
return nil;
}];
NSString *loginUrl = [NSString stringWithFormat:@"https://%@/account/login",Shopify_SHOP_DOMAIN];
id param = @{
@"from_type":@"customer_login",
@"utf8":@"✓",
@"customer[email]":account,
@"customer[password]":password,
};
[manager POST:loginUrl parameters:param success:^(NSURLSessionDataTask * _Nonnull task, id _Nonnull responseObject) {
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if(failure) failure(error);
}];
}
/**
* 手机登录API
* @param mobile 手机号
* @param code 收到的短信验证码
*/
- (NSURLSessionDataTask *) phoneLogin:(NSDictionary *)parameters
success:(void(^)(NSURLSessionDataTask *task,KWMCustomerResult *result))success
failure:(void(^)(NSURLSessionDataTask *task,NSError *error))failure{
NSString *apiPath = [NSString stringWithFormat:@"/shopifyapps/china_social_login/%@/api-check-code",Cemarose_KEY];
return [self startSessionTask:KWMHTTPMethodPOST
apiPath:apiPath
parameters:parameters
result:[KWMCustomerResult class]
model:[KWMCustomer class]
success:success
failure:failure];
}
#pragma mark - 购物车接口
- (NSURLSessionDataTask *)changeProductWithVariantId:(NSNumber *)variantId quantity:(NSInteger) quantity success:(void (^)(NSURLSessionDataTask *, KWMCartResult *))success failure:(void (^)(NSURLSessionDataTask *, NSError *))failure {
NSString *apiPath = [NSString stringWithFormat:@"https://%@/cart/change/%@.js",Shopify_SHOP_DOMAIN,variantId];
id params = @{ @"quantity": @(quantity) };
return [self makeSessionTask:KWMHTTPMethodGET apiPath:apiPath parameters:params result:[KWMCartResult class] model:nil success:success failure:failure];
}
- (NSURLSessionDataTask *)updateProductWithVariantIds:(NSArray<NSNumber *> *)variantIds quantities:(NSArray<NSNumber *> *)quantities success:(void (^)(NSURLSessionDataTask *, KWMCartResult *))success failure:(void (^)(NSURLSessionDataTask *, NSError *))failure {
NSString *apiPath = [NSString stringWithFormat:@"https://%@/cart/update.js",Shopify_SHOP_DOMAIN];
NSMutableDictionary *params = [NSMutableDictionary new];
for (int i=0; i<quantities.count; i++) {
[params setObject:quantities[i] forKey:[NSString stringWithFormat:@"updates[%@]",variantIds[i]]];
}
return [self makeSessionTask:KWMHTTPMethodPOST apiPath:apiPath parameters:params result:[KWMCartResult class] model:nil success:success failure:failure];
}
- (NSURLSessionDataTask *)addProductWithVariantId:(NSNumber *)variantId quantity:(NSInteger)quantity success:(void (^)(NSURLSessionDataTask *, KWMRequestResult *))success failure:(void (^)(NSURLSessionDataTask *, NSError *))failure {
NSString *apiPath = [NSString stringWithFormat:@"https://%@/cart/add.js",Shopify_SHOP_DOMAIN];
id params = @{
@"id":variantId,
@"quantity":@(quantity),
};
return [self makeSessionTask:KWMHTTPMethodPOST apiPath:apiPath parameters:params result:[KWMDictionaryResult class] model:nil success:success failure:failure];
}
-(NSURLSessionDataTask *)getCartSuccess:(void (^)(NSURLSessionDataTask *, KWMCartResult *))success failure:(void (^)(NSURLSessionDataTask *, NSError *))failure {
NSString *apiPath = [NSString stringWithFormat:@"https://%@/cart.js",Shopify_SHOP_DOMAIN];
return [self makeSessionTask:KWMHTTPMethodGET apiPath:apiPath parameters:nil result:[KWMCartResult class] model:nil success:success failure:failure];
}
- (NSURLSessionDataTask *)tmpCartWithVariantId:(NSNumber *)variantId quantity:(NSInteger)quantity success:(void (^)(NSURLSessionDataTask *, NSString *))success failure:(void (^)(NSURLSessionDataTask *, NSError *))failure {
NSString *apiPath = [NSString stringWithFormat:@"https://%@/cart/add.js",Shopify_SHOP_DOMAIN];
id params = @{
@"id":variantId,
@"quantity":@(quantity),
};
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager.requestSerializer setHTTPShouldHandleCookies:NO];
NSString *cookie = [[KWMHttpUtil buildRequestCookieString] stringByReplacingOccurrencesOfRegex:@"cart=.*?; ?" withString:@""];
[manager.requestSerializer setValue:cookie forHTTPHeaderField:@"Cookie"];
return [manager POST:apiPath parameters:params success:^(NSURLSessionDataTask * _Nonnull task, id _Nonnull responseObject) {
NSHTTPURLResponse *response = (NSHTTPURLResponse *)task.response;
NSString *cartCookie = [response.allHeaderFields[@"Set-Cookie"] stringByMatching:@"cart=(.*?);" capture:1];
success(task,cartCookie);
} failure:failure];
}
/**
* 手机登录-获取短信API
* @param mobile 手机号
*/
- (NSURLSessionDataTask *) phoneSms:(NSDictionary *)parameters
success:(void(^)(NSURLSessionDataTask *task,KWMCemaroseResult *result))success
failure:(void(^)(NSURLSessionDataTask *task,NSError *error))failure{
NSString *apiPath = [NSString stringWithFormat:@"/shopifyapps/china_social_login/%@/api-verify-code",Cemarose_KEY];
return [self startSessionTask:KWMHTTPMethodGET
apiPath:apiPath
parameters:parameters
result:[KWMCemaroseResult class]
model:nil
success:success
failure:failure];
}
/**
* 获取所有品牌
*/
- (NSURLSessionDataTask *) getAllBrand:(NSDictionary *)parameters
cacheCallback:(void(^)(KWMBrandsResult *result))cacheCallback
success:(void(^)(NSURLSessionDataTask *task,KWMBrandsResult *result))success
failure:(void(^)(NSURLSessionDataTask *task,NSError *error))failure{
NSString *apiPath = [NSString stringWithFormat:@"%@/shopifyapps/product_search/cemarose/brands",self.baseUrl];
return [self startSessionTask:KWMHTTPMethodGET
apiPath:apiPath
parameters:parameters
result:[KWMBrandsResult class]
model:nil
success:success
failure:failure
cacheCallback:cacheCallback
isNeedCache:YES];
}
/**
* 搜索API
* @param keyword 搜索内容
*/
- (NSURLSessionDataTask *) searchProduct:(NSDictionary *)parameters
success:(void(^)(NSURLSessionDataTask *task,KWMSearchResult *result))success
failure:(void(^)(NSURLSessionDataTask *task,NSError *error))failure{
NSString *apiPath = [NSString stringWithFormat:@"%@/shopifyapps/product_search/cemarose/search",self.baseUrl];
return [self startSessionTask:KWMHTTPMethodGET
apiPath:apiPath
parameters:parameters
result:[KWMSearchResult class]
model:nil
success:success
failure:failure];
}
/**
* 博客API
*/
- (NSURLSessionDataTask *) getAllBlog:(NSDictionary *)parameters
cacheCallback:(void(^)(KWMBlogResult *result))cacheCallback
success:(void(^)(NSURLSessionDataTask *task,KWMBlogResult *result))success
failure:(void(^)(NSURLSessionDataTask *task,NSError *error))failure{
NSString *apiPath = [[NSString alloc] initWithFormat:@"https://%@/admin/articles.json",Shopify_SHOP_DOMAIN];
return [self startSessionTask:KWMHTTPMethodGET
apiPath:apiPath
parameters:parameters
result:[KWMBlogResult class]
model:nil
success:success
failure:failure
cacheCallback:cacheCallback
isNeedCache:YES];
}
- (NSURLSessionDataTask *) getAllPage:(NSDictionary *)parameters
success:(void(^)(NSURLSessionDataTask *task,KWMPageResult *result))success
failure:(void(^)(NSURLSessionDataTask *task,NSError *error))failure{
NSString *apiPath = [[NSString alloc] initWithFormat:@"https://%@/admin/pages.json",Shopify_SHOP_DOMAIN];
return [self startSessionTask:KWMHTTPMethodGET
apiPath:apiPath
parameters:parameters
result:[KWMPageResult class]
model:nil
success:success
failure:failure];
}
/**
* 商品——新品API
*/
- (NSURLSessionDataTask *) getNewProducts:(NSDictionary *)parameters
success:(void(^)(NSURLSessionDataTask *task,KWMNewProducts *result))success
failure:(void(^)(NSURLSessionDataTask *task,NSError *error))failure{
NSString *apiPath = @"https://ogbgohpla.qnssl.com/App.Home.New.json";
NSTimeInterval interval = [[NSDate date] timeIntervalSince1970]/100;
long long time = interval;
apiPath = [[NSString alloc] initWithFormat:@"%@?_=%llu", apiPath, time];
return [self startSessionTask:KWMHTTPMethodGET
apiPath:apiPath
parameters:parameters
result:[KWMNewProducts class]
model:[KWMNewGoodsModel class]
success:success
failure:failure];
}
- (NSURLSessionDataTask *)checkStockInChina:(NSDictionary *)parameters success:(void (^)(NSURLSessionDataTask *, KWMDictionaryResult *))success failure:(void (^)(NSURLSessionDataTask *, NSError *))failure {
NSString *apiPath = [NSString stringWithFormat:@"https://%@/app/variant-inventory", WishList_API_DOMAIN];
return [self startSessionTask:KWMHTTPMethodGET apiPath:apiPath parameters:parameters result:[KWMDictionaryResult class] model:nil success:success failure:failure];
}
/**
* 商品——打折API
*/
- (NSURLSessionDataTask *) getDiscountProducts:(NSDictionary *)parameters
success:(void(^)(NSURLSessionDataTask *task,KWMNewProducts *result))success
failure:(void(^)(NSURLSessionDataTask *task,NSError *error))failure{
NSString *apiPath = @"https://ogbgohpla.qnssl.com/App.Home.SALE.json";
NSTimeInterval interval = [[NSDate date] timeIntervalSince1970]/100;
long long time = interval;
apiPath = [[NSString alloc] initWithFormat:@"%@?_=%llu", apiPath, time];
return [self startSessionTask:KWMHTTPMethodGET
apiPath:apiPath
parameters:parameters
result:[KWMNewProducts class]
model:[KWMNewGoodsModel class]
success:success
failure:failure];
}
/**
* 获取商品分类API
*/
- (NSURLSessionDataTask *) getProductTypes:(NSDictionary *)parameters type:(NSInteger)type
success:(void(^)(NSURLSessionDataTask *task,KWMProductTypeResult *result))success
failure:(void(^)(NSURLSessionDataTask *task,NSError *error))failure{
NSString *apiPath = @"https://ogbgohpla.qnssl.com/Baby.Navigation.json";
if(type == 0){
apiPath = @"https://ogbgohpla.qnssl.com/Baby.Navigation.json";
}else if(type == 1){
apiPath = @"https://ogbgohpla.qnssl.com/Girls.Navigation.json";
}else if(type == 2){
apiPath = @"https://ogbgohpla.qnssl.com/Boys.Navigation.json";
}else if(type == 3){
apiPath = @"https://ogbgohpla.qnssl.com/Shoes.Navigation.json";
}
NSTimeInterval interval = [[NSDate date] timeIntervalSince1970]/100;
long long time = interval;
apiPath = [[NSString alloc] initWithFormat:@"%@?_=%llu", apiPath, time];
return [self startSessionTask:KWMHTTPMethodGET
apiPath:apiPath
parameters:parameters
result:[KWMProductTypeResult class]
model:[KWMProductType class]
success:success
failure:failure];
}
/**
* 品牌类型--API
*/
- (NSURLSessionDataTask *) getBrandsType:(NSDictionary *)parameters
success:(void(^)(NSURLSessionDataTask *task,KWMBrandsTypeResult *result))success
failure:(void(^)(NSURLSessionDataTask *task,NSError *error))failure{
NSString *apiPath = @"https://ogbgohpla.qnssl.com/Designers.json";
NSTimeInterval interval = [[NSDate date] timeIntervalSince1970]/100;
long long time = interval;
apiPath = [[NSString alloc] initWithFormat:@"%@?_=%llu", apiPath, time];
return [self startSessionTask:KWMHTTPMethodGET
apiPath:apiPath
parameters:parameters
result:[KWMBrandsTypeResult class]
model:[KWMBrandsTypeModel class]
success:success
failure:failure];
}
/**
* 汇率--API
*/
- (NSURLSessionDataTask *) getExchangeRate:(NSDictionary *)parameters
success:(void(^)(NSURLSessionDataTask *task,KWMExchangeRateResult *result))success
failure:(void(^)(NSURLSessionDataTask *task,NSError *error))failure{
NSString *apiPath = [NSString stringWithFormat:@"https://%@/app/exchange/rate",Pay_API_DOMAIN];
// NSString *sign = [KWMStringUtil MD5:[KWMStringUtil BASE64:@"currency=EUR&key=bd3f58f5cd3d7a217ac8c8e655ab52f8"]];
// NSLog(@"rate sign: %@",sign);
// NSDictionary *params = @{@"currency":@"EUR",@"sign":sign};
NSDictionary *params = @{@"currency":@"EUR",@"sign":@"665d9716511eb11f68a1a7a862ed2ddd"};
return [self startSessionTask:KWMHTTPMethodPOST
apiPath:apiPath
parameters:params
result:[KWMExchangeRateResult class]
model:nil
success:success
failure:failure];
}
- (NSURLSessionDataTask *)getHomeDataWithSuccess:(void (^)(NSURLSessionDataTask *, KWMHomeDataResult *))success failure:(void (^)(NSURLSessionDataTask *, NSError *))failure {
NSInteger st = [[NSDate date] timeIntervalSince1970];
// st = st - st % 86400 - 28800;
NSString *apiPath = [NSString stringWithFormat:@"https://ogbgohpla.qnssl.com/App.Home.json?st=%zi",st];
return [self startSessionTask:KWMHTTPMethodGET apiPath:apiPath parameters:nil result:[KWMHomeDataResult class] model:nil success:success failure:failure];
}
/**
* 获取产品扩展字段--API
*/
- (NSURLSessionDataTask *) getProductMetafield:(NSDictionary *)parameters productId:(NSNumber *)productId
success:(void(^)(NSURLSessionDataTask *task,KWMMetafieldResult *result))success
failure:(void(^)(NSURLSessionDataTask *task,NSError *error))failure{
NSString *apiPath = [[NSString alloc] initWithFormat:@"https://%@/admin/products/%@/metafields.json",Shopify_SHOP_DOMAIN,productId.stringValue];
return [self startSessionTask:KWMHTTPMethodGET
apiPath:apiPath
parameters:parameters
result:[KWMMetafieldResult class]
model:[KWMMetafield class]
success:success
failure:failure];
}
/**
* 获取获取某个订单api
*/
- (NSURLSessionDataTask *) getAdminOrder:(NSDictionary *)parameters orderId:(NSNumber *)orderId
success:(void(^)(NSURLSessionDataTask *task,KWMOrdersResult *result))success
failure:(void(^)(NSURLSessionDataTask *task,NSError *error))failure{
NSString *apiPath = [[NSString alloc] initWithFormat:@"https://%@/admin/orders.json?ids=%@",Shopify_SHOP_DOMAIN,orderId.stringValue];
return [self startSessionTask:KWMHTTPMethodGET
apiPath:apiPath
parameters:parameters
result:[KWMOrdersResult class]
model:[KWMOrder class]
success:success
failure:failure];
}
/**
* 获取商品api,因為admin商品有剩余库存数量字段,所以商品詳細還需要請求此api。
*/
- (NSURLSessionDataTask *) getAdminProduct:(NSDictionary *)parameters productId:(NSNumber *)productId
success:(void(^)(NSURLSessionDataTask *task,KWMProductResult *result))success
failure:(void(^)(NSURLSessionDataTask *task,NSError *error))failure{
NSString *apiPath = [[NSString alloc] initWithFormat:@"https://%@/admin/products/%@.json",Shopify_SHOP_DOMAIN,productId.stringValue];
return [self startSessionTask:KWMHTTPMethodGET
apiPath:apiPath
parameters:parameters
result:[KWMProductResult class]
model:[KWMProduct class]
success:success
failure:failure];
}
- (NSURLSessionDataTask *)getAdminProductsById:(NSArray *)ids success:(void (^)(NSURLSessionDataTask *, KWMProductResult *))success failure:(void (^)(NSURLSessionDataTask *, NSError *))failure {
NSString *apiPath = [[NSString alloc] initWithFormat:@"https://%@/admin/products.json",Shopify_SHOP_DOMAIN];
return [self startSessionTask:KWMHTTPMethodGET apiPath:apiPath parameters:@{@"ids":[ids componentsJoinedByString:@","]} result:[KWMProductResult class] model:[KWMProduct class] success:success failure:failure];
}
/**
* 取消订单api
* @param amount 退款金额 (default: false)(十进制:21.20)If set, Shopify will attempt to void/refund the payment depending on the status.
* @param restock 是否将此订单商品重新包装会商店 (default: false)
* @param reason 订单取消原因 (用户customer, 库存inventory, 假货fraud, 其他other)(default: other)
* @param email 发送邮箱通知用户 (default: false)
* @param refund 更复杂的退款方式 As specified in the Refund documentation. Required for some more complex refund situations.
*/
- (NSURLSessionDataTask *) cancelOrder:(NSDictionary *)parameters orderId:(NSNumber *)orderId
success:(void(^)(NSURLSessionDataTask *task,KWMProductResult *result))success
failure:(void(^)(NSURLSessionDataTask *task,NSError *error))failure{
NSString *apiPath = [[NSString alloc] initWithFormat:@"https://%@/admin/orders/%@/cancel.json",Shopify_SHOP_DOMAIN,orderId.stringValue];
return [self startSessionTask:KWMHTTPMethodGET
apiPath:apiPath
parameters:parameters
result:[KWMCemaroseResult class]
model:nil
success:success
failure:failure];
}
/**
* 微信统一下单API
* @param checkout_token shopify订单token
* @param total_price 订单总金额
* @param ip_address 手机IP
* @param subject 订单描述
* @param sign 签名验证
* 签名规则
* str_sign = "ip_address=127.0.0.1&checkout_token=5173547165345&subject=test&total_price=23423&key=bd3f58f5cd3d7a217ac8c8e655ab52f8";
* sign = md5(base64_encode($str_sign));
*/
- (NSURLSessionDataTask *) wechatPayUnifiedOrder:(NSDictionary *)parameters
success:(void(^)(NSURLSessionDataTask *task,KWMCheckoutPayResult *result))success
failure:(void(^)(NSURLSessionDataTask *task,NSError *error))failure{
NSString *apiPath = [NSString stringWithFormat:@"https://%@/wechatpay/app/unified_order",Pay_API_DOMAIN];
// return [self startSessionTask:KWMHTTPMethodPOST
// apiPath:apiPath
// parameters:parameters
// result:[KWMCheckoutPayResult class]
// model:[KWMWechatPayData class]
// success:success
// failure:failure];
return [self startSessionTask:KWMHTTPMethodPOST apiPath:apiPath parameters:parameters filePaths:nil timeout:30 images:nil jsonParameter:NO result:[KWMCheckoutPayResult class] model:[KWMWechatPayData class] success:success failure:failure cacheCallback:nil isNeedCache:NO];
}
/**
* 支付宝统一下单API
* @param checkout_token shopify订单token
* @param total_price 订单总金额
* @param subject 订单描述
* @param sign 签名验证
* 签名规则:
* str_sign = checkout_token=5173547165345&subject=test&total_price=23423&key=bd3f58f5cd3d7a217ac8c8e655ab52f8";
* sign = md5(base64_encode($str_sign));
*/
- (NSURLSessionDataTask *) aliPayUnifiedOrder:(NSDictionary *)parameters
success:(void(^)(NSURLSessionDataTask *task,KWMCheckoutPayResult *result))success
failure:(void(^)(NSURLSessionDataTask *task,NSError *error))failure{
NSString *apiPath = [NSString stringWithFormat:@"https://%@/alipay/app/unified_order",Pay_API_DOMAIN];
// return [self startSessionTask:KWMHTTPMethodPOST
// apiPath:apiPath
// parameters:parameters
// result:[KWMCheckoutPayResult class]
// model:nil
// success:success
// failure:failure];
return [self startSessionTask:KWMHTTPMethodPOST apiPath:apiPath parameters:parameters filePaths:nil timeout:30 images:nil jsonParameter:NO result:[KWMCheckoutPayResult class] model:nil success:success failure:failure cacheCallback:nil isNeedCache:NO];
}
- (NSURLSessionDataTask *) wechatPayOrderQuery:(NSDictionary *)parameters
success:(void(^)(NSURLSessionDataTask *task,KWMCheckoutPayResult *result))success
failure:(void(^)(NSURLSessionDataTask *task,NSError *error))failure{
NSString *apiPath = [NSString stringWithFormat:@"https://%@/wechatpay/order/query",Pay_API_DOMAIN];
return [self startSessionTask:KWMHTTPMethodPOST
apiPath:apiPath
parameters:parameters
result:[KWMCheckoutPayResult class]
model:nil
success:success
failure:failure];
}
- (NSURLSessionDataTask *) aliPayOrderQuery:(NSDictionary *)parameters
success:(void(^)(NSURLSessionDataTask *task,KWMCheckoutPayResult *result))success
failure:(void(^)(NSURLSessionDataTask *task,NSError *error))failure{
NSString *apiPath = [NSString stringWithFormat:@"https://%@/alipay/order/query",Pay_API_DOMAIN];
return [self startSessionTask:KWMHTTPMethodPOST
apiPath:apiPath
parameters:parameters
result:[KWMCheckoutPayResult class]
model:nil
success:success
failure:failure];
}
/**
* 通过checkout_token查询shopify订单API
* @param checkout_token shopify订单token
* @param out_trade_no 订单支付编号
* @param sign 签名验证
* 签名规则:
* str_sign = checkout_token=5173547165345&out_trade_no=201708011602269375&key=bd3f58f5cd3d7a217ac8c8e655ab52f8";
* sign = md5(base64_encode($str_sign));
*/
- (NSURLSessionDataTask *) appOrderQuery:(NSDictionary *)parameters
success:(void(^)(NSURLSessionDataTask *task,KWMCheckoutPayResult *result))success
failure:(void(^)(NSURLSessionDataTask *task,NSError *error))failure{
NSString *apiPath = [NSString stringWithFormat:@"https://%@/app/order/query",Pay_API_DOMAIN];
return [self startSessionTask:KWMHTTPMethodPOST
apiPath:apiPath
parameters:parameters
result:[KWMCheckoutPayResult class]
model:[KWMOrderPaid class]
success:success
failure:failure];
}
/**
* 用户添加 wish API
* @param customer_id 用户ID
* @param customer_email 用户邮箱
* @param customer_name 用户名
* @param product_title 产品标题
* @param prodcut_id 产品ID
* @param variant_id 变体ID
* @param variant_sku sku
* @param variant_title 变体标题
* @param product_handle 产品handle
* @param shop 店铺名(cemarose-test.myshopify.com)
*/
- (NSURLSessionDataTask *) saveWish:(NSDictionary *)parameters
success:(void(^)(NSURLSessionDataTask *task,KWMAdditionalListResult *result))success
failure:(void(^)(NSURLSessionDataTask *task,NSError *error))failure{
NSString *apiPath = [NSString stringWithFormat:@"https://%@/app/wishlist-save",WishList_API_DOMAIN];
return [self startSessionTask:KWMHTTPMethodPOST
apiPath:apiPath
parameters:parameters
result:[KWMAdditionalListResult class]
model:nil
success:success
failure:failure];
}
/**
* 获取用户 wishlist API
* @param customer_id 用户ID
* @param customer_email 用户邮箱
* @param variant_id 变体ID(用于获取单个愿望商品)
* @param shop 店铺名(cemarose-test.myshopify.com)
*/
- (NSURLSessionDataTask *) getWishList:(NSDictionary *)parameters
success:(void(^)(NSURLSessionDataTask *task,KWMAdditionalListResult *result))success
failure:(void(^)(NSURLSessionDataTask *task,NSError *error))failure{
NSString *apiPath = [NSString stringWithFormat:@"https://%@/app/wishlist",WishList_API_DOMAIN];
return [self startSessionTask:KWMHTTPMethodGET
apiPath:apiPath
parameters:parameters
result:[KWMAdditionalListResult class]
model:[KWMWish class]
success:success
failure:failure];
}
/**
* 移除指定 wish API
* @param customer_id 用户ID
* @param customer_email 用户邮箱
* @param variant_id 变体ID
* @param shop 店铺名(cemarose-test.myshopify.com)
*/
- (NSURLSessionDataTask *) removeWish:(NSDictionary *)parameters
success:(void(^)(NSURLSessionDataTask *task,KWMAdditionalListResult *result))success
failure:(void(^)(NSURLSessionDataTask *task,NSError *error))failure{
NSString *apiPath = [NSString stringWithFormat:@"https://%@/app/wishlist-prune",WishList_API_DOMAIN];
return [self startSessionTask:KWMHTTPMethodPOST
apiPath:apiPath
parameters:parameters
result:[KWMAdditionalListResult class]
model:[KWMWish class]
success:success
failure:failure];
}
/**
* 清空愿望单 API
* @param customer_id 用户ID
* @param customer_email 用户邮箱
* @param shop 店铺名(cemarose-test.myshopify.com)
*/
- (NSURLSessionDataTask *) clearWishList:(NSDictionary *)parameters
success:(void(^)(NSURLSessionDataTask *task,KWMAdditionalListResult *result))success
failure:(void(^)(NSURLSessionDataTask *task,NSError *error))failure{
NSString *apiPath = [NSString stringWithFormat:@"https://%@/app/wishlist-delete",WishList_API_DOMAIN];
return [self startSessionTask:KWMHTTPMethodPOST
apiPath:apiPath
parameters:parameters
result:[KWMAdditionalListResult class]
model:[KWMWish class]
success:success
failure:failure];
}
/**
* 获取所以订单 API
* @param customer_id 用户ID
* @param customer_email 用户邮箱
* @param shop 店铺名(cemarose-test.myshopify.com)
*/
- (NSURLSessionDataTask *) getOrderList:(NSDictionary *)parameters
success:(void(^)(NSURLSessionDataTask *task,KWMOrderListResult *result))success
failure:(void(^)(NSURLSessionDataTask *task,NSError *error))failure{
NSString *apiPath = [NSString stringWithFormat:@"https://%@/customer/orders/custom-orders",WishList_API_DOMAIN];
// apiPath = @"https://test.cemarose.com/customer/orders/custom-orders";
return [self startSessionTask:KWMHTTPMethodGET
apiPath:apiPath
parameters:parameters
result:[KWMOrderListResult class]
model:[BUYOrder class]
success:success
failure:failure];
}
@end