export default { checkAndAuth() { let _this = this; // 通过 wx.getSetting 先查询一下用户是否授权了 "scope.userInfo" 这个 scope wx.getSetting({ success(res) { if (!res.authSetting['scope.userInfo']) { // 用户授权 wx.authorize({ scope: 'scope.userInfo', success() { // 用户已经同意, 后续调用此接口不会弹窗询问 _this.login(); }, fail() { // 用户已经拒绝过授权 wx.openSetting({ success(res) { if (res['scope.userInfo']) { _this.checkAndAuth(); } } }) } }) } else { _this.login(); } } }) }, async login() { // 从缓存中获取登录信息 let userInfo = uni.getStorageSync('userInfo'); if (userInfo) { return; } // 获取微信登录凭证 const wxLoginCode = wx.login(); // TODO:调用小程序服务端确认是否是授权登录过的用户 const loginRes = { logined: true, userInfo: {} }; // 未登录过的获取微信用户信息 if (loginRes || !loginRes.logined) { userInfo = await wx.getUserProfile({ desc: '用于小程序登录' }); } else { userInfo = loginRes.userInfo; } // 页面存储用户登录有效信息,以便其他页面调用 uni.setStorageSync('userInfo', userInfo); } }