《省钱达人》与《猎豆优选》UI相同版。域名tbk

DRSettingViewController.m 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. //
  2. // DRSettingViewController.m
  3. // YouHuiProject
  4. //
  5. // Created by 小花 on 2018/1/25.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "DRSettingViewController.h"
  9. #import "DRAuthorityManager.h"
  10. #import "CCActionSheet.h"
  11. #import <AlibabaAuthSDK/ALBBSession.h>
  12. #import <AlibabaAuthSDK/ALBBSDK.h>
  13. #import "DXAlertView.h"
  14. #import "DRLoginViewController.h"
  15. #import <StoreKit/StoreKit.h>
  16. #import "DRMySuperViewController.h"
  17. #import "DRMyWechatViewController.h"
  18. #import "YBCacheTool.h"
  19. #import "DRLinkAliPayViewController.h"
  20. @interface DRSettingViewController ()<UITableViewDelegate,UITableViewDataSource,CCActionSheetDelegate,DXAlertViewDelegate>
  21. {
  22. NSString *_weChatStr;
  23. }
  24. @property (nonatomic, strong) UITableView *tableView;
  25. @property (nonatomic, strong) NSArray *dataArr;
  26. @property (nonatomic, strong) NSArray *personArr;
  27. @property (nonatomic, strong) NSArray *sectionDataArr;
  28. @property (nonatomic, strong) NSArray *sexArr;
  29. @property (nonatomic, strong) NSDictionary *userInfo;
  30. @end
  31. @implementation DRSettingViewController
  32. - (void)viewWillAppear:(BOOL)animated {
  33. [super viewWillAppear:animated];
  34. [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
  35. self.navigationBar.backgroundColor = [UIColor changeColor];
  36. [self requestWeChatStr];
  37. [self loadUserInfo];
  38. }
  39. - (void)viewDidLoad {
  40. [super viewDidLoad];
  41. [self configNavigationBar];
  42. [self addObserToController];
  43. }
  44. - (void)configNavigationBar {
  45. self.view.backgroundColor = [UIColor whiteColor];
  46. [self.navigationBar setNavTitle:@"设置"];
  47. self.navigationBar.showNavigationBarBottomLine = YES;
  48. [self.view addSubview:self.tableView];
  49. self.navigationBar.navTitleLabel.textColor = [UIColor whiteColor];
  50. UIButton *leftBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
  51. [leftBtn setImage:[UIImage imageNamed:@"back_white"] forState:UIControlStateNormal];
  52. [leftBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
  53. [self.navigationBar setCustomLeftButtons:@[leftBtn]];
  54. UILabel *logOut = [[UILabel alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT-50-BottomMargin, SCREEN_WIDTH, 50)];
  55. logOut.backgroundColor = [UIColor whiteColor];
  56. logOut.textColor = [UIColor homeRedColor];
  57. logOut.textAlignment = NSTextAlignmentCenter;
  58. logOut.font = [UIFont systemFontOfSize:14];
  59. logOut.text = [AccountTool isLogin] ? @"退出登录":@"快速登录";
  60. logOut.userInteractionEnabled = YES;
  61. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(logOutAction)];
  62. [logOut addGestureRecognizer:tap];
  63. [self.view addSubview:logOut];}
  64. - (void)addObserToController {
  65. [[NSNotificationCenter defaultCenter] addObserver:self
  66. selector:@selector(becomActive)
  67. name:UIApplicationDidBecomeActiveNotification object:nil];
  68. }
  69. - (void)requestWeChatStr {
  70. if (![AccountTool isLogin]) {
  71. return;
  72. }
  73. // NSString *url = [NSString stringWithFormat:@"%@/api/v2/adzoneCreate/displayWeChat",BaseURL];
  74. // [DRHttp post:url params:nil success:^(id json) {
  75. //
  76. // NSString *wechat = [NSString stringWithFormat:@"%@",json[@"weChat"]];
  77. // if (wechat.length > 0) {
  78. // _weChatStr = wechat;
  79. // }
  80. // [self.tableView reloadData];
  81. // } failure:^(NSError *error) {
  82. //
  83. // }];
  84. }
  85. - (void)becomActive {
  86. [self.tableView reloadData];
  87. }
  88. /**
  89. 返回
  90. */
  91. - (void)backAction {
  92. [self.navigationController popViewControllerAnimated:YES];
  93. }
  94. /**
  95. 退出登录
  96. */
  97. - (void)logOutAction {
  98. if ([AccountTool isLogin]) {
  99. UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"是否要退出登录?" preferredStyle:UIAlertControllerStyleAlert];
  100. UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  101. }];
  102. UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
  103. [self logOutManager];
  104. }];
  105. [alert addAction:cancel];
  106. [alert addAction:sure];
  107. [self presentViewController:alert animated:YES completion:nil];
  108. }else {
  109. DRLoginViewController *login = [[DRLoginViewController alloc] init];
  110. [self presentViewController:login animated:YES completion:nil];
  111. }
  112. }
  113. - (void)logOutManager {
  114. [DRHttp post:Logout params:nil success:^(id json) {
  115. [AccountTool deleteAccount];
  116. [[NSNotificationCenter defaultCenter] postNotificationName:ChangeSex object:nil];
  117. [self.navigationController popViewControllerAnimated:YES];
  118. } failure:^(NSError *error) {
  119. }];
  120. }
  121. /**
  122. switch 点击
  123. */
  124. - (void)switchAction {
  125. NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
  126. if([[UIApplication sharedApplication]canOpenURL:url] ) {
  127. if (@available(iOS 10.0, *)) {
  128. [[UIApplication sharedApplication] openURL:url options:@{}completionHandler:^(BOOL success) {
  129. }];
  130. } else {
  131. [[UIApplication sharedApplication]openURL:url];
  132. }
  133. }
  134. }
  135. #pragma mark ------------------
  136. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
  137. if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
  138. [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
  139. }
  140. }
  141. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  142. if (section == 0) {
  143. return self.sectionDataArr.count;
  144. }
  145. if (section == 1) {
  146. return self.dataArr.count;
  147. }
  148. return self.dataArr.count;
  149. }
  150. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  151. return 50;
  152. }
  153. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  154. return FITSIZE(5);
  155. }
  156. -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  157. {
  158. return 2;
  159. }
  160. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  161. UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cellID"];
  162. if ((indexPath.section == 0 && indexPath.row !=2) ||(indexPath.section == 1 &&indexPath.row !=0)) {
  163. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  164. }
  165. if (indexPath.row == 0 && indexPath.section ==1) {
  166. UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-60, 10, 50, 30)];
  167. [switchView addTarget:self action:@selector(switchAction) forControlEvents:UIControlEventTouchUpInside]; // 开关事件切换通知
  168. [cell addSubview:switchView];
  169. if ([DRAuthorityManager isObtainUserNotificationAuthority]) {
  170. switchView.on = YES;
  171. }else {
  172. switchView.on = NO;
  173. }
  174. }
  175. if (indexPath.section == 0) {
  176. cell.textLabel.text = self.sectionDataArr[indexPath.row][@"title"];
  177. }else if (indexPath.section == 1) {
  178. cell.textLabel.text = self.dataArr[indexPath.row][@"title"];
  179. }
  180. cell.textLabel.font = [UIFont systemFontOfSize:15];
  181. cell.textLabel.textColor = [UIColor YHColorWithHex:0x666666];
  182. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  183. cell.detailTextLabel.font = [UIFont systemFontOfSize:15];
  184. cell.detailTextLabel.textColor = [UIColor YHColorWithHex:0x999999];
  185. cell.detailTextLabel.textAlignment=NSTextAlignmentRight;
  186. //缓存
  187. if (indexPath.row == 0 && indexPath.section ==0) {
  188. NSUInteger intg = [[SDImageCache sharedImageCache] getSize];
  189. NSString * currentVolum = [NSString stringWithFormat:@"%@",[self fileSizeWithInterge:intg]];
  190. cell.detailTextLabel.text = currentVolum;
  191. }
  192. if (indexPath.row == 1 && indexPath.section ==1) {
  193. if ([[ALBBSession sharedInstance] isLogin]) {
  194. cell.detailTextLabel.text = @"已授权";
  195. }else {
  196. cell.detailTextLabel.text = @"未授权";
  197. }
  198. }
  199. if (indexPath.section ==0 && indexPath.row ==2) {
  200. NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
  201. NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
  202. cell.detailTextLabel.text = app_Version;
  203. }
  204. return cell;
  205. }
  206. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  207. if (indexPath.section == 1) {
  208. switch (indexPath.row) {
  209. case 0:
  210. break;
  211. case 1:
  212. //淘宝授权
  213. [self taobaoAuthor];
  214. break;
  215. default:
  216. break;
  217. }
  218. }
  219. if (indexPath.section ==0) {
  220. switch (indexPath.row) {
  221. case 0:
  222. //清理缓存
  223. [self clearCacheWith:indexPath];
  224. break;
  225. case 1:
  226. //五星好评
  227. [self commentAndStart];
  228. break;
  229. default:
  230. break;
  231. }
  232. }
  233. }
  234. /**
  235. 加载用户信息
  236. */
  237. - (void)loadUserInfo {
  238. [DRCacheHttp post:NewUserInfo params:nil success:^(id json, BOOL isCache) {
  239. self.userInfo =[NSDictionary dictionaryWithDictionary:json[@"data"]];
  240. [self.tableView reloadData];
  241. } failure:^(NSError *error) {
  242. }];
  243. }
  244. #pragma mark ------------ private----------
  245. /**
  246. 五星好评
  247. */
  248. - (void)commentAndStart {
  249. NSString * nsStringToOpen = [NSString stringWithFormat: @"itms-apps://itunes.apple.com/app/id%@?action=write-review",APP_ID];//替换为对应的APPID
  250. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:nsStringToOpen]];
  251. }
  252. /**
  253. 支付宝账号
  254. */
  255. - (void)alipayAccount {
  256. DRLinkAliPayViewController *alipay = [[DRLinkAliPayViewController alloc] init];
  257. alipay.isUpdate = [self.alipayFlag boolValue];
  258. [self.navigationController pushViewController:alipay animated:YES];
  259. }
  260. /**
  261. 用户登录
  262. */
  263. - (void)goToLoginPage {
  264. DRLoginViewController *login = [[DRLoginViewController alloc] init];
  265. [self presentViewController:login animated:YES completion:nil];
  266. }
  267. /**
  268. 改变性别
  269. */
  270. - (void)changeUserSex {
  271. CCActionSheet *actionSheet = [[CCActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:self.sexArr];
  272. [actionSheet showInView:self.view];
  273. }
  274. /**
  275. 清理缓存
  276. */
  277. - (void)clearCacheWith:(NSIndexPath *)indexPath {
  278. [[SDImageCache sharedImageCache] clearMemory];
  279. [[SDImageCache sharedImageCache] clearDiskOnCompletion:^{
  280. NSUInteger intg = [[SDImageCache sharedImageCache] getSize];
  281. NSString * currentVolum = [NSString stringWithFormat:@"%@",[self fileSizeWithInterge:intg]];
  282. UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
  283. cell.detailTextLabel.text = currentVolum;
  284. }];
  285. [YBCacheTool clearCache];
  286. }
  287. /**
  288. 淘宝授权
  289. */
  290. - (void)taobaoAuthor {
  291. if ([[ALBBSession sharedInstance] isLogin]) {
  292. // DXAlertView *alert = [[DXAlertView alloc] initWithTitle:@"温馨提示" message:@"是否取消淘宝授权?" cancelBtnTitle:@"取消" otherBtnTitle:@"确定"];
  293. // alert.delegate = self;
  294. // [alert show];
  295. UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"是否要取消淘宝授权?" preferredStyle:UIAlertControllerStyleAlert];
  296. UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  297. }];
  298. UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
  299. ALBBSDK *albbSDK = [ALBBSDK sharedInstance];
  300. [albbSDK logoutWithCallback:^{
  301. [[NSNotificationCenter defaultCenter] postNotificationName:ChangeTaoBaoAuthor object:nil];
  302. [self.tableView reloadData];
  303. }];
  304. }];
  305. [alert addAction:cancel];
  306. [alert addAction:sure];
  307. [self presentViewController:alert animated:YES completion:nil];
  308. }else {
  309. ALBBSDK *albbSDK = [ALBBSDK sharedInstance];
  310. [albbSDK setAppkey:ALBC_APP_KEY];
  311. [albbSDK setAuthOption:NormalAuth];
  312. [albbSDK auth:self successCallback:^(ALBBSession *session){
  313. [[NSNotificationCenter defaultCenter] postNotificationName:ChangeTaoBaoAuthor object:nil];
  314. // ALBBUser *user = [session getUser];
  315. [self.tableView reloadData];
  316. } failureCallback:^(ALBBSession *session,NSError *error){
  317. NSLog(@"session == %@,error == %@",session,error);
  318. }];
  319. [MobClick event:ImpowerShopping];
  320. }
  321. }
  322. #pragma mark ---------------- DXAlertView delegate ---------
  323. - (void)dxAlertView:(DXAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
  324. if (buttonIndex == 1) {
  325. ALBBSDK *albbSDK = [ALBBSDK sharedInstance];
  326. [albbSDK logoutWithCallback:^{
  327. [self.tableView reloadData];
  328. }];
  329. }
  330. }
  331. //计算出大小
  332. - (NSString *)fileSizeWithInterge:(NSInteger)size{
  333. // 1k = 1024, 1m = 1024k
  334. if (size < 1024) {// 小于1k
  335. return [NSString stringWithFormat:@"%ld B",(long)size];
  336. }else if (size < 1024 * 1024){// 小于1m
  337. CGFloat aFloat = size/1024;
  338. return [NSString stringWithFormat:@"%.0fK",aFloat];
  339. }else if (size < 1024 * 1024 * 1024){// 小于1G
  340. CGFloat aFloat = size/(1024 * 1024);
  341. return [NSString stringWithFormat:@"%.1fM",aFloat];
  342. }else{
  343. CGFloat aFloat = size/(1024*1024*1024);
  344. return [NSString stringWithFormat:@"%.1fG",aFloat];
  345. }
  346. }
  347. #pragma mark ------------------- CCActionSheet Delegate ---------
  348. - (void)actionSheet:(CCActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
  349. NSDictionary *para = @{@"sex":@(buttonIndex)};
  350. if ([AccountTool isLogin]) {
  351. [DRHttp post:MySetting params:para success:^(id json) {
  352. NSInteger localSex = [[[NSUserDefaults standardUserDefaults] objectForKey:UserSexKey] integerValue];
  353. if (buttonIndex != localSex) {
  354. [self saveSexWithButtonIndex:buttonIndex];
  355. [[NSNotificationCenter defaultCenter] postNotificationName:ChangeSex object:nil];
  356. }
  357. } failure:^(NSError *error) {
  358. }];
  359. }else {
  360. [self saveSexWithButtonIndex:buttonIndex];
  361. [[NSNotificationCenter defaultCenter] postNotificationName:ChangeSex object:nil];
  362. }
  363. }
  364. - (void)saveSexWithButtonIndex:(NSInteger)buttonIndex {
  365. if (buttonIndex == 0) {
  366. [[NSUserDefaults standardUserDefaults] setObject:@"0" forKey:UserSexKey];
  367. }else {
  368. [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:UserSexKey];
  369. }
  370. [[NSUserDefaults standardUserDefaults] synchronize];
  371. [self.tableView reloadData];
  372. }
  373. - (UITableView *)tableView {
  374. if (!_tableView) {
  375. _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, NavBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
  376. _tableView.estimatedSectionHeaderHeight = 0;
  377. _tableView.estimatedSectionFooterHeight = 0;
  378. _tableView.sectionFooterHeight = 0;
  379. _tableView.sectionHeaderHeight = 0;
  380. _tableView.delegate = self;
  381. _tableView.dataSource = self;
  382. _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
  383. _tableView.backgroundColor = [UIColor yhGrayColor];
  384. _tableView.bounces = YES;
  385. _tableView.showsVerticalScrollIndicator = NO;
  386. [_tableView setSeparatorColor:[UIColor YHColorWithHex:0xdddddd]];
  387. }
  388. return _tableView;
  389. }
  390. -(NSArray *)personArr
  391. {
  392. if (!_personArr) {
  393. _personArr=@[@{@"title":@"我的手机号",@"image":@"push_img"},
  394. @{@"title":@"我的微信号",@"image":@"push_img"},
  395. @{@"title":@"我的支付宝",@"image":@"push_img"},
  396. ];
  397. }
  398. return _personArr;
  399. }
  400. - (NSArray *)dataArr {
  401. if (!_dataArr) {
  402. _dataArr = @[@{@"title":@"推送设置",@"image":@"push_img"},
  403. @{@"title":@"淘宝授权",@"image":@"taobao_man"},
  404. ];
  405. }
  406. return _dataArr;
  407. }
  408. - (NSArray *)sectionDataArr {
  409. if (!_sectionDataArr) {
  410. _sectionDataArr = @[
  411. @{@"title":@"清理缓存",@"image":@"clear_cache"},
  412. @{@"title":@"五星好评",@"image":@"goodAndStart"},
  413. @{@"title":@"版本信息",@"image":@"verinfo"},
  414. ];
  415. }
  416. return _sectionDataArr;
  417. }
  418. - (NSArray *)sexArr {
  419. if (!_sexArr) {
  420. _sexArr = @[@"女",@"男"];
  421. }
  422. return _sexArr;
  423. }
  424. - (void)dealloc {
  425. [[NSNotificationCenter defaultCenter] removeObserver:self];
  426. }
  427. - (void)didReceiveMemoryWarning {
  428. [super didReceiveMemoryWarning];
  429. // Dispose of any resources that can be recreated.
  430. }
  431. /*
  432. #pragma mark - Navigation
  433. // In a storyboard-based application, you will often want to do a little preparation before navigation
  434. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  435. // Get the new view controller using [segue destinationViewController].
  436. // Pass the selected object to the new view controller.
  437. }
  438. */
  439. @end