小程序獲取用戶信息的兩種方法詳解(getUserProfile和頭像昵稱填寫)
相信大家之前也經(jīng)常使用open-data獲取用戶的頭像和昵稱吧,但微信的這個(gè)改編意味著我們要使用新的方法獲取信息了。在討論區(qū)引發(fā)了很大的討論,接下來我們一起嘗試兩種獲取信息的方法。
第一種使用 getUserProfile
我們可以查看一下官方文檔 wx.getUserProfile(Object object),獲取用戶信息。頁面產(chǎn)生點(diǎn)擊事件(例如 button 上 bindtap 的回調(diào)中)后才可調(diào)用,每次請求都會彈出授權(quán)窗口,用戶同意后返回 userInfo。但要注意每次都需要授權(quán)一次是不是很麻煩,我們可以將他保存在我們數(shù)據(jù)庫中授權(quán)一次日后直接調(diào)用。
代碼示例
<view class="container"> <view class="userinfo"> <block wx:if="{{!hasUserInfo}}"> <button wx:if="{{canIUseGetUserProfile}}" bindtap="getUserProfile"> 獲取頭像昵稱 </button> <button wx:else open-type="getUserInfo" bindgetuserinfo="getUserInfo"> 獲取頭像昵稱 </button> </block> <block wx:else> <image bindtap="bindViewTap" class="userinfo-avatar" src="{{userInfo.avatarUrl}}" mode="cover"></image> <text class="userinfo-nickname">{{userInfo.nickName}}</text> </block> </view> </view>
Page({ data: { userInfo: {}, hasUserInfo: false, canIUseGetUserProfile: false, }, onLoad() { if (wx.getUserProfile) { this.setData({ canIUseGetUserProfile: true }) } }, getUserProfile(e) { wx.getUserProfile({ desc: '用于完善會員資料', // 聲明獲取用戶個(gè)人信息后的用途,后續(xù)會展示在彈窗中,請謹(jǐn)慎填寫 success: (res) => { this.setData({ userInfo: res.userInfo, hasUserInfo: true }) } }) },
第二種使用 頭像昵稱填寫
當(dāng)小程序需要讓用戶完善個(gè)人資料時(shí),可以通過微信提供的頭像昵稱填寫能力快速完善。
頭像選擇
需要將 button 組件 open-type 的值設(shè)置為 chooseAvatar,當(dāng)用戶選擇需要使用的頭像之后,可以通過 bindchooseavatar 事件回調(diào)獲取到獲取到頭像信息的臨時(shí)路徑。
昵稱填寫
需要將 input 組件 type 的值設(shè)置為 nickname,當(dāng)用戶在此input進(jìn)行輸入時(shí),鍵盤上方會展示微信昵稱。
然后我們將他存到數(shù)據(jù)庫,日后直接調(diào)用即可!
<button class="avatar-wrapper" open-type="chooseAvatar" bind:chooseavatar="onChooseAvatar"> <image class="avatar" src="{{avatarUrl}}"></image> </button> <input type="nickname" class="weui-input" placeholder="請輸入昵稱"/>
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0' Page({ data: { avatarUrl: defaultAvatarUrl, }, onChooseAvatar(e) { const { avatarUrl } = e.detail this.setData({ avatarUrl, }) } })
接下來我們要將值進(jìn)行存儲,并上傳數(shù)據(jù)庫。我們使用form將數(shù)據(jù)保存到data里面。
<button class="avatar-wrapper" open-type="chooseAvatar" bind:chooseavatar="onChooseAvatar"> <image class="avatar" src="{{avatarUrl}}"></image> </button> <form catchsubmit="formSubmit"> <view class="row"> <view class="text1">名稱:</view> <input type="nickname" class="weui-input" name="input" placeholder="請輸入昵稱" /> </view> <button type="primary" style="margin-top:40rpx;margin-bottom:20rpx" formType="submit">提交</button> </form>
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0' Page({ /** * 頁面的初始數(shù)據(jù) */ data: { avatarUrl: defaultAvatarUrl, name: '', }, onChooseAvatar(e) { const { avatarUrl } = e.detail this.setData({ avatarUrl, }) }, formSubmit(e) { console.log(e.detail.value.input) this.setData({ name: e.detail.value.input }) } })
這樣我們點(diǎn)擊提交時(shí)候發(fā)現(xiàn)值保存data里面了,接下來我們獲取openid,可以參考之前視頻哦!這里默認(rèn)已經(jīng)將openid保存到app.js里面了!
onLoad: function (options) { const app = getApp() var userid = app.globalData.openid this.setData({ userid: userid, }) },
接下來我們上傳圖片到云開發(fā),然后存到數(shù)據(jù)庫中,這里在cms創(chuàng)建內(nèi)容模型。
// pages/getuser/getuser.js const db = wx.cloud.database() const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0' Page({ /** * 頁面的初始數(shù)據(jù) */ data: { avatarUrl: defaultAvatarUrl, name: '', userid: ' ', userphoto: ' ', imgrl: ' ' }, onChooseAvatar(e) { const { avatarUrl } = e.detail this.setData({ avatarUrl, }) }, formSubmit(e) { console.log(e.detail.value.input) this.setData({ name: e.detail.value.input }) var that = this; wx.cloud.uploadFile({ cloudPath: (new Date()).valueOf() + '.png', // 文件名 filePath: this.data.avatarUrl, // 文件路徑 success: res => { // get resource ID console.log(res.fileID) // 賦值圖片 that.setData({ imgrl: res.fileID }) that.upload(res.fileID); }, fail: err => { // handle error } }) }, upload(filepath){ console.log(filepath) db.collection("user").add({ data: { name:this.data.name, openid:this.data.userid, userphoto:filepath, _createTime: Date.parse(new Date()), } }).then(res => { wx.showToast({ title: '添加成功', icon: 'success', duration: 2000 }) }) }, /** * 生命周期函數(shù)--監(jiān)聽頁面加載 */ onLoad: function (options) { const app = getApp() var userid = app.globalData.openid this.setData({ userid: userid, }) }, })
這樣我們就完成了!
總結(jié)
到此這篇關(guān)于小程序獲取用戶信息的文章就介紹到這了,更多相關(guān)小程序獲取用戶信息方法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
原生JS實(shí)現(xiàn)逼真的圖片3D旋轉(zhuǎn)效果詳解
這篇文章主要介紹了原生JS實(shí)現(xiàn)逼真的圖片3D旋轉(zhuǎn)效果,結(jié)合實(shí)例形式詳細(xì)分析了javascript實(shí)現(xiàn)圖片3D旋轉(zhuǎn)相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下2019-02-02JavaScript日期和時(shí)間的格式化及其它常用處理方法
這篇文章主要給大家介紹了關(guān)于JavaScript日期和時(shí)間的格式化及其它常用處理方法,JavaScript中可以使用Date對象來表示日期和時(shí)間,如果需要格式化日期和時(shí)間,可以使用Date對象的幾個(gè)方法和一些字符串操作方法來實(shí)現(xiàn),需要的朋友可以參考下2023-09-09form.submit()不能提交表單的錯(cuò)誤原因及解決方法
button的id不要設(shè)置為submit,否則可能會引起混淆,導(dǎo)致表單的submit()方法不能提交表單2014-10-10