微信用户授权,新老版本兼容的处理方法
emer 发布于 2018-6-13 11:06 3104 次阅读
getUserInfo(left,bottom) { return new Promise((resolve, reject) => { let sysInfo = wx.getSystemInfoSync(); let sdkVersion = sysInfo.SDKVersion; sdkVersion = sdkVersion.replace(/\./g, ""); sdkVersion = sdkVersion.substr(0, 3); let sdkVersionNum = parseInt(sdkVersion); console.log("platform获取用户授权:", sdkVersionNum); if (sdkVersionNum >= 201) { var button = wx.createUserInfoButton({ type: 'image', text: '微信授权', image: "resource/assets_game/main/button_wx_getuserinfo.png", style: { left: left, bottom: bottom, width: 183, height: 62, lineHeight: 40, backgroundColor: '#ff0000', color: '#ffffff', textAlign: 'center', fontSize: 16, borderRadius: 4 } }); button.onTap((res) => { console.log("用户授权:", res); var userInfo = res.userInfo; var nickName = userInfo.nickName; var avatarUrl = userInfo.avatarUrl; var gender = userInfo.gender; //性别 0:未知、1:男、2:女 var province = userInfo.province; var city = userInfo.city; var country = userInfo.country; button.destroy(); resolve(userInfo); }); }else { wx.getUserInfo({ withCredentials: true, success: res => { var userInfo = res.userInfo; var nickName = userInfo.nickName; var avatarUrl = userInfo.avatarUrl; var gender = userInfo.gender; //性别 0:未知、1:男、2:女 var province = userInfo.province; var city = userInfo.city; var country = userInfo.country; resolve(userInfo); }, fail: res => { wx.showModal({ title: '友情提醒', content: '请允许微信获得授权!', confirmText: "授权", showCancel: false, success: res => { resolve(null); } }); } }); } }); }
嗯,外面加一个while(true) 循环,判断是否已授权