TotalRank.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. // foo: {
  14. // // ATTRIBUTES:
  15. // default: null, // The default value will be used only when the component attaching
  16. // // to a node for the first time
  17. // type: cc.SpriteFrame, // optional, default is typeof default
  18. // serializable: true, // optional, default is true
  19. // },
  20. // bar: {
  21. // get () {
  22. // return this._bar;
  23. // },
  24. // set (value) {
  25. // this._bar = value;
  26. // }
  27. // },
  28. itemTemplate: { // item template to instantiate other items
  29. default: null,
  30. type: cc.Node
  31. },
  32. content:cc.Node,
  33. totalRankUrl:"https://api-touhu.duowan.com/user/getAllRank.do",
  34. timeline:"0",
  35. count:10,
  36. // item:[],
  37. // ver:100,
  38. // os:1,
  39. // channel:"wexin",
  40. // hasMore:true,
  41. spacing:0,
  42. current:0,
  43. userInfo:null,
  44. mine:null,
  45. },
  46. // LIFE-CYCLE CALLBACKS:
  47. onLoad () {
  48. },
  49. onEnable(){
  50. this.users = [];
  51. this.items = [];
  52. this.current = 0;
  53. this.timeline = "0";
  54. // this.hasMore = true;
  55. this.getTotalRank(Global.user);
  56. },
  57. onDisable(){
  58. },
  59. getTotalRank:function(userInfo){
  60. console.log(userInfo.token)
  61. console.log(userInfo.uid)
  62. this.userInfo = userInfo;
  63. if(wx!=undefined){
  64. // if(this.hasMore){
  65. wx.showLoading({
  66. mask:true,
  67. });
  68. wx.request({
  69. url:this.totalRankUrl,
  70. data:{
  71. token:userInfo.token,
  72. uid:userInfo.uid,
  73. channel:Global.channel,
  74. os:Global.os,
  75. ver:Global.ver,
  76. timeline:this.timeline,
  77. count:this.count
  78. },
  79. success:(response)=>{
  80. console.log(response);
  81. if(response.data.code==-5){
  82. wx.showToast({
  83. title:'登录过期'
  84. })
  85. Global.showLoginPage = true;
  86. cc.director.loadScene('MainMenu');
  87. }
  88. var data =response.data.data;
  89. if(data!=undefined){
  90. console.log(this.users)
  91. this.users = this.users.concat(data.list);
  92. this.mine = data.me;
  93. this.fillMine();
  94. this.notifyData();
  95. }else{
  96. wx.showToast({
  97. title:"网络错误",
  98. })
  99. }
  100. },
  101. fail:()=>{
  102. wx.showToast({
  103. title:"网络错误",
  104. })
  105. },
  106. complete:()=>{
  107. wx.hideLoading();
  108. }
  109. })
  110. // }
  111. }
  112. },
  113. fillMine(){
  114. var mineItem = this.itemTemplate.getComponent('Item');
  115. mineItem.updateItem(this.mine);
  116. if(this.mine.rank==0){
  117. mineItem.outSide();
  118. }
  119. },
  120. notifyData(){
  121. let start = this.current;
  122. let end = this.users.length-start>5?start+5:this.users.length;
  123. let users = this.users.slice(start,end);
  124. let offset = this.content.height/2-20;
  125. this.content.removeAllChildren();
  126. console.log(users);
  127. for (let i = 0; i < users.length; ++i) {
  128. let item = cc.instantiate(this.itemTemplate);
  129. this.content.addChild(item);
  130. item.getComponent('Item').isSelf = false;
  131. item.setPosition(0, offset-item.height * (0.5 + i) - this.spacing * (i + 1));
  132. let user = this.users[this.current+i];
  133. item.getComponent('Item').updateItem(user,i);
  134. }
  135. },
  136. previousPage(){
  137. if(this.current>0){
  138. let start = this.current - 5;
  139. this.current = start;
  140. this.notifyData();
  141. }else{
  142. wx.showToast({
  143. title:'已经是第一页了~'
  144. })
  145. }
  146. },
  147. nextPage(){
  148. let start = this.current + 5;
  149. if(start >= this.users.length){
  150. // if(this.hasMore){
  151. // this.current = start;
  152. // this.getTotalRank(Global.user);
  153. // }else{
  154. wx.showToast({
  155. title:'没有下一页了~'
  156. })
  157. // }
  158. }else{
  159. this.current = start;
  160. this.notifyData();
  161. }
  162. }
  163. });