ShopCartViewController.m 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. //
  2. // ShopCartViewController.m
  3. // IBOSSmini
  4. //
  5. // Created by guan hong hou on 2017/5/18.
  6. // Copyright © 2017年 elongtian. All rights reserved.
  7. //
  8. #import "ShopCartViewController.h"
  9. #import "CartTableViewCell.h"
  10. #import "CartFrame.h"
  11. #import "ShopCartModel.h"
  12. #import "SubmitOrderVC.h"
  13. @interface ShopCartViewController ()
  14. @end
  15. @implementation ShopCartViewController
  16. @synthesize vCustomTableView;
  17. #pragma mark - 公共函数
  18. /**
  19. viewDidLoad函数
  20. */
  21. - (void)viewDidLoad {
  22. [super viewDidLoad];
  23. [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
  24. [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
  25. [self initUI];
  26. _detailArr=[[NSMutableArray alloc] init];
  27. _cartM=[ShopCartModel new];
  28. _cartM.rootVC=self;
  29. [self reloadData];
  30. }
  31. /**
  32. 修改:2017-9-25
  33. 适配机型
  34. 安全区视图发生变化
  35. */
  36. -(void)viewSafeAreaInsetsDidChange{
  37. self.view.backgroundColor = [UIColor whiteColor];
  38. vCustomTableView.frame = CGRectMake(0, 0, SCREENWIDTH, self.view.safeAreaLayoutGuide.layoutFrame.size.height - 50);
  39. _bottomView.frame=CGRectMake(0, self.view.safeAreaLayoutGuide.layoutFrame.size.height - 50, self.view.frame.size.width, 50);
  40. [super viewSafeAreaInsetsDidChange];
  41. }
  42. #pragma mark - 委托函数
  43. //文本编辑回調
  44. -(void)textValueChange:(CartTableViewCell *)cell txtField:(UITextField*)txtfield
  45. {
  46. [_cartM updateCellSaleText:txtfield checkeId:cell.lblCartID.text];
  47. }
  48. //商品删除成功回调函数
  49. - (void)onDeleteFinish:(ASIDownManager *)sender {
  50. NSDictionary *dic = [sender.mWebStr JSONValue];
  51. [self cancel];
  52. if (dic && [dic isKindOfClass:[NSDictionary class]]) {
  53. int iStatus = [[dic objectForKey:@"Status"] intValue];
  54. NSString *message=[dic objectForKey:@"Message"];
  55. if (iStatus == 0) {
  56. NSArray * approvArr=[dic objectForKey:@"Result"];
  57. if(approvArr!=nil&&approvArr.count>0)
  58. {
  59. [_detailArr removeAllObjects];
  60. _detailArr= [[NSMutableArray alloc] initWithArray:approvArr];
  61. [_cartM parseWorkFlowArr:_detailArr];
  62. if([_cartM isCheckedAll])
  63. {
  64. [self changeCheckALLSelect];
  65. }
  66. else
  67. {
  68. [self changeCheckAllUnSelect];
  69. }
  70. int orderCount=[self getOrderCount];
  71. NSString *balance=[NSString stringWithFormat:@"去结算(%d)",orderCount];
  72. [_btnBalance setTitle:balance forState:UIControlStateNormal];
  73. [vCustomTableView reloadData];
  74. }
  75. else{
  76. _detailArr= [[NSMutableArray alloc] initWithArray:approvArr];
  77. [_cartM parseWorkFlowArr:_detailArr];
  78. vCustomTableView.backgroundView = nil;
  79. UIView *noDataView=[[UIView alloc]init];
  80. noDataView.frame=vCustomTableView.bounds;
  81. UIImageView *nodataImgView=[[UIImageView alloc] initWithFrame:CGRectMake(noDataView.frame.size.width/2-16,noDataView.frame.size.height/2-16,32,32)];
  82. [nodataImgView setImage:[UIImage imageNamed:@"icon_no_data"]];
  83. [noDataView addSubview:nodataImgView];
  84. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(noDataView.frame.size.width/2-16-12,CGRectGetMaxY(nodataImgView.frame)+3,70, 25)];
  85. label.font=[UIFont systemFontOfSize:NoDataFontOfSize];
  86. label.text = @"无数据";
  87. label.numberOfLines = 2;
  88. label.textColor = [UIColor lightGrayColor];
  89. [noDataView addSubview:label];
  90. vCustomTableView.backgroundView=noDataView;
  91. [vCustomTableView reloadData];
  92. [_btnBalance setTitle:@"去结算(0)" forState:UIControlStateNormal];
  93. }
  94. }
  95. else if(iStatus==ActionResultStatusAuthError
  96. ||iStatus==ActionResultStatusNoLogin
  97. ||iStatus==ActionResultStatusLogined ||iStatus == ActionResultStatusLoginedInvalid){
  98. [self showReLoginDialog:message];
  99. return;
  100. }
  101. else {
  102. [self showAlertViewText:message];
  103. }
  104. }
  105. }
  106. //商品删除失败回调函数
  107. - (void)onDeleteFail:(ASIDownManager *)sender {
  108. [self cancel];
  109. [self showAlertViewText:@"操作失败"];
  110. }
  111. //购物车加载成功函数
  112. - (void)onLoadFinish:(ASIDownManager *)sender {
  113. NSDictionary *dic = [sender.mWebStr JSONValue];
  114. [self cancel];
  115. if (dic && [dic isKindOfClass:[NSDictionary class]]) {
  116. int iStatus = [[dic objectForKey:@"Status"] intValue];
  117. NSString *message=[dic objectForKey:@"Message"];
  118. vCustomTableView.backgroundView=nil;
  119. if (iStatus == 0) {
  120. NSArray * approvArr=[dic objectForKey:@"Result"];
  121. if(approvArr!=nil&&approvArr.count>0)
  122. {
  123. _detailArr= [[NSMutableArray alloc] initWithArray:approvArr];
  124. [_cartM parseWorkFlowArr:_detailArr];
  125. if([_cartM isCheckedAll])
  126. {
  127. [self changeCheckALLSelect];
  128. int orderCount=[self getOrderCount];
  129. NSString *balance=[NSString stringWithFormat:@"去结算(%d)",orderCount];
  130. [_btnBalance setTitle:balance forState:UIControlStateNormal];
  131. }
  132. else
  133. {
  134. [_btnBalance setTitle:@"去结算(0)" forState:UIControlStateNormal];
  135. [self changeCheckAllUnSelect];
  136. }
  137. [self.vCustomTableView reloadData];
  138. }
  139. else{
  140. vCustomTableView.backgroundView = nil;
  141. UIView *noDataView=[[UIView alloc]init];
  142. noDataView.frame=vCustomTableView.bounds;
  143. UIImageView *nodataImgView=[[UIImageView alloc] initWithFrame:CGRectMake(noDataView.frame.size.width/2-16,noDataView.frame.size.height/2-16,32,32)];
  144. [nodataImgView setImage:[UIImage imageNamed:@"icon_no_data"]];
  145. [noDataView addSubview:nodataImgView];
  146. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(noDataView.frame.size.width/2-16-12,CGRectGetMaxY(nodataImgView.frame)+3,70, 25)];
  147. label.font=[UIFont systemFontOfSize:NoDataFontOfSize];
  148. label.text = @"无数据";
  149. label.numberOfLines = 2;
  150. label.textColor = [UIColor lightGrayColor];
  151. [noDataView addSubview:label];
  152. vCustomTableView.backgroundView =noDataView;
  153. [_btnBalance setTitle:@"去结算(0)" forState:UIControlStateNormal];
  154. }
  155. }
  156. else if(iStatus==ActionResultStatusAuthError
  157. ||iStatus==ActionResultStatusNoLogin
  158. ||iStatus==ActionResultStatusLogined ||iStatus == ActionResultStatusLoginedInvalid){
  159. [self showReLoginDialog:message];
  160. return;
  161. }
  162. else {
  163. [self showAlertViewText:message];
  164. }
  165. }
  166. }
  167. //购物车加载失败函数
  168. - (void)onLoadFail:(ASIDownManager *)sender {
  169. [self cancel];
  170. [self showAlertViewText:@"加载失败"];
  171. }
  172. //屏幕触摸回调函数
  173. -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
  174. {
  175. [self.view endEditing:YES];
  176. }
  177. // scrollview回调函数
  178. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
  179. [self.view endEditing:YES];
  180. }
  181. //checkbox点击回调函数
  182. -(void) btnCheckPressed:(CartTableViewCell *)cell
  183. {
  184. BOOL checked = [_cartM updateCellChecked:cell.cartId];
  185. [cell setCheckBackground:checked];
  186. [self changeCheckAllState];
  187. }
  188. //tableview cell的高度
  189. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  190. {
  191. CartFrame *cartFrame=_cartM.workflowArr[indexPath.row];
  192. return cartFrame.cellHeight;
  193. }
  194. //tableview的行数
  195. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  196. {
  197. return _detailArr.count;
  198. }
  199. //获取tableviewcell
  200. - (UITableViewCell *)tableView:(UITableView *)tableView
  201. cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  202. static NSString *cellIdentifier = @"ShopCartCell";
  203. CartTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
  204. cell=[[CartTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
  205. cell.selectionStyle=UITableViewCellSelectionStyleNone;
  206. CartFrame *cartFrame=_cartM.workflowArr[indexPath.row];
  207. [cell parseCartInfo:cartFrame];
  208. cell.delegate=self;
  209. cell.cellIndex=(int)indexPath.row;
  210. BOOL checked = cartFrame.cartModel.isChecked;
  211. [cell setCheckBackground:checked];
  212. return cell;
  213. }
  214. //弹出消息回调
  215. -(void)alertMessage:(NSString*)message{
  216. [self showAlertViewText:message];
  217. return;
  218. }
  219. //刷新数据
  220. -(void)refreshData{
  221. if(_detailArr!=nil){
  222. [_detailArr removeAllObjects];
  223. [vCustomTableView reloadData];
  224. }
  225. [self reloadData];
  226. }
  227. #pragma mark - 私有函数
  228. //初始化UI
  229. -(void)initUI{
  230. _detailArr=[[NSMutableArray alloc] init];
  231. self.navigationItem.title=@"购物车";
  232. vCustomTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-50)];
  233. vCustomTableView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
  234. vCustomTableView.backgroundColor = [UIColor whiteColor];
  235. vCustomTableView.separatorStyle=UITableViewCellSeparatorStyleNone;
  236. vCustomTableView.delegate = self;
  237. vCustomTableView.dataSource=self;
  238. [self.view addSubview:vCustomTableView];
  239. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  240. [button setBackgroundImage:[UIImage imageNamed:@"icon_back.png"]
  241. forState:UIControlStateNormal];
  242. [button addTarget:self action:@selector(goBack)
  243. forControlEvents:UIControlEventTouchUpInside];
  244. button.frame = CGRectMake(0, 0, 15, 18);
  245. UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithCustomView:button];
  246. self.navigationItem.leftBarButtonItem = menuButton;
  247. //保存
  248. _bottomView=[UIView new];
  249. _bottomView.frame=CGRectMake(0, Screen_Height-110, self.view.frame.size.width, 50);
  250. [self.view addSubview:_bottomView];
  251. _btnCheckAll=[UIButton buttonWithType:UIButtonTypeCustom];
  252. _btnCheckAll.frame=CGRectMake(15,15, 25, 25);
  253. [_btnCheckAll addTarget:self action:@selector(btnCheckall)
  254. forControlEvents:UIControlEventTouchUpInside];
  255. [_bottomView addSubview:_btnCheckAll];
  256. UILabel *lblall=[UILabel new];
  257. lblall.frame=CGRectMake(CGRectGetMaxX(_btnCheckAll.frame)+3,15, 60, 25);
  258. lblall.text=@"全选";
  259. [_bottomView addSubview:lblall];
  260. //提交结算
  261. _btnBalance=[UIButton buttonWithType:UIButtonTypeCustom];
  262. [_btnBalance setTitle:@"去结算" forState:UIControlStateNormal];
  263. [_btnBalance setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  264. _btnBalance.frame=CGRectMake(Screen_Width-114,0, 114,50);
  265. [_btnBalance setBackgroundColor:[UIColor colorWithRed:189.0/255.0 green:0 blue:7.0/255.0 alpha:1]];
  266. [_btnBalance addTarget:self action:@selector(submitOrder) forControlEvents:UIControlEventTouchUpInside];
  267. [_bottomView addSubview:_btnBalance];
  268. [self changeCheckALLSelect];
  269. }
  270. //结算函数
  271. -(void)submitOrder
  272. {
  273. [self.view endEditing:YES];
  274. if(![_cartM isSelectAtLeastOne])
  275. {
  276. [self showAlertViewText:@"至少选择一种商品"];
  277. return;
  278. }
  279. if(![_cartM checkFormat])
  280. {
  281. [self showAlertViewText:@"每件商品的数量不能为0"];
  282. return;
  283. }
  284. [self gotoOrder];
  285. }
  286. //全部选中
  287. -(void)changeCheckALLSelect
  288. {
  289. [_btnCheckAll setBackgroundImage:[UIImage imageNamed:@"order_checked"] forState:UIControlStateNormal];
  290. }
  291. //全部未选中
  292. -(void)changeCheckAllUnSelect
  293. {
  294. [_btnCheckAll setBackgroundImage:[UIImage imageNamed:@"order_unchecked"] forState:UIControlStateNormal];
  295. }
  296. /**
  297. * 返回函数
  298. */
  299. -(void)goBack
  300. {
  301. [self.navigationController popViewControllerAnimated:YES];
  302. }
  303. //到提交订单函数
  304. -(void)gotoOrder
  305. {
  306. self.hidesBottomBarWhenPushed=YES;
  307. SubmitOrderVC *detailVC=[[SubmitOrderVC alloc] init];
  308. detailVC.cartModel=_cartM;
  309. detailVC.detailArr=[_cartM checkedOriginalArr];
  310. detailVC.refreshDelegate=self;
  311. [self.navigationController pushViewController:detailVC animated:YES];
  312. }
  313. /**
  314. 键盘弹出
  315. @param note <#note description#>
  316. */
  317. -(void)keyboardWillShow:(NSNotification *)note
  318. {
  319. CGRect keyBoardRect=[note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
  320. vCustomTableView.contentInset = UIEdgeInsetsMake(0, 0, keyBoardRect.size.height, 0);
  321. }
  322. /**
  323. 键盘隐藏
  324. @param note <#note description#>
  325. */
  326. -(void)keyboardWillHide:(NSNotification *)note
  327. {
  328. vCustomTableView.contentInset = UIEdgeInsetsZero;
  329. }
  330. //删除订单
  331. -(void)deleteOrderItem:(ShopCartItemModel*)cartItem{
  332. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"" message:@"确定要删除吗?" preferredStyle:UIAlertControllerStyleAlert];
  333. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  334. }];
  335. UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"确定"
  336. style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  337. _mDownManager = [[ASIDownManager alloc] init];
  338. _mDownManager.delegate = self;
  339. _mDownManager.OnImageDown = @selector(onDeleteFinish:);
  340. _mDownManager.OnImageFail = @selector(onDeleteFail:);
  341. [self startLoading];
  342. NSString *urlStr = ServerURL;
  343. NSMutableDictionary *dict = [NSMutableDictionary dictionary];
  344. [dict setObject:@"DeleteOrderShoppingCartIphone" forKey:@"Action"];
  345. [dict setObject:[NSString stringWithFormat:@"%@",kkAccountCode]forKey:@"AccountCode"];
  346. [dict setObject:kkUserCode forKey:@"UserCode"];
  347. [dict setObject:kkUserPwd forKey:@"UserPassword"];
  348. [dict setObject:kkSessionKey forKey:@"SessionKey"];
  349. [dict setObject:cartItem.cartId forKey:@"CartID"];
  350. [_mDownManager postHttpRequest:urlStr dic:dict path:nil fileName:nil];
  351. }];
  352. UIColor *cancelColor=[UIColor blackColor];
  353. UIColor *sureColor=[UIColor redColor];
  354. [cancelAction setValue:cancelColor forKey:@"titleTextColor"];
  355. [otherAction setValue:sureColor forKey:@"titleTextColor"];
  356. [alertController addAction:cancelAction];
  357. [alertController addAction:otherAction];
  358. [self presentViewController:alertController animated:YES completion:nil];
  359. }
  360. //加载订单数据函数
  361. -(void)reloadData
  362. {
  363. self.mDownManager = [[ASIDownManager alloc] init];
  364. _mDownManager.delegate = self;
  365. _mDownManager.OnImageDown = @selector(onLoadFinish:);
  366. _mDownManager.OnImageFail = @selector(onLoadFail:);
  367. [self startLoading];
  368. NSString *urlStr = ServerURL;
  369. NSMutableDictionary *dict = [NSMutableDictionary dictionary];
  370. [dict setObject:@"GetShoppingCartListIphone" forKey:@"Action"];
  371. [dict setObject:[NSString stringWithFormat:@"%@",kkAccountCode]forKey:@"AccountCode"];
  372. [dict setObject:kkUserCode forKey:@"UserCode"];
  373. [dict setObject:kkUserPwd forKey:@"UserPassword"];
  374. [dict setObject:kkSessionKey forKey:@"SessionKey"];
  375. [_mDownManager postHttpRequest:urlStr dic:dict path:nil fileName:nil];}
  376. //获得订单数
  377. -(int) getOrderCount{
  378. NSMutableArray *orderArray= _cartM.workflowArr;
  379. int count=0;
  380. for(CartFrame *frame in orderArray){
  381. if(frame.cartModel.isChecked){
  382. ++count;
  383. }
  384. }
  385. return count;
  386. }
  387. //改变订单选中状态
  388. -(void)changeCheckAllState
  389. {
  390. if([_cartM isCheckedAll])
  391. {
  392. [self changeCheckALLSelect];
  393. }
  394. else
  395. {
  396. [self changeCheckAllUnSelect];
  397. }
  398. long orderCount= [self getOrderCount];
  399. NSString *balance=[NSString stringWithFormat:@"去结算(%ld)",orderCount];
  400. [_btnBalance setTitle:balance forState:UIControlStateNormal];
  401. }
  402. //选中或反选所有订单
  403. -(void)btnCheckall
  404. {
  405. [_cartM updateAllCellCheck];
  406. [vCustomTableView reloadData];
  407. [self changeCheckAllState];
  408. }
  409. //进度条取消
  410. - (void)cancel {
  411. [self stopLoading];
  412. }
  413. @end