123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541 |
- //
- // DRSettingViewController.m
- // YouHuiProject
- //
- // Created by 小花 on 2018/1/25.
- // Copyright © 2018年 kuxuan. All rights reserved.
- //
- #import "DRSettingViewController.h"
- #import "DRAuthorityManager.h"
- #import "CCActionSheet.h"
- #import <AlibabaAuthSDK/ALBBSession.h>
- #import <AlibabaAuthSDK/ALBBSDK.h>
- #import "DXAlertView.h"
- #import "DRLoginViewController.h"
- #import <StoreKit/StoreKit.h>
- #import "DRMySuperViewController.h"
- #import "DRMyWechatViewController.h"
- #import "YBCacheTool.h"
- #import "DRLinkAliPayViewController.h"
- @interface DRSettingViewController ()<UITableViewDelegate,UITableViewDataSource,CCActionSheetDelegate,DXAlertViewDelegate>
- {
- NSString *_weChatStr;
- }
- @property (nonatomic, strong) UITableView *tableView;
- @property (nonatomic, strong) NSArray *dataArr;
- @property (nonatomic, strong) NSArray *personArr;
- @property (nonatomic, strong) NSArray *sectionDataArr;
- @property (nonatomic, strong) NSArray *sexArr;
- @property (nonatomic, strong) NSDictionary *userInfo;
- @end
- @implementation DRSettingViewController
- - (void)viewWillAppear:(BOOL)animated {
- [super viewWillAppear:animated];
- [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
- self.navigationBar.backgroundColor = [UIColor changeColor];
- [self requestWeChatStr];
- [self loadUserInfo];
- }
- - (void)viewDidLoad {
-
- [super viewDidLoad];
-
- [self configNavigationBar];
- [self addObserToController];
-
- }
- - (void)configNavigationBar {
-
- self.view.backgroundColor = [UIColor whiteColor];
- [self.navigationBar setNavTitle:@"设置"];
- self.navigationBar.showNavigationBarBottomLine = YES;
- [self.view addSubview:self.tableView];
- self.navigationBar.navTitleLabel.textColor = [UIColor whiteColor];
- UIButton *leftBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
- [leftBtn setImage:[UIImage imageNamed:@"back_white"] forState:UIControlStateNormal];
- [leftBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
- [self.navigationBar setCustomLeftButtons:@[leftBtn]];
-
- UILabel *logOut = [[UILabel alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT-50-BottomMargin, SCREEN_WIDTH, 50)];
- logOut.backgroundColor = [UIColor whiteColor];
- logOut.textColor = [UIColor homeRedColor];
- logOut.textAlignment = NSTextAlignmentCenter;
- logOut.font = [UIFont systemFontOfSize:14];
- logOut.text = [AccountTool isLogin] ? @"退出登录":@"快速登录";
- logOut.userInteractionEnabled = YES;
- UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(logOutAction)];
- [logOut addGestureRecognizer:tap];
- [self.view addSubview:logOut];}
- - (void)addObserToController {
- [[NSNotificationCenter defaultCenter] addObserver:self
- selector:@selector(becomActive)
- name:UIApplicationDidBecomeActiveNotification object:nil];
- }
- - (void)requestWeChatStr {
- if (![AccountTool isLogin]) {
- return;
- }
- // NSString *url = [NSString stringWithFormat:@"%@/api/v2/adzoneCreate/displayWeChat",BaseURL];
- // [DRHttp post:url params:nil success:^(id json) {
- //
- // NSString *wechat = [NSString stringWithFormat:@"%@",json[@"weChat"]];
- // if (wechat.length > 0) {
- // _weChatStr = wechat;
- // }
- // [self.tableView reloadData];
- // } failure:^(NSError *error) {
- //
- // }];
- }
- - (void)becomActive {
- [self.tableView reloadData];
- }
- /**
- 返回
- */
- - (void)backAction {
- [self.navigationController popViewControllerAnimated:YES];
- }
- /**
- 退出登录
- */
- - (void)logOutAction {
-
- if ([AccountTool isLogin]) {
- UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"是否要退出登录?" preferredStyle:UIAlertControllerStyleAlert];
- UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
-
- }];
- UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
- [self logOutManager];
- }];
- [alert addAction:cancel];
- [alert addAction:sure];
- [self presentViewController:alert animated:YES completion:nil];
- }else {
- DRLoginViewController *login = [[DRLoginViewController alloc] init];
- [self presentViewController:login animated:YES completion:nil];
- }
-
-
- }
- - (void)logOutManager {
- [DRHttp post:Logout params:nil success:^(id json) {
- [AccountTool deleteAccount];
- [[NSNotificationCenter defaultCenter] postNotificationName:ChangeSex object:nil];
- [self.navigationController popViewControllerAnimated:YES];
-
- } failure:^(NSError *error) {
-
- }];
- }
- /**
- switch 点击
- */
- - (void)switchAction {
-
- NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
- if([[UIApplication sharedApplication]canOpenURL:url] ) {
- if (@available(iOS 10.0, *)) {
- [[UIApplication sharedApplication] openURL:url options:@{}completionHandler:^(BOOL success) {
- }];
- } else {
- [[UIApplication sharedApplication]openURL:url];
- }
- }
-
- }
- #pragma mark ------------------
- - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
-
- if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
- [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
- }
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- if (section == 0) {
- return self.sectionDataArr.count;
- }
- if (section == 1) {
- return self.dataArr.count;
- }
- return self.dataArr.count;
- }
- - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
- return 50;
- }
- - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
- return FITSIZE(5);
- }
- -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- {
- return 2;
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
-
-
- UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cellID"];
-
-
- if ((indexPath.section == 0 && indexPath.row !=2) ||(indexPath.section == 1 &&indexPath.row !=0)) {
- cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
- }
-
- if (indexPath.row == 0 && indexPath.section ==1) {
- UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-60, 10, 50, 30)];
- [switchView addTarget:self action:@selector(switchAction) forControlEvents:UIControlEventTouchUpInside]; // 开关事件切换通知
- [cell addSubview:switchView];
-
- if ([DRAuthorityManager isObtainUserNotificationAuthority]) {
- switchView.on = YES;
- }else {
- switchView.on = NO;
- }
-
- }
- if (indexPath.section == 0) {
- cell.textLabel.text = self.sectionDataArr[indexPath.row][@"title"];
-
- }else if (indexPath.section == 1) {
- cell.textLabel.text = self.dataArr[indexPath.row][@"title"];
-
- }
- cell.textLabel.font = [UIFont systemFontOfSize:15];
- cell.textLabel.textColor = [UIColor YHColorWithHex:0x666666];
- cell.selectionStyle = UITableViewCellSelectionStyleNone;
- cell.detailTextLabel.font = [UIFont systemFontOfSize:15];
- cell.detailTextLabel.textColor = [UIColor YHColorWithHex:0x999999];
- cell.detailTextLabel.textAlignment=NSTextAlignmentRight;
-
- //缓存
- if (indexPath.row == 0 && indexPath.section ==0) {
- NSUInteger intg = [[SDImageCache sharedImageCache] getSize];
- NSString * currentVolum = [NSString stringWithFormat:@"%@",[self fileSizeWithInterge:intg]];
- cell.detailTextLabel.text = currentVolum;
- }
- if (indexPath.row == 1 && indexPath.section ==1) {
- if ([[ALBBSession sharedInstance] isLogin]) {
- cell.detailTextLabel.text = @"已授权";
- }else {
- cell.detailTextLabel.text = @"未授权";
- }
-
- }
- if (indexPath.section ==0 && indexPath.row ==2) {
- NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
- NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
- cell.detailTextLabel.text = app_Version;
- }
- return cell;
-
- }
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
- if (indexPath.section == 1) {
- switch (indexPath.row) {
- case 0:
- break;
-
- case 1:
- //淘宝授权
- [self taobaoAuthor];
- break;
- default:
- break;
- }
- }
- if (indexPath.section ==0) {
- switch (indexPath.row) {
- case 0:
- //清理缓存
- [self clearCacheWith:indexPath];
- break;
- case 1:
- //五星好评
- [self commentAndStart];
- break;
-
- default:
- break;
- }
- }
- }
- /**
- 加载用户信息
- */
- - (void)loadUserInfo {
-
-
-
- [DRCacheHttp post:NewUserInfo params:nil success:^(id json, BOOL isCache) {
- self.userInfo =[NSDictionary dictionaryWithDictionary:json[@"data"]];
- [self.tableView reloadData];
- } failure:^(NSError *error) {
-
- }];
-
- }
- #pragma mark ------------ private----------
- /**
- 五星好评
- */
- - (void)commentAndStart {
-
- NSString * nsStringToOpen = [NSString stringWithFormat: @"itms-apps://itunes.apple.com/app/id%@?action=write-review",APP_ID];//替换为对应的APPID
- [[UIApplication sharedApplication] openURL:[NSURL URLWithString:nsStringToOpen]];
- }
- /**
- 支付宝账号
- */
- - (void)alipayAccount {
- DRLinkAliPayViewController *alipay = [[DRLinkAliPayViewController alloc] init];
- alipay.isUpdate = [self.alipayFlag boolValue];
- [self.navigationController pushViewController:alipay animated:YES];
- }
- /**
- 用户登录
- */
- - (void)goToLoginPage {
- DRLoginViewController *login = [[DRLoginViewController alloc] init];
- [self presentViewController:login animated:YES completion:nil];
- }
- /**
- 改变性别
- */
- - (void)changeUserSex {
- CCActionSheet *actionSheet = [[CCActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:self.sexArr];
- [actionSheet showInView:self.view];
- }
- /**
- 清理缓存
- */
- - (void)clearCacheWith:(NSIndexPath *)indexPath {
- [[SDImageCache sharedImageCache] clearMemory];
- [[SDImageCache sharedImageCache] clearDiskOnCompletion:^{
-
- NSUInteger intg = [[SDImageCache sharedImageCache] getSize];
- NSString * currentVolum = [NSString stringWithFormat:@"%@",[self fileSizeWithInterge:intg]];
- UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
- cell.detailTextLabel.text = currentVolum;
-
- }];
- [YBCacheTool clearCache];
- }
- /**
- 淘宝授权
- */
- - (void)taobaoAuthor {
- if ([[ALBBSession sharedInstance] isLogin]) {
- // DXAlertView *alert = [[DXAlertView alloc] initWithTitle:@"温馨提示" message:@"是否取消淘宝授权?" cancelBtnTitle:@"取消" otherBtnTitle:@"确定"];
- // alert.delegate = self;
- // [alert show];
- UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"是否要取消淘宝授权?" preferredStyle:UIAlertControllerStyleAlert];
- UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
-
- }];
- UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
- ALBBSDK *albbSDK = [ALBBSDK sharedInstance];
- [albbSDK logoutWithCallback:^{
- [[NSNotificationCenter defaultCenter] postNotificationName:ChangeTaoBaoAuthor object:nil];
- [self.tableView reloadData];
- }];
- }];
- [alert addAction:cancel];
- [alert addAction:sure];
- [self presentViewController:alert animated:YES completion:nil];
-
- }else {
- ALBBSDK *albbSDK = [ALBBSDK sharedInstance];
- [albbSDK setAppkey:ALBC_APP_KEY];
- [albbSDK setAuthOption:NormalAuth];
-
- [albbSDK auth:self successCallback:^(ALBBSession *session){
- [[NSNotificationCenter defaultCenter] postNotificationName:ChangeTaoBaoAuthor object:nil];
- // ALBBUser *user = [session getUser];
- [self.tableView reloadData];
- } failureCallback:^(ALBBSession *session,NSError *error){
- NSLog(@"session == %@,error == %@",session,error);
- }];
- [MobClick event:ImpowerShopping];
- }
- }
- #pragma mark ---------------- DXAlertView delegate ---------
- - (void)dxAlertView:(DXAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
- if (buttonIndex == 1) {
- ALBBSDK *albbSDK = [ALBBSDK sharedInstance];
- [albbSDK logoutWithCallback:^{
- [self.tableView reloadData];
- }];
- }
- }
- //计算出大小
- - (NSString *)fileSizeWithInterge:(NSInteger)size{
- // 1k = 1024, 1m = 1024k
- if (size < 1024) {// 小于1k
- return [NSString stringWithFormat:@"%ld B",(long)size];
- }else if (size < 1024 * 1024){// 小于1m
- CGFloat aFloat = size/1024;
- return [NSString stringWithFormat:@"%.0fK",aFloat];
- }else if (size < 1024 * 1024 * 1024){// 小于1G
- CGFloat aFloat = size/(1024 * 1024);
- return [NSString stringWithFormat:@"%.1fM",aFloat];
- }else{
- CGFloat aFloat = size/(1024*1024*1024);
- return [NSString stringWithFormat:@"%.1fG",aFloat];
- }
- }
- #pragma mark ------------------- CCActionSheet Delegate ---------
- - (void)actionSheet:(CCActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
- NSDictionary *para = @{@"sex":@(buttonIndex)};
- if ([AccountTool isLogin]) {
- [DRHttp post:MySetting params:para success:^(id json) {
- NSInteger localSex = [[[NSUserDefaults standardUserDefaults] objectForKey:UserSexKey] integerValue];
-
- if (buttonIndex != localSex) {
- [self saveSexWithButtonIndex:buttonIndex];
- [[NSNotificationCenter defaultCenter] postNotificationName:ChangeSex object:nil];
- }
-
- } failure:^(NSError *error) {
-
- }];
-
- }else {
- [self saveSexWithButtonIndex:buttonIndex];
- [[NSNotificationCenter defaultCenter] postNotificationName:ChangeSex object:nil];
- }
- }
- - (void)saveSexWithButtonIndex:(NSInteger)buttonIndex {
-
- if (buttonIndex == 0) {
- [[NSUserDefaults standardUserDefaults] setObject:@"0" forKey:UserSexKey];
- }else {
- [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:UserSexKey];
- }
- [[NSUserDefaults standardUserDefaults] synchronize];
- [self.tableView reloadData];
- }
- - (UITableView *)tableView {
- if (!_tableView) {
- _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, NavBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
- _tableView.estimatedSectionHeaderHeight = 0;
- _tableView.estimatedSectionFooterHeight = 0;
- _tableView.sectionFooterHeight = 0;
- _tableView.sectionHeaderHeight = 0;
- _tableView.delegate = self;
- _tableView.dataSource = self;
- _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
- _tableView.backgroundColor = [UIColor yhGrayColor];
- _tableView.bounces = YES;
- _tableView.showsVerticalScrollIndicator = NO;
- [_tableView setSeparatorColor:[UIColor YHColorWithHex:0xdddddd]];
- }
- return _tableView;
- }
- -(NSArray *)personArr
- {
- if (!_personArr) {
- _personArr=@[@{@"title":@"我的手机号",@"image":@"push_img"},
- @{@"title":@"我的微信号",@"image":@"push_img"},
- @{@"title":@"我的支付宝",@"image":@"push_img"},
- ];
- }
- return _personArr;
- }
- - (NSArray *)dataArr {
- if (!_dataArr) {
- _dataArr = @[@{@"title":@"推送设置",@"image":@"push_img"},
- @{@"title":@"淘宝授权",@"image":@"taobao_man"},
- ];
- }
- return _dataArr;
- }
- - (NSArray *)sectionDataArr {
- if (!_sectionDataArr) {
- _sectionDataArr = @[
- @{@"title":@"清理缓存",@"image":@"clear_cache"},
- @{@"title":@"五星好评",@"image":@"goodAndStart"},
- @{@"title":@"版本信息",@"image":@"verinfo"},
- ];
- }
- return _sectionDataArr;
- }
- - (NSArray *)sexArr {
- if (!_sexArr) {
- _sexArr = @[@"女",@"男"];
- }
- return _sexArr;
- }
- - (void)dealloc {
- [[NSNotificationCenter defaultCenter] removeObserver:self];
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- /*
- #pragma mark - Navigation
- // In a storyboard-based application, you will often want to do a little preparation before navigation
- - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
- // Get the new view controller using [segue destinationViewController].
- // Pass the selected object to the new view controller.
- }
- */
- @end
|