微信小程序之 wx.getUserInfo引导用户授权问题
首先,在page外定义一个函数用户判断是否为空对象 var isEmptyObject = function (e) { var temp; for (temp in e) return ! 1 ; return ! 0 } 然后,在page中的onload里面调用授权 onLoad: function () { var that = this ; if (app.globalData.userInfo) { this .setData({ userInfo: app.globalData.userInfo }) } else if ( this .data.canIUse) { // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回 // 所以此处加入 callback 以防止这种情况 app.userInfoReadyCallback = res => { this .setData({ userInfo: res.userInfo }) } } else { // 在没有 open-type=getUserInfo 版本的兼容处理 wx.getUserInfo({ success: res => { app.globalData.userInfo = res.userInfo this .setData({ userInfo: res