1
0

FriendList.js 4.2 KB

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