123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270 |
- //
- // KBLoginViewController.m
- // YouHuiProject
- //
- // Created by 小花 on 2018/1/22.
- // Copyright © 2018年 kuxuan. All rights reserved.
- //
- #import "KBLoginViewController.h"
- #import "WLCaptcheButton.h"
- #import "KBSendCodeRequest.h"
- #import "KBLoginRequest.h"
- #import "WXApiManager.h"
- #import "TYAlertView.h"
- #import "UIView+TYAlertView.h"
- #import "TYAlertController.h"
- #import "KBLinkPhoneController.h"
- #import "KBLinkFansController.h"
- #import "KBLinkPhoneController.h"
- #import "KBUserInfo.h"
- #import "KBOldLoginViewController.h"
- #import "PhoneLoginManager.h"
- @interface KBLoginViewController ()<WXApiManagerDelegate>
- @property (nonatomic, strong) UIImageView *iconImage;
- @property (nonatomic, strong) UIButton *wxLoginBtn;
- @property (nonatomic, strong) UIButton *phoneLoginBtn;
- @end
- @implementation KBLoginViewController
- - (void)viewWillAppear:(BOOL)animated {
- [super viewWillAppear:animated];
- if (![PublicFunction isAvailableNetworkType]) {
- [MBProgressHUD showMessage:@"当前网络不可用"];
- }
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
-
- [self initSubViews];
- [self setDelegate];
- [self checkWxApp];
-
- }
- - (void)initSubViews {
-
-
- UIImageView *bgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, Fitsize(461))];
- bgView.userInteractionEnabled = YES;
- bgView.image = [UIImage imageNamed:@"login_img_bg"];
- [self.view addSubview:bgView];
-
- UIButton *closeBtn = [[UIButton alloc] initWithFrame:CGRectMake(10, 40, 30, 30)];
- [closeBtn setImage:[UIImage imageNamed:@"close_web"] forState:UIControlStateNormal];
- [closeBtn addTarget:self action:@selector(closeAction) forControlEvents:UIControlEventTouchUpInside];
- [bgView addSubview:closeBtn];
-
- NSDictionary *infoPlist = [[NSBundle mainBundle] infoDictionary];
- NSString *icon = [[infoPlist valueForKeyPath:@"CFBundleIcons.CFBundlePrimaryIcon.CFBundleIconFiles"] lastObject];
- UIImage* image = [UIImage imageNamed:icon];
- self.iconImage = [[UIImageView alloc] initWithFrame:CGRectMake(Fitsize(141), SCREEN_HEIGHT-Fitsize(28)-15, Fitsize(28), Fitsize(28))];
- self.iconImage.image = image;
- self.iconImage.layer.cornerRadius = 6;
- self.iconImage.layer.masksToBounds = YES;
- self.iconImage.centerX = SCREEN_WIDTH/2;
- [self.view addSubview:self.iconImage];
-
- self.wxLoginBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, bgView.bottom+Fitsize(47), Fitsize(189), Fitsize(40))];
- [self.wxLoginBtn setImage:[UIImage imageNamed:@"wx_log"] forState:UIControlStateNormal];
- self.wxLoginBtn.centerX = SCREEN_WIDTH/2;
- [self.wxLoginBtn addTarget:self action:@selector(weChatLoginAction:) forControlEvents:UIControlEventTouchUpInside];
- [self.view addSubview:self.wxLoginBtn];
-
- NSString *deviceType = [UIDevice currentDevice].model;
- if([deviceType isEqualToString:@"iPad"] || self.wxLoginBtn.bottom > SCREEN_HEIGHT-100) {
- self.wxLoginBtn.bottom = SCREEN_HEIGHT-100;
- }
-
- self.phoneLoginBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, self.wxLoginBtn.bottom+15, 150, 30)];
- [self.phoneLoginBtn setTitle:@"手机号码登录" forState:UIControlStateNormal];
- self.phoneLoginBtn.centerX = self.wxLoginBtn.centerX;
- self.phoneLoginBtn.backgroundColor = [UIColor greenColor];
- self.phoneLoginBtn.layer.cornerRadius = 15;
- [self.phoneLoginBtn setTitleColor:[UIColor homeRedColor] forState:UIControlStateNormal];
- [self.view addSubview:self.phoneLoginBtn];
- [self.phoneLoginBtn addTarget:self action:@selector(phoneLoginClick) forControlEvents:UIControlEventTouchUpInside];
- self.phoneLoginBtn.titleLabel.font = [UIFont systemFontOfSize:14];
- self.phoneLoginBtn.hidden = YES;
- NSNumber *showPhoneLogin = [PhoneLoginManager shareManager].showPhoneBtn;
- if (showPhoneLogin == nil) {
- [self loadPhoneLogInButton];
- }else {
- self.phoneLoginBtn.hidden = !showPhoneLogin.boolValue;
- }
-
- }
- - (void)checkWxApp {
-
- self.wxLoginBtn.hidden = ![WXApi isWXAppInstalled];
- }
- - (void)loadPhoneLogInButton {
-
- NSString *url = [NSString stringWithFormat:@"%@/api/v2/channel/getDevByChannelId",BaseURL];
- NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
- NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
- NSDictionary *para = @{@"channel_id":Channel_id,
- @"version":app_Version
- };
- [KBHttp get:url params:para success:^(id json) {
- if ([json[@"ios"] boolValue]) {
- self.phoneLoginBtn.hidden = NO;
- }else {
- self.phoneLoginBtn.hidden = YES;
- }
-
- } failure:^(NSError *error) {
-
- }];
-
- }
- - (void)setDelegate {
- [WXApiManager sharedManager].delegate = self;
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- /**
- 关闭
- */
- - (void)closeAction {
- [SVProgressHUD dismiss];
- [self dismissViewControllerAnimated:YES completion:nil];
- }
- /**
- 微信登录
- */
- - (void)weChatLoginAction:(id)sender {
- // [self sendReq];
-
- //登录前先取消友盟授权,友盟授权会带缓存
- [SVProgressHUD show];
- [[UMSocialManager defaultManager] cancelAuthWithPlatform:UMSocialPlatformType_WechatSession completion:^(id result, NSError *error) {
- if (error) {
- [SVProgressHUD dismiss];
- [MBProgressHUD showMessage:@"读取微信信息失败,请重试"];
- }
- [self getWechatAuthorInfo];
- }];
-
-
- }
- /**
- 唤起微信进行授权
- */
- - (void)getWechatAuthorInfo {
- [AccountTool deleteAccount];
- [[KBUMbindWeChat sharedInstance]loginWithPlatformSuccess:^(UMSocialUserInfoResponse *response) {
- if (response.openid && response.unionId && response.iconurl && response.name) {
-
- }else {
- [SVProgressHUD dismiss];
- [MBProgressHUD showMessage:@"读取微信信息失败,请重试"];
- return;
- }
-
- NSString *sex = response.unionGender==nil?@"":response.unionGender;
- NSString *country = response.originalResponse[@"country"]==nil?@"":response.originalResponse[@"country"];
- NSString *province = response.originalResponse[@"province"]==nil?@"":response.originalResponse[@"province"];
- NSString *city = response.originalResponse[@"city"]==nil?@"":response.originalResponse[@"city"];
-
- NSDictionary *para = @{@"openid":response.openid,
- @"unionid":response.unionId,
- @"nickname":response.name,
- @"headimgurl":response.iconurl,
- @"gender":sex,
- @"country":country,
- @"province":province,
- @"city":city
- };
- NSString *url = [NSString stringWithFormat:@"%@/api/v2/users/weChatLoginNew",BaseURL];
- [KBHttp post:url params:para success:^(id json) {
-
- BOOL is_binded_mobile = [json[@"is_binded_mobile"] boolValue];
-
- if (is_binded_mobile) {
- //已绑定手机,直接登录成功
-
- AccountModel *model = [AccountModel yy_modelWithJSON:json];
- model.unionid = response.unionId;
- model.openId = response.openid;
- [AccountTool saveAccount:model];
- NSString *sex = [NSString stringWithFormat:@"%@",json[@"sex"]];
- [[NSUserDefaults standardUserDefaults] setObject:sex forKey:UserSexKey];
- [[NSUserDefaults standardUserDefaults] synchronize];
- [[NSNotificationCenter defaultCenter] postNotificationName:ChangeSex object:nil];
- if (self.loginSucc) {
- self.loginSucc();
- }
- [self dismissViewControllerAnimated:YES completion:nil];
- }else {
- //未绑定手机 去绑定
- KBLinkPhoneController *linkPhone = [[KBLinkPhoneController alloc] init];
- [self dismissViewControllerAnimated:NO completion:nil];
- UITabBarController *tabVC = (UITabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
- linkPhone.wx_union_id = response.unionId;
- linkPhone.wx_open_id = response.openid;
- [tabVC presentViewController:linkPhone animated:YES completion:nil];
- }
-
-
- [SVProgressHUD dismiss];
- } failure:^(NSError *error) {
- [SVProgressHUD dismiss];
- [MBProgressHUD showMessage:@"加载失败"];
- }];
- } fail:^(NSError *error) {
- [SVProgressHUD dismiss];
- [MBProgressHUD showMessage:@"读取微信信息失败,请重试"];
- }];
- }
- /**
- 手机号码登录
- */
- - (void)phoneLoginClick {
- KBOldLoginViewController *old = [[KBOldLoginViewController alloc] init];
- old.loginSucc = self.loginSucc;
- [self dismissViewControllerAnimated:NO completion:nil];
- UITabBarController *tabVC = (UITabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
- [tabVC presentViewController:old animated:YES completion:^{
-
- }];
- }
- @end
|