FriendList.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. this.scrollView.scrollToTop(0);
  48. },
  49. getNetworkData(ignoneRequestDuration) {
  50. if (this.canLoad) {
  51. console.log('canload');
  52. this.canLoad = false;
  53. let self = this;
  54. if (this.tab === 0) {
  55. let offset = (Date.parse(new Date()) - this.friendLastRequestTime) / 1000;
  56. if (ignoneRequestDuration || this.friendLastRequestTime === -1 || offset > this.requestDuration) {
  57. this.friendLastRequestTime = Date.parse(new Date());
  58. FriendSystemApi.getFriend(this.timeline, 20, this.configData.bind(self), this.networkError.bind(self));
  59. }
  60. } else {
  61. let offset = (Date.parse(new Date()) - this.recommentRequestTime) / 1000;
  62. if (ignoneRequestDuration || this.recommentRequestTime === -1 || offset > this.requestDuration) {
  63. this.recommentRequestTime = Date.parse(new Date());
  64. FriendSystemApi.getFriendRecommend(this.timeline, 20, this.configData.bind(self), this.networkError.bind(self));
  65. }
  66. }
  67. }
  68. },
  69. networkError(error) {
  70. console.log(error);
  71. this.tipLabel.node.active = true;
  72. this.tipLabel.string = '网络错误';
  73. this.content.active = false;
  74. this.canLoad = true;
  75. },
  76. configData(responseData) {
  77. console.log('configData');
  78. if (this.timeline === 0) {
  79. this.dataList = responseData.users;
  80. this.layout();
  81. } else {
  82. this.listAdapter.addData(responseData.users);
  83. }
  84. this.timeline = responseData.timeline;
  85. this.hasNext = responseData.next;
  86. this.canLoad = true;
  87. },
  88. layout() {
  89. console.log('layout');
  90. if (this.dataList === undefined || this.dataList === null || this.dataList.length === 0) {
  91. this.tipLabel.node.active = true;
  92. this.tipLabel.string = this.tab === 0 ? '暂时没有好友陪你玩这个游戏,赶紧去邀请好友一起来玩吧!' : '暂时没有好友推荐';
  93. this.content.active = false;
  94. } else {
  95. this.tipLabel.node.active = false;
  96. this.content.active = true;
  97. this.listAdapter.updateItems(this.dataList, this.item, this.itemScriptName);
  98. this.listAdapter.setLoadMoreCallBack(() => {
  99. this.loadMore();
  100. });
  101. }
  102. },
  103. loadMore() {
  104. if (this.hasNext > 0) {
  105. getNetworkData(true);
  106. }
  107. },
  108. // update(dt) {
  109. // if (this.listAdapter != undefined) {
  110. // this.listAdapter.update(dt);
  111. // }
  112. // },
  113. showToast() {
  114. this.addFriendApplySended.active = true;
  115. this.scheduleOnce(() => {
  116. this.addFriendApplySended.active = false;
  117. }, 2);
  118. },
  119. onDestroy() {
  120. if (this.tab === 0) {
  121. GameEvent.off('refresh_friend_list', this);
  122. GameEvent.off("friend_relation_change", this);
  123. }
  124. },
  125. onScrollEvent(){
  126. if (this.listAdapter != undefined) {
  127. this.listAdapter.update();
  128. }
  129. },
  130. });