FriendList.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. const FriendSystemApi = require('../net/FriendSystemApi');
  2. const ListViewAdapter = require('ListViewAdapter');
  3. const GameModule = require("../utils/GameModule");
  4. cc.Class({
  5. extends: cc.Component,
  6. properties: {
  7. item: cc.Prefab,
  8. itemScriptName: '',
  9. scrollView: cc.ScrollView,
  10. content: cc.Node,
  11. tipLabel: cc.Label,
  12. // 0是好友列表, 1是推荐好友
  13. tab: 0,
  14. items: [],
  15. /**
  16. * 发送添加好友之后的Toast提示
  17. */
  18. addFriendApplySended: cc.Node,
  19. requestDuration: 10,
  20. friendLastRequestTime: -1,
  21. recommentRequestTime: -1,
  22. dataList: [],
  23. canLoad: true,
  24. },
  25. // LIFE-CYCLE CALLBACKS:
  26. onLoad() {
  27. if (this.tab === 0) {
  28. GameEvent.on('refresh_friend_list', this, () => {
  29. this.timeline = 0;
  30. this.getNetworkData(true);
  31. });
  32. GameEvent.on("friend_relation_change", this, () => {
  33. this.timeline = 0;
  34. this.getNetworkData(true);
  35. });
  36. // 尝试触发抢夺好友引导
  37. let friends = GameModule.tab.getComponent('Tab').friends
  38. GameModule.homeGuide.getComponent('HomeGuide').handleGuideState14(friends)
  39. }
  40. this.listAdapter = new ListViewAdapter(this.scrollView);
  41. },
  42. onEnable() {
  43. this.timeline = 0;
  44. this.hasNext = 0;
  45. this.getNetworkData(false);
  46. this.canLoad = true;
  47. },
  48. getNetworkData(ignoneRequestDuration) {
  49. if (this.canLoad) {
  50. console.log('canload');
  51. this.canLoad = false;
  52. let self = this;
  53. if (this.tab === 0) {
  54. let offset = (Date.parse(new Date()) - this.friendLastRequestTime) / 1000;
  55. if (ignoneRequestDuration || this.friendLastRequestTime === -1 || offset > this.requestDuration) {
  56. this.friendLastRequestTime = Date.parse(new Date());
  57. FriendSystemApi.getFriend(this.timeline, 20, this.configData.bind(self), this.networkError.bind(self));
  58. }
  59. } else {
  60. let offset = (Date.parse(new Date()) - this.recommentRequestTime) / 1000;
  61. if (ignoneRequestDuration || this.recommentRequestTime === -1 || offset > this.requestDuration) {
  62. this.recommentRequestTime = Date.parse(new Date());
  63. FriendSystemApi.getFriendRecommend(this.timeline, 20, this.configData.bind(self), this.networkError.bind(self));
  64. }
  65. }
  66. }
  67. },
  68. networkError(error) {
  69. console.log(error);
  70. this.tipLabel.node.active = true;
  71. this.tipLabel.string = '网络错误';
  72. this.content.active = false;
  73. this.canLoad = true;
  74. },
  75. configData(responseData) {
  76. console.log('configData');
  77. if (this.timeline === 0) {
  78. this.dataList = responseData.users;
  79. this.layout();
  80. } else {
  81. this.listAdapter.addData(responseData.users);
  82. }
  83. this.timeline = responseData.timeline;
  84. this.hasNext = responseData.next;
  85. this.canLoad = true;
  86. },
  87. layout() {
  88. console.log('layout');
  89. if (this.dataList === undefined || this.dataList === null || this.dataList.length === 0) {
  90. this.tipLabel.node.active = true;
  91. this.tipLabel.string = this.tab === 0 ? '暂时没有好友陪你玩这个游戏,赶紧去邀请好友一起来玩吧!' : '暂时没有好友推荐';
  92. this.content.active = false;
  93. } else {
  94. this.tipLabel.node.active = false;
  95. this.content.active = true;
  96. this.listAdapter.updateItems(this.dataList, this.item, this.itemScriptName);
  97. this.listAdapter.setLoadMoreCallBack(() => {
  98. this.loadMore();
  99. });
  100. }
  101. },
  102. loadMore() {
  103. if (this.hasNext > 0) {
  104. getNetworkData(true);
  105. }
  106. },
  107. // update(dt) {
  108. // if (this.listAdapter != undefined) {
  109. // this.listAdapter.update(dt);
  110. // }
  111. // },
  112. showToast() {
  113. this.addFriendApplySended.active = true;
  114. this.scheduleOnce(() => {
  115. this.addFriendApplySended.active = false;
  116. }, 2);
  117. },
  118. onDestroy() {
  119. if (this.tab === 0) {
  120. GameEvent.off('refresh_friend_list', this);
  121. GameEvent.off("friend_relation_change", this);
  122. }
  123. },
  124. onScrollEvent(){
  125. if (this.listAdapter != undefined) {
  126. this.listAdapter.update();
  127. }
  128. },
  129. });