Main.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. // Learn cc.Class:
  2. // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
  3. // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
  4. // Learn Attribute:
  5. // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
  6. // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
  7. // Learn life-cycle callbacks:
  8. // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
  9. // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html
  10. cc.Class({
  11. extends: cc.Component,
  12. properties: {
  13. rankScrollView: cc.Node,
  14. _viewitemTemp: null,
  15. _content: null,
  16. SignAtlas:{
  17. default:null,
  18. type:cc.SpriteAtlas,
  19. },
  20. },
  21. // LIFE-CYCLE CALLBACKS:
  22. onLoad() {
  23. this._viewlist = this.node.getChildByName("PHB").getChildByName("rankScrollView");
  24. this._content = this.rankScrollView.getChildByName("view").getChildByName("content");
  25. if (this._viewitemTemp == null || this._viewitemTemp == 'undefined') {
  26. this._viewitemTemp = this._content.children[0];
  27. this._content.removeChild(this._viewitemTemp);
  28. }
  29. this.showFriendRank();
  30. },
  31. start() {
  32. this.userhead = "";
  33. var self = this;
  34. this.GetUserHead();
  35. wx.onMessage(function (data) {
  36. switch (data.method) {
  37. case 'updateRank': {
  38. break;
  39. }
  40. case 'showMyRank': {
  41. self.node.getChildByName("PHB").active = false;
  42. self.node.getChildByName("MYPHB").active = true;
  43. self.showFriendRank();
  44. break;
  45. }
  46. case 'showGroupRank': {
  47. break;
  48. }
  49. case 'hideRank': {
  50. self.node.getChildByName("PHB").active = false;
  51. self.node.getChildByName("MYPHB").active = false;
  52. break;
  53. }
  54. case 'showFriendRank': {
  55. var show = data.data == "true";
  56. self.node.getChildByName("PHB").active = show;
  57. self.node.getChildByName("MYPHB").active = !show;
  58. self.showFriendRank();
  59. break;
  60. }
  61. }
  62. })
  63. },
  64. packHeadImg(data) {
  65. var headlist = data.split('/');
  66. var len = headlist.length;
  67. if (len === 0 || len === 1) {
  68. return data;
  69. }
  70. var headimg = "";
  71. for (var i = 0; i < len - 1; ++i) {
  72. headimg += headlist[i] + '/';
  73. }
  74. headimg += '132';
  75. return headimg;
  76. },
  77. createImage(sprite, url) {
  78. let image = wx.createImage();
  79. image.onload = function () {
  80. let texture = new cc.Texture2D();
  81. texture.initWithElement(image);
  82. texture.handleLoadedTexture();
  83. sprite.spriteFrame = new cc.SpriteFrame(texture);
  84. };
  85. image.src = url;
  86. },
  87. // bubbleSort(arr) {
  88. // for (var i = 1; i < arr.length; i++) {
  89. // for (var r = 0; r < arr.length - i; r++) {
  90. // if (parseInt(arr[r].score) < parseInt(arr[r + 1].score)) {
  91. // arr[r] = [arr[r + 1], arr[r + 1] = arr[r]][0];
  92. // }
  93. // }
  94. // }
  95. // },
  96. bubbleSort(arr) {
  97. var len = arr.length;
  98. for (var i = 0; i < len; i++) {
  99. for (var j = 0; j < len - 1 - i; j++) {
  100. if (parseInt(arr[j].score) < parseInt(arr[j + 1].score)) { //相邻元素两两对比
  101. var temp = arr[j + 1]; //元素交换
  102. arr[j + 1] = arr[j];
  103. arr[j] = temp;
  104. }
  105. }
  106. }
  107. return arr;
  108. },
  109. onClickRankItem: function (event) {
  110. //var strIndex = event.target.getChildByName("ranknum").getComponent(cc.Label).string;
  111. //this.node.getChildByName("PHB").getChildByName("IndexPos").getComponent(cc.Label).string = "名次:" + strIndex;
  112. //this.node.getChildByName("PHB").getChildByName("PlayScore").getComponent(cc.Label).string = event.target.getChildByName("score").getComponent(cc.Label).string;
  113. //this.node.getChildByName("PHB").getChildByName("headimg").getComponent(cc.Sprite).spriteFrame = event.target.getChildByName("headimg").getComponent(cc.Sprite).spriteFrame;
  114. },
  115. addRank(data) {
  116. var rankArr = new Array();
  117. for (var index = 0; index < data.length; ++index) {
  118. var openid = data[index].openid;
  119. var nickname = data[index].nickname;
  120. var headimg = this.packHeadImg(data[index].avatarUrl);
  121. var score = '0';
  122. var driLevel = '0';
  123. if (!this.IsEmpty(data[index].KVDataList)) {
  124. for (var num = 0; num < data[index].KVDataList.length; ++num) {
  125. if (data[index].KVDataList[num].key == 'driLevel') {
  126. driLevel = data[index].KVDataList[num].value;
  127. } else {
  128. score = data[index].KVDataList[num].value;
  129. }
  130. }
  131. if (data[index].KVDataList.length == 2) {
  132. rankArr.push({ openid: openid, nickname: nickname, headimg: headimg, score: score, driLevel: driLevel });
  133. }
  134. }
  135. // if (!this.IsEmpty(data[index].KVDataList)) {
  136. // score = data[index].KVDataList[0].value;
  137. // rankArr.push({ openid: openid, nickname: nickname, headimg: headimg, score: score });
  138. // }
  139. }
  140. this.bubbleSort(rankArr);
  141. //this.node.getChildByName("PHB").active = true;
  142. this._content.removeAllChildren();
  143. //for (var n = 0; n < 5; ++n) {
  144. for (var index = 0; index < rankArr.length; ++index) {
  145. var node = cc.instantiate(this._viewitemTemp);
  146. node.getChildByName('ranknum').getComponent(cc.Label).string = index + 1;
  147. if (index < 3) {
  148. node.getChildByName('ranknunbg' + index).active = true;
  149. }
  150. node.getChildByName('username').getComponent(cc.Label).string = rankArr[index].nickname;
  151. this.createImage(node.getChildByName('headimg').getComponent(cc.Sprite), rankArr[index].headimg);
  152. node.getChildByName('score').getComponent(cc.Label).string = rankArr[index].score;
  153. node.getChildByName('CheckMark').active = ((index % 2) != 0);
  154. this._content.addChild(node);
  155. node.getChildByName("levelSgin").active = false;
  156. var level = parseInt(rankArr[index].driLevel);
  157. if (level != 'NAN' && level > 0) {
  158. node.getChildByName("levelSgin").active = true;
  159. node.getChildByName("levelSgin").getComponent(cc.Sprite).spriteFrame = this.SignAtlas.getSpriteFrame('c'+level);
  160. }else{
  161. node.getChildByName("levelSgin").active = false;
  162. }
  163. // if (index == 0) {
  164. // node.getComponent(cc.Toggle).check();
  165. // var strIndex = node.getChildByName("ranknum").getComponent(cc.Label).string;
  166. // this.node.getChildByName("PHB").getChildByName("IndexPos").getComponent(cc.Label).string = "名次:" + strIndex;
  167. // this.node.getChildByName("PHB").getChildByName("PlayScore").getComponent(cc.Label).string = node.getChildByName("score").getComponent(cc.Label).string;
  168. // this.createImage(this.node.getChildByName("PHB").getChildByName("headimg").getComponent(cc.Sprite), rankArr[index].headimg);
  169. // }
  170. if (this.isMySelf(rankArr[index].headimg)) {
  171. this.node.getChildByName("PHB").getChildByName("itemMy").getChildByName('ranknum').active = true;
  172. this.node.getChildByName("PHB").getChildByName("itemMy").getChildByName('headimg').active = true;
  173. this.node.getChildByName("PHB").getChildByName("itemMy").getChildByName('score').active = true;
  174. this.node.getChildByName("PHB").getChildByName("itemMy").getChildByName('username').active = true;
  175. this.node.getChildByName("PHB").getChildByName("itemMy").getChildByName('headSprite').active = true;
  176. this.node.getChildByName("PHB").getChildByName("itemMy").getChildByName('ranknum').getComponent(cc.Label).string = index + 1;
  177. this.node.getChildByName("PHB").getChildByName("itemMy").getChildByName('username').getComponent(cc.Label).string = rankArr[index].nickname;
  178. this.createImage(this.node.getChildByName("PHB").getChildByName("itemMy").getChildByName('headimg').getComponent(cc.Sprite), rankArr[index].headimg);
  179. this.node.getChildByName("PHB").getChildByName("itemMy").getChildByName('score').getComponent(cc.Label).string = rankArr[index].score;
  180. var level = parseInt(rankArr[index].driLevel);
  181. if (level != 'NAN' && level > 0) {
  182. this.node.getChildByName("PHB").getChildByName("itemMy").getChildByName("levelSgin").active = true;
  183. this.node.getChildByName("PHB").getChildByName("itemMy").getChildByName("levelSgin").getComponent(cc.Sprite).spriteFrame = this.SignAtlas.getSpriteFrame('c'+level);
  184. }else{
  185. this.node.getChildByName("PHB").getChildByName("itemMy").getChildByName("levelSgin").active = false;
  186. }
  187. if (rankArr.length > 3) {
  188. if (index == 0) {//第一
  189. this.showInfoNode(this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("InfoNode0"), index, rankArr[index]);
  190. if (index+1 < rankArr.length) {
  191. this.showInfoNode(this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("InfoNode1"), index+1, rankArr[index+1]);
  192. }
  193. if (index+2 < rankArr.length) {
  194. this.showInfoNode(this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("InfoNode2"), index+2, rankArr[index+2]);
  195. }
  196. }else if (index == rankArr.length - 1) {
  197. this.showInfoNode(this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("InfoNode0"), index-2, rankArr[index-2]);
  198. this.showInfoNode(this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("InfoNode1"), index-1, rankArr[index-1]);
  199. this.showInfoNode(this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("InfoNode2"), index, rankArr[index]);
  200. }
  201. else {
  202. this.showInfoNode(this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("InfoNode0"), index-1, rankArr[index-1]);
  203. this.showInfoNode(this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("InfoNode1"), index, rankArr[index]);
  204. this.showInfoNode(this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("InfoNode2"), index+1, rankArr[index+1]);
  205. }
  206. }else {
  207. this.showInfoNode(this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("InfoNode0"), 0, rankArr[0]);
  208. if (1 < rankArr.length)
  209. this.showInfoNode(this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("InfoNode1"), 1, rankArr[1]);
  210. if (2 < rankArr.length)
  211. this.showInfoNode(this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("InfoNode2"), 2, rankArr[2]);
  212. }
  213. /*
  214. this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("myNode").getChildByName("ranknum").getComponent(cc.Label).string = index + 1;
  215. this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("myNode").getChildByName("username").getComponent(cc.Label).string = rankArr[index].nickname;
  216. this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("myNode").getChildByName("score").getComponent(cc.Label).string = "#" + rankArr[index].score;
  217. this.createImage(this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("myNode").getChildByName("headimg").getComponent(cc.Sprite), rankArr[index].headimg);
  218. this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("myNode").active = true;
  219. level = parseInt(rankArr[index].driLevel);
  220. if (level != 'NAN' && level > 0) {
  221. this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("myNode").getChildByName("levelSgin").active = true;
  222. this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("myNode").getChildByName("levelSgin").getComponent(cc.Sprite).spriteFrame = this.SignAtlas.getSpriteFrame('c'+level);
  223. }else{
  224. this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("myNode").getChildByName("levelSgin").active = false;
  225. }
  226. */
  227. /*
  228. this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("leftNode").active = false;
  229. if (index > 0) {
  230. var strIndex = node.getChildByName("ranknum").getComponent(cc.Label).string;
  231. this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("leftNode").getChildByName("ranknum").getComponent(cc.Label).string = index;
  232. this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("leftNode").getChildByName("username").getComponent(cc.Label).string = rankArr[index - 1].nickname;
  233. this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("leftNode").getChildByName("score").getComponent(cc.Label).string = "#" + rankArr[index - 1].score;
  234. this.createImage(this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("leftNode").getChildByName("headimg").getComponent(cc.Sprite), rankArr[index - 1].headimg);
  235. this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("leftNode").active = true;
  236. var level = parseInt(rankArr[index - 1].driLevel);
  237. if (level != 'NAN' && level > 0) {
  238. this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("leftNode").getChildByName("levelSgin").active = true;
  239. this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("leftNode").getChildByName("levelSgin").getComponent(cc.Sprite).spriteFrame = this.SignAtlas.getSpriteFrame('c'+level);
  240. }
  241. else{
  242. this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("leftNode").getChildByName("levelSgin").active = false;
  243. }
  244. }
  245. */
  246. /*
  247. this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("rightNode").active = false;
  248. if (index < rankArr.length - 1) {
  249. var strIndex = node.getChildByName("ranknum").getComponent(cc.Label).string;
  250. this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("rightNode").getChildByName("ranknum").getComponent(cc.Label).string = index + 2;
  251. this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("rightNode").getChildByName("username").getComponent(cc.Label).string = rankArr[index + 1].nickname;
  252. this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("rightNode").getChildByName("score").getComponent(cc.Label).string = "#" + rankArr[index + 1].score;
  253. this.createImage(this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("rightNode").getChildByName("headimg").getComponent(cc.Sprite), rankArr[index + 1].headimg);
  254. this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("rightNode").active = true;
  255. var level = parseInt(rankArr[index + 1].driLevel);
  256. if (level != 'NAN' && level > 0) {
  257. this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("rightNode").getChildByName("levelSgin").active = true;
  258. this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("rightNode").getChildByName("levelSgin").getComponent(cc.Sprite).spriteFrame = this.SignAtlas.getSpriteFrame('c'+level);
  259. }
  260. else{
  261. this.node.getChildByName("MYPHB").getChildByName("rootNode").getChildByName("rightNode").getChildByName("levelSgin").active = false;
  262. }
  263. }*/
  264. }
  265. }
  266. //}
  267. rankArr = null;
  268. },
  269. showInfoNode:function (node, index, data) {
  270. //var strIndex = node.getChildByName("ranknum").getComponent(cc.Label).string;
  271. node.getChildByName("ranknum").getComponent(cc.Label).string = index+1;
  272. node.getChildByName("username").getComponent(cc.Label).string = data.nickname;
  273. node.getChildByName("score").getComponent(cc.Label).string = "#" + data.score;
  274. this.createImage(node.getChildByName("headimg").getComponent(cc.Sprite), data.headimg);
  275. node.active = true;
  276. node.getChildByName('ranknunbg0').active = false;
  277. node.getChildByName('ranknunbg1').active = false;
  278. node.getChildByName('ranknunbg2').active = false;
  279. if (index < 3) {
  280. node.getChildByName('ranknunbg' + index).active = true;
  281. }
  282. var level = parseInt(data.driLevel);
  283. if (level != 'NAN' && level > 0) {
  284. node.getChildByName("levelSgin").active = true;
  285. node.getChildByName("levelSgin").getComponent(cc.Sprite).spriteFrame = this.SignAtlas.getSpriteFrame('c'+level);
  286. }
  287. else{
  288. node.getChildByName("levelSgin").active = false;
  289. }
  290. },
  291. showFriendRank() {
  292. var self = this;
  293. var scorekey = self.getCurWeekScoreValue();
  294. wx.getFriendCloudStorage({
  295. keyList: [scorekey, 'driLevel'],
  296. success: function (res) {
  297. // 转发成功
  298. self.addRank(res.data);
  299. // console.error(res);
  300. },
  301. fail: function (res) {
  302. // 转发失败
  303. // console.error(res);
  304. },
  305. complete: function (res) {
  306. // console.error(res);
  307. }
  308. })
  309. },
  310. isMySelf: function (strAvt) {
  311. return this.userhead == strAvt;
  312. },
  313. GetUserHead() {
  314. var self = this;
  315. wx.getUserInfo({
  316. openIdList: ['selfOpenId'],
  317. success: function (res) {
  318. self.userhead = self.packHeadImg(res.data[0].avatarUrl);
  319. }
  320. })
  321. },
  322. _hide() {
  323. // let moveTo = cc.moveTo(0.5, 0, 1000);
  324. // this.display.runAction(moveTo);
  325. },
  326. getCurWeekScoreValue() {
  327. return this.getNowFormatDate();
  328. },
  329. getNowFormatDate() {
  330. var date = new Date();
  331. var seperator1 = "-";
  332. var year = date.getFullYear();
  333. // var month = date.getMonth() + 1;
  334. // if (month >= 1 && month <= 9) {
  335. // month = "0" + month;
  336. // }
  337. var week = this.Getweek();
  338. var currentdate = year + seperator1 + week;
  339. return currentdate;
  340. },
  341. getMonthWeek() {
  342. var date = new Date(),
  343. w = date.getDay(),
  344. d = date.getDate();
  345. return Math.ceil(
  346. (d + 7 - w) / 7
  347. );
  348. },
  349. Getweek(dateString) {
  350. var da = '';
  351. if (dateString == undefined) {
  352. var now = new Date();
  353. var now_m = now.getMonth() + 1;
  354. now_m = (now_m < 10) ? '0' + now_m : now_m;
  355. var now_d = now.getDate();
  356. now_d = (now_d < 10) ? '0' + now_d : now_d;
  357. da = now.getFullYear() + '-' + now_m + '-' + now_d;
  358. } else {
  359. da = dateString; //日期格式2015-12-30
  360. }
  361. var date1 = new Date(da.substring(0, 4), parseInt(da.substring(5, 7)) - 1, da.substring(8, 10)); //当前日期
  362. var date2 = new Date(da.substring(0, 4), 0, 1); //1月1号
  363. //获取1月1号星期(以周一为第一天,0周一~6周日)
  364. var dateWeekNum = date2.getDay() - 1;
  365. if (dateWeekNum < 0) {
  366. dateWeekNum = 6;
  367. }
  368. if (dateWeekNum < 4) {
  369. //前移日期
  370. date2.setDate(date2.getDate() - dateWeekNum);
  371. } else {
  372. //后移日期
  373. date2.setDate(date2.getDate() + 7 - dateWeekNum);
  374. }
  375. var d = Math.round((date1.valueOf() - date2.valueOf()) / 86400000);
  376. var year = date1.getFullYear();
  377. var week = Math.ceil((d + 1) / 7);
  378. return week;
  379. },
  380. IsEmpty(obj) {//判断是否为空
  381. if (typeof obj == "undefined" || obj == null || obj == "") {
  382. return true;
  383. } else {
  384. return false;
  385. }
  386. },
  387. // update (dt) {},
  388. });