美文网首页selector
第三方平台账号的绑定

第三方平台账号的绑定

作者: 刀客传奇 | 来源:发表于2017-05-27 23:54 被阅读18次

    版本记录

    版本号 时间
    V1.0 2017.05.27

    前言

      很多时候,我们app的个人中心这个模块都需要绑定QQ,微信和微博等第三方平台的需求,我们以前的项目也有这方面的需求,正好我也负责这一块,下面我就详细的和大家说一个这块的实现方法。

    详情

    一、需求展示

    首先我们先看一下需求,如下图所示:

    第三方平台绑定需求

      我这里都绑定好了,所以这里都显示的是已绑定,正常的需求是如果没有绑定,这里就显示未绑定,大家凑合着看吧,知道意思即可。


    二、技术方案

    我这里可以想到的是两种方案:

    • 集成QQ等需要绑定的QQ,微信等SDK,根据接口发送请求获取openid,location,nickname等参数,然后在给自己的服务器发送请求,实现绑定。
    • 利用第三方SDK,比如shareSDK直接可以获取接口数据信息,然后给请求自己的服务器绑定。

    我这里采取的是第二种方法,下面我分析下优缺点:

    • 优点:与登录模块使用相同的shareSDK,这里同样用这个就减小了包的大小,同时减少了分着平台集成的工作量,减少时间成本。
    • 缺点:如果是分着集成,可以很自由的功能定制,使用第三方ShareSDK受限于接口规则,不能自由定制,自由度相对小,但是这里采用第二种方案已足够,可以了。

    三、方案实现

      下面我就给出主要的代码实现,至于具体的ShareSDK的集成,schemeURL的配置和跳转我就不说了,感兴趣的可以看我的别的博客,里面都有讲述,下面看代码。

    1. cell的点击方法
    
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        
        self.activityIndicatorView = [[JJUIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
        self.activityIndicatorView.center = self.view.center;
        [self.view addSubview:self.activityIndicatorView];
        [self.activityIndicatorView startAnimating];
        
        if (indexPath.section == 0) {
            switch (indexPath.row) {
                    
                //手机号
                case 0:
                    {
                        if (self.mobileBindStr.length < 5) {
                            NSLog(@"可以绑定手机号了");
                            JJLoginNewUserRegistVC *mobileBindedVC = [[JJLoginNewUserRegistVC alloc] init];
                            mobileBindedVC.isBindPhoneNum = YES;
                            [self.navigationController pushViewController:mobileBindedVC animated:YES];
                        }
                    }
                    break;
                    
                //QQ号
                case 1:
                {
                    if (!self.isQQBinded) {
                        NSLog(@"可以绑定QQ号了");
                        
                        [SSEThirdPartyLoginHelper loginByPlatform:SSDKPlatformTypeQQ onUserSync:^(SSDKUser *user, SSEUserAssociateHandler associateHandler) {
                            [self requestLogin:user loginType:SSDKPlatformTypeQQ];
                        } onLoginResult:^(SSDKResponseState state, SSEBaseUser *user, NSError *error) {
                            if (state == SSDKResponseStateSuccess) {
                                NSLog(@"登录成功");
                            }
                            [self.activityIndicatorView stopAnimating];
                            [self.activityIndicatorView setHidesWhenStopped:YES];
                        }];
                    }
                }
                    break;
                    
                //微信号
                    
                case 2:
                {
                    if (!self.isWechatBinded) {
                        NSLog(@"可以绑定微信号了");
                        
                        [SSEThirdPartyLoginHelper loginByPlatform:SSDKPlatformTypeWechat onUserSync:^(SSDKUser *user, SSEUserAssociateHandler associateHandler) {
                            [self requestLogin:user loginType:SSDKPlatformTypeWechat];
                        } onLoginResult:^(SSDKResponseState state, SSEBaseUser *user, NSError *error) {
                            if (state == SSDKResponseStateSuccess) {
                                NSLog(@"登录成功");
                            }
                            [self.activityIndicatorView stopAnimating];
                            [self.activityIndicatorView setHidesWhenStopped:YES];
                        }];
                    }
                }
                    break;
                    
                //微博号
                case 3:
                {
                    if (!self.isWeiboBinded) {
                        NSLog(@"可以绑定微博号了");
                        
                        [SSEThirdPartyLoginHelper loginByPlatform:SSDKPlatformTypeSinaWeibo onUserSync:^(SSDKUser *user, SSEUserAssociateHandler associateHandler) {
                            [self requestLogin:user loginType:SSDKPlatformTypeSinaWeibo];
                        } onLoginResult:^(SSDKResponseState state, SSEBaseUser *user, NSError *error) {
                            if (state == SSDKResponseStateSuccess) {
                                NSLog(@"登录成功");
                            }
                            [self.activityIndicatorView stopAnimating];
                            [self.activityIndicatorView setHidesWhenStopped:YES];
                        }];
                    }
                }
                    break;
                    
                default:
                    break;
            }
        }
        else {
            JJLoginForgetPasswordVC *resetPasswordVC = [[JJLoginForgetPasswordVC alloc] init];
            [self.navigationController pushViewController:resetPasswordVC animated:YES];
        }
    }
    
    2. cell的显示
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if (indexPath.section == 0) {
            JJPersonCenterSexCell *cell = [tableView dequeueReusableCellWithIdentifier:kJJSettingAccountSecurityVCPhoneReuseIdentify forIndexPath:indexPath];
            cell.accountSecurityStr = self.titleArr[indexPath.row];
            switch (indexPath.row) {
                    
                //手机号绑定
                case 0:
                    {
                        if (self.mobileBindStr.length > 5) {
                            cell.accountSecurityDetailStr = self.mobileBindStr;
                        }
                        else {
                            cell.accountSecurityDetailStr = @"未绑定";
                        }
                    }
                    break;
                    
                //QQ绑定
                case 1:
                    {
                        if (self.isQQBinded) {
                            cell.accountSecurityDetailStr = @"已绑定";
                        }
                        else{
                            cell.accountSecurityDetailStr = @"未绑定";
                        }
                    }
                    break;
                    
                //微信绑定
                case 2:
                {
                    if (self.isWechatBinded) {
                        cell.accountSecurityDetailStr = @"已绑定";
                    }
                    else{
                        cell.accountSecurityDetailStr = @"未绑定";
                    }
                }
                    break;
                    
                //微博绑定
                case 3:
                {
                    if (self.isWeiboBinded) {
                        cell.accountSecurityDetailStr = @"已绑定";
                    }
                    else{
                        cell.accountSecurityDetailStr = @"未绑定";
                    }
                }
                    break;
                    
                default:
                    break;
            }
            
            return cell;
        }
        
        JJPersonCenterSecurityCell *cell = [tableView dequeueReusableCellWithIdentifier:kJJSettingAccountSecurityVCPsdRemakeReuseIdentify forIndexPath:indexPath];
        cell.accountSecurityStr = @"重置密码";
        return cell;
    }
    
    3. 向我们自己的服务器发送请求,实现绑定
    
    - (void)requestLogin:(SSDKUser *)user loginType:(SSDKPlatformType)loginType
    {
        [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        NSMutableDictionary *paramDict = [self gainParameterWithUser:user loginType:loginType];
        [self bindThirdPartyWithParam:paramDict bindType:loginType];
    }
    
    - (NSMutableDictionary *)gainParameterWithUser:(SSDKUser *)user loginType:(SSDKPlatformType)loginType
    {
        NSMutableDictionary *paramDict = [NSMutableDictionary dictionary];
        
        NSString *logAuthTypeType = nil;
        
        switch (loginType) {
                
            case SSDKPlatformTypeWechat:
            {
                logAuthTypeType = @"2";
            }
                break;
                
            case SSDKPlatformTypeQQ:
            {
                logAuthTypeType = @"3";
            }
                break;
                
            case SSDKPlatformTypeSinaWeibo:
            {
                logAuthTypeType = @"4";
            }
                break;
                
            default:
                break;
        }
        
        switch (user.gender) {
            case SSDKGenderMale:
                [paramDict setObject:@"1" forKey:@"userSex"];
                break;
            case SSDKGenderFemale:
                [paramDict setObject:@"2" forKey:@"userSex"];
                break;
            case SSDKGenderUnknown:
                [paramDict setObject:@"0" forKey:@"userSex"];
                break;
            default:
                break;
        }
        
        NSString *icon = nil;
        if (loginType == SSDKPlatformTypeQQ) {
            icon = [user.rawData valueForKey:@"figureurl_qq_2"];
        } else if(loginType == SSDKPlatformTypeSinaWeibo){
            icon = user.icon;
        } else if (loginType == SSDKPlatformTypeWechat) {
            icon = user.icon;
        }
        NSString *avatar =[NSString stringWithFormat:@"%@",icon];
       
        [paramDict setObject:user.uid?:@"" forKey:@"authOpenid"];                                       //openid
        [paramDict setObject:logAuthTypeType?:@"" forKey:@"authType"];                                  //authorType
        [paramDict setObject:[user.rawData objectForKey:@"unionid"]?:@"" forKey:@"authUnionid"];        //authUnionid
        [paramDict setObject:avatar?:@"" forKey:@"userAvatar"];                                         //userAvatar
        [paramDict setObject:[user.rawData objectForKey:@"city"]?:@"" forKey:@"userCity"];              //userCity
        [paramDict setObject:user.nickname?:@"" forKey:@"userNickname"];                                //userNickname
        [paramDict setObject:[user.rawData objectForKey:@"province"]?:@"" forKey:@"userProvince"];      //userProvince
        [paramDict setObject:user.verifyReason?:@"" forKey:@"verifiedReason"];                          //verifiedReason
        
        return paramDict;
    }
    
    //请求自己的服务器
    
    - (void)bindThirdPartyWithParam:(NSMutableDictionary *)paramDict bindType:(SSDKPlatformType)bindType
    {
        if (![JJNetworkReachabilityTool sharedNetworkReachabilityTool].isReachable) {
            return;
        }
        
        [[JJNetWorkManager manager] requestByPostNetworkWithServerUrl:kPThirdPartyBind parameters:paramDict success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            NSLog(@"%@",responseObject);
            
            if ([[responseObject objectForKey:@"code"] integerValue] == 0) {
                switch (bindType) {
                        
                    case SSDKPlatformTypeWechat:
                    {
                        self.isWechatBinded = YES;
                    }
                        break;
                        
                    case SSDKPlatformTypeQQ:
                    {
                        self.isQQBinded = YES;
                    }
                        break;
                        
                    case SSDKPlatformTypeSinaWeibo:
                    {
                        self.isWeiboBinded = YES;
                    }
                        break;
                        
                    default:
                        break;
                }
                
                [self.tableView reloadData];
            }
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            
        } error:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            NSLog(@"error---%@",error);
            
        }];
    }
    

      核心代码就这么多,相信大家也都能看懂,就不多说了,未来的三天端午假期祝大家过的愉快。


    后记

      三天假期来了,大家可以休息一下了,哈哈,提前祝大家假期快乐,最近几天总是一遍遍的回顾08奥运北京的开幕式,越看越喜欢,每次看歌唱祖国升旗的那一段,眼眶都不由得湿润,祖国近现代屈辱了好几百年,可算慢慢崛起了,希望越走越顺,我大的贡献做不了,希望做好我的本职工作,做到极致,我想就可以了。再次祝大家端午节快乐!!!

    歌唱祖国

    相关文章

      网友评论

        本文标题:第三方平台账号的绑定

        本文链接:https://www.haomeiwen.com/subject/qakcfxtx.html