前言:2021年4月28日起,微信小程序将不再支持用户授权调用wx.getUserInfo,将变更为wx.getUserInfo获取用户信息。基于这种调整简单微信小程序代码,很多开发者还没有来得及更新代码。项目整理了一段代码,一起来学习吧。
案例鉴赏
代码展示
1、点击登录按钮的代码。这一步是通过openid创建一个账户。如果账号已经存在,直接登录。如果不存在,则弹出框简单微信小程序代码,然后进行下一次用户信息更新
wxml
点击登录
js
//弹出登录框
wxlogin:function(){
var that = this;
wx.showLoading({ title: '正在登录中' ,'mask' : true });
wx.login({
success:function(res){
wx.request({
url: HTTP_REQUEST_URL+'/api/common/login',
data: {
code: res.code,
},
dataType: 'json',
method: 'POST',
header: {
'Accept': 'application/json'
},
success: function (res2) {
wx.hideLoading();
if(res2['data']['status']==200){
that.setData({
is_update_profile:res2['data']['data']['user_info']['is_update_profile'],
hiddenLogin:false,
token:res2['data']['data']['token'],
})
}else{
wx.showToast({
title: res2['data']['msg'],
icon : 'none'
})
}
},
fail(res2){
wx.hideLoading();
}
})
},
fail(){
wx.hideLoading();
}
})
},
php
$code=input("post.code");
$s_data=[];
$s_data['appid'] = $wechat["appId"];
$s_data['secret'] = $wechat["appsecret"];
$s_data['js_code'] = $code;
$s_data['grant_type']="authorization_code";
$session_url = 'https://api.weixin.qq.com/sns/jscode2session?' . http_build_query ( $s_data );
$content = file_get_contents($session_url);
$content = json_decode ( $content, true );
if(!isset($content['openid']) || !isset($content['session_key'])){
return_json("授权失败",100);
}
2、如果第一步的openid不存在,通过getUserProfile获取对应信息,然后传给后台保存。
wxml
暂不登录
请升级微信版本
js
//授权登录
getUserProfile(userInfo,isLogin) {
let that = this;
var token = that.data.token;
var is_update_profile = that.data.is_update_profile;
if(is_update_profile-0>0){
wx.request({
url: HTTP_REQUEST_URL+'/api/user',
data: {
},
dataType: 'json',
method: 'get',
header: {
'Accept': 'application/json',
'Authori-zation' : 'Bearer ' + token,
},
success: function (res2) {
wx.setStorageSync('TOKEN', that.data.token);
that.setData({hiddenLogin:true,userInfo: res2.data.data});
},
fail(res2){
}
})
}else{
wx.getUserProfile({
lang: 'zh_CN',
desc:'用于完善会员资料',
success:function(res){
if (res.errMsg == "getUserProfile:ok"){
that.setData({
hiddenLogin:true
})
var UserInfo = JSON.parse(res.rawData);
wx.request({
url: HTTP_REQUEST_URL+'/api/user/update_profile',
data: {
invite_id: app.globalData.spid,
avatar: UserInfo.avatarUrl,
sex: UserInfo.gender,
nickname: UserInfo.nickName,
city: UserInfo.city,
province: UserInfo.province,
country: UserInfo.country,
},
dataType: 'json',
method: 'POST',
header: {
'Accept': 'application/json',
'Authori-zation' : 'Bearer ' + token,
},
success: function (res2) {
wx.hideLoading();
wx.setStorageSync('TOKEN', token);
that.setData({hiddenLogin:true,userInfo: res2.data.data.user_info});
},
fail(res2){
//wx.hideLoading();
}
})
}else{
}
}
})
}
},
php
$updateData = array(
"avatar"=>input("post.avatar"),
"sex"=>input("post.sex"),
"nickname"=>input("post.nickname"),
"city"=>input("post.city"),
"province"=>input("post.province"),
"country"=>input("post.country"),
"invite_id"=>$invite_id,
"invite_center"=>$invite_center,
"invite_bottom"=>$invite_bottom,
"invite_str"=>$invite_str,
"is_update_profile"=>1,
);
Db::name("user")->where(["uid"=>$user_id])->update($updateData);
总结
登录过程分为两个步骤。第一步,获取小程序用户唯一的openid作为标识。第二步,将小程序用户的基本信息更新为创建的用户,这样整个登录过程就准备好了。.
我是一名小程序软件开发人员。每天分享开发过程中遇到的知识点。如果对你有帮助,请给我点个赞,然后去,非常感谢。
© 版权声明
THE END
喜欢就支持一下吧
请登录后发表评论
注册
社交帐号登录