Android 获取微信ua,微信小程序实现获取用户高清头像

Android获取微信用户头像与UA实现
本文介绍如何在Android应用中获取微信用户的UA信息,并通过微信小程序获取用户高清头像的步骤。通过监听加载事件,利用wx.getUserInfo接口获取用户信息,再通过下载并处理头像URL以获取高清头像。

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;

}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值