leancloud / leancloud-social-ios

LeanCloud 社交登录组件,轻松加入 QQ、微博、微信登录
https://leancloud.cn/docs/sns.html
159 stars 50 forks source link

微信登录和小程序登录帐号互通 #57

Open ChenYilong opened 7 years ago

ChenYilong commented 7 years ago

前微信开放平台,微信公众平台与微信小程序 OAuth 登录会得到三个不同的 openid。微信提供了 unionId 来平台间区分独立用户。

小程序中,unionId 需要从 encryptedData 中解密:

https://mp.weixin.qq.com/debug/wxadoc/dev/api/signature.html

所以在登录时,想保持一致就不能再使用openId了。只能使用unionId

也就是说,下面教程提到的:

https://leancloud.cn/docs/rest_api.html#连接用户账户和第三方平台

{
"第三方平台名称,例如facebook":
{
"uid": "在第三方平台上的唯一用户id字符串",
"access_token": "在第三方平台的 access token",
……其他可选属性
}
}

其中uid部分需要传入unionId。

对应工单:https://leanticket.cn/t/leancloud/3638

ChenYilong commented 7 years ago

如果要想实现小程序和微信互通的功能,就不能再按照微信平台登录,需要按照下面理解里的“其它第三方平台”,比如当作facebook这样的平台接入。然后自定义uid,uid需要传unionId。

https://leancloud.cn/docs/rest_api.html#连接用户账户和第三方平台

所以下面的代码就不能这么写了:

        [AVUser loginWithAuthData:dataDict platform:AVOSCloudSNSPlatformWeiXin block:^(AVUser *user, NSError *error) {

AVOSCloudSNSPlatformWeiXin 不能再这样传,需要改成自定义的字符串:

@"weixin_unionid"

@"weixin_login_with_unionid"

等等一切非专属名字

代码示例:

-(void)loginClick {
        [[UMSocialManager defaultManager] getUserInfoWithPlatform:UMSocialPlatformType_WechatSession  currentViewController:self completion:^(id result, NSError *error) {

              NSLog(@"微信登陆成功之后的结果%@",result);

            if (error) {
                NSLog(@"微信未授权!!!");
                return ;
            }else {

                /*!
                 *   "ACL": {
                 "*": {
                 "read": true,
                 "write": true
                 }
                 },
                 "username": "s6f9xyp8l1yxpnkq3m856g743",
                 "emailVerified": false,
                 "authData": {
                 "weixin": {
                 "openid": "oj8jeshvg5aL-t2f5RaRIc_FDwlg",
                 "avatar": "http://wx.qlogo.cn/mmopen/1wyPAPkN3Dz6lBWTcrAufLMW7PMF86dgiarHWLeOnj8W7GcLOgT3OzclSRGgqzJeLPc2lUA94q7C95lIicpWJF712G4wMa5yjD/0",
                 "id": "o_aU1wobNqdn3Pg1Qyf4egmmRXoc",
                 "platform": "3",
                 "username": "wely",
                 "access_token": "7g96iftRN5FKRQcs3Msc1EvKAq_xY-9yno4pL3W6Hs8Oz_UeksKnIB76udSWyzLwxhuZN01ANTKwj2kyfO_v5Rmb-2GmZsxNS9MeZBPbpP4",
                 "raw-user": {
                 "sex": 1,
                 "nickname": "wely",
                 "city": "郑州",
                 "headimgurl": "http://wx.qlogo.cn/mmopen/1wyPAPkN3Dz6lBWTcrAufLMW7PMF86dgiarHWLeOnj8W7GcLOgT3OzclSRGgqzJeLPc2lUA94q7C95lIicpWJF712G4wMa5yjD/0",
                 "openid": "oj8jeshvg5aL-t2f5RaRIc_FDwlg",
                 "language": "zh_CN",
                 "province": "河南",
                 "country": "中国",
                 "unionid": "o_aU1wobNqdn3Pg1Qyf4egmmRXoc",
                 "privilege": []
                 },
                 "expires_in": "2017-01-18 11:10:50"
                 }
                 },
                 "mobilePhoneVerified": false,
                 "objectId": "587ec09a570c35220114f0ad",
                 "createdAt": "2017-01-18T01:10:50.593Z",
                 "updatedAt": "2017-01-18T01:10:50.593Z"
                 }
                 */
                UMSocialUserInfoResponse *resp = result;
                NSMutableDictionary * dataDict = [NSMutableDictionary new];
                dataDict[@"access_token"] = resp.accessToken;
                //            dataDict[@"avatar"] = resp.iconurl;
                NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];//创建一个日期格式化器
                dateFormatter.dateFormat=@"yyyy-MM-dd hh:mm:ss";//指定转date得日期格式化形式
                NSLog(@"%@",[dateFormatter stringFromDate:resp.expiration]);//2015-11-20 08:24:04
                dataDict[@"expires_in"] =[dateFormatter stringFromDate:resp.expiration] ;
//                dataDict[@"uid"] = resp.uid;
                dataDict[@"uid"] = resp.originalResponse[@"unionid"];

                //            dataDict[@"openid"] = resp.openid;
                //            dataDict[@"city"] = resp.originalResponse[@"city"];
                //            dataDict[@"username"] = resp.name;
                //            dataDict[@"language"] = resp.originalResponse[@"language"];
                //            dataDict[@"gender"] = resp.gender;
                //            dataDict[@"province"]  =resp.originalResponse[@"province"];
                //            dataDict[@"country"] = resp.originalResponse[@"country"];
                //            dataDict[@"avatarUrl"] = resp.iconurl;
                [AVUser loginWithAuthData:dataDict platform:@"weixin==" block:^(AVUser *user, NSError *error) {
                    if (error) {
                        // 登录失败,可能为网络问题或 authData 无效
                        NSLog(@"登陆失败 %@",error);
                    } else {
                        // 登录成功
                        NSLog(@"登陆成功");
                        AVUser * user = [AVUser currentUser];
                        NSLog(@"打印登陆成功的数据的信息%@",user);
                        [self dismissViewControllerAnimated:YES completion:nil];
                    }
                }];
            }
        }];

    AVUser * user = [AVUser currentUser];
    NSLog(@"如果登陆成功 获取 登陆用户信息%@",user.username);

}