WechatFriendRank.js 9.5 KB

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