第三方登录

# 第三方登录

提示

一定要用手机和电脑连接 usb 口连接测试,不然报错:API login is not yet implemented

bo(e) {
    let type = e.currentTarget.dataset.type
    uni.login({
        provider:type,
        success(loginRes) {
            console.log(loginRes) //token值
        // {
        // 	"authResult": {
        // 		"access_token": "39_o5-NRYUPfGIHcDU3Iu_vwvZ7S_ofEMjSvKyuzh_SytUVCGzr1ucFij9srSq4CVDFi-TjnB5yupJVIMNvBPj3OfR3V1X5wYbuSX1Ikn-cx7s",
        // 		"expires_in": 7200,
        // 		"refresh_token": "39_uxQu255bPM4ObSptaCC_EzF7fF9DGHpyjLYZ-jtd6ffL4I2idO3w0wRlsxkJwEUVRDz0Ld3vX9HiIQlYQUGhOvl4gF1Sfsv1J_0gIRa11zY",
        // 		"openid": "oRrdQt3VBYkstpOVGqE3zPd1fNU4",
        // 		"scope": "snsapi_userinfo",
        // 		"unionid": "oU5Yytw47n1i8M0oqhef9QTeoYX8"
        // 	},
        // 	"errMsg": "login:ok"
        // }
        登陆成功后,获取用户数据
            uni.getUserInfo({
                provider:type,
                withCredentials:true,
                lang:'zh_CN',
                success(e) {
                    // console.log(e) // 用户信息:
                    // {
                    // 	"errMsg": "getUserInfo:ok",
                    // 	"userInfo": {
                    // 		"openId": "oRrdQt3VBYkstpOVGqE3zPd1fNU4", 该服务商唯一用户标识
                    // 		"nickName": "我名声不好但我待人不差", 用户昵称
                    // 		"gender": 1,
                    // 		"city": "墨尔本",
                    // 		"province": "维多利亚",
                    // 		"country": "澳大利亚",
                    // 		"avatarUrl": "https://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83eqG3MAibKVpvKkuonrMa4BAicvaNFJ6ial68C5Q37VjffUMNsbyicosQZJmCNS3KpN5tzjBAYap4QAeHw/132",
                    // 		"unionId": "oU5Yytw47n1i8M0oqhef9QTeoYX8" 用户头像
                    // 	}
                    // }
                }
            }),
           获取服务器供应商
            uni.getProvider({
							service:'oauth', // 不同的值 provider返回不能的数据
							success(res) {
								console.log(res)
								// {
								// 	"errMsg": "getProvider:ok",
								// 	"service": "oauth",
								// 	"provider": ["weixin", "qq", "sinaweibo"]
								// }

							}
						})
        }
    })
}

最后把获取到的用户数据传递给后端, 后端绑定用户信息

写法

login_weixin() {
    var vm = this;
    uni.getProvider({
        service: 'oauth',
        success: function (res) {
            console.log(res.provider) //qq,xiaomi,sinaweibo,weixin
            vm.log.log1 = res.provider
            if (~res.provider.indexOf('weixin')) {
                uni.login({
                    provider: 'weixin',
                    success: function (loginRes) {
                        vm.$HTTP({
                        method: 'GET',
                        baseURL:'https://api.weixin.qq.com/sns/userinfo',
                        url: '',
                        data: {
                            openid:loginRes.authResult.openid,
                            access_token:loginRes.authResult.access_token
                        },
                        load:true
                        }).then((data) =>{
                        uni.setStorage({
                        key: 'userInfo',
                        data:{
                            name:data.nickname,
                            picUrl:data.headimgurl
                        }
                        });
                        uni.switchTab({
                        url: "../../tabBar/me/me"
                        })
                    }, (err) => {
                        console.log(err)
                    });
                }
            });
        }
    }
});
},
login_qq() {
var vm = this;
uni.getProvider({
    service: 'oauth',
    success: function (res) {
        console.log(res.provider) //qq,xiaomi,sinaweibo,weixin
        vm.log.log1 = res.provider
        if (~res.provider.indexOf('qq')) {
            uni.login({
                provider: 'qq',
                success: function (loginRes) {
                    vm.$HTTP({
                        method: 'GET',
                        baseURL:'https://graph.qq.com/user/get_user_info',
                        url: '',
                        data: {
                            openid:loginRes.authResult.openid,
                            access_token:loginRes.authResult.access_token,
                            appid:'101832674'
                        },
                        load:true
                    }).then((data) =>{
                        console.log(data)
                        uni.setStorage({
                        key: 'userInfo',
                        data:{
                            name:data.nickname,
                                picUrl:data.figureurl_qq
                            }
                        });
                        uni.switchTab({
                            url: "../../tabBar/me/me"
                        })
                        }, (err) => {
                        console.log(err)
                        });
                    }
                });
            }
        }
    });
}

# 登录的扩展

App 端登陆相关的 SDK 需要在 manifest 中配置: