UtilsWX.js 14 KB

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