微信小程序——获取用户信息
文章目錄
- 用戶信息
- 使用open-data展示微信開(kāi)放的數(shù)據(jù)
- 使用wx.getUserInfo保存用戶信息
用戶信息
- 首先明確一下用戶信息有哪些:
微信用戶昵稱、頭像、性別、城市、省份、國(guó)家和語(yǔ)言等。
【注意】上述信息都是微信資料中填寫的,并不是用戶的實(shí)時(shí)定位城市等。
- 獲取用戶信息分為兩種情況:
使用open-data展示微信開(kāi)放的數(shù)據(jù)
官方文檔:https://developers.weixin.qq.com/miniprogram/dev/component/open-data.html
對(duì)應(yīng)的信息會(huì)直接展示到頁(yè)面上。
<open-data type="userAvatarUrl"></open-data> <open-data type="userNickName"></open-data> <open-data type="userGender" lang="zh_CN"></open-data> <open-data type="userCity" lang="zh_CN"></open-data> <open-data type="userCountry" lang="zh_CN"></open-data>使用wx.getUserInfo保存用戶信息
官方文檔:https://developers.weixin.qq.com/miniprogram/dev/api/open-api/user-info/wx.getUserInfo.html
根據(jù)微信官方接口調(diào)整消息:https://developers.weixin.qq.com/community/develop/doc/0000a26e1aca6012e896a517556c01
使用wx.getUserInfo接口直接彈出授權(quán)框不再支持。接口調(diào)用的機(jī)制是:如果用戶已經(jīng)授權(quán)過(guò)了,可以直接拿到用戶信息,否則直接進(jìn)入fail回調(diào)。
所以【解決方案】如下:
onload時(shí)查看權(quán)限,如果已經(jīng)授權(quán)直接獲取用戶信息。在頁(yè)面中放置button,將open-type指定為getUserInfo類型,讓用戶主動(dòng)點(diǎn)擊發(fā)起授權(quán)詢問(wèn),從而獲得用戶信息。
lang默認(rèn)為en,表示返回用戶信息的語(yǔ)言,例如en模式下返回“China”,而zh_CN是“中國(guó)”。
<button open-type="getUserInfo" lang="zh_CN" bindgetuserinfo="onGotUserInfo">獲取用戶信息</button> Page({onLoad: function(e) {// 查看是否授權(quán)wx.getSetting({success:function(res){if (res.authSetting['scope.userInfo']) {//已授權(quán),可以獲取用戶信息wx.getUserInfo({success: function(res) {console.log(res.userInfo); //對(duì)象console.log(res.rawData); //Json}})}}})},onGotUserInfo(e){//點(diǎn)擊回調(diào),如果授權(quán)成功拿到用戶數(shù)據(jù),否則為undefinedconsole.log(e.detail.userInfo); //對(duì)象console.log(e.detail.rawData); //Json} })總結(jié)
以上是生活随笔為你收集整理的微信小程序——获取用户信息的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: cron每两个小时_cron命令每12小
- 下一篇: 收发一体超声波测距离传感器模块_空气中7