window.UtilsWX = { soundManage: null, //是否 是分享 0 不是 分享 1 是分享 shareTag: 0, ranklisttag: false, sharesUrl: [ "https://www.youaigclm.top/media/sharing-pictures/S1.jpg", "https://www.youaigclm.top/media/sharing-pictures/S2.jpg", "https://www.youaigclm.top/media/sharing-pictures/S3.jpg", "https://www.youaigclm.top/media/sharing-pictures/S4.jpg", // "https://www.youaigclm.top/media/Share5.png", ], sharesName: [ "{有人@你}做富豪就是这么嗨、立即坐拥香车美女", "{有人@你}完成一亿的小目标、建立属于你的商业帝国", "{有人@你}一秒开店、日进斗金、赚到手软!", "{有人@你}我用两天就当上大富豪了、你有我优秀吗?", // "我不嫁豪门,我自己就是豪门!", ], setShareTag: function (tag) { this.shareTag = tag; }, 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); let name = this.getWXname(userdata.userInfo.nickName); var avatarUrl = userdata.userInfo.avatarUrl; var gender = userdata.userInfo.gender; var nickName = name; var city = userdata.userInfo.city; var province = userdata.userInfo.province; console.log("现在 调用 微信登录api", nickName); wx.login({ success: function (res) { console.log("现在 调用 咱们后台登录api", 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) { console.log("现在 调用 咱们后台登录api 成功", 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 () { console.log("现在 调用 咱们后台登录api 失败"); if (callbackfail != null) { callbackfail(); } } }); } }); }, getWXname: function (namewx) { let sss = namewx; let namehan = sss.replace(/[^\u4E00-\u9FA5]/g, ''); let nameshu = sss.replace(/[^\d]/g, ''); let name = sss.replace(/[^a-zA-Z]/g, ''); return namehan + nameshu + name; }, 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("点击完了 按钮 是什么呢 ", 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 () { console.log("删除按钮", this.userInfoButton); if (this.userInfoButton != null) { this.userInfoButton.destroy(); } }, shareMenu: function () { if (cc.sys.platform === cc.sys.WECHAT_GAME) { // let m = Math.floor(Math.random() * 5); // let Shares = [ // "https://www.youaigclm.top/media/Share1.png", // "https://www.youaigclm.top/media/Share2.png", // "https://www.youaigclm.top/media/Share3.png", // "https://www.youaigclm.top/media/Share4.png", // "https://www.youaigclm.top/media/Share5.png", // ] // let Sharesname = [ // "全球富豪排行榜出炉,快来看看你和我相差多少!", // "全球富豪排行榜出炉,你与首富之间隔着多少个马爸爸?", // "全国工资最高的一个女人被曝光!看看你认识她吗?", // "这档大尺度相亲节目揭开嫁入豪门的潜规则.....", // "我不嫁豪门,我自己就是豪门!", // ] // "{有人@你}我用两天就当上大富豪了、你有我优秀吗?", var image = this.sharesUrl[3]; var name = this.sharesName[3]; //开启右上角的分享 wx.showShareMenu(); //监听右上角的分享调用  cc.loader.loadRes("Share", function (err, data) { // console.log("图片地址",data); wx.onShareAppMessage(function (res) { UtilsWX.setShareTag(1); return { title: name, // imageUrl: data.url, imageUrl: image, success(res) { // console.log("转发成功!!!") // common.diamond += 20; UtilsWX.setShareTag(1); }, fail(res) { // console.log("转发失败!!!") UtilsWX.setShareTag(1); } } }) }); } }, sharebtn: function (callback) { if (cc.sys.platform === cc.sys.WECHAT_GAME) { let m = Math.floor(Math.random() * 4); // let Shares = [ // "https://www.youaigclm.top/media/Share1.png", // "https://www.youaigclm.top/media/Share2.png", // "https://www.youaigclm.top/media/Share3.png", // "https://www.youaigclm.top/media/Share4.png", // "https://www.youaigclm.top/media/Share5.png", // ] // let Sharesname = [ // "全球富豪排行榜出炉,快来看看你和我相差多少!", // "全球富豪排行榜出炉,你与首富之间隔着多少个马爸爸?", // "全国工资最高的一个女人被曝光!看看你认识她吗?", // "这档大尺度相亲节目揭开嫁入豪门的潜规则.....", // "我不嫁豪门,我自己就是豪门!", // ] UtilsWX.setShareTag(1); var image = this.sharesUrl[m]; var name = this.sharesName[m]; //监听右上角的分享调用  cc.loader.loadRes("Share", function (err, data) { // console.log("图片地址",data); // console.log("转发进来!!!") if (callback != null) { callback(); } wx.shareAppMessage({ title: name, imageUrl: image, 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 () { } }); } }, initWXSubContextView: function (node) { this.WXSubContextView = node; }, openWXSubContextView: function (b) { if (this.WXSubContextView != null && !this.ranklisttag) { this.WXSubContextView.active = b; } }, lookRankList: function (b) { if (this.WXSubContextView != null) { this.ranklisttag = b; this.WXSubContextView.active = b; } }, //给子域 发消息 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); } }); } }, getAllBuild: function (callback) { var arr = []; var countAssetValue = 0; // var aaa = { // "0":"a", // "1":"b", // "2":"c", // "aa":"d", // "4":"e" // }; // // console.log("测试",aaa .hasOwnProperty('5')); for (var i = 0; i < GlobalD.game.buildingsTiledMapUnit.length; i++) { var building = GlobalD.game.buildingsTiledMapUnit[i]; // if (building.getBuildingsInfo.buildInfo.buildType == 5) { arr.push({ name: building.getBuildingsInfo.buildInfo.buildingName, AssetValue: building.getBuildingsInfo.buildInfo.AssetValue, }); countAssetValue += building.getBuildingsInfo.buildInfo.AssetValue; // } } var postdata = {}; for (var i = 0; i < arr.length; i++) { if (postdata.hasOwnProperty(arr[i].name) == false) { postdata[arr[i].name] = arr[i]; } else { postdata[arr[i].name].AssetValue += arr[i].AssetValue; } } var postdataarr = []; for (var prop in postdata) { if (postdata.hasOwnProperty(prop)) { postdataarr.push(postdata[prop]) } } // console.log("要发送的数据是撒打算打算的", countAssetValue,GlobalD.GameData.Golden); countAssetValue += parseInt(GlobalD.GameData.Golden); countAssetValue += parseInt(GlobalD.GameData.Diamond); // console.log("商店建筑数据啊实打实的",countAssetValue); UtilsWX.setUserData("score", countAssetValue); // console.log("商店建筑数据",arr); var data = { name: "Whoareyou", openId: userData.openId, id: countAssetValue } UtilsWX.postMessage(data); if (callback != null) { callback(); } } }