ShopGoodsUIView.m 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. //
  2. // ShopGoodsUIView.m
  3. // UIAuctionShop
  4. //
  5. // Created by 王珂 on 16/9/18.
  6. // Copyright © 2016年 qhy. All rights reserved.
  7. //
  8. #import "ShopGoodsUIView.h"
  9. #import "ShopGoodsTableViewCell.h"
  10. #import "ShopGoodsModel.h"
  11. @interface ShopGoodsUIView()<UITableViewDelegate,UITableViewDataSource,ShopGoodsTableViewCellDelegate>
  12. @property (nonatomic, strong) UITableView *shopGoodsTable;
  13. //进入主播星店的按钮
  14. @property (nonatomic, strong) UIButton *starShopBtn;
  15. //购物车按钮
  16. @property (nonatomic, strong) UIButton *shoppingBtn;
  17. //购物数据
  18. @property (nonatomic, copy) NSString *starShopUrl;//星店url地址
  19. //购物数据
  20. @property (nonatomic, strong) NSMutableArray*shopGoodsArray;
  21. //@property (nonatomic, strong) UILabel *noGoodsLabel;
  22. @property (nonatomic, assign) int has_next;
  23. @property (nonatomic, assign) int currentPage;
  24. @end
  25. @implementation ShopGoodsUIView
  26. - (instancetype)initWithFrame:(CGRect)frame
  27. {
  28. if (self = [super initWithFrame:frame])
  29. {
  30. if (_shopGoodsTable == nil)
  31. {
  32. _shopGoodsTable =[[UITableView alloc] init];
  33. _shopGoodsTable.delegate = self;
  34. _shopGoodsTable.dataSource = self;
  35. _shopGoodsTable.separatorStyle = UITableViewCellSeparatorStyleNone;
  36. _shopGoodsTable.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
  37. [self addSubview:_shopGoodsTable];
  38. }
  39. if (_starShopBtn== nil)
  40. {
  41. _starShopBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  42. [self addSubview:_starShopBtn];
  43. }
  44. if (_shopGoodsArray == nil)
  45. {
  46. _shopGoodsArray = [NSMutableArray array];
  47. }
  48. _currentPage = 1;
  49. [self createMJRefresh];
  50. }
  51. return self;
  52. }
  53. - (void)starShopBtn:(UIButton *)starShopButton
  54. {
  55. if (_delegate && [_delegate respondsToSelector:@selector(closeShopGoodsViewWithDic: andIsOTOShop:)])
  56. {
  57. NSString * url = _starShopUrl.length > 0 ? _starShopUrl : @"";
  58. [_delegate closeShopGoodsViewWithDic:@{@"url":url} andIsOTOShop:_isOTOShop];
  59. }
  60. }
  61. - (void)loadDataWithPage:(int)page
  62. {
  63. _shopGoodsTable.frame = CGRectMake(0, 0, kScreenW, kShoppingHeight-40) ;
  64. _starShopBtn.frame = CGRectMake(0, kShoppingHeight-40, kScreenW, 40);
  65. _starShopBtn.backgroundColor = kAppMainColor;
  66. if (_type==0)
  67. {
  68. if (kSupportH5Shopping || _isOTOShop)
  69. {
  70. _shopGoodsTable.frame = CGRectMake(0, 0, kScreenW, kShoppingHeight) ;
  71. _starShopBtn.hidden = YES;
  72. }
  73. else
  74. {
  75. [_starShopBtn setTitle:ASLocalizedString(@"星店商品管理")forState:UIControlStateNormal];
  76. [_starShopBtn addTarget:self action:@selector(starShopBtn:) forControlEvents:UIControlEventTouchUpInside];
  77. }
  78. }
  79. else if (_type==1)
  80. {
  81. if (_isOTOShop)
  82. {
  83. _shopGoodsTable.frame = CGRectMake(0, 0, kScreenW, kShoppingHeight) ;
  84. _starShopBtn.hidden = YES;
  85. }
  86. else
  87. {
  88. [_starShopBtn setTitle:ASLocalizedString(@"查看星店")forState:UIControlStateNormal];
  89. [_starShopBtn addTarget:self action:@selector(starShopBtn:) forControlEvents:UIControlEventTouchUpInside];
  90. }
  91. }
  92. if (_hostID.length == 0)
  93. {
  94. return;
  95. }
  96. NSMutableDictionary *mDict = [NSMutableDictionary dictionary];
  97. [mDict setObject:@"shop" forKey:@"ctl"];
  98. if (self.BuguLive.appModel.open_podcast_goods == 1 && _isOTOShop)
  99. {
  100. [mDict setObject:@"podcast_mystore" forKey:@"act"];
  101. }
  102. else
  103. {
  104. [mDict setObject:@"mystore" forKey:@"act"];
  105. }
  106. [mDict setObject:_hostID forKey:@"podcast_user_id"];
  107. [mDict setObject:[NSString stringWithFormat:@"%d",page] forKey:@"page"];
  108. [mDict setObject:@"shop" forKey:@"itype"];
  109. FWWeakify(self)
  110. [self.httpsManager POSTWithParameters:mDict SuccessBlock:^(NSDictionary *responseJson) {
  111. FWStrongify(self)
  112. if ([responseJson toInt:@"status"] == 1)
  113. {
  114. if([[responseJson allKeys] containsObject:@"url"])
  115. {
  116. self.starShopUrl = [responseJson stringForKey:@"url"];
  117. }
  118. NSDictionary * dic = [responseJson objectForKey:@"page"];
  119. if (dic && [dic isKindOfClass:[NSDictionary class]])
  120. {
  121. self.currentPage = [dic toInt:@"page"];
  122. if (self.currentPage == 1 || self.currentPage == 0)
  123. {
  124. [self.shopGoodsArray removeAllObjects];
  125. }
  126. self.has_next = [dic toInt:@"has_next"];
  127. }
  128. NSArray * dataArray = [responseJson objectForKey:@"list"];
  129. if (dataArray && [dataArray isKindOfClass:[NSArray class]])
  130. {
  131. if(dataArray.count)
  132. {
  133. for (NSDictionary * dic in dataArray)
  134. {
  135. ShopGoodsModel * model = [ShopGoodsModel mj_objectWithKeyValues:dic];
  136. [self.shopGoodsArray addObject:model];
  137. }
  138. }
  139. [self.shopGoodsTable reloadData];
  140. if (!self.shopGoodsArray.count)
  141. {
  142. [self showNoContentViewOnView:self];
  143. }
  144. else
  145. {
  146. [self hideNoContentViewOnView:self];
  147. }
  148. }
  149. }
  150. [BGMJRefreshManager endRefresh:self.shopGoodsTable];
  151. } FailureBlock:^(NSError *error) {
  152. FWStrongify(self)
  153. [BGMJRefreshManager endRefresh:self.shopGoodsTable];
  154. }];
  155. }
  156. //上拉刷新
  157. - (void)createMJRefresh
  158. {
  159. [BGMJRefreshManager refresh:_shopGoodsTable target:self headerRereshAction:@selector(headerRefresh) footerRereshAction:@selector(footerReresh)];
  160. }
  161. - (void)headerRefresh
  162. {
  163. [self loadDataWithPage:1];
  164. }
  165. //尾部刷新
  166. - (void)footerReresh
  167. {
  168. if (_has_next == 1)
  169. {
  170. _currentPage ++;
  171. [self loadDataWithPage:_currentPage];
  172. }
  173. else
  174. {
  175. [BGMJRefreshManager endRefresh:_shopGoodsTable];
  176. }
  177. }
  178. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  179. {
  180. return self.shopGoodsArray.count;
  181. }
  182. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  183. {
  184. ShopGoodsTableViewCell * cell = [ShopGoodsTableViewCell cellWithTableView:tableView];
  185. cell.delegate = self;
  186. ShopGoodsModel * model = _shopGoodsArray[indexPath.row];
  187. model.type = self.type;
  188. //model.showDes = self.isOTOShop;
  189. cell.model = model;
  190. return cell;
  191. }
  192. //点击tableView的cell的时候跳转界面
  193. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  194. {
  195. ShopGoodsModel * model = _shopGoodsArray[indexPath.row];
  196. if (_delegate && [_delegate respondsToSelector:@selector(closeShopGoodsViewWithDic: andIsOTOShop:)]) {
  197. NSString * goodsUrl = model.url.length >0 ? model.url :@"";
  198. NSString * goodsId = model.goodsId.length >0 ? model.goodsId : @"";
  199. [_delegate closeShopGoodsViewWithDic:@{@"url":goodsUrl,@"goodsId":goodsId} andIsOTOShop:_isOTOShop];
  200. }
  201. }
  202. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
  203. {
  204. if ([touch.view isKindOfClass:[UITableView class]]){
  205. return NO;
  206. }
  207. if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {
  208. return NO;
  209. }
  210. return YES;
  211. }
  212. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  213. {
  214. if (self.BuguLive.appModel.open_podcast_goods == 1 && _isOTOShop) {
  215. return 120;
  216. }
  217. return 100;
  218. }
  219. - (void)closeViewWithShopGoodsTableViewCell:(ShopGoodsTableViewCell *)cell
  220. {
  221. NSIndexPath * indexPath = [_shopGoodsTable indexPathForCell:cell];
  222. ShopGoodsModel * goodsModel = _shopGoodsArray[indexPath.row];
  223. NSString * goodsID =goodsModel.goodsId;
  224. NSString * goodsUrl =goodsModel.url;
  225. if (self.type==0)
  226. {
  227. if (_delegate && [_delegate respondsToSelector:@selector(closeShopGoodsViewWithDic:andIsOTOShop:)]) {
  228. [_delegate closeShopGoodsViewWithDic:nil andIsOTOShop:_isOTOShop];
  229. }
  230. NSMutableDictionary *mDict = [NSMutableDictionary dictionary];
  231. [mDict setObject:@"shop" forKey:@"ctl"];
  232. if (self.BuguLive.appModel.open_podcast_goods == 1 && _isOTOShop) {
  233. [mDict setObject:@"push_podcast_goods" forKey:@"act"];
  234. }
  235. else
  236. {
  237. [mDict setObject:@"push_goods" forKey:@"act"];
  238. [mDict setObject:_hostID forKey:@"podcast_user_id"];
  239. }
  240. [mDict setObject:goodsID forKey:@"goods_id"];
  241. [mDict setObject:@"shop" forKey:@"itype"];
  242. [self.httpsManager POSTWithParameters:mDict SuccessBlock:^(NSDictionary *responseJson) {
  243. [[BGHUDHelper sharedInstance] tipMessage:[NSString stringWithFormat:@"%@",[responseJson toString:@"error"]]];
  244. } FailureBlock:^(NSError *error) {
  245. }];
  246. }
  247. //如果是观众点击之后跳转到商品的购买链接
  248. else if (self.type==1)
  249. {
  250. if (_delegate && [_delegate respondsToSelector:@selector(closeShopGoodsViewWithDic:andIsOTOShop:)]) {
  251. if (goodsUrl.length>0) {
  252. [_delegate closeShopGoodsViewWithDic:@{@"url":goodsUrl} andIsOTOShop:_isOTOShop];
  253. }
  254. else
  255. {
  256. [_delegate closeShopGoodsViewWithDic:@{@"url":@""} andIsOTOShop:_isOTOShop];
  257. }
  258. }
  259. }
  260. }
  261. @end