// // SPSBMineViewController.m // 我的社保 // // Created by shanp on 2021/4/20. // #import "SPSBMineViewController.h" #import "SPSBUIGeneralHeader.h" #import "SPSBMineTableViewController.h" #import "SPSBNotificationDelegateManager.h" #import "UIViewController+SPSBInitialization.h" #import "UIViewController+SPSBNavigationBar.h" #import "UIViewController+SPSBNetworkManager.h" #import "SPSBBroadcastView.h" #import "SPSBBusinessManager.h" #import "SPSBKeyProfile.h" #import "SPSBSQLProfile.h" #import "SPSBURLApiProfile.h" #import #import #import "SPSBRedDotModel.h" #import "SPSBPurchasersModel.h" #import "SPSBUMEventAnalysis.h" #import "SPSBSelectSinglePhotoManager.h" #import "SPSBActionSheetViewController.h" #import #import "SPSBWaitForPayViewController.h" #import "SPSBWholeOrderListViewController.h" #import "SPSBSalaryViewController.h" #import "SPSBOrderBalanceViewController.h" #import "SPSBWaitToDoViewController.h" #import "SPSBBankDetailsViewController.h" #define spsb_mineHeaderButtonTipsTag 4000 typedef NS_ENUM(NSUInteger, SPSBMineHeaderButtonType) { SPSBMineHeaderButtonTypeWaitToPay = 3000, SPSBMineHeaderButtonTypeSalaryHistory,//工资流水 SPSBMineHeaderButtonTypeOrderBalance,//补差额 SPSBMineHeaderButtonTypeWaitToDo, SPSBMineHeaderButtonTypeMyOrder }; @interface SPSBMineViewController () { SPSBMineTableViewController *_tableVC; UIView *_headerView; UIImageView *_headImageView; UILabel *_nameLabel; SPSBBroadcastView *_broadcastView; UIImage *_avatar; CGRect _rect; SPSBPurchasersModel *_editPurchasers; bool _needRefreshRedDot; SPSBSelectSinglePhotoManager *_selectSinglePhotoManager; } @end @implementation SPSBMineViewController - (instancetype)init { self = [super init]; if (!self) return nil; self.spsb_isLightContent = true; return self; } - (UIStatusBarStyle)preferredStatusBarStyle { return self.spsb_statusBarStyle; } - (void)viewDidLoad { [super viewDidLoad]; spsb_setNotificationKey(SPSBLoggingNotificationKey); [self initData]; [self setupUI]; } - (void)initData { @weakify(self) _selectSinglePhotoManager = [[SPSBSelectSinglePhotoManager alloc] initWithProportion:1 myController:self completion:^(UIImage * _Nullable image, NSValue * _Nullable rect) { @strongify(self) [self finishSelectPhoto:image rect:rect]; }]; } - (void)dealloc { spsb_removeNotificationKey(SPSBLoggingNotificationKey); } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; //去掉智齿的toolbar [self.navigationController setToolbarHidden:true animated:true]; [self refreshName]; if (_needRefreshRedDot || _tableVC.spsb_needRefreshRedDot) { _needRefreshRedDot = false; _tableVC.spsb_needRefreshRedDot = false; [self getRedDot]; } else { [self refreshRedDot]; } } #pragma mark - Action - (void)headerImageAction { spsb_needLogin(^{ @weakify(self) SPSBActionSheetViewController *vc = SPSBActionSheetViewController.new; [vc addActionWithTitle:@"拍照" action:^{ @strongify(self) [self takePhoto]; }]; [vc addActionWithTitle:@"从手机相册选择" action:^{ @strongify(self) [self chooseFromAlbum]; }]; [self presentViewController:vc animated:false completion:nil]; }); } - (void)takePhoto { [_selectSinglePhotoManager openCamera]; } - (void)chooseFromAlbum { [_selectSinglePhotoManager openPhotoAlbum]; } - (void)finishSelectPhoto:(nullable UIImage *)image rect:(nullable NSValue *)rect { if (image) { [self.view showLoadingToastWithTitle:@"正在上传" userClick:false]; _avatar = image; _rect = [rect CGRectValue]; } else { dispatch_async(dispatch_get_global_queue(0, 0), ^{ self->_avatar = [UIImage fixOrientation:self->_avatar]; CGImageRef imageRef = CGImageCreateWithImageInRect(self->_avatar.CGImage, self->_rect); self->_avatar = [UIImage imageWithCGImage:imageRef]; CGImageRelease(imageRef); self->_avatar = [UIImage imageWithImage:self->_avatar scale:480]; dispatch_async(dispatch_get_main_queue(), ^{ [self uploadAvatar]; }); }); } } - (void)headerAction:(UIButton *)sender { SPSBMineHeaderButtonType type = sender.tag; switch (type) { case SPSBMineHeaderButtonTypeWaitToPay: { [SPSBUMEventAnalysis analyseEventWithId:@"BeingPaid"]; @weakify(self) spsb_needLogin(^{ @strongify(self) self->_needRefreshRedDot = true; [self.navigationController pushViewController:SPSBWaitForPayViewController.new animated:true]; }); } break; case SPSBMineHeaderButtonTypeSalaryHistory: { @weakify(self) spsb_needLogin(^{ @strongify(self) self->_needRefreshRedDot = true; [self.navigationController pushViewController:SPSBSalaryViewController.new animated:true]; }); } break; case SPSBMineHeaderButtonTypeOrderBalance: { [SPSBUMEventAnalysis analyseEventWithId:@"Arrearage"]; @weakify(self) spsb_needLogin(^{ @strongify(self) self->_needRefreshRedDot = true; [self.navigationController pushViewController:SPSBOrderBalanceViewController.new animated:true]; }); } break; case SPSBMineHeaderButtonTypeWaitToDo: { [SPSBUMEventAnalysis analyseEventWithId:@"ToDoList"]; @weakify(self) spsb_needLogin(^{ @strongify(self) self->_needRefreshRedDot = true; [self.navigationController pushViewController:SPSBWaitToDoViewController.new animated:true]; }); } break; case SPSBMineHeaderButtonTypeMyOrder: { [SPSBUMEventAnalysis analyseEventWithId:@"AllOrders"]; @weakify(self) spsb_needLogin(^{ @strongify(self) self->_needRefreshRedDot = true; [self.navigationController pushViewController:SPSBWholeOrderListViewController.new animated:true]; }); } break; default: break; } } - (void)broadcastTap { _needRefreshRedDot = true; SPSBBankDetailsViewController *vc = SPSBBankDetailsViewController.new; vc.spsb_isEdit = true; vc.spsb_purchasers = _editPurchasers; [self.navigationController pushViewController:vc animated:true]; } - (void)refreshRedDot { [self refreshHeaderRedDot]; [_tableVC.tableView reloadData]; } - (void)refreshHeaderRedDot { NSArray *redDot = @[spsb_redDotModel().spsb_buy, spsb_redDotModel().spsb_wage, spsb_redDotModel().spsb_added, spsb_redDotModel().spsb_material_flow]; for (NSInteger i = 0; i < 4; i ++) { NSString *r = redDot[i]; UIView *redDotView = [_headerView viewWithTag:spsb_mineHeaderButtonTipsTag + i]; redDotView.hidden = r.integerValue == 0; UILabel *label = [redDotView viewWithTag:spsb_numberTipsViewLabelTag]; if (r.integerValue >= 100) { label.text = @"99+"; } else { label.text = r; } } NSString *str; if (spsb_redDotModel().spsb_sbuids.spsb_purchasers.spsb_id.integerValue == 0) { _editPurchasers = nil; str = @""; } else { _editPurchasers = spsb_redDotModel().spsb_sbuids.spsb_purchasers; str = spsb_redDotModel().spsb_sbuids.spsb_tips; } [_broadcastView reloadData:str]; } - (void)handleImageLocation { @weakify(self) dispatch_async(dispatch_get_global_queue(0, 0), ^{ @strongify(self) if ([JXHFMDBManager searchWithPath:SPSBShanpSocialInsuranceLocationSQL key:@"avatar" condition:^id(FMDatabase *database) { return [database executeQuery:@"select* from avatar where id = ?", [jxh_userDefaults() objectForKey:SPSBUser]]; }]) { [JXHFMDBManager updateWithPath:SPSBShanpSocialInsuranceLocationSQL condition:^id(FMDatabase *database) { return @([database executeUpdate:@"delete from avatar where id = ?", [jxh_userDefaults() objectForKey:SPSBUser]]); }]; } NSData *data = jxh_archiver(self->_avatar, @"avatar"); [JXHFMDBManager updateWithPath:SPSBShanpSocialInsuranceLocationSQL condition:^id(FMDatabase *database) { return @([database executeUpdate:@"insert into avatar values(?, ?, ?)", [jxh_userDefaults() objectForKey:SPSBUser], spsb_userAvatar(), data]); }]; }); } #pragma mark - Network Action - (void)uploadAvatar { @weakify(self) [self.spsb_networkManager networkPostImageWihtUrl:spsb_wwwUrl(SPSBUrlUploadAvatar) parameters:nil imageArray:@[_avatar] completion:^(NSError * _Nonnull error, id _Nullable data) { @strongify(self) if (error.code == SPSBNetworkErrorCodeSuccess && data[@"data"]) { [self updateAvatarWithPath:data[@"data"]]; } else { [self handleFailureTipsWithError:error tips:@"上传头像失败"]; } }]; } - (void)updateAvatarWithPath:(NSString *)path { @weakify(self) [self networkUseMethod:SPSBNetworkMethodPOST loadingTips:nil isLogin:true url:spsb_appUrl(SPSBUrlUpdateAvatar) urlParameters:nil parameters:^NSDictionary * _Nonnull{ return @{@"head_img": path}; } success:^NSString * _Nullable(NSError * _Nonnull error, id _Nullable data) { @strongify(self) spsb_setUserAvatar(data[@"data"]); self->_headImageView.image = self->_avatar; [self handleImageLocation]; return @"上传成功"; } failure:^NSString * _Nullable(NSError * _Nonnull error, id _Nullable data) { return @"上传头像失败"; }]; } - (void)getRedDot { @weakify(self) [self networkUseMethod:SPSBNetworkMethodPOST loadingTips:nil isLogin:true url:spsb_appUrl(SPSBUrlPersonalPageCount) urlParameters:nil parameters:nil success:^NSString * _Nullable(NSError * _Nonnull error, id _Nullable data) { @strongify(self) spsb_setRedDotModel([SPSBRedDotModel getModelWithDictionary:data[@"data"]]); [self refreshRedDot]; return nil; } failure:^NSString * _Nullable(NSError * _Nonnull error, id _Nullable data) { return nil; }]; } #pragma mark - SPSBNotificationDelegate - (void)spsb_loggingStatusChanged:(SPSBLoggingStatus)status needCheckOrderBalanceNotPayOrders:(bool)flag { [self refreshHeaderView]; [self refreshRedDot]; } #pragma mark - UI - (void)setupUI { //取消返回按钮 self.navigationItem.leftBarButtonItem = nil; self.spsb_isHiddenNavigationBar = true; self.title = @"我的"; [self createHeaderView]; [self createTableView]; [self createNavigationBar]; [self setNavigationBar]; } - (void)setNavigationBar { self.spsb_barBackButton.hidden = true; [self setBackButtonAndTitleColorIsWhite:false]; self.spsb_navigationBar.backgroundColor = spsb_FFFFFF_color(1.f); self.spsb_navigationBar.alpha = 0; } - (void)judgeNavigationBarWithOffset:(CGFloat)offset { self.spsb_navigationBar.alpha = offset * 0.025; if (self.spsb_navigationBar.alpha > 0.5) { self.spsb_isLightContent = false; } else { self.spsb_isLightContent = true; } } - (void)createTableView { _tableVC = SPSBMineTableViewController.new; _tableVC.tableView.tableHeaderView = _headerView; @weakify(self) _tableVC.spsb_scrollAction = ^(CGFloat offset) { @strongify(self) [self judgeNavigationBarWithOffset:offset]; }; [self addChildViewController:_tableVC]; [self.view addSubview:_tableVC.tableView]; [_tableVC.tableView makeConstraints:^(JXHConstraintMaker *make) { make.top.and.leading.and.trailing.equalTo(0); make.bottom.equalTo(self.view.safebottom).offset(-SPSBTabBarHeight); }]; } - (void)createHeaderView { CGFloat headerHeight = jxh_screenWidth() / 2.2866; _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, jxh_screenWidth(), headerHeight + 86)]; _headerView.backgroundColor = spsb_FFFFFF_color(1.f); UIImageView *headImageBackgroundView = [[UIImageView alloc] initWithImage:jxh_getImage(my_bg)]; headImageBackgroundView.userInteractionEnabled = true; [_headerView addSubview:headImageBackgroundView]; [headImageBackgroundView makeConstraints:^(JXHConstraintMaker *make) { make.top.and.leading.and.trailing.equalTo(0); make.size.equalTo(CGSizeMake(jxh_screenWidth(), headerHeight)); }]; UIView *avatarBG = [[UIView alloc] init]; avatarBG.backgroundColor = spsb_FFFFFF_color(1.f); [avatarBG setLayerCornerRadius:30 clipToBounds:false]; [headImageBackgroundView addSubview:avatarBG]; [avatarBG makeConstraints:^(JXHConstraintMaker *make) { make.leading.equalTo(23); make.bottom.equalTo(-40); make.size.equalTo(CGSizeMake(60, 60)); }]; _headImageView = [[UIImageView alloc] initWithImage:jxh_getImage(default_avatar)]; [_headImageView setLayerCornerRadius:25 clipToBounds:true]; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(headerImageAction)]; [headImageBackgroundView addGestureRecognizer:tap]; [avatarBG addSubview:_headImageView]; [_headImageView makeConstraints:^(JXHConstraintMaker *make) { make.center.equalTo(avatarBG); make.size.equalTo(CGSizeMake(50, 50)); }]; _nameLabel = [UILabel convenienceWithFont:spsb_semiboldFont(17) text:nil textColor:spsb_FFFFFF_color(1.f)]; [_headerView addSubview:_nameLabel]; [_nameLabel makeConstraints:^(JXHConstraintMaker *make) { make.leading.equalTo(avatarBG.trailing).offset(13); make.centerY.equalTo(avatarBG); }]; UIView *buttonView = UIView.new; buttonView.backgroundColor = spsb_FFFFFF_color(1.f); [buttonView.layer setShadowWithOffset:CGSizeZero alpha:0.08 blur:32 color:spsb_3296FB_color() cornerRadius:4]; [_headerView addSubview:buttonView]; [buttonView makeConstraints:^(JXHConstraintMaker *make) { make.top.equalTo(headImageBackgroundView.bottom).offset(-20); make.leading.equalTo(10); make.trailing.equalTo(-10); make.height.equalTo(90); }]; NSArray *titleArray = @[@"待付款", @"社保流水", @"补差额", @"待办事项", @"我的订单"]; NSArray *imageArray = @[jxh_getImage(fukuai), jxh_getImage(liushui), jxh_getImage(cash), jxh_getImage(shixiang), jxh_getImage(dingdang)]; JXHCenterButton *tempButton = nil; CGFloat width = (jxh_screenWidth() - 20) / titleArray.count; for (NSInteger i = 0; i < titleArray.count; i ++) { JXHCenterButton *button = [JXHCenterButton convenienceWithAxis:JXHCenterButtonAxisVertical interval:8 font:spsb_font(13) target:self action:@selector(headerAction:)]; [button setTitle:titleArray[i] titleColor:spsb_333333_color() image:imageArray[i] backgroundImage:nil state:JXHButtonControlStateNormal | JXHButtonControlStateHighlighted]; button.tag = SPSBMineHeaderButtonTypeWaitToPay + i; [buttonView addSubview:button]; [button makeConstraints:^(JXHConstraintMaker *make) { make.top.and.bottom.equalTo(0); make.width.equalTo(width); if (tempButton) { make.leading.equalTo(tempButton.trailing); } else { make.leading.equalTo(0); } }]; tempButton = button; } for (NSInteger i = 0; i < titleArray.count - 1; i ++) { UIView *tipsView = spsb_createNumberTipsViewWithTag(spsb_mineHeaderButtonTipsTag + i); [buttonView addSubview:tipsView]; [tipsView makeConstraints:^(JXHConstraintMaker *make) { make.leading.equalTo(width * i + width / 2 + 7); make.top.equalTo(15); }]; } @weakify(self) _broadcastView = [[SPSBBroadcastView alloc] initWithReloadDataAction:^(CGFloat height) { @strongify(self) self -> _headerView.frame = CGRectMake(0, 0, jxh_screenWidth(), headerHeight + height + 86); [self->_broadcastView makeConstraints:^(JXHConstraintMaker *make) { make.height.update(height); }]; [self->_tableVC.tableView reloadData]; }]; [_broadcastView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(broadcastTap)]]; [_headerView addSubview:_broadcastView]; [_broadcastView makeConstraints:^(JXHConstraintMaker *make) { make.top.equalTo(buttonView.bottom).offset(16); make.leading.and.trailing.equalTo(0); make.height.equalTo(0); }]; [self refreshHeaderView]; [self refreshHeaderRedDot]; } - (void)refreshName { if (spsb_isLogin()) { _nameLabel.text = [NSString stringWithFormat:@"手机用户_%@", [jxh_userDefaults() objectForKey:SPSBUserName]]; } else { _nameLabel.text = @"注册登录"; } } - (void)refreshHeaderView { [self refreshName]; _headImageView.image = jxh_getImage(default_avatar); if (spsb_isLogin()) { //因图片名称与账号绑定,如果两台手机同时登录会造成图片不能同步 if (![spsb_userAvatar() isEqualToString:@""]) { if ([JXHFMDBManager searchWithPath:SPSBShanpSocialInsuranceLocationSQL key:@"avatar" condition:^id(FMDatabase *database) { return [database executeQuery:@"select* from avatar where id = ?", [jxh_userDefaults() objectForKey:SPSBUser]]; }]) { self->_avatar = [JXHFMDBManager searchWithPath:SPSBShanpSocialInsuranceLocationSQL key:@"avatar" condition:^id(FMDatabase *database) { return [database executeQuery:@"select* from avatar where id = ?", [jxh_userDefaults() objectForKey:SPSBUser]]; }]; self->_headImageView.image = self->_avatar; } else { [self->_headImageView sd_setImageWithURL:spsb_fileURL(spsb_userAvatar()) completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) { if (image) { self->_avatar = image; [self handleImageLocation]; } }]; } } } } @end