UtilsWX.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. window.UtilsWX = {
  2. soundManage: null,
  3. //是否 是分享 0 不是 分享 1 是分享
  4. shareTag: 0,
  5. ranklisttag: false,
  6. sharesUrl: [
  7. "https://www.youaigclm.top/media/sharing-pictures/S1.jpg",
  8. "https://www.youaigclm.top/media/sharing-pictures/S2.jpg",
  9. "https://www.youaigclm.top/media/sharing-pictures/S3.jpg",
  10. "https://www.youaigclm.top/media/sharing-pictures/S4.jpg",
  11. // "https://www.youaigclm.top/media/Share5.png",
  12. ],
  13. sharesName: [
  14. "{有人@你}做富豪就是这么嗨、立即坐拥香车美女",
  15. "{有人@你}完成一亿的小目标、建立属于你的商业帝国",
  16. "{有人@你}一秒开店、日进斗金、赚到手软!",
  17. "{有人@你}我用两天就当上大富豪了、你有我优秀吗?",
  18. // "我不嫁豪门,我自己就是豪门!",
  19. ],
  20. setShareTag: function (tag) {
  21. this.shareTag = tag;
  22. },
  23. wxgameLogin: function (callbacks) {
  24. this.userLogin(function (callback) {
  25. this.getUserInfo(function (res) {
  26. // userData.httpData.data = res.userInfo;
  27. // console.log('登陆过的信息',res.userInfo)
  28. userData.httpData.data = res.userInfo;
  29. // this.wxlogin(res);
  30. if (callbacks != null) {
  31. callbacks(res);
  32. }
  33. }.bind(this), function () {
  34. this.createUserInfoButton(callbacks);
  35. }.bind(this));
  36. }.bind(this), function () {
  37. // console.log('没登陆过')
  38. //创建获取授权的按钮
  39. this.createUserInfoButton(callbacks);
  40. }.bind(this));
  41. },
  42. wxlogin: function (userdata, callback, callbackfail) {
  43. // console.log("userdata", userdata.userInfo);
  44. let name = this.getWXname(userdata.userInfo.nickName);
  45. var avatarUrl = userdata.userInfo.avatarUrl;
  46. var gender = userdata.userInfo.gender;
  47. var nickName = name;
  48. var city = userdata.userInfo.city;
  49. var province = userdata.userInfo.province;
  50. console.log("现在 调用 微信登录api", nickName);
  51. wx.login({
  52. success: function (res) {
  53. console.log("现在 调用 咱们后台登录api", res);
  54. wx.request({
  55. url: "https://www.youaigclm.top/Iamrichman_app/Wxlogingame/?wx=" + res.code
  56. +
  57. "&avatarUrl=" + avatarUrl +
  58. "&nickName=" + nickName +
  59. "&gender=" + gender +
  60. "&city=" + city +
  61. "&province=" + province
  62. ,
  63. method: "GET",
  64. success: function (data) {
  65. console.log("现在 调用 咱们后台登录api 成功", data);
  66. if (data.statusCode == 200) {
  67. // console.log("request咱们后台给的", data);
  68. userData.httpData = data.data;
  69. userData.openId = data.data.data.openid;
  70. if (callback != null) {
  71. callback();
  72. }
  73. // console.log("获取到临时代码保存的数据是", userData);
  74. }
  75. }.bind(this),
  76. fail: function () {
  77. console.log("现在 调用 咱们后台登录api 失败");
  78. if (callbackfail != null) {
  79. callbackfail();
  80. }
  81. }
  82. });
  83. }
  84. });
  85. },
  86. getWXname: function (namewx) {
  87. let sss = namewx;
  88. let namehan = sss.replace(/[^\u4E00-\u9FA5]/g, '');
  89. let nameshu = sss.replace(/[^\d]/g, '');
  90. let name = sss.replace(/[^a-zA-Z]/g, '');
  91. return namehan + nameshu + name;
  92. },
  93. userLogin: function (successCallback, failCallback) {
  94. if (cc.sys.platform === cc.sys.WECHAT_GAME) {
  95. wx.checkSession({
  96. success: function () {
  97. //存在登陆态
  98. if (successCallback != null) {
  99. successCallback();
  100. // console.log('微信登陆状态 --- 登陆');
  101. }
  102. },
  103. fail: function () {
  104. //不存在登陆态
  105. if (failCallback != null) {
  106. failCallback();
  107. // console.log('微信登陆状态 --- 未登陆');
  108. }
  109. }
  110. })
  111. }
  112. },
  113. getUserInfo: function (successCallback, failCallback) {
  114. if (cc.sys.platform === cc.sys.WECHAT_GAME) {
  115. wx.getUserInfo({
  116. withCredentials: true,//此处设为true,才会返回encryptedData等敏感信息
  117. success: function (res) {
  118. // 可以将 res 发送给后台解码出 unionId
  119. // app.globalData.userInfo = res.userInfo;
  120. // app.globalData.encryptedData = res.encryptedData;
  121. // app.globalData.iv = res.iv;
  122. if (successCallback != null) {
  123. successCallback(res);
  124. // console.log('微信登陆状态 --- 获取个人信息');
  125. }
  126. }.bind(this),
  127. fail: function (res) {
  128. if (failCallback != null) {
  129. failCallback(res);
  130. // console.log('微信登陆状态 --- 没授权过');
  131. }
  132. }.bind(this),
  133. })
  134. }
  135. },
  136. createUserInfoButton: function (onclickCallback) {
  137. if (cc.sys.platform === cc.sys.WECHAT_GAME) {
  138. this.width = 160;
  139. this.height = 60;
  140. this.getSystemInfo(function (obj) {
  141. this.userInfoButton = wx.createUserInfoButton({
  142. type: 'image',
  143. text: '',
  144. image: httpUtils.PlayButton,
  145. style: {
  146. left: obj.windowWidth / 2 - this.width / 2,
  147. top: obj.windowHeight / 1.3 - this.height / 2,
  148. width: this.width,
  149. height: this.height,
  150. lineHeight: 40,
  151. backgroundColor: '#ff0000',
  152. color: '#ffffff',
  153. textAlign: 'center',
  154. fontSize: 16,
  155. borderRadius: 4
  156. }
  157. })
  158. this.userInfoButton.onTap(function (res) {
  159. console.log("点击完了 按钮 是什么呢 ", res);
  160. if (res.errMsg == "getUserInfo:ok") {
  161. // this.userInfoButton.destroy();
  162. if (onclickCallback != null) {
  163. onclickCallback(res);
  164. // console.log('点击登陆授权按钮获取授权之后',res);
  165. }
  166. //
  167. }
  168. }.bind(this))
  169. }.bind(this));
  170. return this.userInfoButton;
  171. }
  172. },
  173. removeUserInfoButton: function () {
  174. console.log("删除按钮", this.userInfoButton);
  175. if (this.userInfoButton != null) {
  176. this.userInfoButton.destroy();
  177. }
  178. },
  179. shareMenu: function () {
  180. if (cc.sys.platform === cc.sys.WECHAT_GAME) {
  181. // let m = Math.floor(Math.random() * 5);
  182. // let Shares = [
  183. // "https://www.youaigclm.top/media/Share1.png",
  184. // "https://www.youaigclm.top/media/Share2.png",
  185. // "https://www.youaigclm.top/media/Share3.png",
  186. // "https://www.youaigclm.top/media/Share4.png",
  187. // "https://www.youaigclm.top/media/Share5.png",
  188. // ]
  189. // let Sharesname = [
  190. // "全球富豪排行榜出炉,快来看看你和我相差多少!",
  191. // "全球富豪排行榜出炉,你与首富之间隔着多少个马爸爸?",
  192. // "全国工资最高的一个女人被曝光!看看你认识她吗?",
  193. // "这档大尺度相亲节目揭开嫁入豪门的潜规则.....",
  194. // "我不嫁豪门,我自己就是豪门!",
  195. // ]
  196. // "{有人@你}我用两天就当上大富豪了、你有我优秀吗?",
  197. var image = this.sharesUrl[3];
  198. var name = this.sharesName[3];
  199. //开启右上角的分享
  200. wx.showShareMenu();
  201. //监听右上角的分享调用 
  202. cc.loader.loadRes("Share", function (err, data) {
  203. // console.log("图片地址",data);
  204. wx.onShareAppMessage(function (res) {
  205. UtilsWX.setShareTag(1);
  206. return {
  207. title: name,
  208. // imageUrl: data.url,
  209. imageUrl: image,
  210. success(res) {
  211. // console.log("转发成功!!!")
  212. // common.diamond += 20;
  213. UtilsWX.setShareTag(1);
  214. },
  215. fail(res) {
  216. // console.log("转发失败!!!")
  217. UtilsWX.setShareTag(1);
  218. }
  219. }
  220. })
  221. });
  222. }
  223. },
  224. sharebtn: function (callback) {
  225. if (cc.sys.platform === cc.sys.WECHAT_GAME) {
  226. let m = Math.floor(Math.random() * 4);
  227. // let Shares = [
  228. // "https://www.youaigclm.top/media/Share1.png",
  229. // "https://www.youaigclm.top/media/Share2.png",
  230. // "https://www.youaigclm.top/media/Share3.png",
  231. // "https://www.youaigclm.top/media/Share4.png",
  232. // "https://www.youaigclm.top/media/Share5.png",
  233. // ]
  234. // let Sharesname = [
  235. // "全球富豪排行榜出炉,快来看看你和我相差多少!",
  236. // "全球富豪排行榜出炉,你与首富之间隔着多少个马爸爸?",
  237. // "全国工资最高的一个女人被曝光!看看你认识她吗?",
  238. // "这档大尺度相亲节目揭开嫁入豪门的潜规则.....",
  239. // "我不嫁豪门,我自己就是豪门!",
  240. // ]
  241. UtilsWX.setShareTag(1);
  242. var image = this.sharesUrl[m];
  243. var name = this.sharesName[m];
  244. //监听右上角的分享调用 
  245. cc.loader.loadRes("Share", function (err, data) {
  246. // console.log("图片地址",data);
  247. // console.log("转发进来!!!")
  248. if (callback != null) {
  249. callback();
  250. }
  251. wx.shareAppMessage({
  252. title: name,
  253. imageUrl: image,
  254. success(res) {
  255. console.log("转发成功!!!")
  256. if (callback != null) {
  257. callback();
  258. }
  259. },
  260. fail(res) {
  261. console.log("转发失败!!!")
  262. if (callback != null) {
  263. callback();
  264. }
  265. }
  266. })
  267. });
  268. } else {
  269. // console.log("啥都没做!!!")
  270. if (callback != null) {
  271. callback();
  272. }
  273. }
  274. },
  275. getSystemInfo: function (success) {
  276. if (cc.sys.platform === cc.sys.WECHAT_GAME) {
  277. wx.getSystemInfo({
  278. success: function (obj) {
  279. // console.log('微信 --- 获取设备尺寸',obj);
  280. if (success != null) {
  281. success(obj);
  282. }
  283. },
  284. fail: function () {
  285. }
  286. });
  287. }
  288. },
  289. initWXSubContextView: function (node) {
  290. this.WXSubContextView = node;
  291. },
  292. openWXSubContextView: function (b) {
  293. if (this.WXSubContextView != null && !this.ranklisttag) {
  294. this.WXSubContextView.active = b;
  295. }
  296. },
  297. lookRankList: function (b) {
  298. if (this.WXSubContextView != null) {
  299. this.ranklisttag = b;
  300. this.WXSubContextView.active = b;
  301. }
  302. },
  303. //给子域 发消息
  304. postMessage: function (data) {
  305. if (cc.sys.platform === cc.sys.WECHAT_GAME) {
  306. wx.postMessage({
  307. message: data
  308. });
  309. }
  310. },
  311. createVideo: function (data) {
  312. if (cc.sys.platform === cc.sys.WECHAT_GAME) {
  313. this.getSystemInfo(function (obj) {
  314. this.width = obj.windowWidth;
  315. this.height = obj.windowHeight / 1.5;
  316. // console.log("那个",this.height);
  317. this.video = wx.createVideo({
  318. x: obj.windowWidth / 2 - this.width / 2,
  319. y: obj.windowHeight / 2 - this.height / 2,
  320. width: this.width,
  321. height: this.height,
  322. // poster: 'http://192.168.1.25:9999/NoviceBG.png',
  323. // src: "https://www.yuyekeji.cn/static/Cocos/fuhao/Course.mp4",
  324. src: "http://192.168.1.25:9999/Course.mp4",
  325. objectFit: "contain"
  326. })
  327. this.soundManage.startVideo();
  328. // this.video.requestFullScreen({ direction: 0 })
  329. this.video.play();
  330. // this.video.exitFullScreen()
  331. }.bind(this));
  332. }
  333. },
  334. removeVideo: function () {
  335. if (cc.sys.platform === cc.sys.WECHAT_GAME) {
  336. if (this.video != null) {
  337. this.video.destroy();
  338. this.soundManage.stopVideo();
  339. }
  340. }
  341. },
  342. //设置排行榜上面的数据
  343. setUserData: function (dataKey, dataValue) {
  344. if (cc.sys.platform === cc.sys.WECHAT_GAME) {
  345. wx.setUserCloudStorage({
  346. KVDataList: [
  347. { key: String(dataKey), value: String(dataValue) },
  348. ],
  349. success: res => {
  350. // console.log('成功setUserCloudStorage',res);
  351. },
  352. fail: res => {
  353. // console.log('失败setUserCloudStorage',res);
  354. }
  355. });
  356. }
  357. },
  358. getAllBuild: function (callback) {
  359. var arr = [];
  360. var countAssetValue = 0;
  361. // var aaa = {
  362. // "0":"a",
  363. // "1":"b",
  364. // "2":"c",
  365. // "aa":"d",
  366. // "4":"e"
  367. // };
  368. //
  369. // console.log("测试",aaa .hasOwnProperty('5'));
  370. for (var i = 0; i < GlobalD.game.buildingsTiledMapUnit.length; i++) {
  371. var building = GlobalD.game.buildingsTiledMapUnit[i];
  372. // if (building.getBuildingsInfo.buildInfo.buildType == 5) {
  373. arr.push({
  374. name: building.getBuildingsInfo.buildInfo.buildingName,
  375. AssetValue: building.getBuildingsInfo.buildInfo.AssetValue,
  376. });
  377. countAssetValue += building.getBuildingsInfo.buildInfo.AssetValue;
  378. // }
  379. }
  380. var postdata = {};
  381. for (var i = 0; i < arr.length; i++) {
  382. if (postdata.hasOwnProperty(arr[i].name) == false) {
  383. postdata[arr[i].name] = arr[i];
  384. } else {
  385. postdata[arr[i].name].AssetValue += arr[i].AssetValue;
  386. }
  387. }
  388. var postdataarr = [];
  389. for (var prop in postdata) {
  390. if (postdata.hasOwnProperty(prop)) {
  391. postdataarr.push(postdata[prop])
  392. }
  393. }
  394. // console.log("要发送的数据是撒打算打算的", countAssetValue,GlobalD.GameData.Golden);
  395. countAssetValue += parseInt(GlobalD.GameData.Golden);
  396. countAssetValue += parseInt(GlobalD.GameData.Diamond);
  397. // console.log("商店建筑数据啊实打实的",countAssetValue);
  398. UtilsWX.setUserData("score", countAssetValue);
  399. // console.log("商店建筑数据",arr);
  400. var data = {
  401. name: "Whoareyou",
  402. openId: userData.openId,
  403. id: countAssetValue
  404. }
  405. UtilsWX.postMessage(data);
  406. if (callback != null) {
  407. callback();
  408. }
  409. }
  410. }