Main.js 24 KB

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