UtilsWX.js 14 KB

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