cdts/xdts-ios 3/TreeHole/Code/Features/Profile/ViewController/ProfileViewController.m
2023-07-27 09:20:00 +08:00

468 lines
23 KiB
Objective-C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// ProfileViewController.m
// TreeHole
//
// Created by mambaxie on 2022/4/24.
//
#import "ProfileViewController.h"
#import "MTCommonListTableViewCell.h"
#import "UserService.h"
#import "SettingsViewController.h"
#import "ProfileCardViewController.h"
#import "VIPCardInfo.h"
#import "VIPViewController.h"
#import "AlertInputView.h"
#import <WXApi.h>
#import "WZLBadgeImport.h"
#import "MTCacheManager.h"
#import "WalletViewController.h"
#import "InviteCodeViewController.h"
static NSString * const DidActionScoreKey = @"DidActionScoreKey";
static NSString * const DidActionFollowPublicAccountKey = @"DidActionFollowPublicAccountKey";
@interface ProfileViewController ()
@property (nonatomic, strong) VIPCardInfo *vipCardInfo;
@property (nonatomic, strong) UIImageView *vipTagView;
@property (nonatomic, strong) UIImage *avatarImage;
@property (nonatomic, assign) NSTimeInterval actionTime;
@property (nonatomic, assign) BOOL isScoreAction;
@end
@implementation ProfileViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self refreshVIPInfo];
self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshVIPInfo) name:BuyVIPSuccessNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showVIPAlertIfNeed) name:UIApplicationDidBecomeActiveNotification object:nil];
}
- (void)showVIPAlertIfNeed {
if (self.actionTime > 0) {
if ([[NSDate date] timeIntervalSince1970] - self.actionTime > 8) {
int extraVipDays = self.isScoreAction ? 15 : 15;
NSString *extarVipKey = self.isScoreAction ? @"extraVipDaysAppStore" : @"extraVipDaysWxGzh";
[MTAlertView showWithSetupBlcok:^(MTAlertViewConfig *config) {
config.title = @"领取奖励";
config.message = [NSString stringWithFormat:@"额外%d天VIP", extraVipDays];
config.cancelTitle = @"放弃";
config.otherTitle = @"领取";
config.otherHandler = ^(MTAlertButton *button) {
[MTAlertView showWithSetupBlcok:^(MTAlertViewConfig *config) {
config.title = @"领取成功";
config.message = [NSString stringWithFormat:@"当你购买VIP成功后会额外赠送%d天VIP", extraVipDays];
config.cancelTitle = @"保存奖励";
config.otherTitle = @"购买VIP";
config.otherHandler = ^(MTAlertButton *button) {
[[NSUserDefaults standardUserDefaults] setObject:@(extraVipDays) forKey:extarVipKey];
[PYAppService showVipVC:@"market_score"];
};
config.cancelHandler = ^(MTAlertButton *button) {
[[NSUserDefaults standardUserDefaults] setObject:@(extraVipDays) forKey:extarVipKey];
[ToastUtil showToast:@"额外VIP天数保存本地成功下次购买VIP会生效"];
};
}];
self.isScoreAction = NO;
};
config.cancelHandler = ^(MTAlertButton *button) {
self.isScoreAction = NO;
};
}];
} else {
[MTAlertView showWithSetupBlcok:^(MTAlertViewConfig *config) {
config.title = @"奖励未获取";
config.message = self.isScoreAction ? @"需评分+评价才可以获得免费VIP哦" : @"成功关注公众号才可得免费VIP哦";
config.cancelTitle = @"取消";
config.otherTitle = self.isScoreAction ? @"去评价" : @"去关注";
config.otherHandler = ^(MTAlertButton *button) {
if (self.isScoreAction) {
[self scoreApp];
} else {
[self followPublicAccount];
}
self.isScoreAction = NO;
};
config.cancelHandler = ^(MTAlertButton *button) {
self.isScoreAction = NO;
};
}];
}
}
self.actionTime = 0;
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[UserService getUserInfoWithCompletion:^(User * _Nonnull user) {
[UserService loadUserOtherInfosWithUserIDs:@[@(user.ID).stringValue] userCache:YES comoletion:^(id _Nullable rsp, NSError * _Nullable error) {
[self reloadData];
[[SDWebImageManager sharedManager] loadImageWithURL:[NSURL URLWithString:user.avatar?: @""] options:0 progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
self.avatarImage = image;
[self.tableView reloadData];
}];
}];
}];
[self refreshVIPInfo];
}
- (void)refreshVIPInfo {
[PYHTTPManager postWithPath:@"myvipcard" params:nil callback:^(id _Nullable rsp, NSError * _Nullable error) {
NSLog(@"rsp===%@",rsp);
VIPCardInfo *info = [VIPCardInfo mj_objectWithKeyValues:rsp];
self.vipCardInfo = info;
self.tableView.tableHeaderView = nil;
if (!info.is_audit && info.vipcard) {
UIView *headerView = [[UIView alloc] init];
headerView.width = self.view.width;
PYImageView *vipBgImageView = [[PYImageView alloc] init];
vipBgImageView.x = FIX_SIZE(20);
vipBgImageView.y = FIX_SIZE(64);
vipBgImageView.width = headerView.width - vipBgImageView.x * 2;
vipBgImageView.imageUrl = info.vipcard.card_bg_img;
vipBgImageView.height = vipBgImageView.width * 134.0 / 335.0;
[headerView addSubview:vipBgImageView];
vipBgImageView.userInteractionEnabled = YES;
UILabel *titleLabel = [[UILabel alloc] init];
titleLabel.textColor = COLOR_WITH_RGB(0xBF751B);
titleLabel.font = MT_FONT_REGULAR_SIZE(13);
titleLabel.x = FIX_SIZE(15);
titleLabel.y = FIX_SIZE(40);
titleLabel.text = info.vipcard.vip_des;
titleLabel.height = FIX_SIZE(20);
titleLabel.width = vipBgImageView.width - titleLabel.x * 2;
[vipBgImageView addSubview:titleLabel];
UIButton *buyButton = [UIButton buttonWithType:UIButtonTypeCustom];
[buyButton setTitle:info.vipcard.buy_button_title forState:UIControlStateNormal];
buyButton.titleLabel.font = MT_FONT_REGULAR_SIZE(15);
buyButton.layer.borderColor = UIColor.whiteColor.CGColor;
buyButton.layer.borderWidth = FIX_SIZE(2.5);
buyButton.layer.cornerRadius = FIX_SIZE(16);
[buyButton sizeToFit];
buyButton.width += FIX_SIZE(44);
buyButton.height = FIX_SIZE(38);
buyButton.right = vipBgImageView.width - FIX_SIZE(15);
buyButton.centerY = vipBgImageView.height * 0.5;
buyButton.clipsToBounds = YES;
[buyButton setBackgroundGradientColorWithBeginColorHex:0x66563E endColorHex:0x362B1D gradientType:GradientColorTypeFromLeftTopToRightBottom];
buyButton.hidden = [info isForever] && info.is_buyed_vip;
[buyButton addTouchUpInsideWithAction:^(UIButton * _Nullable button) {
[PYAppService showVipVC:@"vip_page"];
}];
[vipBgImageView addSubview:buyButton];
if (info.vipcard.buy_button_promotion_title.length > 0) {
UIButton *tipsButton = [UIButton buttonWithType:UIButtonTypeCustom];
tipsButton.userInteractionEnabled = NO;
[tipsButton setTitle:info.vipcard.buy_button_promotion_title forState:UIControlStateNormal];
tipsButton.titleEdgeInsets = UIEdgeInsetsMake(0, 0, FIX_SIZE(4), 0);
tipsButton.titleLabel.font = MT_FONT_REGULAR_SIZE(11);
[tipsButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[tipsButton sizeToFit];
tipsButton.width += FIX_SIZE(8);
UIImage *bgImage = [[UIImage imageNamed:@"TH_discount_tag_bg"] resizableImageWithCapInsets:UIEdgeInsetsMake(10, 25, 10, 10) resizingMode:UIImageResizingModeStretch];
[tipsButton setBackgroundImage:bgImage forState:UIControlStateNormal];
tipsButton.x = buyButton.centerX - FIX_SIZE(15);
tipsButton.bottom = buyButton.y + FIX_SIZE(8);
[vipBgImageView addSubview:tipsButton];
}
if (info.is_buyed_vip) {
UILabel *timeLabel = [[UILabel alloc] init];
timeLabel.textColor = COLOR_WITH_RGB(0x806C4D);
timeLabel.text = info.vipcard.vip_exp_date;
timeLabel.font = MT_FONT_REGULAR_SIZE(11);
[timeLabel sizeToFit];
timeLabel.x = FIX_SIZE(15);
[vipBgImageView addSubview:timeLabel];
UILabel *noLabel = [[UILabel alloc] init];
noLabel.textColor = COLOR_WITH_RGB(0x806C4D);
noLabel.text = info.vipcard.vip_no;
noLabel.font = MT_FONT_REGULAR_SIZE(11);
[noLabel sizeToFit];
noLabel.right = vipBgImageView.width - FIX_SIZE(15);
[vipBgImageView addSubview:noLabel];
timeLabel.bottom = noLabel.bottom = vipBgImageView.height - FIX_SIZE(15);
} else {
PYImageView *avatarImageView = [[PYImageView alloc] init];
avatarImageView.width = FIX_SIZE(142);
avatarImageView.height = FIX_SIZE(22);
avatarImageView.x = FIX_SIZE(14);
avatarImageView.imageUrl = info.vipcard.vip_avatars_img;
avatarImageView.bottom = vipBgImageView.height - FIX_SIZE(11);
[vipBgImageView addSubview:avatarImageView];
UILabel *tipsLabel = [[UILabel alloc] init];
tipsLabel.text = info.vipcard.vip_num;
tipsLabel.font = MT_FONT_REGULAR_SIZE(11);
tipsLabel.textColor = COLOR_WITH_RGB(0x040000);
[tipsLabel sizeToFit];
tipsLabel.x = avatarImageView.right + FIX_SIZE(6);
tipsLabel.centerY = avatarImageView.centerY;
[vipBgImageView addSubview:tipsLabel];
}
[headerView addTapWithAction:^{
[PYAppService showVipVC:@"vip_page"];
}];
headerView.height = vipBgImageView.bottom + FIX_SIZE(20);
self.tableView.tableHeaderView = headerView;
[self reloadData];
}
}];
}
- (BOOL)mt_nagationBarTransparent {
return YES;
}
- (MTCommonListTableConfig *)tableConfigForTableViewContorller {
MTCommonListTableConfig *config = [MTCommonListTableConfig defaultConfigWithCellStyle:MTCommonListTableCellStyleDisclosureIndicator rowsCountForSections:@[@(5)]];
config.cellTitles = @[[UserService currentUser].nickname ?: @"我的资料", @"我的瓶子", @"APP密码锁", @"更多",@"邀请有奖"];
config.cellImageNames = @[@"TH_pro_data_icon", @"TH_pro_bottle_icon", @"TH_pro_password_icon", @"TH_pro_more_icon", @"TH_pro_more_icon"];
WeakSelf(self);
config.sectionFooterHeight = 10;
config.sectionHeaderHeight = 0.01;
config.cellBackgroudColor = COLOR_WITH_RGB(0x221F39);
PYImageView *vipTagView = [[PYImageView alloc] init];
vipTagView.size = CGSizeMake(15, 15);
vipTagView.imageUrl = [UserService.currentUser vip_flag_img];
self.vipTagView = vipTagView;
[config resetCellConfigForSection:0 row:0 config:^(MTCommonListTableCellConfig *cellConfig) {
cellConfig.cellAction = ^(MTCommonListTableViewController *tableViewController, NSIndexPath *indexPath, MTCommonListTableCellConfig *cellConfig) {
[PYAppService pushViewControllerAnimated:[[ProfileCardViewController alloc] initWithUserID:[UserService currentUserID]]];
};
[cellConfig.cell.imageView sd_setImageWithURL:[NSURL URLWithString:[UserService currentUser].avatar ?: @""] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
[self.tableView reloadData];
}];
cellConfig.didLayoutSubviewsAction = ^(MTCommonListTableViewCell *cell) {
[weakself.vipTagView removeFromSuperview];
if ([weakself.vipCardInfo isVIP]) {
[cell addSubview:weakself.vipTagView];
weakself.vipTagView.left = cell.textLabel.right + FIX_SIZE(4);
weakself.vipTagView.centerY = cell.textLabel.centerY;
cell.textLabel.textColor = COLOR_WITH_RGB(0xFDCF7F);
}
cell.imageView.image = weakself.avatarImage;
cell.imageView.backgroundColor = [UIColor clearColor];
cell.imageView.layer.cornerRadius = FIX_SIZE(6);
cell.imageView.layer.borderWidth = 0.5;
cell.imageView.layer.borderColor = [UIColor whiteColor].CGColor;
cell.imageView.clipsToBounds = YES;
cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
};
}];
[config resetCellConfigForSection:0 row:1 config:^(MTCommonListTableCellConfig *cellConfig) {
cellConfig.cellAction = ^(MTCommonListTableViewController *tableViewController, NSIndexPath *indexPath, MTCommonListTableCellConfig *cellConfig) {
[SVProgressHUD showImage:nil status:@"已放之瓶无须念,往事随风自然开~"];
};
}];
// [config resetCellConfigForSection:1 row:1 config:^(MTCommonListTableCellConfig *cellConfig) {
// AppConfig *appConfig = [PYAppService appConfig];
// cellConfig.accessoryTitle = appConfig.wallet_des;
// cellConfig.cellAction = ^(MTCommonListTableViewController *tableViewController, NSIndexPath *indexPath, MTCommonListTableCellConfig *cellConfig) { // 钱包
// [PYAppService appConfig].is_wallet_notice = NO;
// [weakself.navigationController pushViewController:[WalletViewController new] animated:YES];
// [self reloadData];
// };
// if ([weakself.class shouldShowWalletBadge]) {
// cellConfig.didLayoutSubviewsAction = ^(MTCommonListTableViewCell *cell) {
// [cell.textLabel showBadgeWithStyle:WBadgeStyleRedDot value:0 animationType:WBadgeAnimTypeNone];
// cell.textLabel.badgeCenterOffset = CGPointMake(4, cell.textLabel.height * 0.5);
// };
// }
// }];
[config resetCellConfigForSection:0 row:2 config:^(MTCommonListTableCellConfig *cellConfig) {
cellConfig.style = MTCommonListTableCellStyleSwitch;
BOOL canOpen = [UserService isSecurityCodeOpen];
[cellConfig.switchButton setOn:canOpen];
cellConfig.switchButtonAction = ^(UISwitch *switchButton, MTCommonListTableViewCell *cell) {
if (switchButton.isOn) {
if ([UserService currentUser].security_code.length != SecurityCodeLength) {
[switchButton setOn:NO animated:NO];
[AlertInputView showWithTitle:@"设置密码" content:nil maxLength:SecurityCodeLength action:^BOOL(NSString * _Nonnull content) {
if (content.length != SecurityCodeLength) {
[ToastUtil showToast:[NSString stringWithFormat:@"请输入%lu位数字", (unsigned long)SecurityCodeLength] position:CSToastPositionTop];
return NO;
}
[UserService updateUserSecurityToRemote:@{
@"security_code" : content ?: @"",
@"is_security_on" : @(1),
} completion:^(id _Nullable rsp, NSError * _Nullable error) {
if (error) {
[switchButton setOn:NO animated:YES];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[SVProgressHUD showErrorWithStatus:[error.userInfo objectForKey:@"message"]];
});
return;
} else { // 设置成功
[switchButton setOn:YES animated:YES];
[UserService currentUser].security_code = content;
[UserService currentUser].is_security_on = 1;
}
}];
return YES;
}];
return;
}else{
[switchButton setOn:NO animated:NO];
[UserService updateUserSecurityToRemote:@{
@"security_code" : [UserService currentUser].security_code,
@"is_security_on" : @(1),
} completion:^(id _Nullable rsp, NSError * _Nullable error) {
if (error) {
[switchButton setOn:NO animated:YES];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[SVProgressHUD showErrorWithStatus:[error.userInfo objectForKey:@"message"]];
});
return;
} else { // 设置成功
[UserService currentUser].is_security_on = 1;
[switchButton setOn:YES animated:YES];
}
}];
}
} else {
[UserService updateUserSecurityToRemote:@{
@"security_code" : [UserService currentUser].security_code,
@"is_security_on" : @(0),
} completion:^(id _Nullable rsp, NSError * _Nullable error) {
if (error) {
[switchButton setOn:YES animated:YES];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[SVProgressHUD showErrorWithStatus:[error.userInfo objectForKey:@"message"]];
});
return;
} else { // 设置成功
[UserService currentUser].is_security_on = 0;
[switchButton setOn:NO animated:YES];
}
}];
}
};
}];
//
// [config resetCellConfigForSection:2 row:1 config:^(MTCommonListTableCellConfig *cellConfig) {
// cellConfig.accessoryTitle = @"额外送15天VIP";
// if ([weakself.class shoulShowScoreBadge]) {
// cellConfig.didLayoutSubviewsAction = ^(MTCommonListTableViewCell *cell) {
// [cell.textLabel showBadgeWithStyle:WBadgeStyleRedDot value:0 animationType:WBadgeAnimTypeNone];
// cell.textLabel.badgeCenterOffset = CGPointMake(4, cell.textLabel.height * 0.5);
// };
// }
// cellConfig.cellAction = ^(MTCommonListTableViewController *tableViewController, NSIndexPath *indexPath, MTCommonListTableCellConfig *cellConfig) { // 跳转到app store
// [[MTCacheManager defaultCacheManager] setObject:@(YES) forKey:DidActionScoreKey];
// [weakself reloadData];
// [weakself scoreApp];
// };
// }];
//
// [config resetCellConfigForSection:2 row:2 config:^(MTCommonListTableCellConfig *cellConfig) {
// cellConfig.accessoryTitle = @"额外送15天VIP";
// cellConfig.didLayoutSubviewsAction = ^(MTCommonListTableViewCell *cell) {
// if ([weakself.class shouldShowFollowPublicAccountBadge]) {
// [cell.textLabel showBadgeWithStyle:WBadgeStyleRedDot value:0 animationType:WBadgeAnimTypeNone];
// cell.textLabel.badgeCenterOffset = CGPointMake(4, cell.textLabel.height * 0.5);
// }
// };
//
// cellConfig.cellAction = ^(MTCommonListTableViewController *tableViewController, NSIndexPath *indexPath, MTCommonListTableCellConfig *cellConfig) {
// [[MTCacheManager defaultCacheManager] setObject:@(YES) forKey:DidActionFollowPublicAccountKey];
// [weakself reloadData];
// [weakself followPublicAccount];
// };
// }];
[config resetCellConfigForSection:0 row:3 config:^(MTCommonListTableCellConfig *cellConfig) {
cellConfig.cellAction = ^(MTCommonListTableViewController *tableViewController, NSIndexPath *indexPath, MTCommonListTableCellConfig *cellConfig) {
[PYAppService pushViewControllerAnimated:[SettingsViewController new]];
};
}];
[config resetCellConfigForSection:0 row:4 config:^(MTCommonListTableCellConfig *cellConfig) {
cellConfig.cellAction = ^(MTCommonListTableViewController *tableViewController, NSIndexPath *indexPath, MTCommonListTableCellConfig *cellConfig) {
[PYAppService pushViewControllerAnimated:[InviteCodeViewController new]];
};
}];
return config;
}
- (void)scoreApp {
NSURL *url = [NSURL URLWithString:@"itms-apps://itunes.apple.com/cn/app/id1623786430?mt=8"];
[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:^(BOOL success) {
if (!success) {
[SVProgressHUD showErrorWithStatus:@"唤起App store失败请手动唤起"];
} else {
self.actionTime = [[NSDate date] timeIntervalSince1970];
self.isScoreAction = YES;
}
}];
}
- (void)followPublicAccount {
[MTAlertView showWithSetupBlcok:^(MTAlertViewConfig *config) {
config.title = @"公众号复制成功";
config.message = @"点击微信右上角+号,添加”添加朋友“,”公众号“,粘贴搜索,即可关注";
config.cancelTitle = @"取消";
config.otherTitle = @"前往微信";
[UIPasteboard generalPasteboard].string = [PYAppService appConfig].wx_gong_zhong_hao.length > 0 ? [PYAppService appConfig].wx_gong_zhong_hao : @"shudongxiaojiuguan";
config.otherHandler = ^(MTAlertButton *button) { // 唤起微信
if (![WXApi openWXApp]) {
[SVProgressHUD showErrorWithStatus:@"唤起微信失败,请手动唤起"];
} else {
self.actionTime = [[NSDate date] timeIntervalSince1970];
}
};
}];
}
- (void)reloadData {
[super reloadData];
if (![self.class shouldShowBadge]) { // 清空红点
[PYAppService showBadgeOnTabBarItemAtIndex:0 number:0];
}
}
+ (BOOL)shouldShowWalletBadge {
return [PYAppService appConfig].is_wallet_notice;
}
+ (BOOL)shoulShowScoreBadge {
return ![[MTCacheManager defaultCacheManager] boolForKey:DidActionScoreKey];
}
+ (BOOL)shouldShowFollowPublicAccountBadge {
return ![[MTCacheManager defaultCacheManager] boolForKey:DidActionFollowPublicAccountKey];
}
+ (BOOL)shouldShowBadge {
return NO;
// return [self shoulShowScoreBadge] || [self shouldShowFollowPublicAccountBadge] || [self shouldShowWalletBadge];
}
@end