Commit adcdd5a1 by lee

mrege

parents 7edd04d3 93111181
{
"images" : [
{
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "ic_add@2x.png",
"scale" : "2x"
},
{
"idiom" : "universal",
"filename" : "ic_add@3x.png",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
\ No newline at end of file
{
"images" : [
{
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "ic_like_unable@2x.png",
"scale" : "2x"
},
{
"idiom" : "universal",
"filename" : "ic_like_unable@3x.png",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
\ No newline at end of file
......@@ -30,8 +30,11 @@
#import "KWMExchangeRateResult.h"
#import "KWMCartResult.h"
#import "KWMCheckoutPayResult.h"
#import "KWMAdditionalResult.h"
#import "KWMAdditionalListResult.h"
#import "KWMWechatPayData.h"
#import "KWMOrderPaid.h"
#import "KWMWish.h"
......@@ -273,4 +276,36 @@
success:(void(^)(NSURLSessionDataTask *task,KWMCheckoutPayResult *result))success
failure:(void(^)(NSURLSessionDataTask *task,NSError *error))failure;
/**
* 用户添加 wish API
* @return NSURLSessionDataTask
*/
- (NSURLSessionDataTask *) saveWish:(NSDictionary *)parameters
success:(void(^)(NSURLSessionDataTask *task,KWMAdditionalListResult *result))success
failure:(void(^)(NSURLSessionDataTask *task,NSError *error))failure;
/**
* 获取用户 wishlist API
* @return NSURLSessionDataTask
*/
- (NSURLSessionDataTask *) getWishList:(NSDictionary *)parameters
success:(void(^)(NSURLSessionDataTask *task,KWMAdditionalListResult *result))success
failure:(void(^)(NSURLSessionDataTask *task,NSError *error))failure;
/**
* 移除指定 wish API
* @return NSURLSessionDataTask
*/
- (NSURLSessionDataTask *) removeWish:(NSDictionary *)parameters
success:(void(^)(NSURLSessionDataTask *task,KWMAdditionalListResult *result))success
failure:(void(^)(NSURLSessionDataTask *task,NSError *error))failure;
/**
* 清空愿望单 API
* @return NSURLSessionDataTask
*/
- (NSURLSessionDataTask *) clearWishList:(NSDictionary *)parameters
success:(void(^)(NSURLSessionDataTask *task,KWMAdditionalListResult *result))success
failure:(void(^)(NSURLSessionDataTask *task,NSError *error))failure;
@end
//
// KWMAdditionalListResult.h
// iCemarose
//
// Created by HouWeiBin on 2017/7/6.
// Copyright © 2017年 kollway. All rights reserved.
//
#import <JSONModel/JSONModel.h>
@interface KWMAdditionalListResult : KWMCemaroseResult
//结果状态。success:正常;fail:错误
@property (nonatomic) NSString<Optional> *status;
//结果详情
@property (nonatomic) NSString<Optional> *message;
@property (nonatomic) id<Optional> data;
@end
//
// KWMAdditionalListResult.m
// iCemarose
//
// Created by HouWeiBin on 2017/7/6.
// Copyright © 2017年 kollway. All rights reserved.
//
#import "KWMAdditionalListResult.h"
@implementation KWMAdditionalListResult
- (instancetype)initWithDictionary:(NSDictionary *)dict
modelClass:(Class)modelClass
error:(NSError **)err {
self = [super initWithDictionary:dict error:err];
if (self) {
NSArray *dataArray = dict[@"data"];
if(dataArray){
self.data = [self buildListData:modelClass jsonDictionary:dataArray];
}
}
return self;
}
- (NSArray *)buildListData:(Class)modelClass jsonDictionary:(NSArray *)dataArray {
if(modelClass == nil){
NSString *reason = [[NSString alloc] initWithFormat:@"class with name:%@ doesn't exists!!", NSStringFromClass(modelClass)];
@throw [[NSException alloc] initWithName:NSInvalidArgumentException reason:reason userInfo:nil];
}
if(! [dataArray isKindOfClass:[NSArray class]]){
NSString *reason = [[NSString alloc] initWithFormat:@"dataArray object is not a NSArray instance"];
@throw [[NSException alloc] initWithName:NSInvalidArgumentException reason:reason userInfo:nil];
}
NSMutableArray *mutableArray = [[NSMutableArray alloc] init];
for(NSDictionary *itemJson in dataArray){
NSError *error;
id result = [[modelClass alloc] initWithDictionary:itemJson error:&error];
if(error){
NSLog(@"buildListData error: %@", [error localizedDescription]);
}else{
[mutableArray addObject:result];
}
}
return [[NSArray alloc] initWithArray:mutableArray];
}
@end
//
// KWMAdditionalResult.h
// iCemarose
//
// Created by HouWeiBin on 2017/7/6.
// Copyright © 2017年 kollway. All rights reserved.
//
#import <JSONModel/JSONModel.h>
@interface KWMAdditionalResult : KWMCemaroseResult
//结果状态。success:正常;fail:错误
@property (nonatomic) NSString<Optional> *status;
//结果详情
@property (nonatomic) NSString<Optional> *message;
@property (nonatomic) id<Optional> data;
@end
//
// KWMAdditionalResult.m
// iCemarose
//
// Created by HouWeiBin on 2017/7/6.
// Copyright © 2017年 kollway. All rights reserved.
//
#import "KWMAdditionalResult.h"
@implementation KWMAdditionalResult
@end
//
// KWMWishProduct.h
// iCemarose
//
// Created by HouWeiBin on 2017/7/6.
// Copyright © 2017年 . All rights reserved.
//
#import "KWMBaseModel.h"
@interface KWMWish : KWMBaseModel
@property (nonatomic) NSNumber *_id;
//用户id
@property (nonatomic) NSNumber *customerId;
//用户名
@property (nonatomic) NSString *customerName;
//用户邮箱
@property (nonatomic) NSString *customerEmail;
//商品id
@property (nonatomic) NSNumber *productId;
//商品名
@property (nonatomic) NSString *productTitle;
//变体id
@property (nonatomic) NSNumber *variantId;
//变体名
@property (nonatomic) NSString *variantTitle;
//变体sku
@property (nonatomic) NSString *variantSku;
//店铺地址 eg:liang2018.myshopify.com
@property (nonatomic) NSString *shop;
//创建时间 eg:2017-07-05 10:41:52
@property (nonatomic) NSString *createdAt;
//更新时间
@property (nonatomic) NSString *updatedAt;
@end
//
// KWMWishProduct.m
// iCemarose
//
// Created by HouWeiBin on 2017/7/6.
// Copyright © 2017年 kollway. All rights reserved.
// 愿望model
//
#import "KWMWish.h"
@implementation KWMWish
@end
......@@ -10,6 +10,7 @@
#import "KWMOrderCell.h"
#import "KWMProductDetailVC.h"
#import "KWMImageUtil.h"
#import "KWMStringUtil.h"
@interface KWMOrderVC ()
@property (nonatomic) NSMutableArray<BUYOrder *> *orderList;//订单列表
......@@ -163,10 +164,6 @@
for(BUYProduct *product in self.productList){
if([product.identifier isEqualToNumber:productId]){
productImage = [KWMImageUtil getProductImageUrl:product ImageSize:LowImage];
if([product.identifier isEqualToNumber:@9690154060]){
NSLog(@"11111111%@",@"lalal");
}
}
}
}
......@@ -182,30 +179,6 @@
return NO;
}
-(NSArray *)splitArray:(NSArray *)array withSubSize:(NSInteger)subSize{
//拆分后的数组个数
NSInteger count = array.count % subSize == 0?(array.count /subSize): (array.count / subSize + 1);
NSMutableArray *arr = [NSMutableArray array];
for (int i = 0; i < count; i ++) {
//数组下标
NSInteger index = i * subSize;
//保存拆分的固定长度的数组元素的可变数组
NSMutableArray *arr1 = [[NSMutableArray alloc] init];
//移除子数组的所有元素
[arr1 removeAllObjects];
NSInteger j = index;
//将数组下标乘以1、2、3,得到拆分时数组的最大下标值,但最大不能超过数组的总大小
while (j < subSize*(i + 1) && j < array.count) {
[arr1 addObject:[array objectAtIndex:j]];
j += 1;
}
//将子数组添加到保存子数组的数组中
[arr addObject:[arr1 copy]];
}
return [arr copy];
}
//获取订单API
- (void)requestOrderAPI{
[self showLoading];
......@@ -246,7 +219,7 @@
return;
}
__weak KWMOrderVC *weakSelf = self;
NSArray *productIdsArray = [self splitArray:productIds withSubSize:50];
NSArray *productIdsArray = [KWMStringUtil splitArray:productIds withSubSize:50];
for(NSArray *mProductIds in productIdsArray){
//经过测试,该api一次最多返回50个product,所以如果有100个商品,需要分成两次请求
[self.client getProductsByIds:mProductIds completion:^(NSArray<BUYProduct *> * _Nullable products, NSError * _Nullable error) {
......
......@@ -20,6 +20,10 @@
@property (weak, nonatomic) id<KWMFirstDetailViewDelegate>delegate;
@property (nonatomic) BUYProductVariant *variant;
-(void)setData:(BUYProduct *)product;
@end
......@@ -28,6 +28,8 @@
@property(nonatomic,weak) IBOutlet UILabel *lbDetail;
@property(nonatomic,weak) IBOutlet UILabel *lbVariant;
//单位label
@property(nonatomic,weak) IBOutlet UILabel *lbUnit;
......@@ -257,6 +259,12 @@
}
}
-(void)setVariant:(BUYProductVariant *)variant{
_variant = variant;
if(variant){
self.lbVariant.text = variant.title;
}
}
......
......@@ -18,6 +18,7 @@
<outlet property="lbOldPrice" destination="Mql-V1-hiE" id="Sh1-Fz-sfU"/>
<outlet property="lbPrice" destination="gpT-Sg-ghu" id="FGd-AA-Yiv"/>
<outlet property="lbUnit" destination="wfT-JC-g9d" id="MSn-kd-aQ2"/>
<outlet property="lbVariant" destination="o3P-oH-Xcb" id="i3M-ae-jhH"/>
<outlet property="pageControl" destination="M6v-TN-2WE" id="QPe-TI-LdG"/>
<outlet property="vView" destination="iN0-l3-epB" id="TQD-W9-jEg"/>
</connections>
......
//
// KWMWishCell.h
// iCemarose
//
// Created by HouWeiBin on 2017/7/7.
// Copyright © 2017年 kollway. All rights reserved.
//
#import <UIKit/UIKit.h>
#import <Buy/Buy.h>
#import "KWMWish.h"
@class KWMWishCell;
@protocol KWMWishCellDelegate <NSObject>
-(void)kwm_onClickRemove:(KWMWishCell *)wishCell;
-(void)kwm_onClickEdit:(KWMWishCell *)wishCell;
-(void)kwm_onClickAddToShopCart:(KWMWishCell *)wishCell;
@end
@interface KWMWishCell : UITableViewCell
@property (weak, nonatomic) id<KWMWishCellDelegate>delegate;
@property (nonatomic) KWMWish *wish;
@property (nonatomic) BUYProduct *product;
@property (nonatomic) BUYProductVariant *variant;
@end
//
// KWMWishCell.m
// iCemarose
//
// Created by HouWeiBin on 2017/7/7.
// Copyright © 2017年 kollway. All rights reserved.
//
#import "KWMWishCell.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "KWMStringUtil.h"
#import "KWMImageUtil.h"
@interface KWMWishCell()
@property (weak, nonatomic) IBOutlet UIImageView *ivProduct;
@property (weak, nonatomic) IBOutlet UILabel *lbTitle;
@property (weak, nonatomic) IBOutlet UILabel *lbBrand;
@property (weak, nonatomic) IBOutlet UILabel *lbVariant;
@property (weak, nonatomic) IBOutlet UILabel *lbPrice;
@end
@implementation KWMWishCell
- (void)awakeFromNib {
[super awakeFromNib];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
}
- (IBAction)onClickEdit:(id)sender{
if(self.delegate){
[self.delegate kwm_onClickEdit:self];
}
}
- (IBAction)onClickRemove:(id)sender{
if(self.delegate){
[self.delegate kwm_onClickRemove:self];
}
}
- (IBAction)onClickAddToShopCart:(id)sender{
if(self.delegate){
[self.delegate kwm_onClickAddToShopCart:self];
}
}
-(void)setWish:(KWMWish *)wish{
_wish = wish;
if(_wish){
self.lbTitle.text = wish.productTitle;
self.lbVariant.text = wish.variantTitle;
}
}
-(void)setProduct:(BUYProduct *)product{
_product = product;
if(_product){
self.lbBrand.text = product.vendor;
if(product.imagesArray.count >0){
BUYImageLink *link = product.imagesArray.firstObject;
NSString *imageString = [KWMImageUtil getProductImageUrlByOriginalUrl:link.sourceURL.absoluteString ImageSize:NormalImage];
NSURL *imageURL = [NSURL URLWithString:imageString];
[self.ivProduct sd_setImageWithURL:imageURL placeholderImage:[UIImage imageNamed:@"ic_loading"]];
}
}
}
-(void)setVariant:(BUYProductVariant *)variant{
_variant = variant;
if(_variant){
self.lbPrice.text = _variant.price.stringValue;
}
}
@end
......@@ -11,6 +11,7 @@
#import "KWMBeforePayVC.h"
#import "KWMBrandCaramelVC.h"
#import "KWMShareVC.h"
#import "KWMWishListVC.h"
#import "PhotoBroswerVC.h"
#import "KWMUserModel.h"
#import "KWMShoppingCart.h"
......@@ -23,6 +24,14 @@
@property (weak, nonatomic) IBOutlet KWMSecondDetailView *detailView2;
@property (weak, nonatomic) IBOutlet UIButton *btnLike;
@property (weak, nonatomic) IBOutlet UIImageView *ivShare;
@property (nonatomic) BUYProductVariant *variant;
@property (nonatomic) BOOL isLikeVariant;
@end
@implementation KWMNewProductVC
......@@ -47,6 +56,7 @@
}
-(void)initView{
[self.btnLike setImage:[UIImage imageNamed:@"ic_like_unable"] forState:UIControlStateNormal];
self.productId = @(4746085766);
self.detailView1.delegate = self;
[self requestProductDetail];
......@@ -59,6 +69,12 @@
}
-(IBAction)onClickShare:(id)sender{
KWMWishListVC *wishVC = (KWMWishListVC *)[KWMBaseVC findControllerBy:[KWMWishListVC kwmTag] fromStoryboard:@"NewProduct"];
[self.navigationController pushViewController:wishVC animated:YES];
return;
if(self.product){
KWMShareVC *shareVC = (KWMShareVC *)[KWMBaseVC findControllerBy:[KWMShareVC kwmTag] fromStoryboard:@"New"];
shareVC.shareUrl = [NSString stringWithFormat:@"https://www.cemarose.com/products/%@",self.product.handle];
......@@ -70,7 +86,11 @@
}
-(IBAction)onClickLike:(id)sender{
[self showToast:@"LIKE"];
if(self.isLikeVariant){
[self requestRemoveWish];
}else{
[self requestAddWish];
}
}
#pragma mark -- KWMFirstDetailViewDelegate
......@@ -113,6 +133,15 @@
}
#pragma mark - KWMVariantsVCDelegate
-(void)kwm_setVariant:(KWMVariantsVC *)variantsVC variant:(BUYProductVariant *)variant{
self.detailView1.variant = variant;
self.variant = variant;
[self.btnLike setImage:[UIImage imageNamed:@"ic_like"] forState:UIControlStateNormal];
self.isLikeVariant = NO;
[self requestWishList];
}
- (void)kwm_addShopWithVariantId:(NSNumber *)identifier quantity:(NSInteger)quantity variant:(BUYProductVariant *) variant buyNow:(BOOL)buyNow callback:(void (^)(BOOL))callback {
__weak KWMNewProductVC *this = self;
if (!buyNow) {
......@@ -213,16 +242,115 @@
weakSelf.detailView2.isLoadingSameType = NO;
[weakSelf.detailView2 setData:products isSameBrand:NO];
}];
}else{
weakSelf.detailView2.isLoadingSameType = NO;
}
}];
}
-(void)requestWishList{
__weak KWMNewProductVC *weakSelf = self;
NSDictionary *customerDict = [[NSUserDefaults standardUserDefaults] objectForKey:@"customer"];
BUYCustomer *customer = [[BUYCustomer alloc] initWithModelManager:self.client.modelManager JSONDictionary:customerDict];
BUYProductVariant *variant = self.detailView1.variant;
if(!customer || !variant){
return;
}
NSDictionary *parament = @{
@"customer_id":customer.identifier.stringValue,
@"customer_email":customer.email,
@"varaint_id":variant.identifier.stringValue,
@"shop":Shopify_SHOP_DOMAIN
};
void(^failure)(NSURLSessionDataTask *,NSError *) = ^(NSURLSessionDataTask *task,NSError *error){
[weakSelf hideLoading];
[weakSelf showError:error];
};
void(^success)(NSURLSessionDataTask *,KWMAdditionalListResult *) = ^(NSURLSessionDataTask *task,KWMAdditionalListResult *result){
[weakSelf hideLoading];
if(result && result.data){
NSArray *wishArray = (NSArray *)result.data;
if(wishArray.count >0){
UIImage *image = [UIImage imageNamed:@"ic_like_1"];
[weakSelf.btnLike setImage:image forState:UIControlStateNormal];
weakSelf.isLikeVariant = YES;
}
}
};
[self.api getWishList:parament success:success failure:failure];
}
-(void)requestRemoveWish{
__weak KWMNewProductVC *weakSelf = self;
NSDictionary *customerDict = [[NSUserDefaults standardUserDefaults] objectForKey:@"customer"];
BUYCustomer *customer = [[BUYCustomer alloc] initWithModelManager:self.client.modelManager JSONDictionary:customerDict];
BUYProductVariant *variant = self.detailView1.variant;
if(!customer || !variant){
return;
}
NSDictionary *parament = @{
@"customer_id":customer.identifier.stringValue,
@"customer_email":customer.email,
@"variant_id":variant.identifier.stringValue,
@"shop":Shopify_SHOP_DOMAIN
};
void(^failure)(NSURLSessionDataTask *,NSError *) = ^(NSURLSessionDataTask *task,NSError *error){
[weakSelf hideLoading];
[weakSelf showError:error];
};
void(^success)(NSURLSessionDataTask *,KWMAdditionalListResult *) = ^(NSURLSessionDataTask *task,KWMAdditionalListResult *result){
[weakSelf hideLoading];
if(result && result.message){
if([result.message isEqualToString:@"success"] || [result.message isEqualToString:@"wishlist does not exist"]){
UIImage *image = [UIImage imageNamed:@"ic_like"];
[weakSelf.btnLike setImage:image forState:UIControlStateNormal];
weakSelf.isLikeVariant = NO;
}
}
};
[self.api removeWish:parament success:success failure:failure];
[self showLoading];
}
-(void)requestAddWish{
__weak KWMNewProductVC *weakSelf = self;
NSDictionary *customerDict = [[NSUserDefaults standardUserDefaults] objectForKey:@"customer"];
BUYCustomer *customer = [[BUYCustomer alloc] initWithModelManager:self.client.modelManager JSONDictionary:customerDict];
if(!customer || !self.variant){
return;
}
NSDictionary *parament = @{
@"customer_id":customer.identifier.stringValue,
@"customer_email":customer.email,
@"customer_name":customer.lastName,
@"product_title":self.product.title,
@"product_id":self.product.identifier.stringValue,
@"variant_id":self.variant.identifier.stringValue,
@"variant_sku":self.variant.sku,
@"variant_title":self.variant.title,
@"product_handle":self.product.handle,
@"shop":Shopify_SHOP_DOMAIN
};
void(^failure)(NSURLSessionDataTask *,NSError *) = ^(NSURLSessionDataTask *task,NSError *error){
[weakSelf hideLoading];
[weakSelf showError:error];
};
void(^success)(NSURLSessionDataTask *,KWMAdditionalListResult *) = ^(NSURLSessionDataTask *task,KWMAdditionalListResult *result){
[weakSelf hideLoading];
if(result && result.message && [result.message isEqualToString:@"success"]){
if([result.message isEqualToString:@"success"] || [result.message isEqualToString:@"wishlist already exist"]){
UIImage *image = [UIImage imageNamed:@"ic_like_1"];
[weakSelf.btnLike setImage:image forState:UIControlStateNormal];
weakSelf.isLikeVariant = YES;
}
}
};
[self.api saveWish:parament success:success failure:failure];
[self showLoading];
}
@end
......@@ -12,11 +12,15 @@
#import "KWMSizeCell.h"
#import "KWMShopCartData.h"
#import "KWMVariants.h"
#import "KWMWish.h"
@class KWMVariantsVC;
@protocol KWMVariantsVCDelegate <NSObject>
- (void)kwm_addShopWithVariantId:(NSNumber *) identifier quantity:(NSInteger) quantity variant:(BUYProductVariant *) variant buyNow:(BOOL) buyNow callback:(void(^)(BOOL)) callback;
- (void)kwm_setVariant:(KWMVariantsVC *)variantsVC variant:(BUYProductVariant *)variant;
@end
@interface KWMVariantsVC : KWMBaseVC<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
......@@ -31,5 +35,6 @@
@property (nonatomic) BUYProduct *product;
@property (nonatomic) KWMWish *wish;
@end
......@@ -179,21 +179,24 @@
[self showToast:@"请选择您想要的规格"];
return;
}
if (self.product && !self.product.JSONDictionary){
[self showToast:@"该商品信息缺失"];
return;
}
BOOL buyNow = sender.tag == 1;
__weak id this = self;
[self showLoading];
if (self.delegate) {
[self.delegate kwm_addShopWithVariantId:self.selectVariant.identifier quantity:self.count variant:self.selectVariant buyNow:buyNow callback:^(BOOL success) {
[self hideLoading];
if(success) {
[this dismissViewControllerAnimated:YES completion:nil];
}
}];
}
// if (self.product && !self.product.JSONDictionary){
// [self showToast:@"该商品信息缺失"];
// return;
// }
// BOOL buyNow = sender.tag == 1;
// __weak id this = self;
// [self showLoading];
// if (self.delegate) {
// [self.delegate kwm_addShopWithVariantId:self.selectVariant.identifier quantity:self.count variant:self.selectVariant buyNow:buyNow callback:^(BOOL success) {
// [self hideLoading];
// if(success) {
// [this dismissViewControllerAnimated:YES completion:nil];
// }
// }];
// }
//现在不是添加到购物车了,而是显示在商品详情页。
[self.delegate kwm_setVariant:self variant:self.selectVariant];
[self dismissViewControllerAnimated:YES completion:nil];
}
-(IBAction)onClickBg:(id)sender{
......
//
// KWMWishListVC.h
// iCemarose
//
// Created by HouWeiBin on 2017/7/7.
// Copyright © 2017年 kollway. All rights reserved.
//
#import "KWMBaseVC.h"
#import "KWMWishCell.h"
#import "KWMVariantsVC.h"
@interface KWMWishListVC : KWMBaseVC<UITableViewDelegate,UITableViewDataSource,KWMWishCellDelegate,KWMVariantsVCDelegate>
@end
//
// KWMWishListVC.m
// iCemarose
//
// Created by HouWeiBin on 2017/7/7.
// Copyright © 2017年 kollway. All rights reserved.
//
#import "KWMWishListVC.h"
#import "KWMShoppingCart.h"
#import "KWMImageUtil.h"
#import "KWMStringUtil.h"
#import "KWMShopCartModel.h"
@interface KWMWishListVC ()
@property (weak,nonatomic) IBOutlet UILabel *lbTotalPrice;
@property (weak,nonatomic) IBOutlet UITableView *tbvWish;
@property(nonatomic) NSMutableArray *wishArray;
@property(nonatomic) NSMutableArray *productArray;
@end
@implementation KWMWishListVC
+(NSString *)kwmTag{
return @"KWMWishListVC";
}
- (void)viewDidLoad {
[super viewDidLoad];
[self initView];
[self requestWishList];
}
- (void)initView{
self.title = @"我喜欢的";
[self.tbvWish registerNib:[UINib nibWithNibName:NSStringFromClass([KWMWishCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([KWMWishCell class])];
self.wishArray = [NSMutableArray array];
self.productArray = [NSMutableArray array];
[self setTotalPrice];
}
-(void)removeWish:(NSNumber *)variantId{
for(KWMWish *wish in self.wishArray){
if([wish.variantId isEqualToNumber:variantId]){
[self.wishArray removeObject:wish];
}
}
}
//计算总价格
- (void) setTotalPrice{
NSArray *shopCartList = (NSMutableArray *)[[KWMShoppingCart sharedInstance] items];
NSDecimalNumber *total = [NSDecimalNumber decimalNumberWithDecimal:@(0).decimalValue];
for (KWMShopCartModel *model in shopCartList) {
if (model.line_price) {
total = [total decimalNumberByAdding:model.line_price.priceValue];
}else{
total = [total decimalNumberByAdding:[model.price.priceValue decimalNumberByMultiplyingBy:[NSDecimalNumber decimalNumberWithString:@(model.quantity).stringValue]]];
}
}
self.lbTotalPrice.text = total.priceFormatted;
}
#pragma mark --KWMVariantsVCDelegate
-(void)kwm_setVariant:(KWMVariantsVC *)variantsVC variant:(BUYProductVariant *)variant{
for(KWMWish *wish in self.wishArray){
if([wish.variantId isEqualToNumber:variantsVC.wish.variantId]){
wish.variantId = variant.identifier;
wish.variantSku = variant.sku;
wish.variantTitle = variant.title;
[self.tbvWish reloadData];
}
}
}
#pragma mark --KWMWishCellDelegate
-(void)kwm_onClickEdit:(KWMWishCell *)wishCell{
//此edit并没有请求API,而只是暂时的在本页面修改wish,参考网页端cemarose。
KWMVariantsVC *variantsVC = (KWMVariantsVC *)[KWMVariantsVC findControllerBy:[KWMVariantsVC kwmTag] fromStoryboard:@"NewProduct"];
variantsVC.wish = wishCell.wish;
variantsVC.product = wishCell.product;
variantsVC.delegate = self;
CGSize size = [UIScreen mainScreen].bounds.size;
[self showPresentation:variantsVC size:size tapOutsideClose:YES style:MZFormSheetPresentationTransitionStyleFade];
}
-(void)kwm_onClickRemove:(KWMWishCell *)wishCell{
[self requestRemoveWish:wishCell.wish.variantId];
}
-(void)kwm_onClickAddToShopCart:(KWMWishCell *)wishCell{
if(!self.productArray){
[self requestProductsApi];
return;
}
NSNumber *variantId = wishCell.wish.variantId;
BUYProduct *product = wishCell.product;
BUYProductVariant *variant = wishCell.variant;
if(!product || !variant){
return;
}
__weak KWMWishListVC *this = self;
[[KWMShoppingCart sharedInstance] increaseProductWithVariantId:variantId quantity:1 callback:^(NSError *error, KWMCartResult *cart) {
if (cart.items.firstObject && !cart.items.firstObject.product_id) {
KWMShopCartModel *model = cart.items.firstObject;
BUYImageLink *il = product.images.firstObject;
model.imageStr = [KWMImageUtil getProductImageUrlByOriginalUrl:il.sourceURL.absoluteString ImageSize:NormalImage];
model.name = product.title;
model.brand = product.vendor;
model.size = variant.title;
model.price = variant.price;
model.product_id = product.identifier;
}
if (!error) {
// [this showToast:@"添加成功"];
[this setTotalPrice];
//[this.btnShopCart setTitle:cart.item_count.stringValue forState:UIControlStateNormal];
//callback(YES);
}else{
[this showError:error];
//callback(NO);
}
}];
}
#pragma mark --UITableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.wishArray ? self.wishArray.count:0;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
KWMWishCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([KWMWishCell class]) forIndexPath:indexPath];
KWMWish *wish = [self.wishArray objectAtIndex:indexPath.row];
BUYProduct *product;
BUYProductVariant *variant;
if(self.productArray){
for(BUYProduct *mProduct in self.productArray){
if([mProduct.identifier isEqualToNumber:wish.productId]){
product = mProduct;
}
}
}
if(product){
for(BUYProductVariant *mVariant in product.variantsArray){
if([mVariant.identifier isEqualToNumber:wish.variantId]){
variant = mVariant;
break;
}
}
}
cell.wish = wish;
cell.variant = variant;
cell.product = product;
cell.delegate = self;
return cell;
}
#pragma mark-- KWMAPIManager
-(void)requestWishList{
__weak KWMWishListVC *weakSelf = self;
NSDictionary *customerDict = [[NSUserDefaults standardUserDefaults] objectForKey:@"customer"];
BUYCustomer *customer = [[BUYCustomer alloc] initWithModelManager:self.client.modelManager JSONDictionary:customerDict];
if(!customer){
return;
}
NSDictionary *parament = @{
@"customer_id":customer.identifier.stringValue,
@"customer_email":customer.email,
@"shop":Shopify_SHOP_DOMAIN
};
void(^failure)(NSURLSessionDataTask *,NSError *) = ^(NSURLSessionDataTask *task,NSError *error){
[weakSelf hideLoading];
[weakSelf showError:error];
};
void(^success)(NSURLSessionDataTask *,KWMAdditionalListResult *) = ^(NSURLSessionDataTask *task,KWMAdditionalListResult *result){
[weakSelf hideLoading];
if(result && result.data){
NSMutableArray *wishArray = (NSMutableArray *)result.data;
weakSelf.wishArray = wishArray;
[weakSelf.tbvWish reloadData];
[weakSelf requestProductsApi];
}
};
[self.api getWishList:parament success:success failure:failure];
[self showLoading];
}
-(void)requestRemoveWish:(NSNumber *)variantId{
__weak KWMWishListVC *weakSelf = self;
NSDictionary *customerDict = [[NSUserDefaults standardUserDefaults] objectForKey:@"customer"];
BUYCustomer *customer = [[BUYCustomer alloc] initWithModelManager:self.client.modelManager JSONDictionary:customerDict];
if(!customer || !variantId){
return;
}
NSDictionary *parament = @{
@"customer_id":customer.identifier.stringValue,
@"customer_email":customer.email,
@"variant_id":variantId.stringValue,
@"shop":Shopify_SHOP_DOMAIN
};
void(^failure)(NSURLSessionDataTask *,NSError *) = ^(NSURLSessionDataTask *task,NSError *error){
[weakSelf hideLoading];
[weakSelf showError:error];
};
void(^success)(NSURLSessionDataTask *,KWMAdditionalListResult *) = ^(NSURLSessionDataTask *task,KWMAdditionalListResult *result){
[weakSelf hideLoading];
if(result && result.message){
if([result.message isEqualToString:@"success"] || [result.message isEqualToString:@"wishlist does not exist"]){
[weakSelf removeWish:variantId];
}
}
};
[self.api removeWish:parament success:success failure:failure];
[self showLoading];
}
#pragma mark-- BuyClient+storefront.h
-(void)requestProductsApi{
NSMutableArray *productIds = [NSMutableArray array];
for(KWMWish *wish in self.wishArray){
[productIds addObject:wish.productId];
}
if(productIds.count == 0){
return;
}
__weak KWMWishListVC *weakSelf = self;
NSArray *productIdsArray = [KWMStringUtil splitArray:productIds withSubSize:50];
for(NSArray *mProductIds in productIdsArray){
//经过测试,该api一次最多返回50个product,所以如果有100个商品,需要分成两次请求
[self.client getProductsByIds:mProductIds completion:^(NSArray<BUYProduct *> * _Nullable products, NSError * _Nullable error) {
[weakSelf hideLoading];
if (!error && products && products.count>0) {
[weakSelf.productArray addObjectsFromArray:products];
[weakSelf.tbvWish reloadData];
weakSelf.tbvWish.hidden = NO;
}else if (error){
[weakSelf showError:error];
}
}];
}
[self showLoading];
}
@end
......@@ -51,9 +51,10 @@
+ (NSString *)price:(NSNumber *)price;
//欧元转人民币
//+ (NSString *)getEUR2CNYstring:(NSNumber *)price;
+ (NSArray *)splitArray:(NSArray *)array withSubSize:(NSInteger)subSize;
@end
......@@ -507,4 +507,28 @@ static NSString *kEnglishNum = @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstu
return [NSString stringWithFormat:@"¥ %@",CNYstring];
}
+ (NSArray *)splitArray:(NSArray *)array withSubSize:(NSInteger)subSize{
//拆分后的数组个数
NSInteger count = array.count % subSize == 0?(array.count /subSize): (array.count / subSize + 1);
NSMutableArray *arr = [NSMutableArray array];
for (int i = 0; i < count; i ++) {
//数组下标
NSInteger index = i * subSize;
//保存拆分的固定长度的数组元素的可变数组
NSMutableArray *arr1 = [[NSMutableArray alloc] init];
//移除子数组的所有元素
[arr1 removeAllObjects];
NSInteger j = index;
//将数组下标乘以1、2、3,得到拆分时数组的最大下标值,但最大不能超过数组的总大小
while (j < subSize*(i + 1) && j < array.count) {
[arr1 addObject:[array objectAtIndex:j]];
j += 1;
}
//将子数组添加到保存子数组的数组中
[arr addObject:[arr1 copy]];
}
return [arr copy];
}
@end
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