WechatFriendRank.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. cc.Class({
  2. extends: cc.Component,
  3. properties: {
  4. layout: cc.Layout,
  5. rankItem: cc.Prefab,
  6. meNode: cc.Node
  7. },
  8. // LIFE-CYCLE CALLBACKS:
  9. onLoad () {
  10. this.listItem = [];
  11. for(let i = 0; i < 5; i++) {
  12. let item = cc.instantiate(this.rankItem);
  13. this.layout.node.addChild(item);
  14. item.active = false;
  15. this.listItem.push(item);
  16. }
  17. },
  18. start () {
  19. if (window.wx != undefined) {
  20. window.wx.onMessage(data => {
  21. if (data.messageType == 0) { //好友排行榜
  22. this.isGroup = false;
  23. this.loadFriendData();
  24. } else if (data.messageType == 1) { //好友排行榜上一页
  25. this.previousPage();
  26. } else if (data.messageType == 2) { //好友排行榜下一页
  27. this.nextPage();
  28. } else if (data.messageType == 3) { //群排行榜
  29. this.isGroup = true;
  30. this.loadGroupData(data.key1, data.key2);
  31. } else if (data.messageType == 4) { //排行榜关闭销毁
  32. this.clearData();
  33. }
  34. })
  35. }
  36. },
  37. //清除所有数据
  38. clearData() {
  39. this.isGroup = false;
  40. this.listItem.forEach(n => {
  41. n.active = false;
  42. });
  43. this.meNode.active = false;
  44. this.friendPageIndex = 0;
  45. this.groupPageIndex = 0;
  46. this.friendRanks = [];
  47. this.groupRanks = [];
  48. },
  49. loadFriendData() {
  50. this.friendPageIndex = 0;
  51. this.listItem.forEach(n => {
  52. n.active = false;
  53. });
  54. this.meNode.active = false;
  55. //ranks数组大于0证明已经加载过有数据不用再加载
  56. if (this.friendRanks && this.friendRanks.length > 0) {
  57. this._setupFriendList();
  58. this._setupFriendMyData();
  59. } else {
  60. if (window.wx != undefined) {
  61. let self = this;
  62. wx.getUserInfo({
  63. openIdList: ['selfOpenId'],
  64. success: (userRes) => {
  65. if (userRes.data.length > 0) {
  66. this.friendMyData = userRes.data[0];
  67. wx.getFriendCloudStorage({
  68. keyList: ['buildingLevel'],
  69. success: function (res) {
  70. self.configFriendRank(res);
  71. },
  72. fail: function (res) {
  73. console.error('error ' + res);
  74. }
  75. });
  76. }
  77. },
  78. fail: (res) => {
  79. console.error('user error ' + res);
  80. }
  81. });
  82. }
  83. }
  84. },
  85. loadGroupData(key1, key2) {
  86. this.groupPageIndex = 0;
  87. this.listItem.forEach(n => {
  88. n.active = false;
  89. });
  90. this.meNode.active = false;
  91. let isRefresh = key2;
  92. //ranks数组大于0证明已经加载过有数据不用再加载
  93. if (this.groupRanks && this.groupRanks.length > 0 && !isRefresh) {
  94. this._setupGroupList();
  95. this._setupGroupMyData();
  96. } else {
  97. if (window.wx != undefined) {
  98. let self = this;
  99. wx.getUserInfo({
  100. openIdList: ['selfOpenId'],
  101. success: (userRes) => {
  102. if (userRes.data.length > 0) {
  103. this.groupMyData = userRes.data[0];
  104. wx.getGroupCloudStorage({
  105. shareTicket: key1,
  106. keyList: ['buildingLevel'],
  107. success: function (res) {
  108. self.configGroupRank(res);
  109. },
  110. fail: function (res) {
  111. console.error('error ' + res);
  112. }
  113. });
  114. }
  115. },
  116. fail: (res) => {
  117. console.error('user error ' + res);
  118. }
  119. });
  120. }
  121. }
  122. },
  123. //好友排行榜
  124. _setupFriendMyData() {
  125. this.meNode.active = true;
  126. this.meNode.getComponent('GameRankItem').init(this.friendMyData, true);
  127. },
  128. configFriendRank(res) {
  129. let ranks = res.data;
  130. ranks.sort((a, b) => {
  131. if (a.KVDataList.length == 0 && b.KVDataList.length == 0) {
  132. return 0;
  133. }
  134. if (a.KVDataList.length == 0) {
  135. return 1;
  136. }
  137. if (b.KVDataList.length == 0) {
  138. return -1;
  139. }
  140. let b_score = JSON.parse(b.KVDataList[0].value).wxgame.score;
  141. let a_score = JSON.parse(a.KVDataList[0].value).wxgame.score;
  142. return b_score - a_score;
  143. });
  144. for (let i = 0; i < ranks.length; i++) {
  145. if (this.friendMyData.avatarUrl == ranks[i].avatarUrl) {
  146. this.friendMyData.rank = (i + 1);
  147. if (ranks[i].KVDataList && ranks[i].KVDataList.length > 0) {
  148. let value = JSON.parse(ranks[i].KVDataList[0].value);
  149. this.friendMyData.score = value.wxgame.score;
  150. } else {
  151. this.friendMyData.score = 1;
  152. }
  153. }
  154. }
  155. this.friendRanks = ranks;
  156. this._setupFriendList();
  157. this._setupFriendMyData();
  158. },
  159. _setupFriendList() {
  160. let start = this.friendPageIndex * 5;
  161. let end = start + 5;
  162. let sortArray = this.friendRanks.slice(start,end);
  163. this.listItem.forEach(n => {
  164. n.active = false;
  165. });
  166. for(let i = 0; i < sortArray.length; i++) {
  167. let item = this.listItem[i];
  168. let model = sortArray[i];
  169. model.rank = this.friendPageIndex * 5 + (i + 1);
  170. item.active = true;
  171. item.getComponent('GameRankItem').init(model);
  172. }
  173. },
  174. //群排行榜
  175. _setupGroupMyData() {
  176. this.meNode.active = true;
  177. this.meNode.getComponent('GameRankItem').init(this.groupMyData, true);
  178. },
  179. configGroupRank(res) {
  180. let ranks = res.data;
  181. ranks.sort((a, b) => {
  182. if (a.KVDataList.length == 0 && b.KVDataList.length == 0) {
  183. return 0;
  184. }
  185. if (a.KVDataList.length == 0) {
  186. return 1;
  187. }
  188. if (b.KVDataList.length == 0) {
  189. return -1;
  190. }
  191. let b_score = JSON.parse(b.KVDataList[0].value).wxgame.score;
  192. let a_score = JSON.parse(a.KVDataList[0].value).wxgame.score;
  193. return b_score - a_score;
  194. });
  195. for (let i = 0; i < ranks.length; i++) {
  196. if (this.groupMyData.avatarUrl == ranks[i].avatarUrl) {
  197. this.groupMyData.rank = (i + 1);
  198. if (ranks[i].KVDataList && ranks[i].KVDataList.length > 0) {
  199. let value = JSON.parse(ranks[i].KVDataList[0].value);
  200. this.groupMyData.score = value.wxgame.score;
  201. } else {
  202. this.groupMyData.score = 1;
  203. }
  204. }
  205. }
  206. this.groupRanks = ranks;
  207. this._setupGroupList();
  208. this._setupGroupMyData();
  209. },
  210. _setupGroupList() {
  211. let start = this.groupPageIndex * 5;
  212. let end = start + 5;
  213. let sortArray = this.groupRanks.slice(start,end);
  214. this.listItem.forEach(n => {
  215. n.active = false;
  216. });
  217. for(let i = 0; i < sortArray.length; i++) {
  218. let item = this.listItem[i];
  219. let model = sortArray[i];
  220. model.rank = this.groupPageIndex * 5 + (i + 1);
  221. item.active = true;
  222. item.getComponent('GameRankItem').init(model);
  223. }
  224. },
  225. previousPage() {
  226. if (this.isGroup) {
  227. if (this.groupPageIndex <= 0 || this.groupRanks == undefined || this.groupRanks.length == 0) {
  228. return;
  229. }
  230. this.groupPageIndex -= 1;
  231. this._setupGroupList();
  232. } else {
  233. if (this.friendPageIndex <= 0 || this.friendRanks == undefined || this.friendRanks.length == 0) {
  234. return;
  235. }
  236. this.friendPageIndex -= 1;
  237. this._setupFriendList();
  238. }
  239. },
  240. nextPage() {
  241. if (this.isGroup) {
  242. if (this.groupPageIndex >= (Math.ceil(this.groupRanks.length / 5) - 1) || this.groupRanks == undefined || this.groupRanks.length == 0) {
  243. return;
  244. }
  245. this.groupPageIndex += 1;
  246. this._setupGroupList();
  247. } else {
  248. if (this.friendPageIndex >= (Math.ceil(this.friendRanks.length / 5) - 1) || this.friendRanks == undefined || this.friendRanks.length == 0) {
  249. return;
  250. }
  251. this.friendPageIndex += 1;
  252. this._setupFriendList();
  253. }
  254. }
  255. // update (dt) {},
  256. });