小程序用户授权获取用户头像,需点击才可以让用户选择是否授权。
图片上传失败问题解决
图片上传 一定要记得修改合法域名,不然会失败,而且找不到报错信息哟!并且域名采用 https
点击,用户授权,获取用户头像,上传服务器:
<button class="getuserinfo" open-type="getUserInfo" lang="zh_CN" bindgetuserinfo="onGotUserInfo">获取用户信息</button>
onGotUserInfo: function (e) {
// console.log(e)
// console.log(e.detail.userInfo)
// console.log(e.detail.rawData)
// this.setData({
// usertximg: e.detail.userInfo.avatarUrl
// })
wx.login({
success: res => {
// 发送 res.code 到后台换取 openId, sessionKey, unionId
if (this.data.code == '') {
this.setData({
code: res.code
})
wx.request({
url: 'https://wx.xxx.com/wechat/setWxImg',
data: {
encryptedData: e.detail.encryptedData,
iv: e.detail.iv,
code: this.data.code,
wx_img: e.detail.userInfo.avatarUrl
},
method: "post",
success: (res) => {
console.log(res);
if (res.data.message == "ok") {
wx.setStorageSync('openid', res.data.data.openid);
getApp().globalData.openid = wx.getStorageSync('openid');
this.setData({
isdenglu: false,
openid