微信调整后原来的接口可以用只是返回的用户数据为匿名数据
兼容性处理
onLoad(){
if (wx.getUserProfile) {
this.canIUseGetUserProfile = true;
}
}
1.通过wx.login换取sessionKey的时候将open id和union id同时返回到前端(wx.getUserProfile调整后的结果)
下面的代码是我在小程序中的源代码
1.1) that.$request({.....})是自定义发送请求的方法
1.2) code2session.html是换取sessionKey和open id、union id的接口
接口只返回了sessionId和type
其中sessionId是sessionKey和open id、union id后台拼接后做了des加密返回的
对返回的数据进行逻辑处理,如果openid在数据库中存在,则调用自动登录接口,否则去绑定手机号
这个接口如果你之前也是这样写的不需要做任何修改,这一块我之前就是这样写的所以没做任何修改
wxlogin(type){
var that=this;
wx.login({
success: function(res) {
that.wechatlogined=true;//已授权
if (res.code) {
//去获取sessionKey和open id、union id
//下面是自定义请求接口,有点逻辑处理,如果openid在数据库中存在,则调用自动登录接口,否则去绑定手机号
that.$request({
api: 'code2session.html',
method:'POST',
success:function(res){
//保存在本地
let data=res.data;
//sessionId是sessionKey和open id、union id后台拼接后做了des加密返回的
that.sessionId=data.sessionId;
that.type=data.type;
if(type!='phone'){
if(data.type==='toBindUserName'){//去绑定
that.sessionId=data.sessionId;
that.title='绑定手机号';
that.btn='绑定';
that.openPlatform=false;
that.openPlatformBtn=true;
// that.$lln.href("/pages/auth/login/bindUserName?target=redirect&sessionId="+data.sessionId)
}else if(data.type==='toWxLoginAuto'){
that.wechatRegister=true;
setTimeout(function () {
that.wxAutoLogin(data.sessionId);
},1000);
}else{
that.$lln.tips({'title':'微信授权登录失败:'+res.message})
that.wechatlogined=false;
}
}
},
fail:function(){
that.wechatlogined=false;
}
},{'sn':res.code,id:0,type:type})
} else {
// 获取 code 失败
that.wechatlogined=false;
that.$lln.tips({title:'微信登录失败'});
}
},
fail: function(error) {
// 调用 wx.login 接口失败
that.$lln.tips({title:'微信登录失败'});
console.log(error);
}
});
}
2.将button按钮修改如下
原来为:<button open-type="getUserInfo" @getuserinfo="tologin" type="primary">授权绑定</button>
修改为:
<tui-button v-if="canIUseGetUserProfile" @tap="tologin" type="primary">授权绑定</tui-button>
<tui-button v-if="!canIUseGetUserProfile" open-type="getUserInfo" @getuserinfo="tologin" type="primary">立即绑定</tui-button>
我用的是uniapp,原生小程序没做测试,微信官方是改成这样的
<button v-if="!canIUseGetUserProfile" bindtap="tologin" type="primary">授权绑定</button>
3.修改tologin方法
原来为:
tologin(res){
之前点击按钮后res中就是微信返回的用户加密数据
}
tologin(res){
let para=res;//res为微信用户加密数据
para.sessionId=that.sessionId;
para.password=newPassword;//微信登录无需密码 固定密码
para.userName=that.mobile;
para.referrerPhone=referrerPhone;
para.referrerShareCode=referrerShareCode;
para.code=that.code;
para.messageSn=that.messageSn;
para.bindType=that.wxBindPhoneType;
para.source=that.$lln.config('configs').platform || 'WX_MP';
//首次登录获取用户的基本信息入库
const result = await that.$request({
api: 'accessible/wechat/decryptUserInfo.html',
method:'POST',
// loadingTips:'正在登录',
// successTips:'登录成功',
success:function(res){
//保存在本地
that.$lln.setUserInfo(res.data);
that.$lln.deleteConfig('lastRegisterMobile');
that.refreshPageFlag()//设置刷新页面标识
that.$lln.back('/pages/index/index');
}
},para)
}
修改为:
tologin(){
//通过接口获取用户加密数据
wx.getUserProfile({
})
}
tologin(wxres){
if(this.canIUseGetUserProfile){
wx.getUserProfile({
desc: '用于完善用户资料',
success: (wxres2) => {
//授权同意后返回微信加密用户数据 wxres
let para=wxres2;//直接复制微信数据
para.sessionId=that.sessionId;//这个是第一步接口返回的需要带上
//以下是我绑定手机需要的数据,不必关注
para.password=newPassword;//微信登录无需密码 固定密码
para.userName=that.mobile;
para.referrerPhone=referrerPhone;
para.referrerShareCode=referrerShareCode;
para.code=that.code;
para.messageSn=that.messageSn;
para.bindType=that.wxBindPhoneType;
para.source=that.$lln.config('configs').platform || 'WX_MP';
//发送请求,解密微信用户数据,用户新增入库
//首次登录获取用户的基本信息入库
that.$request({
api: 'decryptUserInfo.html',
method:'POST',
success:function(res){
//保存在本地
that.$lln.setUserInfo(res.data);
that.$lln.deleteConfig('lastRegisterMobile');
that.refreshPageFlag()//设置刷新页面标识
that.$lln.back('/pages/index/index');
}
},para)
}
})
}else{
let para=wxres;
para.sessionId=that.sessionId;
para.password=newPassword;//微信登录无需密码 固定密码
para.userName=that.mobile;
para.referrerPhone=referrerPhone;
para.referrerShareCode=referrerShareCode;
para.code=that.code;
para.messageSn=that.messageSn;
para.bindType=that.wxBindPhoneType;
para.source=that.$lln.config('configs').platform || 'WX_MP';
//首次登录获取用户的基本信息入库
const result = await that.$request({
api: 'accessible/wechat/decryptUserInfo.html',
method:'POST',
// loadingTips:'正在登录',
// successTips:'登录成功',
success:function(res){
//保存在本地
that.$lln.setUserInfo(res.data);
that.$lln.deleteConfig('lastRegisterMobile');
that.refreshPageFlag()//设置刷新页面标识
that.$lln.back('/pages/index/index');
}
},para)
}
}
后台没做任何改动,这就是我修改的全部步骤
<<完>>