用户的openid存不进去
Page({
data: {
//判断小程序的API,回调,参数,组件等是否在当前版本可用。
canIUse: wx.canIUse(‘button.open-type.getUserInfo’)
},
onLoad: function () {
var that = this;
// 查看是否授权
wx.getSetting({
success: function (res) {
if (res.authSetting[‘scope.userInfo’]) {
wx.getUserInfo({
success: function (res) {
//从数据库获取用户信息
that.queryUsreInfo();
//用户已经授权过
wx.switchTab({
url: ‘…/index/index’
})
}
});
}
}
})
},
bindGetUserInfo: function (e) {
if (e.detail.userInfo) {
//用户按了允许授权按钮
var that = this;
//插入登录的用户的相关信息到数据库
wx.request({
url: ‘https://odxcx.cn/Ju/public/index.php/api/Login/index’,
data: {
openid: wx.getStorageSync(‘openid’),
nickName: e.detail.userInfo.nickName,
avatarUrl: e.detail.userInfo.avatarUrl,
country: e.detail.userInfo.country,
gender: e.detail.userInfo.gender,
province: e.detail.userInfo.province,
city: e.detail.userInfo.city
},
method:‘POST’,
header: {
‘content-type’: ‘application/json’
},
success: function (res) {
//从数据库获取用户信 息
that.queryUsreInfo();
console.log(“插入小程序登录用户信息成功!”);
}
});
//授权成功后,跳转进入小程序首页
wx.switchTab({
url: ‘…/index/index’
})
} else {
//用户按了拒绝按钮
wx.showModal({
title: ‘警告’,
content: ‘您点击了拒绝授权,将无法进入小程序,请授权之后再进入!!!’,
showCancel: false,
confirmText: ‘返回授权’,
success: function (res) {
if (res.confirm) {
console.log(‘用户点击了“返回授权”’)
}
}
})
}
},
//获取用户信息接口
queryUsreInfo: function (res) {
wx.login({
success: function (res) {
console.log(res.code)
if (res.code) {
//发 起网络请求
wx.request({
url: ‘https://odxcx.cn/Ju/public/index.php/api/Login/login’,
data: {
code: res.code
},
header: {
‘content-type’: ‘application/json’
},
method: ‘POST’,
success: function (res) {
console.log(res)
wx.setStorageSync(‘openid’, res.data.openid)
console.log(res.data.openid)
}
})
} else {
console.log(‘登录失败!’ + res.errMsg)
}
}
})
},
})