Commit fdfa3996 by lee

购物车修改完成

parent 6c9ff8a5
......@@ -103,7 +103,10 @@
*
* @return <#return value description#>
*/
- (NSURLSessionDataTask *) updateProductWithVariantId:(NSNumber *) variantId quantity:(NSInteger) quantity success:(void(^)(NSURLSessionDataTask *task,KWMCartResult *result)) success failure:(void(^)(NSURLSessionDataTask *task,NSError *error)) failure;
- (NSURLSessionDataTask *) changeProductWithVariantId:(NSNumber *) variantId quantity:(NSInteger) quantity success:(void(^)(NSURLSessionDataTask *task,KWMCartResult *result)) success failure:(void(^)(NSURLSessionDataTask *task,NSError *error)) failure;
-(NSURLSessionDataTask *) updateProductWithVariantIds:(NSArray<NSNumber *> *) variantIds quantities:(NSArray<NSNumber *> *) quantities success:(void(^)(NSURLSessionDataTask *task,KWMCartResult *result)) success failure:(void(^)(NSURLSessionDataTask *task,NSError *error)) failure;
/**
* 获取系统购物车
*
......
......@@ -419,12 +419,21 @@ static NSString *const passwordTest = @"9e84aae218c57cdf0762763c4cf5a651";
failure:failure];
}
- (NSURLSessionDataTask *)updateProductWithVariantId:(NSNumber *)variantId quantity:(NSInteger) quantity success:(void (^)(NSURLSessionDataTask *, KWMCartResult *))success failure:(void (^)(NSURLSessionDataTask *, NSError *))failure {
- (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 startSessionTask: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 startSessionTask: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 = @{
......
......@@ -24,7 +24,9 @@ typedef void(^ShoppingCartCallBack) (NSError *error,KWMCartResult *cart);
- (void)increaseProductWithVariantId:(NSNumber *) variantId quantity:(NSInteger) quantity callback:(ShoppingCartCallBack) callback;
- (void)updateProductWithVariantId:(NSNumber *) variantId quantity:(NSInteger) quantity callback:(ShoppingCartCallBack) callback;
- (void)changeProductWithVariantId:(NSNumber *) variantId quantity:(NSInteger) quantity callback:(ShoppingCartCallBack) callback;
- (void)updateProductWithVariantIds:(NSArray<NSNumber *> *) variantIds quantitties:(NSArray<NSNumber *> *) quantites callback:(ShoppingCartCallBack) callback;
- (void)deleteProductWithVariantId:(NSNumber *) variantId callback:(ShoppingCartCallBack) callback;
......
......@@ -63,40 +63,55 @@
}
- (void)increaseProductWithVariantId:(NSNumber *)variantId quantity:(NSInteger)quantity callback:(ShoppingCartCallBack)callback {
callback(nil,[self changeLocalCartWithVariantId:variantId quantity:quantity flag:1]);
[[KWMAPIManager sharedManager] addProductWithVariantId:variantId quantity:quantity success:^(NSURLSessionDataTask *task, KWMRequestResult *result) {
[[KWMShoppingCart sharedInstance] allItemsWithCallback:^(NSError *error, KWMCartResult *cart) {
// callback(error,cart);
callback(error,cart);
}];
} failure:^(NSURLSessionDataTask *task, NSError *error) {
// callback(error,nil);
}];
callback(nil,[self changeLocalCartWithVariantId:variantId quantity:quantity flag:1]);
}
- (void)updateProductWithVariantId:(NSNumber *)variantId quantity:(NSInteger)quantity callback:(ShoppingCartCallBack)callback {
- (void)changeProductWithVariantId:(NSNumber *)variantId quantity:(NSInteger)quantity callback:(ShoppingCartCallBack)callback {
[[KWMAPIManager sharedManager] changeProductWithVariantId:variantId quantity:quantity success:^(NSURLSessionDataTask *task, KWMCartResult *result) {
[[KWMShoppingCart sharedInstance] setResult:result];
callback(nil,result);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
// callback(error,nil);
}];
callback(nil,[self changeLocalCartWithVariantId:variantId quantity:quantity flag:quantity ? 2 : 0]);
[[KWMAPIManager sharedManager] updateProductWithVariantId:variantId quantity:quantity success:^(NSURLSessionDataTask *task, KWMCartResult *result) {
}
-(void)updateProductWithVariantIds:(NSArray<NSNumber *> *)variantIds quantitties:(NSArray<NSNumber *> *)quantites callback:(ShoppingCartCallBack)callback {
[[KWMAPIManager sharedManager] updateProductWithVariantIds:variantIds quantities:quantites success:^(NSURLSessionDataTask *task, KWMCartResult *result) {
[[KWMShoppingCart sharedInstance] setResult:result];
// callback(nil,result);
callback(nil,result);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
// callback(error,nil);
}];
for (int i=0; i<quantites.count; i++) {
[self changeLocalCartWithVariantId:variantIds[i] quantity:quantites[i].integerValue flag:quantites[i].integerValue ? 2 : 0];
}
callback(nil,self.result);
}
- (void)deleteProductWithVariantId:(NSNumber *)variantId callback:(ShoppingCartCallBack)callback {
[self updateProductWithVariantId:variantId quantity:0 callback:callback];
[self changeProductWithVariantId:variantId quantity:0 callback:callback];
}
- (KWMCartResult *) changeLocalCartWithVariantId:(NSNumber *) variantId quantity:(NSInteger) quantity flag:(NSInteger) flag {
NSMutableArray<KWMShopCartModel *> *items= self.items;
NSInteger count = self.result.item_count.integerValue;
BOOL processed = NO;
for (KWMShopCartModel *item in items) {
NSUInteger size = items.count;
for (int i=0;i < size; i++) {
KWMShopCartModel *item = items[i];
if (item.identifier.integerValue == variantId.integerValue) {
switch (flag) {
case 0:
count -= item.quantity;
[items removeObject:item];
[self.items removeObjectAtIndex:i];
break;
case 1:
count += quantity;
......@@ -115,7 +130,8 @@
model.identifier = variantId.stringValue;
model.quantity = quantity;
count += quantity;
[items addObject:model];
// [items addObject:model];
[items insertObject:model atIndex:0];
}
self.result.item_count = @(count);
return self.result;
......
......@@ -57,7 +57,7 @@
[super viewDidLoad];
[self initScrollView];
[self initRecommendProduct];
[self initShopCartCount];
// [self initShopCartCount];
//刷新product,如果是缓存的product,因为没有字典,所以会发生错误,需要重新请求一个有字典的product
if(self.product && self.product.identifier && !self.product.JSONDictionary){
[self getProductBy:nil product:self.product];
......@@ -71,6 +71,7 @@
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:YES animated:YES];
[self initShopCartCount];
}
- (void)viewWillDisappear:(BOOL)animated{
......@@ -213,10 +214,17 @@
#pragma mark - KWMSelectSizeVCDelegate
- (void)kwm_addShopWithVariantId:(NSNumber *)identifier quantity:(NSInteger)quantity buyNow:(BOOL)buyNow callback:(void (^)(BOOL))callback {
- (void)kwm_addShopWithVariantId:(NSNumber *)identifier quantity:(NSInteger)quantity variant:(BUYProductVariant *) variant buyNow:(BOOL)buyNow callback:(void (^)(BOOL))callback {
__weak KWMProductDetailVC *this = self;
if (!buyNow) {
[[KWMShoppingCart sharedInstance] increaseProductWithVariantId:identifier quantity:quantity callback:^(NSError *error, KWMCartResult *cart) {
if (cart.items.firstObject && !cart.items.firstObject.product_id) {
KWMShopCartModel *model = cart.items.firstObject;
model.imageStr = [KWMImageUtil getProductImageUrl:this.product.identifier ImageSize:NormalImage];
model.name = this.product.title;
model.size = variant.title;
model.price = variant.price.floatValue;
}
if (!error) {
[this showToast:@"添加成功"];
[this.btnShopCart setTitle:cart.item_count.stringValue forState:UIControlStateNormal];
......@@ -249,27 +257,27 @@
}
//KWMSelectSizeVCDelegate
-(void)kwm_addShopCartItem:(KWMShopCartModel *)shopCartModel{
[self initShopCartCount];
}
-(void)kwm_fastBuy:(BUYCart *)fastBuyCart{
if(fastBuyCart == nil){
return;
}
KWMUserModel *useModel = [KWMUserModel shareUser];
if (useModel.status) {
KWMBeforePayVC *beforePayVC = (KWMBeforePayVC *)[KWMBaseVC findControllerBy:[KWMBeforePayVC kwmTag] fromStoryboard:@"ShopCart"];
beforePayVC.fastPayCart = fastBuyCart;
[self.navigationController pushViewController:beforePayVC animated:YES];}
//用户未登录
else{
KWMLoginVC *loginVc = (KWMLoginVC *)[KWMBaseVC findControllerBy:[KWMLoginVC kwmTag] fromStoryboard:@"Login"];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:loginVc];
[self presentViewController:nav animated:YES completion:nil];
}
}
////KWMSelectSizeVCDelegate
//-(void)kwm_addShopCartItem:(KWMShopCartModel *)shopCartModel{
// [self initShopCartCount];
//}
//
//-(void)kwm_fastBuy:(BUYCart *)fastBuyCart{
// if(fastBuyCart == nil){
// return;
// }
// KWMUserModel *useModel = [KWMUserModel shareUser];
// if (useModel.status) {
// KWMBeforePayVC *beforePayVC = (KWMBeforePayVC *)[KWMBaseVC findControllerBy:[KWMBeforePayVC kwmTag] fromStoryboard:@"ShopCart"];
// beforePayVC.fastPayCart = fastBuyCart;
// [self.navigationController pushViewController:beforePayVC animated:YES];}
// //用户未登录
// else{
// KWMLoginVC *loginVc = (KWMLoginVC *)[KWMBaseVC findControllerBy:[KWMLoginVC kwmTag] fromStoryboard:@"Login"];
// UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:loginVc];
// [self presentViewController:nav animated:YES completion:nil];
// }
//}
#pragma mark - KWMSelectSizeVCDelegate End
......
......@@ -15,11 +15,11 @@
@protocol KWMSelectSizeVCDelegate <NSObject>
- (void)kwm_addShopCartItem:(KWMShopCartModel *)shopCartModel;
- (void)kwm_fastBuy:(BUYCart *)fastBuyCart;
//- (void)kwm_addShopCartItem:(KWMShopCartModel *)shopCartModel;
//
//- (void)kwm_fastBuy:(BUYCart *)fastBuyCart;
- (void)kwm_addShopWithVariantId:(NSNumber *) identifier quantity:(NSInteger) quantity buyNow:(BOOL) buyNow callback:(void(^)(BOOL)) callback;
- (void)kwm_addShopWithVariantId:(NSNumber *) identifier quantity:(NSInteger) quantity variant:(BUYProductVariant *) variant buyNow:(BOOL) buyNow callback:(void(^)(BOOL)) callback;
@end
@interface KWMSelectSizeVC : KWMBaseVC<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,KWMSizeDelegate>
......
......@@ -200,7 +200,7 @@
__weak id this = self;
[self showLoading];
if (self.delegate) {
[self.delegate kwm_addShopWithVariantId:_selectSize.identifier quantity:_count buyNow:buyNow callback:^(BOOL success) {
[self.delegate kwm_addShopWithVariantId:_selectSize.identifier quantity:_count variant:_selectSize buyNow:buyNow callback:^(BOOL success) {
[self hideLoading];
if(success) {
[this dismissViewControllerAnimated:YES completion:nil];
......
......@@ -156,7 +156,7 @@
#pragma mark -- KWMSelectSizeVCDelegate
- (void)kwm_addShopWithVariantId:(NSNumber *)identifier quantity:(NSInteger)quantity buyNow:(BOOL)buyNow callback:(void (^)(BOOL))callback {
- (void)kwm_addShopWithVariantId:(NSNumber *)identifier quantity:(NSInteger)quantity variant:(BUYProductVariant *) variant buyNow:(BOOL)buyNow callback:(void (^)(BOOL))callback {
__weak KWMShopCartVC *this = self;
void(^success)(NSError*,KWMCartResult*) = ^(NSError *error,KWMCartResult *cart){
if (!error) {
......@@ -168,27 +168,53 @@
}
};
if (_changeModel.identifier.integerValue == identifier.integerValue) {
[[KWMShoppingCart sharedInstance] updateProductWithVariantId:identifier quantity:quantity callback:success];
[[KWMShoppingCart sharedInstance] changeProductWithVariantId:identifier quantity:quantity callback:success];
}else{
NSNumber *oldId = @(_changeModel.identifier.integerValue);
KWMShopCartModel *model = _changeModel;
_changeModel = nil;
[[KWMShoppingCart sharedInstance] deleteProductWithVariantId:oldId callback:^(NSError *error, KWMCartResult *cart) {
[[KWMShoppingCart sharedInstance] increaseProductWithVariantId:identifier quantity:quantity callback:success];
NSArray<NSNumber *> *variantIds = @[oldId,identifier];
NSArray<NSNumber *> *quantities = @[@0,@(quantity)];
[[KWMShoppingCart sharedInstance] updateProductWithVariantIds:variantIds quantitties:quantities callback:^(NSError *error, KWMCartResult *cart) {
if (!cart.items.firstObject.product_id) {
KWMShopCartModel *tmp = cart.items.firstObject;
tmp.imageStr = model.imageStr;
tmp.size = variant.title;
tmp.brand = model.brand;
tmp.name = model.name;
tmp.price = variant.price.floatValue;
tmp.product_id = model.product_id;
}
success(error,cart);
}];
// [[KWMShoppingCart sharedInstance] deleteProductWithVariantId:oldId callback:^(NSError *error, KWMCartResult *cart) {
// [[KWMShoppingCart sharedInstance] increaseProductWithVariantId:identifier quantity:quantity callback:^(NSError *error, KWMCartResult *cart) {
// if (!cart.items.lastObject.product_id) {
// KWMShopCartModel *tmp = cart.items.lastObject;
// tmp.imageStr = model.imageStr;
// tmp.size = variant.title;
// tmp.brand = model.brand;
// tmp.name = model.name;
// tmp.price = variant.price.floatValue;
// tmp.product_id = model.product_id;
// }
// success(error,cart);
// }];
// }];
}
}
- (void)kwm_addShopCartItem:(KWMShopCartModel *)shopCartModel{
[_shopCartList removeAllObjects];
[_shopCartList addObjectsFromArray:[[KWMShopCartData alloc] getALLItems]];
[self.tbvCart reloadData];
[self setTotalPrice];
}
-(void)kwm_fastBuy:(BUYCart *)fastBuyCart{
}
//- (void)kwm_addShopCartItem:(KWMShopCartModel *)shopCartModel{
//
// [_shopCartList removeAllObjects];
// [_shopCartList addObjectsFromArray:[[KWMShopCartData alloc] getALLItems]];
// [self.tbvCart reloadData];
// [self setTotalPrice];
//}
//
//-(void)kwm_fastBuy:(BUYCart *)fastBuyCart{
//
//}
-(void)kwm_deleteProduct{
// BOOL isHas = NO;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment