WechatFriendRank.js 10 KB

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