ShopMallSearchVC.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. //
  2. // ShopMallSearchVC.m
  3. // YBLive
  4. //
  5. // Created by ybRRR on 2020/8/20.
  6. // Copyright © 2020 cat. All rights reserved.
  7. //
  8. #import "ShopMallSearchVC.h"
  9. #import "HXSearchBar.h"
  10. #import "shopCell.h"
  11. #import "SearchHorizontalCell.h"
  12. #import "OutsideGoodsDetailVC.h"
  13. #import "CommodityDetailVC.h"
  14. @interface ShopMallSearchVC ()<UISearchBarDelegate,UICollectionViewDelegate,UICollectionViewDataSource>
  15. {
  16. UIView *navi;
  17. HXSearchBar *searchBars;
  18. UIView *selBtnView;
  19. UIButton *newBtn;
  20. UIButton *sellBtn;
  21. UIButton *priceBtn;
  22. UIButton *changeUIBtn;
  23. BOOL isVertical;
  24. BOOL sellIsUp;
  25. BOOL priceIsUp;
  26. NSString *searchStr;
  27. NSString *sellSortStr;
  28. NSString *priceSortStr;
  29. NSString *isnew;
  30. UICollectionViewFlowLayout *flow;
  31. int pageIndex;
  32. }
  33. @property(nonatomic,strong)UICollectionView *collectionView;
  34. @property(nonatomic,strong)NSMutableArray *infoArray;
  35. @end
  36. @implementation ShopMallSearchVC
  37. #pragma mark-------请求数据----------------
  38. -(void)requestListData{
  39. NSLog(@"shopmallDetail-----classId:%@ \n sellSort:%@ \n priceSort:%@ \n isnew:%@",searchBars.text,sellSortStr,priceSortStr,isnew);
  40. [YBNetworking postWithUrl:@"Home.searchShop" Dic:@{@"key":searchBars.text,@"sell":sellSortStr,@"price":priceSortStr,@"isnew":isnew,@"p":@(pageIndex)} Suc:^(int code, id info, NSString *msg) {
  41. [_collectionView.mj_header endRefreshing];
  42. [_collectionView.mj_footer endRefreshing];
  43. if (code == 0) {
  44. NSArray *list =info;
  45. if (pageIndex == 1) {
  46. [_infoArray removeAllObjects];
  47. }
  48. [_infoArray addObjectsFromArray:list];
  49. [_collectionView reloadData];
  50. if (_infoArray.count < 1) {
  51. [PublicView showTextNoData:_collectionView text1:YZMsg(@"暂无商品") text2:@"" centerY:0.8];
  52. }else{
  53. [PublicView hiddenTextNoData:_collectionView];
  54. }
  55. if (list.count == 0) {
  56. [_collectionView.mj_footer endRefreshingWithNoMoreData];
  57. }
  58. }
  59. } Fail:^(id fail) {
  60. [_collectionView.mj_header endRefreshing];
  61. [_collectionView.mj_footer endRefreshing];
  62. }];
  63. // [YBToolClass postNetworkWithUrl:@"Home.searchShop" andParameter:@{@"key":searchBars.text,@"sell":sellSortStr,@"price":priceSortStr,@"isnew":isnew,@"p":@(pageIndex)} success:^(int code,id info,NSString *msg) {
  64. // [_collectionView.mj_header endRefreshing];
  65. // [_collectionView.mj_footer endRefreshing];
  66. // if (code == 0) {
  67. // NSArray *list =info;
  68. // if (pageIndex == 1) {
  69. // [_infoArray removeAllObjects];
  70. // }
  71. // [_infoArray addObjectsFromArray:list];
  72. // [_collectionView reloadData];
  73. // if (_infoArray.count < 1) {
  74. // [PublicView showTextNoData:_collectionView text1:YZMsg(@"暂无商品") text2:@""];
  75. // }else{
  76. // [PublicView hiddenTextNoData:_collectionView];
  77. // }
  78. // if (list.count == 0) {
  79. // [_collectionView.mj_footer endRefreshingWithNoMoreData];
  80. // }
  81. // }
  82. //
  83. // } fail:^{
  84. // [_collectionView.mj_header endRefreshing];
  85. // [_collectionView.mj_footer endRefreshing];
  86. // }];
  87. }
  88. -(void)initData{
  89. self.infoArray = [NSMutableArray array];
  90. sellSortStr = @"";
  91. priceSortStr = @"";
  92. isnew = @"0";
  93. searchStr = @"";
  94. pageIndex = 1;
  95. }
  96. - (void)viewDidLoad {
  97. [super viewDidLoad];
  98. self.navigationController.interactivePopGestureRecognizer.delegate = nil;
  99. self.navigationController.navigationBarHidden = YES;
  100. self.view.backgroundColor = RGB(250, 250, 250);
  101. [self initData];
  102. [self addSearchBar];
  103. [self setSelClassBtn];
  104. [self createCollectionView];
  105. }
  106. #pragma mark--------添加搜索条---------begin--------
  107. - (void)addSearchBar {
  108. navi = [[UIView alloc]initWithFrame:CGRectMake(0, 0, _window_width, 64+statusbarHeight+10)];
  109. navi.backgroundColor = [UIColor whiteColor];
  110. [self.view addSubview:navi];
  111. //加上 搜索栏
  112. searchBars = [[HXSearchBar alloc] initWithFrame:CGRectMake(10,10 + statusbarHeight, _window_width-20,60)];
  113. searchBars.backgroundColor = [UIColor whiteColor];
  114. searchBars.delegate = self;
  115. //输入框提示
  116. searchBars.placeholder = YZMsg(@"请输入您要搜索的商品名称");
  117. //光标颜色
  118. searchBars.cursorColor = Pink_Cor;
  119. //TextField
  120. searchBars.searchBarTextField.layer.cornerRadius = 16;
  121. searchBars.searchBarTextField.layer.masksToBounds = YES;
  122. searchBars.searchBarTextField.backgroundColor = RGB(241, 241, 241);
  123. searchBars.searchBarTextField.clearButtonMode = UITextFieldViewModeNever;
  124. //去掉取消按钮灰色背景
  125. searchBars.hideSearchBarBackgroundImage = YES;
  126. searchBars.searchBarTextField.font = [UIFont systemFontOfSize:14];
  127. searchBars.cancleButton.backgroundColor = [UIColor clearColor];
  128. [searchBars.cancleButton setTitle:YZMsg(@"取消") forState:UIControlStateNormal];
  129. [searchBars.cancleButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
  130. searchBars.cancleButton.titleLabel.font = [UIFont systemFontOfSize:14];
  131. [navi addSubview:searchBars];
  132. [[UIBarButtonItem appearanceWhenContainedIn: [UISearchBar class], nil] setTintColor:[UIColor grayColor]];
  133. [searchBars becomeFirstResponder];
  134. }
  135. //已经开始编辑时的回调
  136. - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
  137. // HXSearchBar *sear = (HXSearchBar *)searchBar;
  138. // //取消按钮
  139. // sear.cancleButton.backgroundColor = [UIColor clearColor];
  140. // [sear.cancleButton setTitle:YZMsg(@"取消") forState:UIControlStateNormal];
  141. // [sear.cancleButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
  142. // sear.cancleButton.titleLabel.font = [UIFont systemFontOfSize:14];
  143. }
  144. //搜索按钮
  145. - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
  146. [_infoArray removeAllObjects];
  147. [_collectionView reloadData];
  148. pageIndex = 1;
  149. [self requestListData];
  150. [searchBar resignFirstResponder];
  151. }
  152. -(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
  153. [_infoArray removeAllObjects];
  154. [_collectionView reloadData];
  155. pageIndex = 1;
  156. [self requestListData];
  157. }
  158. //取消按钮点击的回调
  159. - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
  160. // searchBar.showsCancelButton = NO;
  161. searchBar.text = nil;
  162. [self.view endEditing:YES];
  163. [[YBBaseAppDelegate sharedAppDelegate]popViewController:YES];
  164. }
  165. #pragma mark--------添加搜索条---------end--------
  166. -(void)setSelClassBtn{
  167. selBtnView = [[UIView alloc]initWithFrame:CGRectMake(0, navi.bottom, _window_width, 40)];
  168. [self.view addSubview:selBtnView];
  169. newBtn = [UIButton buttonWithType:0];
  170. newBtn.frame = CGRectMake(12, 10, 60, 20);
  171. [newBtn setTitle:YZMsg(@"新品") forState:UIControlStateNormal];
  172. [newBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
  173. [newBtn setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
  174. newBtn.titleLabel.font = [UIFont systemFontOfSize:14];
  175. [newBtn addTarget:self action:@selector(newBtnClick) forControlEvents:UIControlEventTouchUpInside];
  176. [selBtnView addSubview:newBtn];
  177. sellBtn = [UIButton buttonWithType:0];
  178. sellBtn.frame = CGRectMake(newBtn.right+12, 10, 60, 20);
  179. [sellBtn setTitle:YZMsg(@"销量") forState:UIControlStateNormal];
  180. [sellBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
  181. [sellBtn setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
  182. sellBtn.titleLabel.font = [UIFont systemFontOfSize:14];
  183. [sellBtn setImage:[UIImage imageNamed:@"sel_up"] forState:UIControlStateSelected];
  184. [sellBtn setImage:[UIImage imageNamed:@"sel_normal"] forState:UIControlStateNormal];
  185. sellBtn.titleEdgeInsets = UIEdgeInsetsMake(0,-sellBtn.imageView.frame.size.width, 0, sellBtn.imageView.frame.size.width);
  186. sellBtn.imageEdgeInsets = UIEdgeInsetsMake(0,sellBtn.titleLabel.bounds.size.width, 0, - sellBtn.titleLabel.bounds.size.width);
  187. [sellBtn addTarget:self action:@selector(sellBtnClick) forControlEvents:UIControlEventTouchUpInside];
  188. [selBtnView addSubview:sellBtn];
  189. priceBtn = [UIButton buttonWithType:0];
  190. priceBtn.frame = CGRectMake(sellBtn.right+12, 10, 60, 20);
  191. [priceBtn setTitle:YZMsg(@"价格") forState:UIControlStateNormal];
  192. [priceBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
  193. [priceBtn setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
  194. priceBtn.titleLabel.font = [UIFont systemFontOfSize:14];
  195. [priceBtn setImage:[UIImage imageNamed:@"sel_up"] forState:UIControlStateSelected];
  196. [priceBtn setImage:[UIImage imageNamed:@"sel_normal"] forState:UIControlStateNormal];
  197. priceBtn.titleEdgeInsets = UIEdgeInsetsMake(0,-priceBtn.imageView.frame.size.width, 0, priceBtn.imageView.frame.size.width);
  198. priceBtn.imageEdgeInsets = UIEdgeInsetsMake(0,priceBtn.titleLabel.bounds.size.width, 0, - priceBtn.titleLabel.bounds.size.width);
  199. [priceBtn addTarget:self action:@selector(priceBtnClick) forControlEvents:UIControlEventTouchUpInside];
  200. [selBtnView addSubview:priceBtn];
  201. changeUIBtn = [UIButton buttonWithType:0];
  202. changeUIBtn.frame = CGRectMake(selBtnView.width-40, 10, 20, 20);
  203. [changeUIBtn setImage:[UIImage imageNamed:@"change_横"] forState:UIControlStateNormal];
  204. [changeUIBtn setImage:[UIImage imageNamed:@"change_竖"] forState:UIControlStateSelected];
  205. [changeUIBtn addTarget:self action:@selector(changeUIClick) forControlEvents:UIControlEventTouchUpInside];
  206. [selBtnView addSubview:changeUIBtn];
  207. }
  208. -(void)newBtnClick{
  209. pageIndex= 1;
  210. sellIsUp = NO;
  211. priceIsUp = NO;
  212. newBtn.selected = YES;
  213. sellBtn.selected = NO;
  214. priceBtn.selected =NO;
  215. isnew = @"1";
  216. sellSortStr = @"";
  217. priceSortStr = @"";
  218. [self requestListData];
  219. }
  220. -(void)sellBtnClick{
  221. pageIndex = 1;
  222. sellBtn.selected = YES;
  223. priceBtn.selected =NO;
  224. newBtn.selected = NO;
  225. priceIsUp = NO;
  226. sellIsUp = !sellIsUp;
  227. if (sellIsUp) {
  228. [sellBtn setImage:[UIImage imageNamed:@"sel_up"] forState:UIControlStateSelected];
  229. sellSortStr = @"asc";
  230. }else{
  231. [sellBtn setImage:[UIImage imageNamed:@"sel_down"] forState:UIControlStateSelected];
  232. sellSortStr = @"desc";
  233. }
  234. isnew = @"0";
  235. priceSortStr = @"";
  236. [self requestListData];
  237. }
  238. -(void)priceBtnClick{
  239. pageIndex = 1;
  240. sellBtn.selected = NO;
  241. priceBtn.selected =YES;
  242. newBtn.selected = NO;
  243. sellIsUp = NO;
  244. priceIsUp = !priceIsUp;
  245. if (priceIsUp) {
  246. [priceBtn setImage:[UIImage imageNamed:@"sel_up"] forState:UIControlStateSelected];
  247. priceSortStr = @"asc";
  248. }else{
  249. [priceBtn setImage:[UIImage imageNamed:@"sel_down"] forState:UIControlStateSelected];
  250. priceSortStr = @"desc";
  251. }
  252. isnew = @"0";
  253. sellSortStr = @"";
  254. [self requestListData];
  255. }
  256. -(void)changeUIClick{
  257. changeUIBtn.selected = !changeUIBtn.selected;
  258. isVertical = changeUIBtn.selected;
  259. if (isVertical) {
  260. flow.itemSize = CGSizeMake(_window_width,118);
  261. }else{
  262. flow.itemSize = CGSizeMake((_window_width-21)/2, (_window_width-21)/2 + 75);
  263. }
  264. [_collectionView reloadData];
  265. }
  266. -(void)createCollectionView{
  267. flow = [[UICollectionViewFlowLayout alloc]init];
  268. flow.scrollDirection = UICollectionViewScrollDirectionVertical;
  269. flow.itemSize = CGSizeMake((_window_width-21)/2, (_window_width-21)/2 + 75);
  270. flow.minimumLineSpacing = 7;
  271. flow.minimumInteritemSpacing = 7;
  272. flow.sectionInset = UIEdgeInsetsMake(7, 7, 7, 7);
  273. _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0,selBtnView.bottom, _window_width, _window_height-64-statusbarHeight-selBtnView.height) collectionViewLayout:flow];
  274. _collectionView.backgroundColor = RGB_COLOR(@"#f5f5f5", 1);
  275. _collectionView.delegate = self;
  276. _collectionView.dataSource = self;
  277. [_collectionView registerNib:[UINib nibWithNibName:@"shopCell" bundle:nil] forCellWithReuseIdentifier:@"shopCELL"];
  278. [_collectionView registerNib:[UINib nibWithNibName:@"SearchHorizontalCell" bundle:nil] forCellWithReuseIdentifier:@"SearchHorizontalCELL"];
  279. [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"hotHeaderV"];
  280. [self.view addSubview:_collectionView];
  281. _collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  282. _collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
  283. pageIndex = 1;
  284. [self requestListData];
  285. }];
  286. _collectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
  287. pageIndex ++;
  288. [self requestListData];
  289. }];
  290. // [self pullInternet];
  291. }
  292. -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
  293. return _infoArray.count;
  294. }
  295. -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
  296. if (isVertical) {
  297. SearchHorizontalCell *cell = (SearchHorizontalCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"SearchHorizontalCELL" forIndexPath:indexPath];
  298. NSDictionary *subDic = _infoArray[indexPath.row];
  299. [cell.thumbImgV sd_setImageWithURL:[NSURL URLWithString:minstr([subDic valueForKey:@"thumb"])]];
  300. cell.titleL.text = minstr([subDic valueForKey:@"name"]);
  301. cell.priceL.text = [NSString stringWithFormat:@"¥%@",minstr([subDic valueForKey:@"price"])];
  302. //type 商品类型 0 站内商品 1 外链商品
  303. if ([minstr([subDic valueForKey:@"type"]) isEqual:@"1"]) {
  304. cell.lineLb.hidden = NO;
  305. cell.sellCountLb.text = [NSString stringWithFormat:@"¥%@",minstr([subDic valueForKey:@"original_price"])];
  306. }else{
  307. cell.lineLb.hidden = YES;
  308. cell.sellCountLb.text =[NSString stringWithFormat:@"%@%@%@",YZMsg(@"已售"),minstr([subDic valueForKey:@"sale_nums"]),YZMsg(@"件")];
  309. }
  310. return cell;
  311. }else{
  312. shopCell *cell = (shopCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"shopCELL" forIndexPath:indexPath];
  313. NSDictionary *subDic = _infoArray[indexPath.row];
  314. [cell.thumbImgV sd_setImageWithURL:[NSURL URLWithString:minstr([subDic valueForKey:@"thumb"])]];
  315. cell.titleL.text = minstr([subDic valueForKey:@"name"]);
  316. cell.priceL.text = [NSString stringWithFormat:@"¥%@",minstr([subDic valueForKey:@"price"])];
  317. //type 商品类型 0 站内商品 1 外链商品
  318. if ([minstr([subDic valueForKey:@"type"]) isEqual:@"1"]) {
  319. cell.lineLb.hidden = NO;
  320. cell.sellCountLb.text = [NSString stringWithFormat:@"¥%@",minstr([subDic valueForKey:@"original_price"])];
  321. }else{
  322. cell.lineLb.hidden = YES;
  323. cell.sellCountLb.text =[NSString stringWithFormat:@"%@%@%@",YZMsg(@"已售"),minstr([subDic valueForKey:@"sale_nums"]),YZMsg(@"件")];
  324. }
  325. return cell;
  326. }
  327. }
  328. -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
  329. [collectionView deselectItemAtIndexPath:indexPath animated:YES];
  330. NSDictionary *subDic = _infoArray[indexPath.row];
  331. [PublicObj checkGoodsExistenceWithID:minstr([subDic valueForKey:@"id"]) Existence:^(int code, NSString *msg) {
  332. if (code == 0) {
  333. if ([minstr([subDic valueForKey:@"type"]) isEqual:@"1"]) {
  334. OutsideGoodsDetailVC *detail = [[OutsideGoodsDetailVC alloc]init];
  335. detail.goodsID = minstr([subDic valueForKey:@"id"]);
  336. detail.liveUid = @"0";
  337. [[YBBaseAppDelegate sharedAppDelegate] pushViewController:detail animated:YES];
  338. }else{
  339. CommodityDetailVC *detail = [[CommodityDetailVC alloc]init];
  340. detail.goodsID = minstr([subDic valueForKey:@"id"]);
  341. detail.liveUid = @"0";
  342. [[YBBaseAppDelegate sharedAppDelegate] pushViewController:detail animated:YES];
  343. }
  344. }else{
  345. [MBProgressHUD showError:msg];
  346. }
  347. }];
  348. }
  349. @end