platform.js

/**
 * 请在白鹭引擎的Main.ts中调用 platform.login() 方法调用至此处。
 */

class WxgamePlatform {

  name = 'wxgame'

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

  //监听小游戏回到前台的事件
  onShow(fun, obj) {
    wx.onShow(function() {
      fun.call(obj);
    })
  }
  //监听小游戏隐藏到后台事件。锁屏、按 HOME 键退到桌面、显示在聊天顶部等操作会触发此事件。

  onHide(fun, obj) {
    wx.onHide(function () {
      fun.call(obj);
    })
  }

  //获取启动参数
  getLaunchOptionsSync() {
    console.log(JSON.stringify(wx.getLaunchOptionsSync().query), "游戏启动");
  }

  //获取用户信息。
  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);
                  }
                });
              }
            });
          }
        }
      })
    });
  }

  openDataContext = new WxgameOpenDataContext();
}

class WxgameOpenDataContext {

  createDisplayObject(type, width, height) {
    const bitmapdata = new egret.BitmapData(sharedCanvas);
    bitmapdata.$deleteSource = false;
    const texture = new egret.Texture();
    texture._setBitmapData(bitmapdata);
    const bitmap = new egret.Bitmap(texture);
    bitmap.width = width;
    bitmap.height = height;

    if (egret.Capabilities.renderMode == "webgl") {
      const renderContext = egret.wxgame.WebGLRenderContext.getInstance();
      const context = renderContext.context;
      ////需要用到最新的微信版本
      ////调用其接口WebGLRenderingContext.wxBindCanvasTexture(number texture, Canvas canvas)
      ////如果没有该接口,会进行如下处理,保证画面渲染正确,但会占用内存。
      if (!context.wxBindCanvasTexture) {
        egret.startTick((timeStarmp) => {
          egret.WebGLUtils.deleteWebGLTexture(bitmapdata.webGLTexture);
          bitmapdata.webGLTexture = null;
          return false;
        }, this);
      }
    }
    return bitmap;
  }


  postMessage(data) {
    const openDataContext = wx.getOpenDataContext();
    openDataContext.postMessage(data);
  }
}


window.platform = new WxgamePlatform();

你可能感兴趣的:(egret微信小游戏)