php后台代码:
// header(‘Content-Type:application/json; charset=UTF-8’);
//header(‘Content-Type:application/json; charset=UTF-8’);
if ($this->data) {
$check = M(‘user’)->where([‘openid’=>$this->data->openId])->find();
$id = 0;
if(!$check){
this->data->openId;
this->data->nickName;
this->data->gender;
this->data->avatarUrl;
$data[‘add_time’] = time();
$id = M(‘user’)->data($data)->add();
if(!$id){
echo json_encode(array(‘code’=>0,‘message’=>‘注册失败!’));
exit();
}
}else{
this->data->nickName;
this->data->avatarUrl;
$data[‘last_time’] = time();
$save = M(‘user’)->where([‘openid’=>$this->data->openId])->data($data)->save();
if(!$save){
echo json_encode(array(‘code’=>0,‘message’=>‘更新失败!’));
exit();
}
check[‘id’];
}
id;
this->data->session_key;
//设置令牌
this->saveToCache($arr);
if($rs){
//rs;
echo json_encode(array(‘code’=>1,‘message’=>‘操作成功’,‘data’=>$rs));
exit();
}else{
echo json_encode(array(‘code’=>0,‘message’=>‘授权失败!’,‘data’=>’’));
exit();
}
}else{
echo json_encode(array(‘code’=>-1,‘message’=>‘暂无数据’));
exit();
}
——————————————————————————
小程序代码:
wx.login({
success: function (res) {
if (res.code) {
var code = res.code;
wx.getUserInfo({
success: function (res) {
console.log(res)
wx.request({
url: ‘https://’,
method: ‘post’,
datatype: ‘json’,
data: {
code: code,
rawdata: res.rawData,
encrypteddata: res.encryptedData,
iv: res.iv,
signature: res.signature
},
header: {
‘content-type’: ‘application/x-www-form-urlencoded’
},
success: function (rs) {
var data = rs.data;
console.log(rs)
console.log(typeof rs)
console.log(rs.data)
console.log(22222222222)
console.log(rs.data.code)
console.log(rs.data.data)
if (data.code == 1) {
wx.hideLoading();
wx.setStorageSync(“sessionkey”, data.data);
wx.setStorageSync(“user_info”, {
nickname: res.userInfo.nickName,
avatar_url: res.userInfo.avatarUrl,
});
} else if (data.code == -1) {
wx.showToast({
title: ‘网络异常请重试’,
duration: 2000
});
} else {
console.log(1211212121)
wx.showToast({
title: data.message
});
}
console.log(wx.getStorageSync(‘user_info’));
}
});
},
fail: function (res) {
wx.hideLoading();
that.getauth({
content: ‘需要获取您的用户信息授权,请到小程序设置中打开授权’,
cancel: true,
success: function (e) {
if (e) {
that.login();
}
},
});
}
});
} else {
//console.log(res);
}
}
});
小程序测试工具输出
真机输出:
但是就是获取不到json对象下面的数据 res.data.code会输出null