网站首页 javascript技术
写游戏涨知识系统-微信接入
发布时间:2019-11-19 01:09查看次数:3859
微信小游戏接入流程
wx.login 获取用户登陆状态code
向后端请求 换取用户openid
返回用户登陆不变的openid
完成登陆动作
如果需要用户头像 地区需要用户授权
授权方式:需要在页面上创建一个授权按钮唤醒授权界面~
wx.createUserInfoButton()
附上小游戏接口文件说明:
写游戏涨知识系统-微信接入
/** * 请在白鹭引擎的Main.ts中调用 platform.login() 方法调用至此处。 */ class WxgamePlatform { name = 'wxgame' //用户登陆 login() { return new Promise((resolve, reject) => { wx.login({ success: (res) => { resolve(res) } }) }) } //获取用户信息 没有授权的情况下 是获取失败的 getUserInfo() { return new Promise((resolve, reject) => { wx.getUserInfo({ withCredentials: true, success: function(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); } }) }) } //发送分享数据到公共域 sendShareData(kvdata) { let openDataContext = wx.getOpenDataContext() openDataContext.postMessage(kvdata); } //分享应用 shareAppMessage(title, imageUrl, query) { return new Promise((resolve, reject) => { wx.shareAppMessage({ title: title, imageUrl: imageUrl, query: query, success: res => { resolve(true); }, fail: res => { console.log(res); resolve(false); } }) }) } //更新分享链接 updateShareMenu(withShareTicket) { return new Promise((resolve, reject) => { wx.updateShareMenu({ withShareTicket: withShareTicket, success: res => { resolve(res) }, fail: res => { resolve(false) } }) }) } //剩下应用 shareApp(title, imageUrl, query) { return this.updateShareMenu(true).then((res) => { if (res) { return new Promise((resolve, reject) => { wx.shareAppMessage({ title: title, imageUrl: imageUrl, query: query, success: res => { resolve(res); }, fail: res => { console.log(res); resolve(false); } }) }) } }); } //存储用户信息 setUserCloudStorage(KVDataList) { return new Promise((resolve, reject) => { wx.setUserCloudStorage({ KVDataList: KVDataList, success: res => { console.log('success', res); resolve(res); }, fail: res => { console.log('fail', res); } }) }) } getLaunchOptionsSync() { return wx.getLaunchOptionsSync(); } 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; 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();
关键字词:踩坑##