场景1:在界面是只是显示用户的头像和昵称
在wxml里添加
<open-data type="userAvatarUrl"></open-data> //头像
<open-data type="userNickName"></open-data> //昵称
<open-data type="userCountry"></open-data> //国家
<open-data type="userCity"></open-data> //城市
场景2:在js里获取已授权的用户信息
wx.getUserInfo({
success:(res)=>{
console.log(res)
}
})
场景3:弹出授权框由用户决定是否授权给当前小程序
<button open-type="getUserInfo" bindgetuserinfo="getUserInfo">获取用户信息</button>
getUserInfo(event){
console.log(event)//缺点:没有openid
}
场景4:获取openid
传统方式获取

const APP_ID ='';//输入小程序appid
const APP_SECRET ='';//输入小程序app_secret
var OPEN_ID=''//储存获取到openid
var SESSION_KEY=''//储存获取到session_key
getOpenIdTap:function(){
var that=this;
wx.login({
success:function(res){
wx.request({
//获取openid接口
url: '',//
data:{
appid:APP_ID,
secret:APP_SECRET,
js_code:res.code,
},
method:'GET',
success:function(res){
console.log(res.data)
OPEN_ID = res.data.openid;//获取到的openid
SESSION_KEY = res.data.session_key;//获取到session_key
console.log(OPEN_ID.length)
console.log(SESSION_KEY.length)
}
})
}
})
}
云开发方式获取openid
<button bind:tap="getOpenid">获取openid</button>
getOpenid(){
wx.cloud.callFunction({
name:"login"
}).then((res)=>{
console.log(res)
})
}
浙公网安备 33010602011771号