window.UtilsWX = { soundManage : null, wxgameLogin : function(callbacks){ this.userLogin(function (callback) { this.getUserInfo(function (res) { // userData.httpData.data = res.userInfo; // console.log('登陆过的信息',res.userInfo) userData.httpData.data = res.userInfo; // this.wxlogin(res); if (callbacks!=null) { callbacks(res); } }.bind(this), function () { this.createUserInfoButton(callbacks); }.bind(this)); }.bind(this), function () { // console.log('没登陆过') //创建获取授权的按钮 this.createUserInfoButton(callbacks); }.bind(this)); }, wxlogin: function (userdata,callback,callbackfail) { // console.log("userdata", userdata.userInfo); var avatarUrl = userdata.userInfo.avatarUrl; var gender = userdata.userInfo.gender; var nickName = userdata.userInfo.nickName; var city = userdata.userInfo.city; var province = userdata.userInfo.province; wx.login({ success: function (res) { wx.request({ url: "https://www.youaigclm.top/Iamrichman_app/Wxlogingame/?wx=" + res.code + "&avatarUrl=" + avatarUrl + "&nickName=" + nickName + "&gender=" + gender + "&city=" + city + "&province=" + province , method: "GET", success: function (data) { if (data.statusCode == 200) { // console.log("request咱们后台给的", data); userData.httpData = data.data; userData.openId = data.data.data.openid; if (callback != null) { callback(); } // console.log("获取到临时代码保存的数据是", userData); } }.bind(this), fail : function () { if (callbackfail != null) { callbackfail(); } } }); } }); }, userLogin : function (successCallback,failCallback) { if (cc.sys.platform === cc.sys.WECHAT_GAME) { wx.checkSession({ success: function () { //存在登陆态 if (successCallback!=null) { successCallback(); // console.log('微信登陆状态 --- 登陆'); } }, fail: function () { //不存在登陆态 if (failCallback!=null) { failCallback(); // console.log('微信登陆状态 --- 未登陆'); } } }) } }, getUserInfo : function(successCallback,failCallback){ if (cc.sys.platform === cc.sys.WECHAT_GAME){ wx.getUserInfo({ withCredentials: true,//此处设为true,才会返回encryptedData等敏感信息 success: function (res) { // 可以将 res 发送给后台解码出 unionId // app.globalData.userInfo = res.userInfo; // app.globalData.encryptedData = res.encryptedData; // app.globalData.iv = res.iv; if (successCallback!=null) { successCallback(res); // console.log('微信登陆状态 --- 获取个人信息'); } }.bind(this), fail: function (res) { if (failCallback!=null) { failCallback(res); // console.log('微信登陆状态 --- 没授权过'); } }.bind(this), }) } }, createUserInfoButton : function (onclickCallback) { if (cc.sys.platform === cc.sys.WECHAT_GAME){ this.width = 160; this.height = 60; this.getSystemInfo(function (obj) { this.userInfoButton = wx.createUserInfoButton({ type: 'image', text: '', image: httpUtils.PlayButton, style: { left: obj.windowWidth/2-this.width/2, top: obj.windowHeight/1.3-this.height/2, width: this.width, height: this.height, lineHeight: 40, backgroundColor: '#ff0000', color: '#ffffff', textAlign: 'center', fontSize: 16, borderRadius: 4 } }) this.userInfoButton.onTap(function (res) { // console.log("createUserInfoButton", res); if (res.errMsg == "getUserInfo:ok") { // this.userInfoButton.destroy(); if (onclickCallback!=null) { onclickCallback(res); // console.log('点击登陆授权按钮获取授权之后',res); } // } }.bind(this)) }.bind(this)); return this.userInfoButton; } }, removeUserInfoButton : function(){ if (this.userInfoButton!=null) { this.userInfoButton.destroy(); } }, shareMenu : function () { if (cc.sys.platform === cc.sys.WECHAT_GAME) { //开启右上角的分享 wx.showShareMenu(); //监听右上角的分享调用  cc.loader.loadRes("Share",function(err,data){ // console.log("图片地址",data); wx.onShareAppMessage(function(res){ return { title: "不怕,就来PK!转发", // imageUrl: data.url, imageUrl: httpUtils.Share, success(res){ // console.log("转发成功!!!") // common.diamond += 20; }, fail(res){ // console.log("转发失败!!!") } } }) }); } }, sharebtn : function (callback) { if (cc.sys.platform === cc.sys.WECHAT_GAME) { //监听右上角的分享调用  cc.loader.loadRes("Share",function(err,data){ // console.log("图片地址",data); // console.log("转发进来!!!") if (callback!=null) { callback(); } wx.shareAppMessage({ title: "不怕,就来PK!按钮", imageUrl: httpUtils.Share, success(res){ console.log("转发成功!!!") if (callback!=null) { callback(); } }, fail(res){ console.log("转发失败!!!") if (callback!=null) { callback(); } } }) }); }else{ // console.log("啥都没做!!!") if (callback!=null) { callback(); } } }, getSystemInfo : function (success) { if (cc.sys.platform === cc.sys.WECHAT_GAME) { wx.getSystemInfo({ success: function (obj) { // console.log('微信 --- 获取设备尺寸',obj); if (success != null) { success(obj); } }, fail: function () { } }); } }, //给子域 发消息 postMessage : function (data) { if (cc.sys.platform === cc.sys.WECHAT_GAME) { wx.postMessage({ message: data }); } }, createVideo : function (data) { if (cc.sys.platform === cc.sys.WECHAT_GAME) { this.getSystemInfo(function (obj) { this.width = obj.windowWidth; this.height = obj.windowHeight/1.5; // console.log("那个",this.height); this.video = wx.createVideo({ x: obj.windowWidth/2-this.width/2, y: obj.windowHeight/2-this.height/2, width: this.width, height: this.height, // poster: 'http://192.168.1.25:9999/NoviceBG.png', // src: "https://www.yuyekeji.cn/static/Cocos/fuhao/Course.mp4", src: "http://192.168.1.25:9999/Course.mp4", objectFit : "contain" }) this.soundManage.startVideo(); // this.video.requestFullScreen({ direction: 0 }) this.video.play(); // this.video.exitFullScreen() }.bind(this)); } }, removeVideo : function () { if (cc.sys.platform === cc.sys.WECHAT_GAME) { if (this.video!=null) { this.video.destroy(); this.soundManage.stopVideo(); } } }, //设置排行榜上面的数据 setUserData : function (dataKey,dataValue) { if (cc.sys.platform === cc.sys.WECHAT_GAME) { wx.setUserCloudStorage({ KVDataList: [ { key: String(dataKey), value: String(dataValue) }, ], success: res => { // console.log('成功setUserCloudStorage',res); }, fail: res => { // console.log('失败setUserCloudStorage',res); } }); } }, }