const app = getApp();
Page({
/**
* 页面的初始数据
*/
data: {
imageUrl: null,
userInfo: {},
hasUserInfo: false,
canIUse: wx.canIUse('button.open-type.getUserInfo')
},
onLoad: function() {
if (app.globalData.userInfo) {
this.setData({
userInfo: app.globalData.userInfo,
hasUserInfo: true
})
} else if (this.data.canIUse) {
// 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
// 所以此处加入 callback 以防止这种情况
app.userInfoReadyCallback = res => {
this.setData({
userInfo: res.userInfo,
hasUserInfo: true
})
}
} else {
// 在没有 open-type=getUserInfo 版本的兼容处理
wx.getUserInfo({
success: res => {
app.globalData.userInfo = res.userInfo
this.setData({
userInfo: res.userInfo,
hasUserInfo: true
})
}
})
}
},
getUserInfo: function (e) {
console.log(e)
app.globalData.userInfo = e.detail.userInfo
this.setData({
userInfo: e.detail.userInfo,
hasUserInfo: true
})
},
getavatar: function(){
var that=this;
wx.downloadFile({
url: this.data.userInfo.avatarUrl,
success: function(res){
console.log(res)
that.setData({
bgPic: headimgHD(app.globalData.userInfo.avatarUrl)
})
}
})
}
})
function headimgHD (imageUrl) {
imageUrl = imageUrl.split('/'); //把头像的路径切成数组
//把大小数值为 46 || 64 || 96 || 132 的转换为0
if (imageUrl[imageUrl.length - 1] && (imageUrl[imageUrl.length - 1] == 46 || imageUrl[imageUrl.length - 1] == 64 || imageUrl[imageUrl.length - 1] == 96 || imageUrl[imageUrl.length - 1] == 132)) {
imageUrl[imageUrl.length - 1] = 0;
}
imageUrl = imageUrl.join('/'); //重新拼接为字符串
return imageUrl;
}
Android获取微信用户头像与UA实现
本文介绍如何在Android应用中获取微信用户的UA信息,并通过微信小程序获取用户高清头像的步骤。通过监听加载事件,利用wx.getUserInfo接口获取用户信息,再通过下载并处理头像URL以获取高清头像。
1万+

被折叠的 条评论
为什么被折叠?



