口袋优选

KBLoginViewController.m 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. //
  2. // KBLoginViewController.m
  3. // YouHuiProject
  4. //
  5. // Created by 小花 on 2018/1/22.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "KBLoginViewController.h"
  9. #import "WLCaptcheButton.h"
  10. #import "KBSendCodeRequest.h"
  11. #import "KBLoginRequest.h"
  12. #import "WXApiManager.h"
  13. #import "TYAlertView.h"
  14. #import "UIView+TYAlertView.h"
  15. #import "TYAlertController.h"
  16. #import "KBLinkPhoneController.h"
  17. #import "KBLinkFansController.h"
  18. #import "KBLinkPhoneController.h"
  19. #import "KBUserInfo.h"
  20. #import "KBOldLoginViewController.h"
  21. #import "PhoneLoginManager.h"
  22. @interface KBLoginViewController ()<WXApiManagerDelegate>
  23. @property (nonatomic, strong) UIImageView *iconImage;
  24. @property (nonatomic, strong) UIButton *wxLoginBtn;
  25. @property (nonatomic, strong) UIButton *phoneLoginBtn;
  26. @end
  27. @implementation KBLoginViewController
  28. - (void)viewWillAppear:(BOOL)animated {
  29. [super viewWillAppear:animated];
  30. if (![PublicFunction isAvailableNetworkType]) {
  31. [MBProgressHUD showMessage:@"当前网络不可用"];
  32. }
  33. }
  34. - (void)viewDidLoad {
  35. [super viewDidLoad];
  36. [self initSubViews];
  37. [self setDelegate];
  38. [self checkWxApp];
  39. }
  40. - (void)initSubViews {
  41. UIImageView *bgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, Fitsize(461))];
  42. bgView.userInteractionEnabled = YES;
  43. bgView.image = [UIImage imageNamed:@"login_img_bg"];
  44. [self.view addSubview:bgView];
  45. UIButton *closeBtn = [[UIButton alloc] initWithFrame:CGRectMake(10, 40, 30, 30)];
  46. [closeBtn setImage:[UIImage imageNamed:@"close_web"] forState:UIControlStateNormal];
  47. [closeBtn addTarget:self action:@selector(closeAction) forControlEvents:UIControlEventTouchUpInside];
  48. [bgView addSubview:closeBtn];
  49. NSDictionary *infoPlist = [[NSBundle mainBundle] infoDictionary];
  50. NSString *icon = [[infoPlist valueForKeyPath:@"CFBundleIcons.CFBundlePrimaryIcon.CFBundleIconFiles"] lastObject];
  51. UIImage* image = [UIImage imageNamed:icon];
  52. self.iconImage = [[UIImageView alloc] initWithFrame:CGRectMake(Fitsize(141), SCREEN_HEIGHT-Fitsize(28)-15, Fitsize(28), Fitsize(28))];
  53. self.iconImage.image = image;
  54. self.iconImage.layer.cornerRadius = 6;
  55. self.iconImage.layer.masksToBounds = YES;
  56. self.iconImage.centerX = SCREEN_WIDTH/2;
  57. [self.view addSubview:self.iconImage];
  58. self.wxLoginBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, bgView.bottom+Fitsize(47), Fitsize(189), Fitsize(40))];
  59. [self.wxLoginBtn setImage:[UIImage imageNamed:@"wx_log"] forState:UIControlStateNormal];
  60. self.wxLoginBtn.centerX = SCREEN_WIDTH/2;
  61. [self.wxLoginBtn addTarget:self action:@selector(weChatLoginAction:) forControlEvents:UIControlEventTouchUpInside];
  62. [self.view addSubview:self.wxLoginBtn];
  63. NSString *deviceType = [UIDevice currentDevice].model;
  64. if([deviceType isEqualToString:@"iPad"] || self.wxLoginBtn.bottom > SCREEN_HEIGHT-100) {
  65. self.wxLoginBtn.bottom = SCREEN_HEIGHT-100;
  66. }
  67. self.phoneLoginBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, self.wxLoginBtn.bottom+15, 150, 30)];
  68. [self.phoneLoginBtn setTitle:@"手机号码登录" forState:UIControlStateNormal];
  69. self.phoneLoginBtn.centerX = self.wxLoginBtn.centerX;
  70. self.phoneLoginBtn.backgroundColor = [UIColor greenColor];
  71. self.phoneLoginBtn.layer.cornerRadius = 15;
  72. [self.phoneLoginBtn setTitleColor:[UIColor homeRedColor] forState:UIControlStateNormal];
  73. [self.view addSubview:self.phoneLoginBtn];
  74. [self.phoneLoginBtn addTarget:self action:@selector(phoneLoginClick) forControlEvents:UIControlEventTouchUpInside];
  75. self.phoneLoginBtn.titleLabel.font = [UIFont systemFontOfSize:14];
  76. self.phoneLoginBtn.hidden = YES;
  77. NSNumber *showPhoneLogin = [PhoneLoginManager shareManager].showPhoneBtn;
  78. if (showPhoneLogin == nil) {
  79. [self loadPhoneLogInButton];
  80. }else {
  81. self.phoneLoginBtn.hidden = !showPhoneLogin.boolValue;
  82. }
  83. }
  84. - (void)checkWxApp {
  85. self.wxLoginBtn.hidden = ![WXApi isWXAppInstalled];
  86. }
  87. - (void)loadPhoneLogInButton {
  88. NSString *url = [NSString stringWithFormat:@"%@/api/v2/channel/getDevByChannelId",BaseURL];
  89. NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
  90. NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
  91. NSDictionary *para = @{@"channel_id":Channel_id,
  92. @"version":app_Version
  93. };
  94. [KBHttp get:url params:para success:^(id json) {
  95. if ([json[@"ios"] boolValue]) {
  96. self.phoneLoginBtn.hidden = NO;
  97. }else {
  98. self.phoneLoginBtn.hidden = YES;
  99. }
  100. } failure:^(NSError *error) {
  101. }];
  102. }
  103. - (void)setDelegate {
  104. [WXApiManager sharedManager].delegate = self;
  105. }
  106. - (void)didReceiveMemoryWarning {
  107. [super didReceiveMemoryWarning];
  108. // Dispose of any resources that can be recreated.
  109. }
  110. /**
  111. 关闭
  112. */
  113. - (void)closeAction {
  114. [SVProgressHUD dismiss];
  115. [self dismissViewControllerAnimated:YES completion:nil];
  116. }
  117. /**
  118. 微信登录
  119. */
  120. - (void)weChatLoginAction:(id)sender {
  121. // [self sendReq];
  122. //登录前先取消友盟授权,友盟授权会带缓存
  123. [SVProgressHUD show];
  124. [[UMSocialManager defaultManager] cancelAuthWithPlatform:UMSocialPlatformType_WechatSession completion:^(id result, NSError *error) {
  125. if (error) {
  126. [SVProgressHUD dismiss];
  127. [MBProgressHUD showMessage:@"读取微信信息失败,请重试"];
  128. }
  129. [self getWechatAuthorInfo];
  130. }];
  131. }
  132. /**
  133. 唤起微信进行授权
  134. */
  135. - (void)getWechatAuthorInfo {
  136. [AccountTool deleteAccount];
  137. [[KBUMbindWeChat sharedInstance]loginWithPlatformSuccess:^(UMSocialUserInfoResponse *response) {
  138. if (response.openid && response.unionId && response.iconurl && response.name) {
  139. }else {
  140. [SVProgressHUD dismiss];
  141. [MBProgressHUD showMessage:@"读取微信信息失败,请重试"];
  142. return;
  143. }
  144. NSString *sex = response.unionGender==nil?@"":response.unionGender;
  145. NSString *country = response.originalResponse[@"country"]==nil?@"":response.originalResponse[@"country"];
  146. NSString *province = response.originalResponse[@"province"]==nil?@"":response.originalResponse[@"province"];
  147. NSString *city = response.originalResponse[@"city"]==nil?@"":response.originalResponse[@"city"];
  148. NSDictionary *para = @{@"openid":response.openid,
  149. @"unionid":response.unionId,
  150. @"nickname":response.name,
  151. @"headimgurl":response.iconurl,
  152. @"gender":sex,
  153. @"country":country,
  154. @"province":province,
  155. @"city":city
  156. };
  157. NSString *url = [NSString stringWithFormat:@"%@/api/v2/users/weChatLoginNew",BaseURL];
  158. [KBHttp post:url params:para success:^(id json) {
  159. BOOL is_binded_mobile = [json[@"is_binded_mobile"] boolValue];
  160. if (is_binded_mobile) {
  161. //已绑定手机,直接登录成功
  162. AccountModel *model = [AccountModel yy_modelWithJSON:json];
  163. model.unionid = response.unionId;
  164. model.openId = response.openid;
  165. [AccountTool saveAccount:model];
  166. NSString *sex = [NSString stringWithFormat:@"%@",json[@"sex"]];
  167. [[NSUserDefaults standardUserDefaults] setObject:sex forKey:UserSexKey];
  168. [[NSUserDefaults standardUserDefaults] synchronize];
  169. [[NSNotificationCenter defaultCenter] postNotificationName:ChangeSex object:nil];
  170. if (self.loginSucc) {
  171. self.loginSucc();
  172. }
  173. [self dismissViewControllerAnimated:YES completion:nil];
  174. }else {
  175. //未绑定手机 去绑定
  176. KBLinkPhoneController *linkPhone = [[KBLinkPhoneController alloc] init];
  177. [self dismissViewControllerAnimated:NO completion:nil];
  178. UITabBarController *tabVC = (UITabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
  179. linkPhone.wx_union_id = response.unionId;
  180. linkPhone.wx_open_id = response.openid;
  181. [tabVC presentViewController:linkPhone animated:YES completion:nil];
  182. }
  183. [SVProgressHUD dismiss];
  184. } failure:^(NSError *error) {
  185. [SVProgressHUD dismiss];
  186. [MBProgressHUD showMessage:@"加载失败"];
  187. }];
  188. } fail:^(NSError *error) {
  189. [SVProgressHUD dismiss];
  190. [MBProgressHUD showMessage:@"读取微信信息失败,请重试"];
  191. }];
  192. }
  193. /**
  194. 手机号码登录
  195. */
  196. - (void)phoneLoginClick {
  197. KBOldLoginViewController *old = [[KBOldLoginViewController alloc] init];
  198. old.loginSucc = self.loginSucc;
  199. [self dismissViewControllerAnimated:NO completion:nil];
  200. UITabBarController *tabVC = (UITabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
  201. [tabVC presentViewController:old animated:YES completion:^{
  202. }];
  203. }
  204. @end