- 微信小程序获取用户新接口采坑解决。
问题描述 相信很多人在使用微信小程序新接口获取到用户信息时,或多或少都遇到过这种情况。 [代码]调用wx.getUserProfile 报错getUserProfile:fail can only be invoked by user TAP gesture. [代码] 这个错误信息是啥意思?大致的意思就是,需要将授权按钮设置为点击事件,也就是给对应的触发条件绑定一个bintap事件。 解决思路 第一步。确认代码无误。 首先我们需要在wxml中这样定义登录按钮。 [代码]<view class="login-btn"> <button class="login-btn" style="background-color: #07C160;color:white;width:100%;" open-type="getUserProfile" bindtap="getUserProfile"> 登录授权 </button> </view> [代码] 接着在js文件中定义如下的信息。 [代码] getUserProfile() { wx.login({ success(res) { let code = res.code; wx.getUserProfile({ lang: 'en', desc: '获取授权', success: function (res) { console.log(res) }, fail: function (res) { console.log('fail-get-userinfo', res) }, complete: function () { console.log('complete-get-userinfo') } }) } }); }, [代码] 在实际中发现,点击事件名称换做非getUserProfile名称,就不行了。 第二步,使用微信真机调试。 符合上面的两种情况,就可以获取到用户信息了。 希望对大家有所帮助。
2021-03-12 - 2020-10-20