微信用户授权,新老版本兼容的处理方法
首页 > >    作者:lininn   2018年6月13日 11:06 星期三   热度:1931°   百度已收录  
时间:2018-6-13 11:06   热度:1931° 
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) 循环,判断是否已授权 
二维码加载中...
本文作者:lininn      文章标题: 微信用户授权,新老版本兼容的处理方法
本文地址:?post=163
版权声明:若无注明,本文皆为“覆手为雨”原创,转载请保留文章出处。
分享本文至:

返回顶部    首页    手机版本    后花园   会员注册   
版权所有:覆手为雨    站长: lininn