ShopListViewController.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. //
  2. // ShopListViewController.m
  3. // BuguLive
  4. //
  5. // Created by yy on 16/9/20.
  6. // Copyright © 2016年 xfg. All rights reserved.
  7. //
  8. #import "ShopListViewController.h"
  9. #import "ShopListTableViewCell.h"
  10. #import "ShopListModel.h"
  11. #import "ReleaseViewController.h"
  12. #import "EditWebView.h"
  13. @interface ShopListViewController ()<UITableViewDelegate,UITableViewDataSource,ShopListTableViewCellDelegate,EditWebViewDelegate>
  14. @end
  15. @implementation ShopListViewController
  16. {
  17. UITableView *_listTableView;
  18. NSMutableArray *_dataArray; //数据数组
  19. BOOL _isEdit; //是否可编辑
  20. ShopListModel *_model;
  21. int _page; //页数
  22. int _has_next; //是否还有下一页
  23. NSInteger _indexRow;
  24. EditWebView *_editView; //编辑网址视图
  25. CGRect _beforeFrame; //记录之前的坐标
  26. NSString *_beforeUrl; //记录商店Url
  27. NSString *_shopUrl; //商店url
  28. UIView *_grayView; //灰色背景
  29. BOOL _isFirstLoad;
  30. }
  31. - (void)viewWillAppear:(BOOL)animated
  32. {
  33. [super viewWillAppear:animated];
  34. [IQKeyboardManager sharedManager].enable = YES;
  35. [IQKeyboardManager sharedManager].enableAutoToolbar = YES;
  36. _editView.editTextView.keyboardType = UIKeyboardTypeURL;
  37. if (!_isFirstLoad)
  38. {
  39. [self headerReresh];
  40. }
  41. _isFirstLoad = NO;
  42. }
  43. - (void)viewWillDisappear:(BOOL)animated
  44. {
  45. [super viewWillDisappear:animated];
  46. [IQKeyboardManager sharedManager].enable = NO;
  47. [IQKeyboardManager sharedManager].enableAutoToolbar = NO;
  48. }
  49. - (void)viewDidLoad
  50. {
  51. [super viewDidLoad];
  52. _isFirstLoad = YES;
  53. self.navigationItem.leftBarButtonItem=[UIBarButtonItem itemWithTarget:self action:@selector(backClick) image:@"com_arrow_vc_back" highImage:@"com_arrow_vc_back"];
  54. _dataArray = [[NSMutableArray alloc]init];;
  55. _page = 1;
  56. [self createTableView];
  57. _editView.editTextView.keyboardType = UIKeyboardTypeURL;
  58. if (_isOTOShop) {
  59. self.title = ASLocalizedString(@"我的小店");
  60. }
  61. else
  62. {
  63. self.title = ASLocalizedString(@"我的星店");
  64. }
  65. }
  66. #pragma mark 创建表
  67. - (void)createTableView
  68. {
  69. if (!_listTableView) {
  70. _listTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH -kStatusBarHeight-kNavigationBarHeight-42)];
  71. if (kSupportH5Shopping || _isOTOShop) {
  72. _listTableView.frame = CGRectMake(0, 0, kScreenW, kScreenH -kStatusBarHeight-kNavigationBarHeight);
  73. }
  74. }
  75. _listTableView.delegate = self;
  76. _listTableView.dataSource = self;
  77. _listTableView.backgroundColor = kBackGroundColor;
  78. _listTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  79. [self.view addSubview:_listTableView];
  80. [self createMJRefresh];
  81. if(kSupportH5Shopping || _isOTOShop)
  82. {
  83. [self createNavItem];
  84. }
  85. else
  86. {
  87. [self createNavItem];
  88. [self editShopAddress];
  89. [self createEditWeb];
  90. }
  91. }
  92. #pragma mark 创建刷新
  93. - (void)createMJRefresh
  94. {
  95. [BGMJRefreshManager refresh:_listTableView target:self headerRereshAction:@selector(headerReresh) footerRereshAction:@selector(footerReresh)];
  96. }
  97. #pragma mark 创建编辑网址视图
  98. - (void)createEditWeb
  99. {
  100. //灰色背景
  101. if (!_grayView)
  102. {
  103. _grayView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH)];
  104. }
  105. _grayView.backgroundColor = kGrayTransparentColor3;
  106. _grayView.hidden = YES;
  107. [self.view addSubview:_grayView];
  108. if (!_editView) {
  109. _editView = [EditWebView EditNibFromXib];
  110. }
  111. _editView.frame = CGRectMake((kScreenW-280)/2, kScreenH, 280 , 220);
  112. _editView.backgroundColor = [UIColor clearColor];
  113. _editView.backgroundView.backgroundColor = kAppMainColor;
  114. _beforeFrame = _editView.frame;
  115. _editView.delegate =self;
  116. _editView.backgroundView.layer.cornerRadius = 12;
  117. _editView.backgroundView.layer.masksToBounds = YES;
  118. [self.view addSubview:_editView];
  119. }
  120. #pragma mark 创建“新增”按钮
  121. - (void)createNavItem
  122. {
  123. UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
  124. rightButton.frame = CGRectMake(0, 5, 45, 30);
  125. rightButton.tag = 1;
  126. rightButton.titleLabel.font = [UIFont systemFontOfSize:16];
  127. [rightButton setTitle:ASLocalizedString(@"新增")forState:UIControlStateNormal];
  128. [rightButton setTitleColor:kAppGrayColor1 forState:UIControlStateNormal];
  129. [rightButton setTitleColor:kAppGrayColor4 forState:UIControlStateSelected];
  130. [rightButton addTarget:self action:@selector(ClickButton:) forControlEvents:UIControlEventTouchUpInside];
  131. UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
  132. self.navigationItem.rightBarButtonItem = rightBarButtonItem;
  133. }
  134. #pragma mark 创建“编辑小店地址”按钮
  135. - (void)editShopAddress
  136. {
  137. UIButton *editAddress_Btn = [UIButton buttonWithType:UIButtonTypeCustom];
  138. editAddress_Btn.frame = CGRectMake(0, kScreenH -kStatusBarHeight-kNavigationBarHeight-42, kScreenW, 42);
  139. editAddress_Btn.tag = 2;
  140. [editAddress_Btn setTitle:ASLocalizedString(@"编辑星店地址")forState:UIControlStateNormal];
  141. [editAddress_Btn setTitleColor:kNavBarThemeColor forState:UIControlStateNormal];
  142. [editAddress_Btn setBackgroundColor:kAppMainColor];
  143. editAddress_Btn.titleLabel.font = [UIFont systemFontOfSize:16];
  144. [editAddress_Btn addTarget:self action:@selector(ClickButton:) forControlEvents:UIControlEventTouchUpInside];
  145. [self.view addSubview:editAddress_Btn];
  146. }
  147. #pragma mark 下拉刷新
  148. - (void)headerReresh
  149. {
  150. [self loadDataWithPage:1];
  151. }
  152. #pragma mark 上拉加载
  153. - (void)footerReresh
  154. {
  155. if (_has_next == 1)
  156. {
  157. _page ++;
  158. [self loadDataWithPage:_page];
  159. }
  160. else
  161. {
  162. [BGMJRefreshManager endRefresh:_listTableView];
  163. }
  164. }
  165. #pragma mark------------------------------网络请求---------------------------------------
  166. #pragma mark 我的星店
  167. - (void)loadDataWithPage:(int)page
  168. {
  169. NSMutableDictionary *parmDict = [[NSMutableDictionary alloc]init];
  170. [parmDict setObject:@"shop" forKey:@"ctl"];
  171. if (_isOTOShop) {
  172. [parmDict setObject:@"podcast_mystore" forKey:@"act"];
  173. }
  174. else
  175. {
  176. [parmDict setObject:@"mystore" forKey:@"act"];
  177. }
  178. [parmDict setObject:[NSString stringWithFormat:@"%d",page] forKey:@"page"];
  179. [parmDict setObject:[NSString stringWithFormat:@"%@",self.user_id] forKey:@"podcast_user_id"];
  180. [parmDict setObject:@"shop" forKey:@"itype"];
  181. [self.httpsManager POSTWithParameters:parmDict SuccessBlock:^(NSDictionary *responseJson) {
  182. if ([responseJson toInt:@"status"] == 1)
  183. {
  184. NSMutableArray *listArray = [responseJson objectForKey:@"list"];
  185. NSDictionary * pageDic = [responseJson objectForKey:@"page"];
  186. _has_next = [pageDic toInt:@"has_next"];
  187. _page = [pageDic toInt:@"page"];
  188. if (page == 1) {
  189. [_dataArray removeAllObjects];
  190. }
  191. if (listArray.count>0)
  192. {
  193. for (NSDictionary *dic in listArray) {
  194. _model = [ShopListModel mj_objectWithKeyValues:dic];
  195. [_dataArray addObject:_model];
  196. }
  197. }
  198. _shopUrl = [responseJson objectForKey:@"url"];
  199. if (_shopUrl.length == 0) {
  200. _editView.editTextView.text = @"http://";
  201. }
  202. else
  203. {
  204. _editView.editTextView.text = _shopUrl;
  205. }
  206. [_listTableView reloadData];
  207. }
  208. [BGMJRefreshManager endRefresh:_listTableView];
  209. if (!_dataArray.count)
  210. {
  211. [self showNoContentView];
  212. }
  213. else
  214. {
  215. [self hideNoContentView];
  216. }
  217. } FailureBlock:^(NSError *error) {
  218. [BGMJRefreshManager endRefresh:_listTableView];
  219. }];
  220. }
  221. #pragma mark 删除商品
  222. - (void)deleteData:(NSIndexPath *)indexPath
  223. {
  224. if (indexPath.row < _dataArray.count) {
  225. _model = _dataArray[indexPath.row];
  226. }
  227. NSMutableDictionary *parmDict = [[NSMutableDictionary alloc]init];
  228. [parmDict setObject:@"shop" forKey:@"ctl"];
  229. [parmDict setObject:@"del_goods" forKey:@"act"];
  230. [parmDict setObject:[NSString stringWithFormat:@"%@",_model.ID]
  231. forKey:@"id"];
  232. [parmDict setObject:@"shop" forKey:@"itype"];
  233. [self.httpsManager POSTWithParameters:parmDict SuccessBlock:^(NSDictionary *responseJson)
  234. {
  235. if ([responseJson toInt:@"status"] == 1){
  236. [_dataArray removeObjectAtIndex:indexPath.row];
  237. [_listTableView reloadData];
  238. }
  239. } FailureBlock:^(NSError *error)
  240. {
  241. }];
  242. }
  243. #pragma mark 编辑星店地址
  244. - (void)addShopUrl:(NSString *)urlStr
  245. {
  246. NSMutableDictionary *parmDict = [[NSMutableDictionary alloc]init];
  247. [parmDict setObject:@"shop" forKey:@"ctl"];
  248. [parmDict setObject:@"edit_store_url" forKey:@"act"];
  249. [parmDict setObject:urlStr forKey:@"store_url"];
  250. [parmDict setObject:@"shop" forKey:@"itype"];
  251. [[BGHUDHelper sharedInstance] syncLoading:@""];
  252. [self.httpsManager POSTWithParameters:parmDict SuccessBlock:^(NSDictionary *responseJson) {
  253. if ([responseJson toInt:@"status"] == 1) {
  254. [[BGHUDHelper sharedInstance] syncStopLoading];
  255. [_editView.editTextView resignFirstResponder];
  256. [UIView animateWithDuration:0.3 animations:^{
  257. _editView.frame = _beforeFrame;
  258. _grayView.hidden = YES;
  259. }];
  260. }
  261. } FailureBlock:^(NSError *error) {
  262. [[BGHUDHelper sharedInstance] syncStopLoading];
  263. }];
  264. }
  265. #pragma mark----------------------------TableView代理方法-------------------------------
  266. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  267. {
  268. return _dataArray.count;
  269. }
  270. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  271. {
  272. if (self.BuguLive.appModel.open_podcast_goods == 1 && _isOTOShop)
  273. {
  274. return 120;
  275. }
  276. return 100;
  277. }
  278. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  279. {
  280. ShopListTableViewCell *cell = [ShopListTableViewCell cellWithTableView:tableView];
  281. ShopListModel * model = _dataArray[indexPath.row];
  282. //model.showDes = _isOTOShop;
  283. cell.model = model;
  284. cell.delegate =self;
  285. return cell;
  286. }
  287. #pragma mark 删除cell方法
  288. - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
  289. {
  290. if (kSupportH5Shopping) {
  291. return UITableViewCellEditingStyleNone;
  292. }
  293. else
  294. {
  295. return UITableViewCellEditingStyleDelete;
  296. }
  297. }
  298. - (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
  299. {
  300. return ASLocalizedString(@"删除");
  301. }
  302. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  303. {
  304. if (editingStyle == UITableViewCellEditingStyleDelete) {
  305. [self createAlert:indexPath];
  306. }
  307. }
  308. #pragma mark 点击cell方法
  309. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  310. {
  311. NSLog(@"222222222222222222");
  312. }
  313. #pragma mark 警告框
  314. - (void)createAlert:(NSIndexPath *)indexPath
  315. {
  316. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil
  317. message:nil
  318. preferredStyle:UIAlertControllerStyleActionSheet];
  319. //确定:UIAlertActionStyleDefault
  320. UIAlertAction *okAction = [UIAlertAction actionWithTitle:ASLocalizedString(@"确定")style:UIAlertActionStyleDestructive
  321. handler:^(UIAlertAction * _Nonnull action) {
  322. [self deleteData:indexPath];
  323. }];
  324. [alertController addAction:okAction];
  325. //取消:UIAlertActionStyleDestructive
  326. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:ASLocalizedString(@"取消")style:UIAlertActionStyleCancel
  327. handler:^(UIAlertAction * _Nonnull action) {
  328. [_listTableView setEditing:NO animated:YES];
  329. }];
  330. [alertController addAction:cancelAction];
  331. [self presentViewController:alertController animated:YES completion:nil];
  332. }
  333. #pragma mark 返回
  334. - (void)backClick
  335. {
  336. [self.navigationController popViewControllerAnimated:YES];
  337. }
  338. #pragma mark Auction方法
  339. - (void)ClickButton:(UIButton *)button
  340. {
  341. //1是新增,2是“编辑星店地址”
  342. if (button.tag == 1) {
  343. ReleaseViewController * releaseVC = [[ReleaseViewController alloc]init];
  344. releaseVC.shopType = @"EntityShopping";
  345. releaseVC.isOTOShop = self.isOTOShop;
  346. [self.navigationController pushViewController:releaseVC animated:YES];
  347. }
  348. else if(button.tag == 2)
  349. {
  350. if (_beforeUrl.length != 0) {
  351. _editView.editTextView.text = _beforeUrl;
  352. }
  353. [UIView animateWithDuration:0.3 animations:^{
  354. _grayView.hidden = NO;
  355. _editView.frame = CGRectMake((kScreenW-280)/2, (kScreenH-220)/2-kStatusBarHeight-kNavigationBarHeight, 280 , 220);
  356. }];
  357. _editView.editTextView.keyboardType = UIKeyboardTypeURL;
  358. //[_editView.editTextView becomeFirstResponder];
  359. }
  360. }
  361. #pragma mark----------------------------------按钮代理方法------------------------------------
  362. #pragma mark “编辑”代理方法
  363. - (void)enterEditWithShopListTableViewCell:(ShopListTableViewCell *)shopListTableViewCell
  364. {
  365. NSIndexPath * indexPath = [_listTableView indexPathForCell:shopListTableViewCell];
  366. ReleaseViewController * releaseVC = [[ReleaseViewController alloc]init];
  367. releaseVC.isOTOShop = self.isOTOShop;
  368. releaseVC.model = _dataArray[indexPath.row];
  369. releaseVC.shopType = @"EditShopping";
  370. [self.navigationController pushViewController:releaseVC animated:YES];
  371. }
  372. #pragma mark "取消,确定"代理方法
  373. - (void)viewDown:(UIButton *)sender
  374. {
  375. //1是取消,2是确定
  376. if (sender.tag == 1) {
  377. [_editView.editTextView resignFirstResponder];
  378. [UIView animateWithDuration:0.3 animations:^{
  379. _editView.frame = _beforeFrame;
  380. _grayView.hidden = YES;
  381. } completion:^(BOOL finished) {
  382. if (_beforeUrl.length != 0) {
  383. _editView.editTextView.text = _beforeUrl;
  384. }
  385. else if (_shopUrl.length == 0)
  386. {
  387. _editView.editTextView.text = @"http://";
  388. }
  389. else
  390. {
  391. _editView.editTextView.text = _shopUrl;
  392. }
  393. }];
  394. }
  395. else
  396. {
  397. //如果网址不为空
  398. if (_editView.editTextView.text.length != 0) {
  399. if ([_editView.editTextView.text isUrl]) {
  400. [self addShopUrl:_editView.editTextView.text];
  401. _beforeUrl = _editView.editTextView.text;
  402. }
  403. else
  404. [[BGHUDHelper sharedInstance] tipMessage:ASLocalizedString(@"请输入正确的网址链接")];
  405. }
  406. else
  407. {
  408. [FanweMessage alert:ASLocalizedString(@"请输入网址")];
  409. }
  410. }
  411. }
  412. //- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
  413. //{
  414. //
  415. // [UIMenuController sharedMenuController].menuVisible = NO; //donot display the menu
  416. // [_editView.editTextView resignFirstResponder]; //do not allow the user to selected anything
  417. // return NO;
  418. //
  419. //}
  420. @end