php微信小程序获取用户信息或授权

    xiaoxiao2022-07-05  165

    在已授权的情况下调用

    // 必须是在用户已经授权的情况下调用 wx.getUserInfo({ success(res) { const userInfo = res.userInfo const nickName = userInfo.nickName const avatarUrl = userInfo.avatarUrl const gender = userInfo.gender // 性别 0:未知、1:男、2:女 const province = userInfo.province const city = userInfo.city const country = userInfo.country } })

     

    小程序用户信息组件示例代码

    未授权时以下方式获取头像及微信昵称 

    <!-- 如果只是展示用户头像昵称,可以使用 <open-data /> 组件 --> <open-data type="userAvatarUrl"></open-data> <open-data type="userNickName"></open-data>

    更改样式----->(微信小程序 open-data更改样式 open-data 显示头像 圆形) 

    需要使用 button 来授权登录 

    <!-- 需要使用 button 来授权登录 --> <button wx:if="{{canIUse}}" open-type="getUserInfo" bindgetuserinfo="bindGetUserInfo" > 授权登录 </button>

    查看是否授权

    wx.login({ success: res => { // 获取用户信息 wx.getSetting({ success: res => { console.log("16.04",res) if (res.authSetting['scope.userInfo']) { // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框 wx.getUserInfo({ success: res => { wx.setStorageSync('userinfo', res.userInfo) console.log(res.userInfo) } }) } }, fail: function () { console.log('获取用户信息失败') //获取用户信息失败后。请跳转授权页面 wx.showModal({ title: '警告', content: '尚未进行授权,请点击确定跳转到授权页面进行授权。', success: function (res) { if (res.confirm) { console.log('用户点击确定') wx.navigateTo({ url: '../tologin/tologin', }) } } }) } }) } }),

    详情走官网https://developers.weixin.qq.com/miniprogram/dev/api/wx.getUserInfo.html 

    最新回复(0)