1
0

FriendList.js 4.2 KB

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