SPSBDiscountCouponTableViewController.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. //
  2. // SPSBDiscountCouponTableViewController.m
  3. // 我的社保
  4. //
  5. // Created by shanp on 2021/5/24.
  6. //
  7. #import "SPSBDiscountCouponTableViewController.h"
  8. #import "SPSBUIGeneralHeader.h"
  9. #import "SPSBDiscountCouponModel.h"
  10. #import "UIViewController+SPSBLoadDataStatus.h"
  11. #import "UIScrollView+SPSBAutoLoadData.h"
  12. #import "UIViewController+SPSBNetworkManager.h"
  13. #import "SPSBKeyProfile.h"
  14. #import "UITableView+SPSBDefault.h"
  15. #import <CALayer+JXHShadow.h>
  16. #import "SPSBRouteManager.h"
  17. @interface SPSBDiscountCouponTableViewCell () {
  18. SPSBDiscountCouponViewType _viewType;
  19. UIImageView *_amountView;
  20. UILabel *_amountLabel;
  21. UILabel *_titleLabel;
  22. UILabel *_description;
  23. UILabel *_outDateLabel;
  24. UILabel *_expiryDateLabel;
  25. UIImageView *_chooseView;
  26. UIButton *_useButton;
  27. }
  28. @end
  29. @implementation SPSBDiscountCouponTableViewCell
  30. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier viewType:(SPSBDiscountCouponViewType)viewType {
  31. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  32. if (!self) return nil;
  33. _viewType = viewType;
  34. self.selectionStyle = UITableViewCellSelectionStyleNone;
  35. UIView *contentView = UIView.new;
  36. contentView.backgroundColor = spsb_FFFFFF_color(1.f);
  37. [contentView.layer setShadowWithOffset:(CGSize){0,2} alpha:0.06 blur:4 spread:0 color:spsb_000000_color(1.f) cornerRadius:4 pathRect:(CGRect){0, 0, jxh_screenWidth() - 30, 100}];
  38. [self.contentView addSubview:contentView];
  39. [contentView makeConstraints:^(JXHConstraintMaker *make) {
  40. make.top.equalTo(0);
  41. make.leading.equalTo(15);
  42. make.height.equalTo(100);
  43. make.trailing.equalTo(-15);
  44. }];
  45. _amountView = UIImageView.new;
  46. _amountView.image = jxh_getImage(discount_coupon_left_unuse);
  47. _amountView.highlightedImage = jxh_getImage(discount_coupon_left_used);
  48. [contentView addSubview:_amountView];
  49. [_amountView makeConstraints:^(JXHConstraintMaker *make) {
  50. make.leading.and.top.equalTo(0);
  51. }];
  52. UIView *amountView = [[UIView alloc] init];
  53. [_amountView addSubview:amountView];
  54. [amountView makeConstraints:^(JXHConstraintMaker *make) {
  55. make.top.equalTo(20);
  56. make.centerX.equalTo(self->_amountView);
  57. }];
  58. UILabel *symbol = [UILabel convenienceWithFont:spsb_font(17) text:@"¥" textColor:spsb_FFFFFF_color(1.f)];
  59. [amountView addSubview:symbol];
  60. [symbol makeConstraints:^(JXHConstraintMaker *make) {
  61. make.leading.equalTo(0);
  62. make.bottom.equalTo(-5);
  63. }];
  64. _amountLabel = [UILabel convenienceWithFont:spsb_semiboldFont(40) text:@"" textColor:spsb_FFFFFF_color(1.f)];
  65. [amountView addSubview:_amountLabel];
  66. [_amountLabel makeConstraints:^(JXHConstraintMaker *make) {
  67. make.trailing.and.top.and.bottom.equalTo(0);
  68. make.leading.equalTo(symbol.trailing).offset(2);
  69. make.height.equalTo(40);
  70. }];
  71. _outDateLabel = [UILabel convenienceWithFont:spsb_font(10) text:nil textColor:spsb_FFFFFF_color(1.f)];
  72. [_amountView addSubview:_outDateLabel];
  73. [_outDateLabel makeConstraints:^(JXHConstraintMaker *make) {
  74. make.top.equalTo(70);
  75. make.centerX.equalTo(amountView);
  76. }];
  77. _titleLabel = [UILabel convenienceWithFont:spsb_font(15) text:@"" textColor:spsb_666666_color()];
  78. [contentView addSubview:_titleLabel];
  79. [_titleLabel makeConstraints:^(JXHConstraintMaker *make) {
  80. make.leading.equalTo(125);
  81. make.top.equalTo(20);
  82. make.trailing.lessThanOrEqualTo(-45);
  83. }];
  84. _description = [UILabel convenienceWithFont:spsb_font(12) text:@"" textColor:spsb_999999_color()];
  85. [contentView addSubview:_description];
  86. [_description makeConstraints:^(JXHConstraintMaker *make) {
  87. make.leading.equalTo(self->_titleLabel);
  88. make.top.equalTo(self->_titleLabel.bottom).offset(8);
  89. make.trailing.lessThanOrEqualTo(-45);
  90. }];
  91. _expiryDateLabel = [UILabel convenienceWithFont:spsb_font(12) text:@"" textColor:spsb_999999_color()];
  92. [contentView addSubview:_expiryDateLabel];
  93. [_expiryDateLabel makeConstraints:^(JXHConstraintMaker *make) {
  94. make.leading.equalTo(self->_titleLabel);
  95. make.top.equalTo(self->_description.bottom).offset(8);
  96. }];
  97. if (viewType == SPSBDiscountCouponViewTypeUse) {
  98. _chooseView = UIImageView.new;
  99. _chooseView.image = jxh_getImage(filter_n);
  100. _chooseView.highlightedImage = jxh_getImage(filter_s);
  101. [contentView addSubview:_chooseView];
  102. [_chooseView makeConstraints:^(JXHConstraintMaker *make) {
  103. make.trailing.equalTo(-15);
  104. make.centerY.equalTo(0);
  105. }];
  106. } else {
  107. _useButton = [UIButton convenienceWithTarget:self action:@selector(useAction)];
  108. [_useButton setImage:jxh_getImage(cash_coupon_right) state:JXHButtonControlStateNormal | JXHButtonControlStateHighlighted];
  109. [contentView addSubview:_useButton];
  110. [_useButton makeConstraints:^(JXHConstraintMaker *make) {
  111. make.trailing.and.top.equalTo(0);
  112. }];
  113. }
  114. return self;
  115. }
  116. - (void)reloadData:(SPSBDiscountCouponModel *)data chose:(bool)chose {
  117. _amountLabel.text = data.spsb_coupon_price;
  118. _description.text = [NSString stringWithFormat:@"说明:%@", data.spsb_coupon_desc];
  119. _titleLabel.text = data.spsb_coupon_name;
  120. _expiryDateLabel.text = [NSString stringWithFormat:@"有效期:%@", data.spsb_end_time];
  121. if (_viewType == SPSBDiscountCouponViewTypeUse) {
  122. _chooseView.highlighted = chose;
  123. } else {
  124. _useButton.hidden = !data.spsba_canUse;
  125. }
  126. _amountView.highlighted = !data.spsba_canUse;
  127. _outDateLabel.text = data.spsba_status;
  128. }
  129. - (void)useAction {
  130. if (_spsb_useAction) {
  131. _spsb_useAction(self);
  132. }
  133. }
  134. @end
  135. @interface SPSBDiscountCouponTableViewController () {
  136. NSMutableArray<SPSBDiscountCouponModel *> *_dataArray;
  137. NSUInteger _currentPage;
  138. NSMutableSet *_chose;
  139. }
  140. @end
  141. @implementation SPSBDiscountCouponTableViewController
  142. - (instancetype)init {
  143. self = [super initWithStyle:UITableViewStyleGrouped];
  144. if (!self) return nil;
  145. return self;
  146. }
  147. - (void)viewDidLoad {
  148. [super viewDidLoad];
  149. [self initData];
  150. [self setupTableView];
  151. }
  152. - (void)initData {
  153. _currentPage = 0;
  154. if (_spsb_viewType == SPSBDiscountCouponViewTypeUse) {
  155. _chose = NSMutableSet.new;
  156. }
  157. }
  158. - (void)viewWillAppear:(BOOL)animated {
  159. [super viewWillAppear:animated];
  160. if (!_dataArray) {
  161. [self getDataList];
  162. }
  163. }
  164. #pragma mark - Action
  165. - (void)useAction:(SPSBDiscountCouponTableViewCell *)cell {
  166. SPSBRouter *router = [[SPSBRouter alloc] initWithClassName:@"" property:nil tabBarType:SPSBTabBarTypeHome];
  167. [SPSBRouteManager navigationBackAndRouteTo:router];
  168. }
  169. - (NSArray<SPSBDiscountCouponModel *> *)spsb_data {
  170. return _dataArray;
  171. }
  172. - (NSSet *)spsb_chose {
  173. return _chose;
  174. }
  175. #pragma mark - Network Action
  176. - (void)getDataList {
  177. self.tableView.spsb_canLoadData = false;
  178. @weakify(self)
  179. NSInteger page = _currentPage + 1;
  180. [self networkUseMethod:SPSBNetworkMethodPOST loadingTips:!_dataArray ? @"加载中" : nil isLogin:true url:spsb_appUrl(SPSBUrlSearchCashCoupon) urlParameters:nil parameters:^NSDictionary * _Nonnull{
  181. return @{@"phone": [jxh_userDefaults() objectForKey:SPSBUserName], @"page": @(page), @"expired": @(!self->_spsb_canUse)};
  182. } success:^NSString * _Nullable(NSError * _Nonnull error, id _Nullable data) {
  183. @strongify(self)
  184. [self getDataListSuccess:data];
  185. return @"";
  186. } failure:^NSString * _Nullable(NSError * _Nonnull error, id _Nullable data) {
  187. @strongify(self)
  188. [self getDataListFailure:error];
  189. return !self->_dataArray ? @"加载失败" : nil;
  190. }];
  191. }
  192. - (void)getDataListSuccess:(id)data {
  193. if (!_dataArray) {
  194. _dataArray = NSMutableArray.new;
  195. }
  196. _currentPage ++;
  197. NSArray *array = [SPSBDiscountCouponModel getModelListWithArray:data[@"data"][@"list"]];
  198. [_dataArray addObjectsFromArray:array];
  199. [self.tableView reloadData];
  200. if (_dataArray.count == 0) {
  201. [self.tableView setDefaultFootViewWithError:nil image:jxh_getImage(youhuijuan_qs_f) height:jxh_viewHeight(self.tableView) action:^{
  202. }];
  203. } else {
  204. [self.tableView removeDefaultFootView];
  205. }
  206. if (array.count < 10) {
  207. self.tableView.spsb_canLoadData = false;
  208. } else {
  209. self.tableView.spsb_canLoadData = true;
  210. }
  211. [self.tableView endAutoLoadData];
  212. }
  213. - (void)getDataListFailure:(NSError *)error {
  214. if (_dataArray.count == 0) {
  215. @weakify(self)
  216. [self.tableView setDefaultFootViewWithError:error image:nil height:jxh_viewHeight(self.tableView) - 15 action:^{
  217. @strongify(self)
  218. [self getDataList];
  219. }];
  220. self.tableView.spsb_canLoadData = false;
  221. } else {
  222. self.tableView.spsb_canLoadData = true;
  223. }
  224. }
  225. #pragma mark - Delegate & DataSource
  226. static NSString * const reuseIdentifier = @"SPSBDiscountCouponTableViewCell";
  227. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  228. SPSBDiscountCouponTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
  229. if (!cell) {
  230. cell = [[SPSBDiscountCouponTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier viewType:_spsb_viewType];
  231. if (_spsb_viewType == SPSBDiscountCouponViewTypeDisplay) {
  232. @weakify(self)
  233. cell.spsb_useAction = ^(SPSBDiscountCouponTableViewCell *cell) {
  234. @strongify(self)
  235. [self useAction:cell];
  236. };
  237. }
  238. }
  239. if (_spsb_viewType == SPSBDiscountCouponViewTypeDisplay) {
  240. [cell reloadData:_dataArray[indexPath.row] chose:false];
  241. return cell;
  242. }
  243. [cell reloadData:_dataArray[indexPath.row] chose:[_chose containsObject:@(indexPath.row)]];
  244. return cell;
  245. }
  246. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  247. return _dataArray.count;
  248. }
  249. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  250. [tableView deselectRowAtIndexPath:indexPath animated:false];
  251. if (_spsb_viewType == SPSBDiscountCouponViewTypeUse) {
  252. SPSBDiscountCouponModel *model = _dataArray[indexPath.row];
  253. if ([model.spsb_status integerValue] == 1 && [model.spsb_expired integerValue] == 0) {
  254. if ([_chose containsObject:@(indexPath.row)]) {
  255. [_chose removeObject:@(indexPath.row)];
  256. [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
  257. if (_spsb_afterChoose) {
  258. _spsb_afterChoose();
  259. }
  260. return;
  261. }
  262. if (_chose.count >= _spsb_maxChose) {
  263. NSString *title = [NSString stringWithFormat:@"本次购买只可选择%ld张抵用券", (long)_spsb_maxChose];
  264. spsb_singleAlertShow(title, self);
  265. return;
  266. }
  267. [_chose addObject:@(indexPath.row)];
  268. [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
  269. if (_spsb_afterChoose) {
  270. _spsb_afterChoose();
  271. }
  272. }
  273. }
  274. }
  275. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  276. return 15;
  277. }
  278. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  279. return UIView.new;
  280. }
  281. - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
  282. return jxh_onePixe();
  283. }
  284. - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
  285. return UIView.new;
  286. }
  287. #pragma mark - UI
  288. - (void)setupTableView {
  289. self.tableView.backgroundColor = spsb_FFFFFF_color(1.f);
  290. self.tableView.rowHeight = 114.f;
  291. self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  292. self.tableView.showsHorizontalScrollIndicator = false;
  293. self.tableView.showsVerticalScrollIndicator = false;
  294. self.tableView.alwaysBounceVertical = true;
  295. @weakify(self)
  296. [self.tableView setAutoLoadDataWithAction:^{
  297. @strongify(self)
  298. [self getDataList];
  299. }];
  300. }
  301. @end