白鹭微信小游戏登陆

本文介绍了如何使用白鹭引擎开发微信小游戏,并详细讲解了登录功能的实现过程,包括参考了CSDN和简书上的相关教程。
摘要由CSDN通过智能技术生成

参考:https://blog.csdn.net/duan003387/article/details/83089765

参考:https://www.jianshu.com/p/56a9ac861c60

 


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);
      //判断用户是否授权过
      wx.getSetting({
        success(res) {
          if (sdkVersionNum >= 201 && !res.authSetting['scope.userInfo']) {
            var button = wx.createUserInfoButton({
              type: 'image',
              image: 'http://www.oneh5.com/iptv/h5/egret/billiard/resource/game/jiqiu.png',
              style: {
                left: 118,
                top: 263,
                width: 140,
                height: 140
              }
            });
            button.onTap((res) => {
              if (res.userInfo) {
                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{
                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);
                  }
                });
              }
            });
          }
        }
      })
    });
  }

第二种

class WxgamePlatform {

  name = 'wxgame'

  getPlatformSign() {
    return {
      platform: "wx"
    };
  }

  login() {
    return new Promise((resolve, reject) => {
      wx.login({
        success: (res) => {
          resolve(res)
        }
      })
    })
  }




  getUserInfo() {
    let sysInfo = wx.getSystemInfoSync();
    //获取微信界面大小
    let width = sysInfo.screenWidth;
    let height = sysInfo.screenHeight;

    let xPercent = (720 - 262) / 2 / 720;
    let yPercent = (1280 - 98) / 2 / 1280;
    let wPercent = (260 / 720);
    let hPercent = (98 / 1280);

    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) {
      if (sdkVersion >= "2.0.1") {
        egret.log("in 1");
        var button = wx.createUserInfoButton({
          type: 'text',
          text: '    ',
          //image: "resource/assets_game/main/button_wx_getuserinfo.png",
          style: {
            left: width * xPercent,
            top: height * yPercent,
            width: width * wPercent,
            height: height * hPercent,
            // backgroundColor: '#ff0000',
            // color: '#ffffff',
          }
        });

        egret.log("in 2");

        button.onTap((res) => {
          egret.log("onTap ", res.userInfo);
          if (res.userInfo) {
            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);
              }
            });
          }
        });
      }
    })
  }


  openDataContext = new WxgameOpenDataContext();
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值