MyViewController.m 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. //
  2. // MyViewController.m
  3. // ChinaTheoryNetwork
  4. //
  5. // Created by 张毅成 on 2019/1/21.
  6. // Copyright © 2019 张毅成. All rights reserved.
  7. //
  8. #import "MyViewController.h"
  9. #import "MyModel.h"
  10. #import "MyTableViewCell.h"
  11. #import "MyDetailViewController.h"
  12. #import "SettingViewController.h"
  13. #import "MyOrderViewController.h"
  14. #import "NoticeViewController.h"
  15. #import "LearninglistViewController.h"
  16. #import "MyClubApplicationViewController.h"
  17. #import "LoginViewController.h"
  18. #import "MyPaperViewController.h"
  19. @interface MyViewController ()<UITableViewDelegate, UITableViewDataSource>
  20. @property (weak, nonatomic) IBOutlet UITableView *tableView;
  21. @property (strong, nonatomic) MyModel *model;
  22. @end
  23. @implementation MyViewController
  24. - (MyModel *)model {
  25. if (!_model) {
  26. _model = [MyModel new];
  27. }
  28. return _model;
  29. }
  30. - (void)Network {
  31. NSString *URL = @"my/info";
  32. NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
  33. [[HttpManager sharedHttpManager] POST:URL parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
  34. NSLog(@"%@",responseObject);
  35. if ([responseObject[@"code"] integerValue] == 0) {
  36. self.model = [MyModel modelWithJSON:responseObject[@"data"][@"userInfo"]];
  37. USERDEFAULTSSET(self.model.nickname, @"nickname");
  38. USERDEFAULTSSET(self.model.isApprove, @"kisApprove");
  39. USERDEFAULTSSET(responseObject[@"data"][@"imageUrl"], @"headimgurl");
  40. [[NSUserDefaults standardUserDefaults] synchronize];
  41. [self.tableView reloadData];
  42. NSString *cacheKey = @"ZYCCacheMyModel";
  43. YYCache *cache = [YYCache cacheWithName:@"ZYCCache"];
  44. [cache setObject:self.model forKey:cacheKey];
  45. }
  46. } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
  47. }];
  48. }
  49. - (void)viewDidLoad {
  50. [super viewDidLoad];
  51. self.view.backgroundColor = kBackgroundColor;
  52. [self createTableView];
  53. [self setupLeftButton];
  54. [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithBgColor:kMainColor alpha:1.0] forBarMetrics:UIBarMetricsDefault];
  55. if (kIsLogin) {
  56. [self getCache];
  57. }
  58. }
  59. - (void)viewWillAppear:(BOOL)animated {
  60. [super viewWillAppear:animated];
  61. if (kIsLogin) {
  62. [self Network];
  63. }else{
  64. [self.tableView reloadData];
  65. }
  66. }
  67. - (void)createTableView {
  68. self.tableView.delegate = self;
  69. self.tableView.dataSource = self;
  70. self.tableView.tableFooterView = [UIView new];
  71. self.tableView.rowHeight = UITableViewAutomaticDimension;
  72. self.tableView.estimatedRowHeight = 60;
  73. // self.tableView.separatorStyle = 0;
  74. self.tableView.backgroundColor = kBackgroundColor;
  75. }
  76. - (void)setupLeftButton {
  77. UIButton *LeftButton = [UIButton buttonWithType:UIButtonTypeCustom];
  78. LeftButton.frame = CGRectMake(0, 0, 40, 40);
  79. [LeftButton setImage:[UIImage imageNamed:@"消息-1"] forState:(UIControlStateNormal)];
  80. UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:LeftButton];
  81. UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:(UIBarButtonSystemItemFixedSpace) target:nil action:nil];
  82. item.width = -7;
  83. self.navigationItem.leftBarButtonItems = @[item,leftItem];
  84. [LeftButton addTarget:self action:@selector(didTouchLeftButton) forControlEvents:UIControlEventTouchUpInside];
  85. }
  86. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  87. return 5;
  88. }
  89. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  90. if (section > 1) {
  91. return 10;
  92. }
  93. return 0.01;
  94. }
  95. - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
  96. if (section == [tableView numberOfSections] - 1) {
  97. return 10;
  98. }
  99. return 0.01;
  100. }
  101. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  102. UIView *view = [UIView new];
  103. view.backgroundColor = kBackgroundColor;
  104. return view;
  105. }
  106. - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
  107. UIView *view = [UIView new];
  108. view.backgroundColor = kBackgroundColor;
  109. return view;
  110. }
  111. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  112. NSArray *array = self.model.arrayData[section];
  113. return array.count;
  114. }
  115. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  116. MyTableViewCell *cell0 = [MyTableViewCell cellWithTableView:tableView AndIndex:0];
  117. MyTableViewCell *cell1 = [MyTableViewCell cellWithTableView:tableView AndIndex:1];
  118. //注册3D Touch
  119. /**
  120. 从iOS9开始,我们可以通过这个类来判断运行程序对应的设备是否支持3D Touch功能。
  121. UIForceTouchCapabilityUnknown = 0, //未知
  122. UIForceTouchCapabilityUnavailable = 1, //不可用
  123. UIForceTouchCapabilityAvailable = 2 //可用
  124. */
  125. if ([self respondsToSelector:@selector(traitCollection)]) {
  126. if ([self.traitCollection respondsToSelector:@selector(forceTouchCapability)]) {
  127. if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
  128. [self registerForPreviewingWithDelegate:(id)self sourceView:cell0];
  129. [self registerForPreviewingWithDelegate:(id)self sourceView:cell1];
  130. }
  131. }
  132. }
  133. if (indexPath.section == 0) {
  134. cell0.imageViewIcon.layer.masksToBounds = true;
  135. cell0.imageViewIcon.layer.cornerRadius = 30;
  136. cell0.imageViewIcon.layer.borderColor = [UIColor hexStringToColor:@"abd1ef"].CGColor;
  137. cell0.imageViewIcon.layer.borderWidth = 2.0f;
  138. if (!kIsLogin) {
  139. cell0.labelTitle.text = @"点击登录";
  140. cell0.labelDetail.hidden = true;
  141. [cell0.imageViewIcon setImage:[UIImage imageNamed:@"imagePlaceholder"]];
  142. }else{
  143. cell0.labelDetail.hidden = false;
  144. cell0.labelTitle.text = self.model.nickname?:@"暂无";
  145. cell0.labelDetail.text = [NSString stringWithFormat:@"%@",self.model.myselfMsg?:@"暂无签名"];
  146. // [cell0.imageViewIcon sd_setImageWithURL:[NSURL URLWithString:kUserHeadImageURL] placeholderImage:[UIImage imageNamed:@"imagePlaceholder"]];
  147. cell0.blockDidTouchLabelDetail = ^{
  148. // MyAchievementViewController *controller = [MyAchievementViewController new];
  149. // controller.model = self.model;
  150. // [self.navigationController pushViewController:controller animated:true];
  151. };
  152. }
  153. return cell0;
  154. }else{
  155. [cell1 setDataWithModel:self.model.arrayData[indexPath.section][indexPath.row]];
  156. // if (kIsVip) {
  157. // if (indexPath.section == 1) {
  158. // if (indexPath.row == 2) {
  159. // cell1.labelDetail.text = @"";
  160. // }
  161. // }
  162. // }
  163. }
  164. return cell1;
  165. }
  166. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  167. //判断是否登录
  168. kIsLoginToPush
  169. if (indexPath.section == 0) {
  170. MyDetailViewController *controller = [MyDetailViewController new];
  171. controller.model = self.model;
  172. [self.navigationController pushViewController:controller animated:true];
  173. }else if (indexPath.section == 1) {
  174. if (indexPath.row == 0) {//同行阅读
  175. LearninglistViewController *controller = [LearninglistViewController new];
  176. [self.navigationController pushViewController:controller animated:true];
  177. }else if (indexPath.row == 2) {//学习报告
  178. }
  179. // else if (indexPath.row == 2) {//我的投稿
  180. // MyClubApplicationViewController *controller = [MyClubApplicationViewController new];
  181. // [self.navigationController pushViewController:controller animated:true];
  182. // }
  183. }else if (indexPath.section == 2) {
  184. if (indexPath.row == 0) {//作者认证
  185. }else if (indexPath.row == 1) {//我的论文
  186. MyPaperViewController *controller = [MyPaperViewController new];
  187. [self.navigationController pushViewController:controller animated:true];
  188. }else if (indexPath.row == 2) {//账户
  189. // MyOrderViewController *controller = [MyOrderViewController new];
  190. // [self.navigationController pushViewController:controller animated:true];
  191. }
  192. }else if (indexPath.section == 3) {
  193. if (indexPath.row == 0) {//我的会员
  194. }else if (indexPath.row == 1) {//分享
  195. // CTNShareController *controller = [CTNShareController new];
  196. // [controller shareBasic];
  197. }
  198. }else if (indexPath.section == 4) {
  199. //设置
  200. SettingViewController *controller = [SettingViewController new];
  201. controller.modelMy = self.model;
  202. [self.navigationController pushViewController:controller animated:true];
  203. }
  204. }
  205. - (void)didTouchLeftButton {
  206. kIsLoginToPush
  207. NoticeViewController *controller = [NoticeViewController new];
  208. [self.navigationController pushViewController:controller animated:true];
  209. }
  210. - (void)getCache {
  211. NSString *cacheKey = @"ZYCCacheMyModel";
  212. YYCache *cache = [YYCache cacheWithName:@"ZYCCache"];
  213. BOOL isContains = [cache containsObjectForKey:cacheKey];
  214. NSLog(@"%d",isContains);
  215. id value = [cache objectForKey:cacheKey];
  216. NSLog(@"%@",value);
  217. if (value) {
  218. self.model = value;
  219. [self.tableView reloadData];
  220. }
  221. }
  222. @end