SPSBMineViewController.m 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. //
  2. // SPSBMineViewController.m
  3. // 我的社保
  4. //
  5. // Created by shanp on 2021/4/20.
  6. //
  7. #import "SPSBMineViewController.h"
  8. #import "SPSBUIGeneralHeader.h"
  9. #import "SPSBMineTableViewController.h"
  10. #import "SPSBNotificationDelegateManager.h"
  11. #import "UIViewController+SPSBInitialization.h"
  12. #import "UIViewController+SPSBNavigationBar.h"
  13. #import "UIViewController+SPSBNetworkManager.h"
  14. #import "SPSBBroadcastView.h"
  15. #import "SPSBBusinessManager.h"
  16. #import "SPSBKeyProfile.h"
  17. #import "SPSBSQLProfile.h"
  18. #import "SPSBURLApiProfile.h"
  19. #import <UIImageView+WebCache.h>
  20. #import <CALayer+JXHShadow.h>
  21. #import "SPSBRedDotModel.h"
  22. #import "SPSBPurchasersModel.h"
  23. #import "SPSBUMEventAnalysis.h"
  24. #import "SPSBSelectSinglePhotoManager.h"
  25. #import "SPSBActionSheetViewController.h"
  26. #import <UIImage+JXHProcessingImage.h>
  27. #import "SPSBWaitForPayViewController.h"
  28. #import "SPSBWholeOrderListViewController.h"
  29. #import "SPSBSalaryViewController.h"
  30. #import "SPSBOrderBalanceViewController.h"
  31. #import "SPSBWaitToDoViewController.h"
  32. #import "SPSBBankDetailsViewController.h"
  33. #define spsb_mineHeaderButtonTipsTag 4000
  34. typedef NS_ENUM(NSUInteger, SPSBMineHeaderButtonType) {
  35. SPSBMineHeaderButtonTypeWaitToPay = 3000,
  36. SPSBMineHeaderButtonTypeSalaryHistory,//工资流水
  37. SPSBMineHeaderButtonTypeOrderBalance,//补差额
  38. SPSBMineHeaderButtonTypeWaitToDo,
  39. SPSBMineHeaderButtonTypeMyOrder
  40. };
  41. @interface SPSBMineViewController ()<SPSBNotificationDelegate> {
  42. SPSBMineTableViewController *_tableVC;
  43. UIView *_headerView;
  44. UIImageView *_headImageView;
  45. UILabel *_nameLabel;
  46. SPSBBroadcastView *_broadcastView;
  47. UIImage *_avatar;
  48. CGRect _rect;
  49. SPSBPurchasersModel *_editPurchasers;
  50. bool _needRefreshRedDot;
  51. SPSBSelectSinglePhotoManager *_selectSinglePhotoManager;
  52. }
  53. @end
  54. @implementation SPSBMineViewController
  55. - (instancetype)init {
  56. self = [super init];
  57. if (!self) return nil;
  58. self.spsb_isLightContent = true;
  59. return self;
  60. }
  61. - (UIStatusBarStyle)preferredStatusBarStyle {
  62. return self.spsb_statusBarStyle;
  63. }
  64. - (void)viewDidLoad {
  65. [super viewDidLoad];
  66. spsb_setNotificationKey(SPSBLoggingNotificationKey);
  67. [self initData];
  68. [self setupUI];
  69. }
  70. - (void)initData {
  71. @weakify(self)
  72. _selectSinglePhotoManager = [[SPSBSelectSinglePhotoManager alloc] initWithProportion:1 myController:self completion:^(UIImage * _Nullable image, NSValue * _Nullable rect) {
  73. @strongify(self)
  74. [self finishSelectPhoto:image rect:rect];
  75. }];
  76. }
  77. - (void)dealloc {
  78. spsb_removeNotificationKey(SPSBLoggingNotificationKey);
  79. }
  80. - (void)viewWillAppear:(BOOL)animated {
  81. [super viewWillAppear:animated];
  82. //去掉智齿的toolbar
  83. [self.navigationController setToolbarHidden:true animated:true];
  84. [self refreshName];
  85. if (_needRefreshRedDot || _tableVC.spsb_needRefreshRedDot) {
  86. _needRefreshRedDot = false;
  87. _tableVC.spsb_needRefreshRedDot = false;
  88. [self getRedDot];
  89. } else {
  90. [self refreshRedDot];
  91. }
  92. }
  93. #pragma mark - Action
  94. - (void)headerImageAction {
  95. spsb_needLogin(^{
  96. @weakify(self)
  97. SPSBActionSheetViewController *vc = SPSBActionSheetViewController.new;
  98. [vc addActionWithTitle:@"拍照" action:^{
  99. @strongify(self)
  100. [self takePhoto];
  101. }];
  102. [vc addActionWithTitle:@"从手机相册选择" action:^{
  103. @strongify(self)
  104. [self chooseFromAlbum];
  105. }];
  106. [self presentViewController:vc animated:false completion:nil];
  107. });
  108. }
  109. - (void)takePhoto {
  110. [_selectSinglePhotoManager openCamera];
  111. }
  112. - (void)chooseFromAlbum {
  113. [_selectSinglePhotoManager openPhotoAlbum];
  114. }
  115. - (void)finishSelectPhoto:(nullable UIImage *)image rect:(nullable NSValue *)rect {
  116. if (image) {
  117. [self.view showLoadingToastWithTitle:@"正在上传" userClick:false];
  118. _avatar = image;
  119. _rect = [rect CGRectValue];
  120. } else {
  121. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  122. self->_avatar = [UIImage fixOrientation:self->_avatar];
  123. CGImageRef imageRef = CGImageCreateWithImageInRect(self->_avatar.CGImage, self->_rect);
  124. self->_avatar = [UIImage imageWithCGImage:imageRef];
  125. CGImageRelease(imageRef);
  126. self->_avatar = [UIImage imageWithImage:self->_avatar scale:480];
  127. dispatch_async(dispatch_get_main_queue(), ^{
  128. [self uploadAvatar];
  129. });
  130. });
  131. }
  132. }
  133. - (void)headerAction:(UIButton *)sender {
  134. SPSBMineHeaderButtonType type = sender.tag;
  135. switch (type) {
  136. case SPSBMineHeaderButtonTypeWaitToPay: {
  137. [SPSBUMEventAnalysis analyseEventWithId:@"BeingPaid"];
  138. @weakify(self)
  139. spsb_needLogin(^{
  140. @strongify(self)
  141. self->_needRefreshRedDot = true;
  142. [self.navigationController pushViewController:SPSBWaitForPayViewController.new animated:true];
  143. });
  144. }
  145. break;
  146. case SPSBMineHeaderButtonTypeSalaryHistory: {
  147. @weakify(self)
  148. spsb_needLogin(^{
  149. @strongify(self)
  150. self->_needRefreshRedDot = true;
  151. [self.navigationController pushViewController:SPSBSalaryViewController.new animated:true];
  152. });
  153. }
  154. break;
  155. case SPSBMineHeaderButtonTypeOrderBalance: {
  156. [SPSBUMEventAnalysis analyseEventWithId:@"Arrearage"];
  157. @weakify(self)
  158. spsb_needLogin(^{
  159. @strongify(self)
  160. self->_needRefreshRedDot = true;
  161. [self.navigationController pushViewController:SPSBOrderBalanceViewController.new animated:true];
  162. });
  163. }
  164. break;
  165. case SPSBMineHeaderButtonTypeWaitToDo: {
  166. [SPSBUMEventAnalysis analyseEventWithId:@"ToDoList"];
  167. @weakify(self)
  168. spsb_needLogin(^{
  169. @strongify(self)
  170. self->_needRefreshRedDot = true;
  171. [self.navigationController pushViewController:SPSBWaitToDoViewController.new animated:true];
  172. });
  173. }
  174. break;
  175. case SPSBMineHeaderButtonTypeMyOrder: {
  176. [SPSBUMEventAnalysis analyseEventWithId:@"AllOrders"];
  177. @weakify(self)
  178. spsb_needLogin(^{
  179. @strongify(self)
  180. self->_needRefreshRedDot = true;
  181. [self.navigationController pushViewController:SPSBWholeOrderListViewController.new animated:true];
  182. });
  183. }
  184. break;
  185. default:
  186. break;
  187. }
  188. }
  189. - (void)broadcastTap {
  190. _needRefreshRedDot = true;
  191. SPSBBankDetailsViewController *vc = SPSBBankDetailsViewController.new;
  192. vc.spsb_isEdit = true;
  193. vc.spsb_purchasers = _editPurchasers;
  194. [self.navigationController pushViewController:vc animated:true];
  195. }
  196. - (void)refreshRedDot {
  197. [self refreshHeaderRedDot];
  198. [_tableVC.tableView reloadData];
  199. }
  200. - (void)refreshHeaderRedDot {
  201. NSArray *redDot = @[spsb_redDotModel().spsb_buy, spsb_redDotModel().spsb_wage, spsb_redDotModel().spsb_added, spsb_redDotModel().spsb_material_flow];
  202. for (NSInteger i = 0; i < 4; i ++) {
  203. NSString *r = redDot[i];
  204. UIView *redDotView = [_headerView viewWithTag:spsb_mineHeaderButtonTipsTag + i];
  205. redDotView.hidden = r.integerValue == 0;
  206. UILabel *label = [redDotView viewWithTag:spsb_numberTipsViewLabelTag];
  207. if (r.integerValue >= 100) {
  208. label.text = @"99+";
  209. } else {
  210. label.text = r;
  211. }
  212. }
  213. NSString *str;
  214. if (spsb_redDotModel().spsb_sbuids.spsb_purchasers.spsb_id.integerValue == 0) {
  215. _editPurchasers = nil;
  216. str = @"";
  217. } else {
  218. _editPurchasers = spsb_redDotModel().spsb_sbuids.spsb_purchasers;
  219. str = spsb_redDotModel().spsb_sbuids.spsb_tips;
  220. }
  221. [_broadcastView reloadData:str];
  222. }
  223. - (void)handleImageLocation {
  224. @weakify(self)
  225. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  226. @strongify(self)
  227. if ([JXHFMDBManager searchWithPath:SPSBShanpSocialInsuranceLocationSQL key:@"avatar" condition:^id(FMDatabase *database) {
  228. return [database executeQuery:@"select* from avatar where id = ?", [jxh_userDefaults() objectForKey:SPSBUser]];
  229. }]) {
  230. [JXHFMDBManager updateWithPath:SPSBShanpSocialInsuranceLocationSQL condition:^id(FMDatabase *database) {
  231. return @([database executeUpdate:@"delete from avatar where id = ?", [jxh_userDefaults() objectForKey:SPSBUser]]);
  232. }];
  233. }
  234. NSData *data = jxh_archiver(self->_avatar, @"avatar");
  235. [JXHFMDBManager updateWithPath:SPSBShanpSocialInsuranceLocationSQL condition:^id(FMDatabase *database) {
  236. return @([database executeUpdate:@"insert into avatar values(?, ?, ?)", [jxh_userDefaults() objectForKey:SPSBUser], spsb_userAvatar(), data]);
  237. }];
  238. });
  239. }
  240. #pragma mark - Network Action
  241. - (void)uploadAvatar {
  242. @weakify(self)
  243. [self.spsb_networkManager networkPostImageWihtUrl:spsb_wwwUrl(SPSBUrlUploadAvatar) parameters:nil imageArray:@[_avatar] completion:^(NSError * _Nonnull error, id _Nullable data) {
  244. @strongify(self)
  245. if (error.code == SPSBNetworkErrorCodeSuccess && data[@"data"]) {
  246. [self updateAvatarWithPath:data[@"data"]];
  247. } else {
  248. [self handleFailureTipsWithError:error tips:@"上传头像失败"];
  249. }
  250. }];
  251. }
  252. - (void)updateAvatarWithPath:(NSString *)path {
  253. @weakify(self)
  254. [self networkUseMethod:SPSBNetworkMethodPOST loadingTips:nil isLogin:true url:spsb_appUrl(SPSBUrlUpdateAvatar) urlParameters:nil parameters:^NSDictionary * _Nonnull{
  255. return @{@"head_img": path};
  256. } success:^NSString * _Nullable(NSError * _Nonnull error, id _Nullable data) {
  257. @strongify(self)
  258. spsb_setUserAvatar(data[@"data"]);
  259. self->_headImageView.image = self->_avatar;
  260. [self handleImageLocation];
  261. return @"上传成功";
  262. } failure:^NSString * _Nullable(NSError * _Nonnull error, id _Nullable data) {
  263. return @"上传头像失败";
  264. }];
  265. }
  266. - (void)getRedDot {
  267. @weakify(self)
  268. [self networkUseMethod:SPSBNetworkMethodPOST loadingTips:nil isLogin:true url:spsb_appUrl(SPSBUrlPersonalPageCount) urlParameters:nil parameters:nil success:^NSString * _Nullable(NSError * _Nonnull error, id _Nullable data) {
  269. @strongify(self)
  270. spsb_setRedDotModel([SPSBRedDotModel getModelWithDictionary:data[@"data"]]);
  271. [self refreshRedDot];
  272. return nil;
  273. } failure:^NSString * _Nullable(NSError * _Nonnull error, id _Nullable data) {
  274. return nil;
  275. }];
  276. }
  277. #pragma mark - SPSBNotificationDelegate
  278. - (void)spsb_loggingStatusChanged:(SPSBLoggingStatus)status needCheckOrderBalanceNotPayOrders:(bool)flag {
  279. [self refreshHeaderView];
  280. [self refreshRedDot];
  281. }
  282. #pragma mark - UI
  283. - (void)setupUI {
  284. //取消返回按钮
  285. self.navigationItem.leftBarButtonItem = nil;
  286. self.spsb_isHiddenNavigationBar = true;
  287. self.title = @"我的";
  288. [self createHeaderView];
  289. [self createTableView];
  290. [self createNavigationBar];
  291. [self setNavigationBar];
  292. }
  293. - (void)setNavigationBar {
  294. self.spsb_barBackButton.hidden = true;
  295. [self setBackButtonAndTitleColorIsWhite:false];
  296. self.spsb_navigationBar.backgroundColor = spsb_FFFFFF_color(1.f);
  297. self.spsb_navigationBar.alpha = 0;
  298. }
  299. - (void)judgeNavigationBarWithOffset:(CGFloat)offset {
  300. self.spsb_navigationBar.alpha = offset * 0.025;
  301. if (self.spsb_navigationBar.alpha > 0.5) {
  302. self.spsb_isLightContent = false;
  303. } else {
  304. self.spsb_isLightContent = true;
  305. }
  306. }
  307. - (void)createTableView {
  308. _tableVC = SPSBMineTableViewController.new;
  309. _tableVC.tableView.tableHeaderView = _headerView;
  310. @weakify(self)
  311. _tableVC.spsb_scrollAction = ^(CGFloat offset) {
  312. @strongify(self)
  313. [self judgeNavigationBarWithOffset:offset];
  314. };
  315. [self addChildViewController:_tableVC];
  316. [self.view addSubview:_tableVC.tableView];
  317. [_tableVC.tableView makeConstraints:^(JXHConstraintMaker *make) {
  318. make.top.and.leading.and.trailing.equalTo(0);
  319. make.bottom.equalTo(self.view.safebottom).offset(-SPSBTabBarHeight);
  320. }];
  321. }
  322. - (void)createHeaderView {
  323. CGFloat headerHeight = jxh_screenWidth() / 2.2866;
  324. _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, jxh_screenWidth(), headerHeight + 86)];
  325. _headerView.backgroundColor = spsb_FFFFFF_color(1.f);
  326. UIImageView *headImageBackgroundView = [[UIImageView alloc] initWithImage:jxh_getImage(my_bg)];
  327. headImageBackgroundView.userInteractionEnabled = true;
  328. [_headerView addSubview:headImageBackgroundView];
  329. [headImageBackgroundView makeConstraints:^(JXHConstraintMaker *make) {
  330. make.top.and.leading.and.trailing.equalTo(0);
  331. make.size.equalTo(CGSizeMake(jxh_screenWidth(), headerHeight));
  332. }];
  333. UIView *avatarBG = [[UIView alloc] init];
  334. avatarBG.backgroundColor = spsb_FFFFFF_color(1.f);
  335. [avatarBG setLayerCornerRadius:30 clipToBounds:false];
  336. [headImageBackgroundView addSubview:avatarBG];
  337. [avatarBG makeConstraints:^(JXHConstraintMaker *make) {
  338. make.leading.equalTo(23);
  339. make.bottom.equalTo(-40);
  340. make.size.equalTo(CGSizeMake(60, 60));
  341. }];
  342. _headImageView = [[UIImageView alloc] initWithImage:jxh_getImage(default_avatar)];
  343. [_headImageView setLayerCornerRadius:25 clipToBounds:true];
  344. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(headerImageAction)];
  345. [headImageBackgroundView addGestureRecognizer:tap];
  346. [avatarBG addSubview:_headImageView];
  347. [_headImageView makeConstraints:^(JXHConstraintMaker *make) {
  348. make.center.equalTo(avatarBG);
  349. make.size.equalTo(CGSizeMake(50, 50));
  350. }];
  351. _nameLabel = [UILabel convenienceWithFont:spsb_semiboldFont(17) text:nil textColor:spsb_FFFFFF_color(1.f)];
  352. [_headerView addSubview:_nameLabel];
  353. [_nameLabel makeConstraints:^(JXHConstraintMaker *make) {
  354. make.leading.equalTo(avatarBG.trailing).offset(13);
  355. make.centerY.equalTo(avatarBG);
  356. }];
  357. UIView *buttonView = UIView.new;
  358. buttonView.backgroundColor = spsb_FFFFFF_color(1.f);
  359. [buttonView.layer setShadowWithOffset:CGSizeZero alpha:0.08 blur:32 color:spsb_3296FB_color() cornerRadius:4];
  360. [_headerView addSubview:buttonView];
  361. [buttonView makeConstraints:^(JXHConstraintMaker *make) {
  362. make.top.equalTo(headImageBackgroundView.bottom).offset(-20);
  363. make.leading.equalTo(10);
  364. make.trailing.equalTo(-10);
  365. make.height.equalTo(90);
  366. }];
  367. NSArray *titleArray = @[@"待付款", @"社保流水", @"补差额", @"待办事项", @"我的订单"];
  368. NSArray *imageArray = @[jxh_getImage(fukuai), jxh_getImage(liushui), jxh_getImage(cash), jxh_getImage(shixiang), jxh_getImage(dingdang)];
  369. JXHCenterButton *tempButton = nil;
  370. CGFloat width = (jxh_screenWidth() - 20) / titleArray.count;
  371. for (NSInteger i = 0; i < titleArray.count; i ++) {
  372. JXHCenterButton *button = [JXHCenterButton convenienceWithAxis:JXHCenterButtonAxisVertical interval:8 font:spsb_font(13) target:self action:@selector(headerAction:)];
  373. [button setTitle:titleArray[i] titleColor:spsb_333333_color() image:imageArray[i] backgroundImage:nil state:JXHButtonControlStateNormal | JXHButtonControlStateHighlighted];
  374. button.tag = SPSBMineHeaderButtonTypeWaitToPay + i;
  375. [buttonView addSubview:button];
  376. [button makeConstraints:^(JXHConstraintMaker *make) {
  377. make.top.and.bottom.equalTo(0);
  378. make.width.equalTo(width);
  379. if (tempButton) {
  380. make.leading.equalTo(tempButton.trailing);
  381. } else {
  382. make.leading.equalTo(0);
  383. }
  384. }];
  385. tempButton = button;
  386. }
  387. for (NSInteger i = 0; i < titleArray.count - 1; i ++) {
  388. UIView *tipsView = spsb_createNumberTipsViewWithTag(spsb_mineHeaderButtonTipsTag + i);
  389. [buttonView addSubview:tipsView];
  390. [tipsView makeConstraints:^(JXHConstraintMaker *make) {
  391. make.leading.equalTo(width * i + width / 2 + 7);
  392. make.top.equalTo(15);
  393. }];
  394. }
  395. @weakify(self)
  396. _broadcastView = [[SPSBBroadcastView alloc] initWithReloadDataAction:^(CGFloat height) {
  397. @strongify(self)
  398. self -> _headerView.frame = CGRectMake(0, 0, jxh_screenWidth(), headerHeight + height + 86);
  399. [self->_broadcastView makeConstraints:^(JXHConstraintMaker *make) {
  400. make.height.update(height);
  401. }];
  402. [self->_tableVC.tableView reloadData];
  403. }];
  404. [_broadcastView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(broadcastTap)]];
  405. [_headerView addSubview:_broadcastView];
  406. [_broadcastView makeConstraints:^(JXHConstraintMaker *make) {
  407. make.top.equalTo(buttonView.bottom).offset(16);
  408. make.leading.and.trailing.equalTo(0);
  409. make.height.equalTo(0);
  410. }];
  411. [self refreshHeaderView];
  412. [self refreshHeaderRedDot];
  413. }
  414. - (void)refreshName {
  415. if (spsb_isLogin()) {
  416. _nameLabel.text = [NSString stringWithFormat:@"手机用户_%@", [jxh_userDefaults() objectForKey:SPSBUserName]];
  417. } else {
  418. _nameLabel.text = @"注册登录";
  419. }
  420. }
  421. - (void)refreshHeaderView {
  422. [self refreshName];
  423. _headImageView.image = jxh_getImage(default_avatar);
  424. if (spsb_isLogin()) {
  425. //因图片名称与账号绑定,如果两台手机同时登录会造成图片不能同步
  426. if (![spsb_userAvatar() isEqualToString:@""]) {
  427. if ([JXHFMDBManager searchWithPath:SPSBShanpSocialInsuranceLocationSQL key:@"avatar" condition:^id(FMDatabase *database) {
  428. return [database executeQuery:@"select* from avatar where id = ?", [jxh_userDefaults() objectForKey:SPSBUser]];
  429. }]) {
  430. self->_avatar = [JXHFMDBManager searchWithPath:SPSBShanpSocialInsuranceLocationSQL key:@"avatar" condition:^id(FMDatabase *database) {
  431. return [database executeQuery:@"select* from avatar where id = ?", [jxh_userDefaults() objectForKey:SPSBUser]];
  432. }];
  433. self->_headImageView.image = self->_avatar;
  434. } else {
  435. [self->_headImageView sd_setImageWithURL:spsb_fileURL(spsb_userAvatar()) completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
  436. if (image) {
  437. self->_avatar = image;
  438. [self handleImageLocation];
  439. }
  440. }];
  441. }
  442. }
  443. }
  444. }
  445. @end