<open-data type="userAvatarUrl"></open-data>
之前是使用open-data獲取微信昵稱和頭像,但是更新之后,小程序不再支持使用該方法
現(xiàn)在需要用wx.getUserProfile這個(gè)API獲取用戶信息。頁(yè)面產(chǎn)生點(diǎn)擊事件(如?button
?上?bindtap
?的回調(diào)中)后才可調(diào)用,每次請(qǐng)求都會(huì)彈出授權(quán)窗口,用戶同意后返回?userInfo
。該接口用于替換?wx.getUserInfo。
官方例子:wx.getUserProfile(Object object) | 微信開放文檔 (qq.com)?
獲取用戶信息按官方的來(lái)即可。但是在登錄小程序獲取一次后我們需要把用戶信息存起來(lái),不能點(diǎn)擊一次獲取一次。?
文件:?
<button open-type="chooseAvatar" bindchooseavatar="assd">
<image src="{{image}}" />
</button>
<form catchsubmit="formSubmit" catchreset="formReset">
<view class="name">
<view class="name_text">
昵稱
</view>
<view>
<input class="weui-input" name="input" type="nickname" placeholder="請(qǐng)輸入昵稱" />
</view>
</view>
<view class="enter">
<button style="margin: 30rpx 0" type="primary" formType="submit">登錄</button>
</view>
</form>
wxss文件:
.button {
width: 100%;
height: 200px;
}
.button button {
width: 25%;
height: 100px;
margin-top: 50px;
background-color: #ffffff;
}
.button image {
width: 150%;
height: 100%;
}
.name {
width: 100%;
height: 50px;
border: 1px solid #cecccc;
display: flex;
}
.name_text {
width: 20%;
height: 50px;
line-height: 50px;
padding-left: 5%;
}
.name input {
width: 80%;
height: 50px;
}
.enter {
width: 100%;
display: flex;
justify-content: center;
margin-top: 100px;
}
js文件:?
這是寫一個(gè)默認(rèn)的灰色頭像
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
Page({
data: {
image: defaultAvatarUrl
},
})
我們需要點(diǎn)擊按鈕獲取到用戶頭像,然后把圖片存儲(chǔ)到本地 wx.setStorageSync() 方法使用
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
Page({
data: {
image: defaultAvatarUrl
},
//用戶頭像
assd: function (e) {
console.log(e.detail.avatarUrl);
let headimg =e.detail.avatarUrl
// 存儲(chǔ)用戶頭像
wx.setStorageSync('headimg', headimg)
this.setData({
image: e.detail.avatarUrl
})
},
//用戶名
formSubmit(e) {
let nickname=e.detail.value.input
// 存儲(chǔ)用戶名稱
wx.setStorageSync('nickname', nickname)
},
})
如何獲取緩存數(shù)據(jù):使用 wx.getStorageSync() 這個(gè)方法就可以獲取本地?cái)?shù)據(jù)文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-504110.html
其他頁(yè)面js:文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-504110.html
Page({
data: {
scrollTop: true
},
onShow: function () {
let img = wx.getStorageSync('headimg')
let name = wx.getStorageSync('nickname')
}
})
到了這里,關(guān)于微信小程序如何獲取微信昵稱和頭像的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!