FriendList.js 4.4 KB

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