Sin descripción

LogisticsViewController.m 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. //
  2. // LogisticsViewController.m
  3. // FirstLink
  4. //
  5. // Created by ascii on 15/5/29.
  6. // Copyright (c) 2015年 FirstLink. All rights reserved.
  7. //
  8. #import "LogisticsViewController.h"
  9. #import "LogisticsViewModel.h"
  10. #import <HMSegmentedControl/HMSegmentedControl.h>
  11. #import "WebViewController.h"
  12. #import "FKOrderDetailProductCell.h"
  13. #import "LogisticsProgressCell.h"
  14. #import "LogisticsSuccessCell.h"
  15. #import "LogisticsQuestionCell.h"
  16. #import "LogisticsAnswerCell.h"
  17. #import "LogisticsTimeOutCell.h"
  18. #import "FKPackageIncompleteView.h"
  19. #import "FKTargetConfigUtil.h"
  20. @interface LogisticsViewController ()
  21. <UITableViewDelegate, UITableViewDataSource, FKCardSegmentViewDelegate>
  22. @property (nonatomic, strong) FKCardSegmentView *cardSegmentView;
  23. @property (nonatomic, strong) FKPackageIncompleteView *packageIncompleteView;
  24. @property (nonatomic, strong) HMSegmentedControl *segmentedControl;
  25. @property (nonatomic, strong) LogisticsViewModel *viewModel;
  26. @property (nonatomic, strong) UITableView *tableView;
  27. @property (nonatomic, assign) BOOL isShowQuestion;
  28. @end
  29. static NSString * const FKOrderDetailProductCellIdentifier = @"FKOrderDetailProductCellIdentifier";
  30. static NSString * const LogisticsProgressCellIdentifier = @"LogisticsProgressCellIdentifier";
  31. static NSString * const LogisticsSuccessCellIdentifier = @"LogisticsSuccessCellIdentifier";
  32. static NSString * const LogisticsQuestionCellIdentifier = @"LogisticsQuestionCellIdentifier";
  33. static NSString * const LogisticsAnswerCellIdentifier = @"LogisticsAnswerCellIdentifier";
  34. static NSString * const LogisticsTimeOutCellIdentifier = @"LogisticsTimeOutCellIdentifier";
  35. @implementation LogisticsViewController
  36. - (void)viewDidLoad {
  37. [super viewDidLoad];
  38. // Do any additional setup after loading the view from its nib.
  39. self.isShowQuestion = YES;
  40. self.navigationItem.title = @"物流信息";
  41. self.view.backgroundColor = UIColorFromRGB(0xf9f9f9);
  42. if (self.openURLPara) {
  43. self.waybillID = self.openURLPara[@"waybill_id"];
  44. self.logisticsID = self.openURLPara[@"id"];
  45. }
  46. [self.hudView show:YES];
  47. [self loadNewRequest];
  48. }
  49. - (void)didReceiveMemoryWarning {
  50. [super didReceiveMemoryWarning];
  51. // Dispose of any resources that can be recreated.
  52. }
  53. #pragma mark - Request
  54. - (void)loadNewRequest {
  55. if (!self.waybillID || self.waybillID.length == 0){
  56. [self finishLoadingData];
  57. [self showStatusTipInView:self.tableView
  58. image:[UIImage imageNamed:@"StatusNoMessageIcon"]
  59. title:NoMessageTip];
  60. return;
  61. }
  62. WeakSelf(weakSelf);
  63. [self.viewModel requestLogistics:self.waybillID
  64. logisticsID:self.logisticsID
  65. success:^(MSGHeader *header, NSDictionary *response)
  66. {
  67. [weakSelf.hudView hide:NO];
  68. [weakSelf finishLoadingData];
  69. if ([header.code intValue] == RESPONSE_MSG_NORMAL) {
  70. weakSelf.viewModel.logisticsStatus = [LogisticsViewModel parseLogisticsStatus:response];
  71. NSArray<FKPackageLogisticsItem *> *inPackageArray = [LogisticsViewModel parsePackages:response[@"data"][@"warehousing_package_list"]];
  72. NSArray<FKPackageLogisticsItem *> *outPackageArray = [LogisticsViewModel parsePackages:response[@"data"][@"send_package_list"]];
  73. weakSelf.viewModel.inPackageArray = inPackageArray;
  74. weakSelf.viewModel.outPackageArray = outPackageArray;
  75. [weakSelf.view addSubview:weakSelf.packageIncompleteView];
  76. [weakSelf.view addSubview:weakSelf.cardSegmentView];
  77. [weakSelf.view addSubview:weakSelf.segmentedControl];
  78. [weakSelf.view addSubview:weakSelf.tableView];
  79. [weakSelf.view addSubview:weakSelf.statusView];
  80. [weakSelf setupViews];
  81. } else {
  82. [FLProgressHUDHelper showText:header.msg inView:weakSelf.view];
  83. }
  84. } failure:^(MSGHeader *header, NSError *error) {
  85. [weakSelf.hudView hide:NO];
  86. [weakSelf finishLoadingData];
  87. [FLProgressHUDHelper showText:error.localizedDescription inView:weakSelf.view];
  88. }];
  89. }
  90. #pragma mark - Layout
  91. - (CGFloat)topEdgeOffset {
  92. return 0;
  93. }
  94. - (void)setupViews {
  95. self.packageIncompleteView.hidden = YES;
  96. if ([self.viewModel isPackageCompleted]) {
  97. [self.cardSegmentView mas_remakeConstraints:^(MASConstraintMaker *make) {
  98. make.left.right.equalTo(self.view);
  99. make.top.equalTo(self.view).offset(6);
  100. make.height.mas_equalTo(48);
  101. }];
  102. } else {
  103. self.packageIncompleteView.hidden = NO;
  104. [self.packageIncompleteView mas_remakeConstraints:^(MASConstraintMaker *make) {
  105. make.left.right.equalTo(self.view);
  106. make.top.equalTo(self.view).offset(0);
  107. make.height.mas_equalTo(40);
  108. }];
  109. [self.cardSegmentView mas_remakeConstraints:^(MASConstraintMaker *make) {
  110. make.left.right.equalTo(self.view);
  111. make.top.equalTo(self.packageIncompleteView.mas_bottom).offset(6);
  112. make.height.mas_equalTo(48);
  113. }];
  114. }
  115. NSArray<FKPackageLogisticsItem *> *packageArray = [self.viewModel selectPackageArray];
  116. self.segmentedControl.hidden = YES;
  117. self.tableView.hidden = YES;
  118. self.statusView.hidden = YES;
  119. if (packageArray.count > 1) {
  120. self.segmentedControl.hidden = NO;
  121. [self.segmentedControl mas_remakeConstraints:^(MASConstraintMaker *make) {
  122. make.left.right.equalTo(self.view);
  123. make.top.equalTo(self.cardSegmentView.mas_bottom).offset([self topEdgeOffset]);
  124. make.height.mas_equalTo(44);
  125. }];
  126. self.segmentedControl.sectionTitles = [self.viewModel sectionTitleArray];
  127. self.segmentedControl.selectedSegmentIndex = [self.viewModel selectedIndex];
  128. [self.segmentedControl setNeedsDisplay];
  129. self.tableView.hidden = NO;
  130. [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
  131. make.top.equalTo(self.segmentedControl.mas_bottom);
  132. make.left.right.bottom.equalTo(self.view);
  133. }];
  134. } else if (packageArray.count == 1) {
  135. self.tableView.hidden = NO;
  136. [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
  137. make.left.right.bottom.equalTo(self.view);
  138. make.top.equalTo(self.cardSegmentView.mas_bottom).offset([self topEdgeOffset]);
  139. }];
  140. } else {
  141. self.statusView.hidden = NO;
  142. self.statusView.statusImageView.image = [UIImage imageNamed:@"StatusNoMessageIcon"];
  143. self.statusView.statusLabel.text = NoMessageTip;
  144. self.statusView.backgroundColor = UIColorFromRGB(0xf4f4f4);
  145. [self.statusView mas_remakeConstraints:^(MASConstraintMaker *make) {
  146. make.left.right.bottom.equalTo(self.view);
  147. make.top.equalTo(self.cardSegmentView.mas_bottom).offset([self topEdgeOffset]);
  148. }];
  149. }
  150. [self.tableView reloadData];
  151. }
  152. /*
  153. #pragma mark - Navigation
  154. // In a storyboard-based application, you will often want to do a little preparation before navigation
  155. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  156. // Get the new view controller using [segue destinationViewController].
  157. // Pass the selected object to the new view controller.
  158. }
  159. */
  160. #pragma mark - UITableViewDataSource, UITableViewDelegate
  161. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  162. return [self.viewModel numberOfSectionsWithSegmentIndex:self.segmentedControl.selectedSegmentIndex];
  163. }
  164. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  165. return [self.viewModel numberOfRowsInSection:section withSegmentIndex:self.segmentedControl.selectedSegmentIndex];
  166. }
  167. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  168. NSInteger segmentIndex = self.segmentedControl.selectedSegmentIndex;
  169. kLogisticsCellType cellType = [self.viewModel cellTypeAtIndexPath:indexPath withSegmentIndex:segmentIndex];
  170. switch (cellType) {
  171. case kLogisticsCellTypeProduct: {
  172. return (95 + 2); break;
  173. }
  174. case kLogisticsCellTypeProgress:
  175. case kLogisticsCellTypeSuccess: {
  176. LogisticsItem *item = [self.viewModel logisticsItemAtIndexPath:indexPath withSegmentIndex:segmentIndex];
  177. return [LogisticsProgressCell cellHeightWith:item.desc];
  178. break;
  179. }
  180. case kLogisticsCellTypeQuestionAnswer: {
  181. return [LogisticsAnswerCell height:[LogisticsViewController LOGISTICS_ANSWER_TEXT]]; break;
  182. }
  183. case kLogisticsCellTypeTimeOut:
  184. break;
  185. default:
  186. break;
  187. }
  188. return 44.0;
  189. }
  190. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  191. if (section == 0) {
  192. return CGFLOAT_MIN;
  193. }
  194. return 20;
  195. }
  196. - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
  197. return CGFLOAT_MIN;
  198. }
  199. - (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  200. UITableViewCell *cell;
  201. NSInteger segmentIndex = self.segmentedControl.selectedSegmentIndex;
  202. kLogisticsCellType cellType = [self.viewModel cellTypeAtIndexPath:indexPath withSegmentIndex:segmentIndex];
  203. switch (cellType) {
  204. case kLogisticsCellTypeProduct: {
  205. FKOrderDetailProductCell *productCell = [tableView dequeueReusableCellWithIdentifier:FKOrderDetailProductCellIdentifier];
  206. FKOrderGoodItem *goodItem = [self.viewModel orderGoodItemAtIndexPath:indexPath withSegmentIndex:segmentIndex];
  207. if (goodItem) {
  208. [productCell.productImgView sd_setImageWithURL:[NSURL URLWithString:goodItem.photoURL] placeholderImage:nil];
  209. productCell.descripLabel.text = goodItem.title;
  210. productCell.priceLabel.text = [NSString stringWithFormat:@"¥%@", [FLStringHelper convertFenToYuan:goodItem.price]];
  211. productCell.numberLabel.text = [NSString stringWithFormat:@"X%@", goodItem.quantity];
  212. productCell.specLabel.text = goodItem.specName;
  213. }
  214. cell = productCell;
  215. break;
  216. }
  217. case kLogisticsCellTypeProgress:
  218. case kLogisticsCellTypeSuccess: {
  219. LogisticsItem *item = [self.viewModel logisticsItemAtIndexPath:indexPath withSegmentIndex:segmentIndex];
  220. if (indexPath.row == 0) {
  221. LogisticsSuccessCell *detailCell = [tableView dequeueReusableCellWithIdentifier:LogisticsSuccessCellIdentifier];
  222. detailCell.logisticsTextLabel.text = item.desc;
  223. NSString *createTime = [item.createTime stringByReplacingOccurrencesOfString:@"T"
  224. withString:@" "];
  225. detailCell.logisticsTimeLabel.text = createTime;
  226. detailCell.horizontalLine.hidden = YES;
  227. cell = detailCell;
  228. } else {
  229. LogisticsProgressCell *detailCell = [tableView dequeueReusableCellWithIdentifier:LogisticsProgressCellIdentifier];
  230. detailCell.logisticsTextLabel.text = item.desc;
  231. detailCell.logisticsTimeLabel.text = [item.createTime stringByReplacingOccurrencesOfString:@"T"
  232. withString:@" "];
  233. detailCell.horizontalLine.hidden = NO;
  234. cell = detailCell;
  235. }
  236. break;
  237. }
  238. case kLogisticsCellTypeQuestionAnswer: {
  239. if (self.isShowQuestion) {
  240. LogisticsQuestionCell *questionCell = [tableView dequeueReusableCellWithIdentifier:LogisticsQuestionCellIdentifier];
  241. [questionCell.iconButton addTarget:self action:@selector(clickAnswerOpenAction:) forControlEvents:UIControlEventTouchUpInside];
  242. cell = questionCell;
  243. } else {
  244. LogisticsAnswerCell *answerCell = [tableView dequeueReusableCellWithIdentifier:LogisticsAnswerCellIdentifier];
  245. [answerCell.closeButton addTarget:self action:@selector(clickAnswerCloseAction:) forControlEvents:UIControlEventTouchUpInside];
  246. answerCell.contentLabel.text = [LogisticsViewController LOGISTICS_ANSWER_TEXT];
  247. cell = answerCell;
  248. }
  249. break;
  250. }
  251. case kLogisticsCellTypeTimeOut: {
  252. LogisticsTimeOutCell *timeOutCell = [tableView dequeueReusableCellWithIdentifier:LogisticsTimeOutCellIdentifier];
  253. [timeOutCell.timeOutBtn addTarget:self action:@selector(clickTimeOutButton:) forControlEvents:UIControlEventTouchUpInside];
  254. cell = timeOutCell;
  255. break;
  256. }
  257. default:
  258. break;
  259. }
  260. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  261. return cell;
  262. }
  263. #pragma mark - FKCardSegmentViewDelegate
  264. - (void)cardSegmentView:(FKCardSegmentView *)view didSelectWithVipIndex:(kCardSegmentIndex)index {
  265. if (index == kCardSegmentIndexLeft) {
  266. [self setButton:self.cardSegmentView.leftButton image:[UIImage imageNamed:@"vip_segment_left_select"]];
  267. [self setButton:self.cardSegmentView.rightButton image:[UIImage imageNamed:@"vip_segment_unselect_gray"]];
  268. } else if (index == kCardSegmentIndexRight) {
  269. [self setButton:self.cardSegmentView.leftButton image:[UIImage imageNamed:@"vip_segment_unselect_gray"]];
  270. [self setButton:self.cardSegmentView.rightButton image:[UIImage imageNamed:@"vip_segment_right_select"]];
  271. }
  272. self.viewModel.cardSegmentIndex = index;
  273. if (self.segmentedControl.hidden == NO) {
  274. [self.segmentedControl setSelectedSegmentIndex:0];
  275. }
  276. [self setupViews];
  277. }
  278. - (void)setButton:(UIButton *)button image:(UIImage *)image {
  279. [button setBackgroundImage:image forState:UIControlStateNormal];
  280. [button setBackgroundImage:image forState:UIControlStateHighlighted];
  281. }
  282. #pragma mark - Action
  283. - (IBAction)clickAnswerOpenAction:(id)sender {
  284. self.isShowQuestion = NO;
  285. [self.tableView reloadData];
  286. }
  287. - (IBAction)clickAnswerCloseAction:(id)sender {
  288. self.isShowQuestion = YES;
  289. [self.tableView reloadData];
  290. }
  291. - (IBAction)clickTimeOutButton:(id)sender {
  292. NSString *urlString = [NSString stringWithFormat:@"%@/support/timeout-pay.html", [[FKServerUtil sharedInstance] webServer]];
  293. WebViewController *newViewController = [[WebViewController alloc] init];
  294. newViewController.url = urlString;
  295. newViewController.hidesBottomBarWhenPushed = YES;
  296. [self.navigationController pushViewController:newViewController animated:YES];
  297. }
  298. #pragma mark - Property
  299. + (NSString *)LOGISTICS_ANSWER_TEXT {
  300. return [NSString stringWithFormat:@"我们会在最短时间内帮您在海外官网下单;\n下单成功后,官网约在3天内发货;\n%@直接提供海外直达中国的物流服务,全程可监控;\n清关后EMS直接收包投递;\n如出现清关缓慢等异常情况,我们将立即与您联系", [FKTargetConfigUtil appName]];
  301. }
  302. - (FKPackageIncompleteView *)packageIncompleteView {
  303. if (!_packageIncompleteView) {
  304. _packageIncompleteView = [FKPackageIncompleteView new];
  305. _packageIncompleteView.backgroundColor = UIColorFromRGB(0xe8d0a2);
  306. _packageIncompleteView.frame = CGRectMake(0, 0, UISCREENWIDTH, 40);
  307. }
  308. return _packageIncompleteView;
  309. }
  310. - (FKCardSegmentView *)cardSegmentView {
  311. if (!_cardSegmentView) {
  312. _cardSegmentView = [[FKCardSegmentView alloc] initWithFrame:CGRectMake(0, 6, UISCREENWIDTH, 48)];
  313. _cardSegmentView.backgroundColor = UIColorFromRGB(0xf9f9f9);
  314. _cardSegmentView.superVipImgView.hidden = YES;
  315. _cardSegmentView.discountImgView.hidden = YES;
  316. _cardSegmentView.leftButtonLabel.text = @"国际物流";
  317. _cardSegmentView.leftButtonLabel.font = [UIFont boldSystemFontOfSize:14];
  318. _cardSegmentView.rightButtonLabel.text = @"国内物流";
  319. _cardSegmentView.rightButtonLabel.font = [UIFont boldSystemFontOfSize:14];
  320. _cardSegmentView.delegate = self;
  321. [self setButton:_cardSegmentView.leftButton image:[UIImage imageNamed:@"vip_segment_left_select"]];
  322. [self setButton:_cardSegmentView.rightButton image:[UIImage imageNamed:@"vip_segment_unselect_gray"]];
  323. }
  324. return _cardSegmentView;
  325. }
  326. - (HMSegmentedControl *)segmentedControl {
  327. if (!_segmentedControl) {
  328. _segmentedControl = [HMSegmentedControl new];
  329. _segmentedControl.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationDown;
  330. _segmentedControl.selectionIndicatorHeight = 2;
  331. _segmentedControl.segmentEdgeInset = UIEdgeInsetsMake(0, 15, -8, 15);
  332. _segmentedControl.selectionIndicatorEdgeInsets = UIEdgeInsetsMake(0, 15, -8, 15);
  333. _segmentedControl.selectionIndicatorColor = UIColorFromRGB(0xff6362);
  334. _segmentedControl.selectionStyle = HMSegmentedControlSelectionStyleTextWidthStripe;
  335. _segmentedControl.selectedTitleTextAttributes = @{NSForegroundColorAttributeName : [UIColor blackColor],
  336. NSFontAttributeName : [UIFont systemFontOfSize:15]};
  337. _segmentedControl.titleTextAttributes = @{NSForegroundColorAttributeName : [UIColor grayColor],
  338. NSFontAttributeName : [UIFont systemFontOfSize:15]};
  339. _segmentedControl.type = HMSegmentedControlTypeText;
  340. _segmentedControl.segmentWidthStyle = HMSegmentedControlSegmentWidthStyleDynamic;
  341. WeakSelf(weakSelf);
  342. _segmentedControl.indexChangeBlock = ^(NSInteger index) {
  343. [weakSelf.tableView reloadData];
  344. };
  345. }
  346. return _segmentedControl;
  347. }
  348. - (LogisticsViewModel *)viewModel {
  349. if (!_viewModel) {
  350. _viewModel = [[LogisticsViewModel alloc] init];
  351. }
  352. return _viewModel;
  353. }
  354. - (UITableView *)tableView {
  355. if (!_tableView) {
  356. _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
  357. _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  358. _tableView.backgroundColor = UIColorFromRGB(0xffffff);
  359. _tableView.dataSource = self;
  360. _tableView.delegate = self;
  361. [_tableView registerClass:[FKOrderDetailProductCell class] forCellReuseIdentifier:FKOrderDetailProductCellIdentifier];
  362. [_tableView registerClass:[LogisticsProgressCell class] forCellReuseIdentifier:LogisticsProgressCellIdentifier];
  363. [_tableView registerClass:[LogisticsSuccessCell class] forCellReuseIdentifier:LogisticsSuccessCellIdentifier];
  364. [_tableView registerClass:[LogisticsQuestionCell class] forCellReuseIdentifier:LogisticsQuestionCellIdentifier];
  365. [_tableView registerClass:[LogisticsAnswerCell class] forCellReuseIdentifier:LogisticsAnswerCellIdentifier];
  366. [_tableView registerClass:[LogisticsTimeOutCell class] forCellReuseIdentifier:LogisticsTimeOutCellIdentifier];
  367. if (@available(iOS 11.0, *)) {
  368. _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  369. }
  370. }
  371. return _tableView;
  372. }
  373. @end