暂无描述

FKPersonOrderController.m 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. //
  2. // FKAllOrderController.m
  3. // FirstLink
  4. //
  5. // Created by ascii on 15/5/29.
  6. // Copyright (c) 2015年 FirstLink. All rights reserved.
  7. //
  8. #import "FKPersonOrderController.h"
  9. #import "FKShareListController.h"
  10. #import "WebViewController.h"
  11. #import "FKPersonOrderRequest.h"
  12. #import "FKPersonOrderReform.h"
  13. #import "SubmitPaymentItem.h"
  14. #import "FKOrderPaymentItem.h"
  15. #import "FKShareTargetItem.h"
  16. #import "FKOrderSupplierItem.h"
  17. #import "FKOrderGoodItem.h"
  18. #import "FKPersonOrderStatusCell.h"
  19. #import "FKPersonOrderCountryCell.h"
  20. #import "FKPersonOrderProductCell.h"
  21. #import "FKPersonOrderSummaryCell.h"
  22. #import "UIExtendAlertView.h"
  23. #import "FKShareView.h"
  24. #import "FLStringHelper.h"
  25. #import "FLThirdPayHelper.h"
  26. #import "FLShareHelper.h"
  27. #import "FLImageHelper.h"
  28. #import "FLControllerHelper.h"
  29. #import <AFNetworking/UIImageView+AFNetworking.h>
  30. #import "FKShareListRequest.h"
  31. #import "FKShareListReform.h"
  32. @interface FKPersonOrderController ()
  33. <UITableViewDelegate, UITableViewDataSource, UIAlertViewDelegate, FLNetworkDelegate>
  34. @property (nonatomic, weak ) id<FKPersonOrderControllerDelegate> delegate;
  35. @property (nonatomic, assign) FKPersonOrderClassify classify;
  36. @property (nonatomic, strong) FKPersonOrderViewModel *viewModel;
  37. @property (nonatomic, strong) UITableView *tableView;
  38. @property (nonatomic, assign) NSInteger shareTargetIndex;
  39. @end
  40. static NSString * const FKPersonOrderCancelAction = @"FKPersonOrderCancelAction";
  41. static NSString * const FKPersonOrderDeletelAction = @"FKPersonOrderDeletelAction";
  42. @implementation FKPersonOrderController
  43. - (instancetype)initWithDelegate:(id<FKPersonOrderControllerDelegate>)delegate
  44. orderClassify:(FKPersonOrderClassify)classify
  45. viewModel:(FKPersonOrderViewModel *)viewModel {
  46. self = [super init];
  47. if (self) {
  48. _delegate = delegate;
  49. _classify = classify;
  50. _viewModel = viewModel;
  51. [self.view addSubview:self.tableView];
  52. [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
  53. make.left.right.top.bottom.equalTo(self.view);
  54. }];
  55. }
  56. return self;
  57. }
  58. - (void)viewDidLoad {
  59. [super viewDidLoad];
  60. // Do any additional setup after loading the view from its nib.
  61. [self configTableView];
  62. }
  63. - (void)viewWillAppear:(BOOL)animated {
  64. [super viewWillAppear:animated];
  65. [self requestTotalCountOfPage];
  66. }
  67. - (void)didReceiveMemoryWarning {
  68. [super didReceiveMemoryWarning];
  69. // Dispose of any resources that can be recreated.
  70. }
  71. #pragma mark - Request
  72. - (void)refreshControl:(FKRefreshControl *)refreshControl didEngageRefreshDirection:(RefreshDirection)direction {
  73. // [self.hudView show:YES];
  74. if (direction==RefreshDirectionTop) {
  75. [FKPersonOrderRequest requestItems:FKPersonOrderRequestNew
  76. startRow:@"0"
  77. pageSize:[self.viewModel getPageRowCount:self.classify]
  78. keyword:self.viewModel.searchKeyword
  79. tradeStatus:[self.viewModel getRequestTradeStatus:self.classify]
  80. deleagate:self];
  81. } else if (direction==RefreshDirectionBottom) {
  82. [FKPersonOrderRequest requestItems:FKPersonOrderRequestNextPage
  83. startRow:[self.viewModel getStartRow:self.classify]
  84. pageSize:[self.viewModel getPageRowCount:self.classify]
  85. keyword:self.viewModel.searchKeyword
  86. tradeStatus:[self.viewModel getRequestTradeStatus:self.classify]
  87. deleagate:self];
  88. }
  89. }
  90. - (void)requestTotalCountOfPage {
  91. [self.hudView show:YES];
  92. [FKPersonOrderRequest requestItems:FKPersonOrderRequestNew
  93. startRow:@"0"
  94. pageSize:[self.viewModel getMaxRowCount:self.classify]
  95. keyword:self.viewModel.searchKeyword
  96. tradeStatus:[self.viewModel getRequestTradeStatus:self.classify]
  97. deleagate:self];
  98. }
  99. /*
  100. #pragma mark - Navigation
  101. // In a storyboard-based application, you will often want to do a little preparation before navigation
  102. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  103. // Get the new view controller using [segue destinationViewController].
  104. // Pass the selected object to the new view controller.
  105. }
  106. */
  107. #pragma mark - Response
  108. - (void)networkDidSuccessResponse:(NSDictionary *)response identify:(int)identify header:(MSGHeader *)header {
  109. WeakSelf(weakSelf);
  110. dispatch_async(dispatch_get_main_queue(), ^{
  111. [weakSelf.hudView hide:YES];
  112. [weakSelf.hudView removeFromSuperview];
  113. weakSelf.hudView = nil;
  114. });
  115. [self finishLoadingData];
  116. [self.statusView removeFromSuperview];
  117. if ([header.code intValue] == RESPONSE_MSG_NORMAL) {
  118. if (identify == FKPersonOrderRequestShareSingle){
  119. // 分享
  120. FKShareTargetItem *item = [FKShareListReform parserShareListWithDict:response].firstObject;
  121. if (item){
  122. FKShareView *shareView = [[FKShareView alloc]init];
  123. [shareView showInView:self.view.window actionBlock:^(kShareViewActionType type, FKShareView *shareView) {
  124. [weakSelf shareSingleGoodWithItem:item type:type shareView:shareView];
  125. }];
  126. }
  127. return;
  128. }
  129. NSArray *orders = [FKPersonOrderReform parseOrderItems:response];
  130. if (identify == FKPersonOrderRequestNew) {
  131. [self.viewModel resetPersonOrders:orders classify:_classify];
  132. }
  133. if (identify == FKPersonOrderRequestNextPage) {
  134. [self.viewModel appendPersonOrders:orders classify:_classify];
  135. }
  136. if (identify == FKPersonOrderRequestCancelOrder || identify == FKPersonOrderRequestDeleteOrder) {
  137. [self requestTotalCountOfPage];
  138. if ([self.delegate respondsToSelector:@selector(orderControllerDidNeedRefreshBadge)]) {
  139. [self.delegate orderControllerDidNeedRefreshBadge];
  140. }
  141. return;
  142. }
  143. [self.tableView reloadData];
  144. // 显示无内容提示
  145. if ([self.viewModel numberOfSectionsInTableView:_classify] == 0) {
  146. [self showStatusTipInView:self.tableView image:[UIImage imageNamed:@"StatusNoOrderIcon"] title:@"没有发现订单哦"];
  147. self.refreshControl.bottomEnabled = NO;
  148. } else {
  149. [self.statusView removeFromSuperview];
  150. }
  151. self.refreshControl.bottomEnabled = orders.count > 0 ? YES : NO;
  152. } else {
  153. [FLProgressHUDHelper showText:header.msg inView:self.view];
  154. }
  155. }
  156. - (void)networkDidReceiveError:(NSError *)error identify:(int)identify header:(MSGHeader *)header {
  157. WeakSelf(weakSelf);
  158. dispatch_async(dispatch_get_main_queue(), ^{
  159. [weakSelf.hudView hide:YES];
  160. [weakSelf.hudView removeFromSuperview];
  161. weakSelf.hudView = nil;
  162. });
  163. [self finishLoadingData];
  164. [FLProgressHUDHelper showText:header.msg inView:self.view];
  165. }
  166. #pragma mark - Table View Delegate
  167. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  168. return [self.viewModel numberOfSectionsInTableView:_classify];
  169. }
  170. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  171. return [self.viewModel numberOfRowsInSection:section classify:_classify];
  172. }
  173. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  174. return 10;
  175. }
  176. - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
  177. return CGFLOAT_MIN;
  178. }
  179. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  180. return [self.viewModel heightAtIndexPath:indexPath classify:_classify];
  181. }
  182. - (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  183. NSString *identify = [self.viewModel cellIdentifyAtIndexPath:indexPath classify:_classify];
  184. UITableViewCell<FKPersonOrderCellDelegate> *cell = [tableView dequeueReusableCellWithIdentifier:identify];
  185. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  186. if ([cell respondsToSelector:@selector(configPersonOrderCell:classify:viewModel:)]) {
  187. [cell configPersonOrderCell:indexPath classify:_classify viewModel:self.viewModel];
  188. }
  189. if ([cell isKindOfClass:[FKPersonOrderSummaryCell class]]) {
  190. [self configCellAction:(FKPersonOrderSummaryCell*)cell indexPath:indexPath];
  191. }
  192. return cell;
  193. }
  194. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  195. FKOrderPaymentItem *item = [self.viewModel paymentItemAtIndex:indexPath.section classify:_classify];
  196. if (item&& self.delegate && [self.delegate respondsToSelector:@selector(orderController:didClickOrderDetailAction:orderType:)]) {
  197. [self.delegate orderController:self didClickOrderDetailAction:item.paymentID orderType:kOrderTypeProduct];
  198. }
  199. }
  200. #pragma mark - Alert View Delegate
  201. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
  202. if (buttonIndex == 1) {
  203. if ([alertView isKindOfClass:[UIExtendAlertView class]]) {
  204. UIExtendAlertView *exAlertView = (UIExtendAlertView*)alertView;
  205. NSInteger index = alertView.tag;
  206. FKOrderPaymentItem *item = [self.viewModel paymentItemAtIndex:index classify:_classify];
  207. if (item) {
  208. if ([exAlertView.userInfo[@"type"] isEqualToString:FKPersonOrderCancelAction]) {
  209. [FKPersonOrderRequest requestCancelOrder:FKPersonOrderRequestCancelOrder
  210. orderID:item.paymentID
  211. deleagate:self];
  212. }
  213. if ([exAlertView.userInfo[@"type"] isEqualToString:FKPersonOrderDeletelAction]) {
  214. [FKPersonOrderRequest requestDeleteOrder:FKPersonOrderRequestDeleteOrder
  215. orderID:item.paymentID
  216. deleagate:self];
  217. }
  218. }
  219. }
  220. }
  221. }
  222. #pragma mark - Action
  223. - (IBAction)clickCancelOrderAction:(UIButton*)sender {
  224. NSInteger index = sender.tag;
  225. FKOrderPaymentItem *item = [self.viewModel paymentItemAtIndex:index classify:_classify];
  226. if (item) {
  227. UIExtendAlertView *alert = [[UIExtendAlertView alloc] initWithTitle:nil
  228. message:@"确认取消该订单?"
  229. delegate:self
  230. cancelButtonTitle:@"否"
  231. otherButtonTitles:@"是", nil];
  232. alert.delegate = self;
  233. alert.userInfo = @{@"type": FKPersonOrderCancelAction};
  234. alert.tag = index;
  235. [alert show];
  236. }
  237. }
  238. - (IBAction)clickDeleteOrderAction:(UIButton*)sender {
  239. NSInteger index = sender.tag;
  240. FKOrderPaymentItem *item = [self.viewModel paymentItemAtIndex:index classify:_classify];
  241. if (item) {
  242. UIExtendAlertView *alert = [[UIExtendAlertView alloc] initWithTitle:nil
  243. message:@"确认删除该订单?"
  244. delegate:self
  245. cancelButtonTitle:@"否"
  246. otherButtonTitles:@"是", nil];
  247. alert.delegate = self;
  248. alert.userInfo = @{@"type": FKPersonOrderDeletelAction};
  249. alert.tag = index;
  250. [alert show];
  251. }
  252. }
  253. - (IBAction)clickPaymentAction:(UIButton*)sender {
  254. FKOrderPaymentItem *item = [self.viewModel paymentItemAtIndex:sender.tag classify:_classify];
  255. [self pushOrderPayController:item];
  256. }
  257. - (void)pushOrderPayController:(FKOrderPaymentItem*)item {
  258. if (item) {
  259. SubmitPaymentItem *paymentItem = [SubmitPaymentItem new];
  260. paymentItem.orderID = item.paymentID;
  261. paymentItem.successController = FKOrderPaymentSuccessControllerRedpacket;
  262. if ([self.delegate respondsToSelector:@selector(orderController:didClickPaymentAction:orderType:)]) {
  263. [self.delegate orderController:self didClickPaymentAction:paymentItem orderType:kOrderTypeProduct];
  264. }
  265. }
  266. }
  267. - (void)pushWebViewControllerWithUrl:(NSString *)url {
  268. WebViewController *newViewController = [[FLControllerHelper currentStoryBoard] instantiateViewControllerWithIdentifier:@"WebViewController"];
  269. newViewController.url = url;
  270. newViewController.hidesBottomBarWhenPushed = YES;
  271. [self pushController:newViewController];
  272. }
  273. - (void)clickUploadIDCardAction:(UIButton *)sender {
  274. FKOrderPaymentItem *item = [self.viewModel paymentItemAtIndex:sender.tag classify:_classify];
  275. if (item.paymentID.length > 0) {
  276. if ([self.delegate respondsToSelector:@selector(orderController:didClickUploadIDCardAction:)]) {
  277. [self.delegate orderController:self didClickUploadIDCardAction:item.paymentID];
  278. }
  279. }
  280. }
  281. - (void)clickShareAction:(UIButton *)sender{
  282. FKOrderPaymentItem *item = [self.viewModel paymentItemAtIndex:sender.tag classify:_classify];
  283. self.shareTargetIndex = sender.tag;
  284. if ([item hasOnlyOneGood]){
  285. // 直接弹出分享
  286. [self.hudView show:YES];
  287. [FKShareListRequest requestShareList:FKPersonOrderRequestShareSingle orderID:item.paymentID deleagate:self];
  288. }else{
  289. FKShareListController *controller = [[FKShareListController alloc]init];
  290. controller.orderId = item.paymentID;
  291. [self pushController:controller];
  292. }
  293. }
  294. #pragma mark - Method
  295. - (void)pushController:(UIViewController *)controller{
  296. if (!controller) return;
  297. if ([self.delegate respondsToSelector:@selector(orderController:pushController:)]) {
  298. [self.delegate orderController:self pushController:controller];
  299. }
  300. }
  301. - (void)shareSingleGoodWithItem:(FKShareTargetItem *)item
  302. type:(kShareViewActionType)type
  303. shareView:(FKShareView *)shareView{
  304. switch (type) {
  305. case kShareViewActionTypeQuestion:{
  306. NSString *qsURL = [NSString stringWithFormat:@"%@/support/about-points.html", [[FKServerUtil sharedInstance] webServer]];
  307. WeakSelf(weakSelf);
  308. [shareView dismissWithCompletion:^(BOOL finish) {
  309. [weakSelf pushWebViewControllerWithUrl:qsURL];
  310. }];
  311. }
  312. break;
  313. case kShareViewActionTypeFriend:{
  314. UIImage *image = [self getShareImgWithSize:CGSizeMake(SHARE_IMAGE_LENGTH, SHARE_IMAGE_LENGTH)];
  315. [FLShareHelper shareToWeixin:item.shareDesc
  316. description:item.title
  317. thumbImage:image
  318. webURL:item.shareUrl
  319. scene:WeixinSceneFriend];
  320. }
  321. break;
  322. case kShareViewActionTypeCircle:{
  323. UIImage *image = [self getShareImgWithSize:CGSizeMake(SHARE_IMAGE_LENGTH, SHARE_IMAGE_LENGTH)];
  324. [FLShareHelper shareToWeixin:item.shareDesc
  325. description:item.title
  326. thumbImage:image
  327. webURL:item.shareUrl
  328. scene:WeixinSceneCicle];
  329. }
  330. break;
  331. case kShareViewActionTypeWeibo:{
  332. UIImage *image = [self getShareImgWithSize:CGSizeMake(SHARE_IMAGE_LENGTH_WEIBO, SHARE_IMAGE_LENGTH_WEIBO)];
  333. [FLShareHelper shareToWeiboWithTitle:item.shareDesc
  334. content:item.title
  335. image:image
  336. urlString:item.shareUrl
  337. tagID:item.itemID];
  338. }
  339. break;
  340. case kShareViewActionTypeQQzone:{
  341. [FLShareHelper shareToQQZoneWithTitle:item.title
  342. content:item.shareDesc
  343. imgUrl:item.picUrl
  344. urlString:item.shareUrl];
  345. }
  346. break;
  347. default:
  348. break;
  349. }
  350. }
  351. - (UIImage *)getShareImgWithSize:(CGSize)size{
  352. UIImage *image = nil;
  353. if (!image){
  354. for (FKPersonOrderProductCell *proCell in self.tableView.visibleCells) {
  355. if ([proCell isKindOfClass:[FKPersonOrderProductCell class]] && proCell.tag == self.shareTargetIndex){
  356. image = proCell.productImgView.image;
  357. break;
  358. }
  359. }
  360. }
  361. if (image) {
  362. image = [FLImageHelper generateThumbnailFromCenter:image frame:size];
  363. } else {
  364. image = [FLImageHelper generateThumbnailFromCenter:[FLImageHelper getAppIcon] frame:size];
  365. }
  366. return image;
  367. }
  368. - (void)configCellAction:(FKPersonOrderSummaryCell *)cell indexPath:(NSIndexPath *)indexPath {
  369. cell.cancelButton.tag = indexPath.section;
  370. cell.paymentButton.tag = indexPath.section;
  371. cell.deleteButton.tag = indexPath.section;
  372. cell.uploadIDCardButton.tag = indexPath.section;
  373. cell.shareButton.tag = indexPath.section;
  374. [cell.cancelButton addTarget:self action:@selector(clickCancelOrderAction:) forControlEvents:UIControlEventTouchUpInside];
  375. [cell.paymentButton addTarget:self action:@selector(clickPaymentAction:) forControlEvents:UIControlEventTouchUpInside];
  376. [cell.deleteButton addTarget:self action:@selector(clickDeleteOrderAction:) forControlEvents:UIControlEventTouchUpInside];
  377. [cell.uploadIDCardButton addTarget:self action:@selector(clickUploadIDCardAction:) forControlEvents:UIControlEventTouchUpInside];
  378. [cell.shareButton addTarget:self action:@selector(clickShareAction:) forControlEvents:UIControlEventTouchUpInside];
  379. }
  380. - (void)configTableView {
  381. [self initRefreshControlWithTableView:self.tableView];
  382. [self.tableView registerClass:[FKPersonOrderStatusCell class] forCellReuseIdentifier:FKPersonOrderStatusCellIdentify];
  383. [self.tableView registerClass:[FKPersonOrderCountryCell class] forCellReuseIdentifier:FKPersonOrderCountryCellIdentify];
  384. [self.tableView registerClass:[FKPersonOrderProductCell class] forCellReuseIdentifier:FKPersonOrderProductCellIdentify];
  385. [self.tableView registerClass:[FKPersonOrderSummaryCell class] forCellReuseIdentifier:FKPersonOrderSummaryCellIdentify];
  386. self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  387. }
  388. #pragma mark - property
  389. - (UITableView *)tableView {
  390. if (!_tableView) {
  391. _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
  392. _tableView.dataSource = self;
  393. _tableView.delegate = self;
  394. _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  395. _tableView.backgroundColor = UIColorFromRGB(0xf4f4f4);
  396. }
  397. return _tableView;
  398. }
  399. @end