getUserInfo获取微信用户的个人信息这里就不写了。
微信获取的个人信息截图
获取来的微信个人信息
微信小程序getUserInfo文档截图
微信小程序getUserInfo文档
这里获取到的avatarUrl是https://wx.qlogo.cn/mmopen/vi_32/DYAIOgq83er5prllVA37yiac4Vv8ZAXwbg0Zicibn6ZjsgJ4ha0hmFBY8MUTRMnRTmSlvzPd8XJZzd0icuyGoiakj4A/132
默认获取的是大小数值是132,我们只要在使用高清头像的时候把后缀的数值替换成0就可以了。
比如https://wx.qlogo.cn/mmopen/vi_32/DYAIOgq83er5prllVA37yiac4Vv8ZAXwbg0Zicibn6ZjsgJ4ha0hmFBY8MUTRMnRTmSlvzPd8XJZzd0icuyGoiakj4A/132转换成https://wx.qlogo.cn/mmopen/vi_32/DYAIOgq83er5prllVA37yiac4Vv8ZAXwbg0Zicibn6ZjsgJ4ha0hmFBY8MUTRMnRTmSlvzPd8XJZzd0icuyGoiakj4A/0
下面是转换代码
function headimgHD(imageUrl) { console.log('原来的头像', 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('/'); //重新拼接为字符串 console.log('高清的头像', imageUrl); return imageUrl; } headimgHD('https://wx.qlogo.cn/mmopen/vi_32/DYAIOgq83er5prllVA37yiac4Vv8ZAXwbg0Zicibn6ZjsgJ4ha0hmFBY8MUTRMnRTmSlvzPd8XJZzd0icuyGoiakj4A/132');'https://wx.qlogo.cn/mmopen/vi_32/DYAIOgq83er5prllVA37yiac4Vv8ZAXwbg0Zicibn6ZjsgJ4ha0hmFBY8MUTRMnRTmSlvzPd8XJZzd0icuyGoiakj4A/0'
代码运行结果
作者:枝枝_zhizhi
链接:https://www.jianshu.com/p/0d454ee65988
热门评论
这就很尴尬了,假如用户头像改了,然后岂不是直接显示不出来?按你截图说用户头像已经更改,假如是132的清晰度,用户头像还在,假如不在的话,岂不是直接无法显示?怎么判断图片是否有效才替换数字,这个判断也关键。
你好不知为什么改了后缀依然不行,就是有些改了后缀直接显示不出来