GameFriendRank.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. const LoginApi = require('../net/LoginApi');
  2. cc.Class({
  3. extends: cc.Component,
  4. properties: {
  5. scrollView: cc.ScrollView,
  6. layout: cc.Layout,
  7. rankItem: cc.Prefab,
  8. meNode: cc.Node
  9. },
  10. // LIFE-CYCLE CALLBACKS:
  11. onLoad () {
  12. if (CC_QQPLAY) {
  13. this.scrollView.node.active = true;
  14. this.pageIndex = 0;
  15. this.listItem = [];
  16. for(let i = 0; i < 5; i++) {
  17. let item = cc.instantiate(this.rankItem);
  18. this.layout.node.addChild(item);
  19. item.active = false;
  20. this.listItem.push(item);
  21. }
  22. if (GameGlobal.winSize.height <= 1000) {
  23. this.scrollView.vertical = true;
  24. }
  25. }
  26. },
  27. start () {
  28. },
  29. onEnable() {
  30. if (CC_WECHATGAME) {
  31. window.wx.postMessage({
  32. messageType: 0
  33. });
  34. } else if (CC_QQPLAY) {
  35. }
  36. },
  37. init() {
  38. if (CC_QQPLAY) {
  39. var attr = "score";//使用哪一种上报数据做排行,可传入score,a1,a2等
  40. var order = 1; //排序的方法:[ 1: 从大到小(单局),2: 从小到大(单局),3: 由大到小(累积)]
  41. var rankType = 0; //要查询的排行榜类型,0: 好友排行榜
  42. let self = this;
  43. BK.QQ.getRankListWithoutRoom(attr, order, rankType, function (errCode, cmd, data) {
  44. // 返回错误码信息
  45. if (errCode !== 0) {
  46. BK.Script.log(1, 1, '获取排行榜数据失败!错误码:' + errCode);
  47. return;
  48. }
  49. // 解析数据
  50. if (data) {
  51. self.ranks = data.data.ranking_list;
  52. self._sortMeData();
  53. self._setupList();
  54. }
  55. });
  56. }
  57. },
  58. _setupList() {
  59. let start = this.pageIndex * 5;
  60. let end = start + 5;
  61. let sortArray = this.ranks.slice(start,end);
  62. this.listItem.forEach(n => {
  63. n.active = false;
  64. });
  65. for(let i = 0; i < sortArray.length; i++) {
  66. let item = this.listItem[i];
  67. item.active = true;
  68. let model = sortArray[i];
  69. model.rank = this.pageIndex * 5 + (i + 1);
  70. item.getComponent('GameRankItem').initQQData(model);
  71. }
  72. },
  73. _sortMeData() {
  74. for(let i = 0; i < this.ranks.length; i++) {
  75. let model = this.ranks[i];
  76. model.rank = i + 1;
  77. if (model.selfFlag) {
  78. this.myData = model;
  79. this._setupMeData();
  80. break;
  81. }
  82. }
  83. },
  84. _setupMeData() {
  85. LoginApi.getUpdateNickOrHead(this.myData.nick,this.myData.url, (res) => {
  86. }, (error) => {
  87. });
  88. this.meNode.active = true;
  89. this.meNode.getComponent('GameRankItem').initQQData(this.myData, true);
  90. },
  91. previousPage() {
  92. if (CC_WECHATGAME) {
  93. window.wx.postMessage({
  94. messageType: 1
  95. });
  96. } else if (CC_QQPLAY) {
  97. if (this.pageIndex <= 0 || this.ranks == undefined || this.ranks.length == 0) {
  98. return;
  99. }
  100. this.pageIndex -= 1;
  101. this._setupList();
  102. this.scrollView.scrollToTop();
  103. }
  104. },
  105. nextPage() {
  106. if (CC_WECHATGAME) {
  107. window.wx.postMessage({
  108. messageType: 2
  109. });
  110. } else if (CC_QQPLAY) {
  111. if (this.pageIndex >= (Math.ceil(this.ranks.length / 5) - 1) || this.ranks == undefined || this.ranks.length == 0) {
  112. return;
  113. }
  114. this.pageIndex += 1;
  115. this._setupList();
  116. this.scrollView.scrollToTop();
  117. }
  118. }
  119. // update (dt) {},
  120. });