Main.js 23 KB

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