PlatformController.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. var PlatformController = cc.Class({
  2. statics:{
  3. wxSubContextViewLock:0,
  4. //微信 wx
  5. //头条 tt
  6. //百度 baidu
  7. platform:"wx",
  8. nickName:"校长王",
  9. Init:function () {
  10. if(!window.wx && !window.tt && !window.swan)
  11. this.platform="pc";
  12. this.InitRandNickName();
  13. this.CreateGameClub();
  14. },
  15. //百度 头条等先判定是否登陆了
  16. IsLoginSync:function(){
  17. if(this.platform == "baidu")
  18. {
  19. var result = swan.isLoginSync();
  20. if(result.isLogin)
  21. {
  22. console.log("已经有登陆了");
  23. return true;
  24. }
  25. return false;
  26. }
  27. else if(this.platform == "tt")
  28. {
  29. }
  30. return true;
  31. },
  32. Login:function(){
  33. var self = this;
  34. if(this.platform == "baidu")
  35. {
  36. swan.login({
  37. success: function () {
  38. self.setUserCloudStorage(cc.Mgr.UserDataMgr.HistoryHighAssets);
  39. },
  40. fail: function () {
  41. swan.showModal({
  42. title: "登录失败",
  43. content: "是否重新登录?",
  44. cancelText: "退出游戏",
  45. success: function (res) {
  46. if (res.confirm) {
  47. console.log("点击了确定");
  48. self.Login();
  49. }
  50. else if (res.cancel) {
  51. console.log("点击了取消");
  52. swan.exit();
  53. }
  54. }
  55. })
  56. }
  57. })
  58. }
  59. else if(this.platform == "tt")
  60. {
  61. }
  62. return true;
  63. },
  64. //游戏圈按钮
  65. CreateGameClub:function(){
  66. if(this.platform == "wx")
  67. {
  68. var winSize = cc.view.getVisibleSize();
  69. var leftRatio = 68 / winSize.width;
  70. var topRatio = 275 / winSize.height;
  71. var sysInfo = wx.getSystemInfoSync();
  72. this.leftPos = sysInfo.windowWidth * leftRatio;
  73. this.topPos = sysInfo.windowHeight * topRatio;
  74. console.log(this.topPos + " ================创建游戏圈按钮===============" + this.leftPos);
  75. var self = this;
  76. this.gameClubBtn = wx.createGameClubButton({
  77. icon:'green',
  78. text:"游戏圈",
  79. style:{
  80. left:self.leftPos - 20,
  81. top:self.topPos - 20,
  82. width: 45,
  83. height: 45,
  84. }
  85. });
  86. }
  87. },
  88. ShowClubButton:function(flag = false)
  89. {
  90. if(this.platform == "wx" && this.gameClubBtn != null)
  91. {
  92. if(flag == true)
  93. this.gameClubBtn.show();
  94. else
  95. this.gameClubBtn.hide();
  96. }
  97. },
  98. //随机一个名字
  99. InitRandNickName:function(){
  100. var seed = Math.floor(Math.random()*24) + 1;
  101. this.nickName = cc.Mgr.global.getTranslation("Name"+ seed);
  102. cc.log("================================ " + this.nickName)
  103. },
  104. //右上角的转发按钮
  105. ShareTopNav:function(){
  106. var index = Math.floor(Math.random()*6);
  107. let info = cc.Mgr.ShareInfos.getShareInfos(index);
  108. if(this.platform == "wx")
  109. {
  110. wx.showShareMenu({withShareTicket:true});
  111. wx.onShareAppMessage(function () {
  112. // 用户点击了“转发”按钮
  113. return {
  114. title: "【有人@你】我在这个游戏里面挣了一个亿",
  115. imageUrlId: 'dwMg-qBPS8S7MIWk4VRHxw',
  116. imageUrl: "https://paopao.talkyun.com.cn/yiyi/1.jpg",
  117. }
  118. })
  119. }
  120. else if(this.platform == "tt")
  121. {
  122. tt.showShareMenu(false);
  123. tt.onShareAppMessage(function () {
  124. // 用户点击了“转发”按钮
  125. return {
  126. title: info.text,
  127. //imageUrlId: 'Ik14RZj7SV2BtigrtE3d1g',
  128. imageUrl: info.url,
  129. }
  130. })
  131. }
  132. else if(this.platform == "baidu")
  133. {
  134. swan.showShareMenu(false);
  135. swan.onShareAppMessage(function () {
  136. // 用户点击了“转发”按钮
  137. return {
  138. title: info.text,
  139. imageUrl: info.url
  140. }
  141. })
  142. }
  143. },
  144. //转发一段文本
  145. ShareToFriendTxt:function(str){
  146. if(this.platform == "wx")
  147. {
  148. console.log("点击了分享啊");
  149. wx.shareAppMessage({
  150. title: str,
  151. })
  152. }
  153. else if(this.platform == "tt")
  154. {
  155. tt.shareAppMessage({
  156. title: str,
  157. })
  158. }
  159. else if(this.platform == "baidu")
  160. {
  161. console.log("点击了分享啊");
  162. swan.shareAppMessage({
  163. title: str,
  164. })
  165. }
  166. },
  167. //自定义转发
  168. ShareToFriend:function(index){
  169. var info = cc.Mgr.ShareInfos.getShareInfos(index);
  170. if(this.platform == "wx")
  171. {
  172. console.log("点击了分享啊");
  173. wx.shareAppMessage({
  174. title: info.text,
  175. imageUrl: info.url,
  176. })
  177. }
  178. else if(this.platform == "tt")
  179. {
  180. tt.shareAppMessage({
  181. title: info.text,
  182. imageUrl: info.url
  183. })
  184. }
  185. else if(this.platform == "baidu")
  186. {
  187. console.log("点击了分享啊");
  188. swan.shareAppMessage({
  189. title: info.text,
  190. imageUrl: info.url
  191. })
  192. }
  193. },
  194. //显示平台的小弹窗 回调用
  195. showToast:function(text)
  196. {
  197. cc.log(text);
  198. if(this.platform == "wx")
  199. {
  200. wx.showToast({
  201. title: text,
  202. icon: 'success',
  203. duration: 2000
  204. })
  205. }
  206. else if(this.platform == "tt")
  207. {
  208. tt.showToast({
  209. title: text,
  210. icon: 'success',
  211. duration: 2000
  212. })
  213. }
  214. else if(this.platform == "baidu")
  215. {
  216. swan.showToast({
  217. title: text,
  218. icon: 'success',
  219. duration: 2000
  220. })
  221. }
  222. },
  223. //微信开放数据存储 score 代表当前要保存的东西
  224. setUserCloudStorage:function(socre)
  225. {
  226. if(!this.IsLoginSync())
  227. {
  228. return;
  229. }
  230. cc.log("setUserCloudStorage socre = " + socre);
  231. if(this.platform == "wx")
  232. {
  233. var kvData={};
  234. kvData.wxgame={};
  235. kvData.wxgame.score = socre;
  236. kvData.wxgame.update_time = new Date().getTime();
  237. console.log(JSON.stringify(kvData));
  238. var kvDataList = new Array();
  239. kvDataList.push({key: "xmbScore", value: JSON.stringify(kvData)});
  240. wx.setUserCloudStorage({
  241. KVDataList: kvDataList,
  242. success: res => {
  243. console.log("success:" + JSON.stringify(res))
  244. },
  245. fail: res => {
  246. console.log("fail : " + res);
  247. }
  248. });
  249. }
  250. else if(this.platform == "baidu")
  251. {
  252. var kvData={};
  253. kvData.wxgame={};
  254. kvData.wxgame.score = socre;
  255. kvData.wxgame.update_time = new Date().getTime();
  256. console.log(JSON.stringify(kvData));
  257. var kvDataList = new Array();
  258. kvDataList.push({key: "xmbScore", value: JSON.stringify(kvData)});
  259. swan.setUserCloudStorage({
  260. KVDataList: kvDataList,
  261. success: res => console.log("success "+res),
  262. fail: res => console.log("fail "+res),
  263. complete: res => console.log("complete "+res)
  264. });
  265. }
  266. },
  267. //是否支持排行功能
  268. IsSupportRank:function()
  269. {
  270. if(this.platform == "wx" || this.platform == "baidu")
  271. return true;
  272. return false;
  273. },
  274. //显示子域
  275. showSubContentView:function()
  276. {
  277. if(this.platform == "wx" || this.platform == "baidu" )
  278. {
  279. var param = {};
  280. param.platform = this.platform;
  281. param.flag = true;
  282. cc.director.GlobalEvent.emit(cc.Mgr.Event.ShowRank, param);
  283. }
  284. },
  285. hideSubContentView:function()
  286. {
  287. if(this.platform == "wx" || this.platform == "baidu" )
  288. {
  289. var param = {};
  290. param.flag = false;
  291. param.platform = this.platform;
  292. cc.director.GlobalEvent.emit(cc.Mgr.Event.ShowRank, param);
  293. }
  294. },
  295. SendMessageToSubView:function(code,curScore=0)
  296. {
  297. var msg={};
  298. msg.code = code;
  299. msg.curScore = curScore;
  300. if(this.platform == "wx")
  301. {
  302. wx.getOpenDataContext().postMessage({
  303. message: msg
  304. });
  305. }
  306. else if(this.platform == "baidu")
  307. {
  308. swan.getOpenDataContext().postMessage({
  309. message: msg
  310. });
  311. }
  312. },
  313. //整理数据
  314. CollateData:function(data)
  315. {
  316. this.userInfo = [];
  317. var j = 0;
  318. for (let i = 0; i < data.length; ++i) {
  319. if(data[i].KVDataList.length == 0)
  320. {
  321. console.log("combData 没有此项数据");
  322. continue;
  323. }
  324. //{"openid":"o0OhZ5Kn2xO9b5FVR8TE0YXOl3Wc","nickname":"杨彬","avatarUrl":"https://wx.qlogo.cn/mmopen/vi_32/FcRibGPkicSKhn6LA1OWfL7ictiaKiajzibzW8zwQibkGKdCJmmiasvibcRasI2KytcVtW8ClH5IQTsOs3CB8vyMSUAOarA/132","KVDataList":[{"key":"\belsfkScore","value":"{\"wxgame\":{\"score\":1889,\"update_time\":1553824369920}}"}]}
  325. this.userInfo[j]={};
  326. this.userInfo[j].openid = data[i].openid;
  327. this.userInfo[j].nickname = data[i].nickname;
  328. this.userInfo[j].avatarUrl = data[i].avatarUrl;
  329. var kVDataListValuestr = data[i].KVDataList[0].value;
  330. kVDataListValuestr = kVDataListValuestr.substr(1); //删除第一个字符
  331. kVDataListValuestr = kVDataListValuestr.substr(0, kVDataListValuestr.length-1);
  332. kVDataListValuestr = "{" + kVDataListValuestr + "}";
  333. var gameScore = JSON.parse(kVDataListValuestr).wxgame;
  334. this.userInfo[j].score = gameScore.score;
  335. j++;
  336. }
  337. //给用户数据进行排序
  338. this.userInfo.sort((a,b)=>{
  339. if(a.score<b.score)
  340. return 1;
  341. else if(a.score>b.score)
  342. return -1
  343. else
  344. return 0;
  345. }
  346. );
  347. console.log("CollateData--- " + JSON.stringify(this.userInfo));
  348. for(var i=0 ;i<this.userInfo.length;i++)
  349. { //挑出自己的排名
  350. if(this.userInfo[i].avatarUrl == this.avatarUrl)
  351. {
  352. this.mySordId = i;
  353. }
  354. }
  355. },
  356. },
  357. });
  358. module.exports = PlatformController;