Tab.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. const DWTool = require('./utils/DWTool');
  2. const Style = require('./utils/GameEnum').LevelHomeFriendItemStyle;
  3. const HomeApi = require('./net/HomeApi');
  4. const GameModule = require("./utils/GameModule");
  5. const Notikey = require('./utils/GameEnum').GameNotificationKey;
  6. cc.Class({
  7. extends: cc.Component,
  8. properties: {
  9. bgNode: cc.Node,
  10. friendSpriteFrames: [cc.SpriteFrame],
  11. artistSpriteFrames: [cc.SpriteFrame],
  12. friendBtn: cc.Button,
  13. artistBtn: cc.Button,
  14. friendItem: cc.Prefab,
  15. friendScrollView: cc.ScrollView,
  16. artistScrollView: cc.ScrollView,
  17. noArtistTipNode: cc.Node,
  18. _currentTab: 0 ,
  19. allFriendNode: cc.Node
  20. },
  21. // LIFE-CYCLE CALLBACKS:
  22. onLoad () {
  23. GameModule.tab = this.node;
  24. this.friends = [];
  25. this.artists = [];
  26. this.setBtnSpriteFrames();
  27. this.bgNode.zIndex = 1;
  28. this.friendScrollView.node.zIndex = 1;
  29. // this.artistScrollView.node.zIndex = 1;
  30. if (this._currentTab === 0) {
  31. // this.friendBtn.node.setScale(1.0, 1.0);
  32. // this.artistBtn.node.setScale(0.9, 0.9);
  33. // this.friendBtn.node.zIndex = 2;
  34. // this.artistBtn.node.zIndex = 0;
  35. this.friendScrollView.node.active = true;
  36. // this.artistScrollView.node.active = false;
  37. // this.artistScrollView.node.position = cc.v2(0, -250);
  38. } else {
  39. // this.friendBtn.node.setScale(0.9, 0.9);
  40. // this.artistBtn.node.setScale(1.0, 1.0);
  41. // this.friendBtn.node.zIndex = 0;
  42. // this.artistBtn.node.zIndex = 2;
  43. // this.friendScrollView.node.active = false;
  44. // this.artistScrollView.node.active = true;
  45. // this.friendScrollView.node.position = cc.v2(0, -250);
  46. }
  47. this.allFriendNode.on(cc.Node.EventType.TOUCH_END, _.debounce(() => {
  48. GameEvent.fire(Notikey.ShowFriendSystem);
  49. this.updateFriendList();
  50. GameModule.audioMng.playButton();
  51. }, 1000, true), this);
  52. // 刷新好友列表
  53. GameEvent.on(Notikey.RefreshFriendList, this, () => {
  54. this.updateFriendList();
  55. });
  56. // 刷新艺人列表
  57. GameEvent.on(Notikey.RefreshArtistManagerList, this, () => {
  58. this.loadArtists()
  59. .then((result) => {
  60. if (result != undefined) {
  61. this.artists = result;
  62. this.noArtistTipNode.active = false;
  63. } else {
  64. this.artists = [];
  65. this.noArtistTipNode.active = true;
  66. }
  67. this.artistLayout();
  68. }).catch((err) => {
  69. console.log(err);
  70. });
  71. });
  72. this.changeTabEvent = _.debounce((eventData) => {
  73. this._currentTab = eventData;
  74. this.setBtnSpriteFrames();
  75. let showBtn = (eventData == 0) ? this.friendBtn : this.artistBtn;
  76. let hideBtn = (eventData == 0) ? this.artistBtn : this.friendBtn;
  77. let showScrollView = (eventData == 0) ? this.friendScrollView : this.artistScrollView;
  78. let hideScrollView = (eventData == 0) ? this.artistScrollView : this.friendScrollView;
  79. this.showAnimation(showBtn, hideScrollView);
  80. this.hideAnimation(hideBtn, showScrollView);
  81. }, 500, true);
  82. },
  83. init(game) {
  84. this.game = game;
  85. this.initFriendList = false;
  86. this.updateFriendList();
  87. },
  88. updateFriendList () {
  89. this.loadFriendData().then((data) => { // 好友数据回调
  90. let _friendList = data.users || [];
  91. // 过滤好友申请项
  92. _friendList = _friendList.filter(n => {
  93. return n.isApplied != 1
  94. });
  95. Global.friendList = _friendList;
  96. this.friends = _friendList.slice(0, 20);
  97. this.noArtistTipNode.active = false;
  98. this.friendLayout();
  99. // 暂时注释,国庆回来可能要用到
  100. // if(!this.initFriendList) {
  101. // Global.friendList = this.friends = _friendList;
  102. // if(_friendList.length > 0) {
  103. // this.initFriendList = true;
  104. // }
  105. // this.noArtistTipNode.active = false;
  106. // this.friendLayout();
  107. // } else {
  108. // // 判断列表是否有更改,如果没有更改则不重新渲染
  109. // let fChange = _friendList[0].uid != this.friends[0].uid;
  110. // let lChange = _friendList[_friendList.length - 1].uid != this.friends[this.friends.length - 1].uid;
  111. // if(fChange || lChange) {
  112. // Global.friendList = this.friends = _friendList;
  113. // this.noArtistTipNode.active = false;
  114. // this.friendLayout();
  115. // }
  116. // }
  117. }).catch((err) => { // 捕获到报错
  118. console.log(err);
  119. });
  120. },
  121. setBtnSpriteFrames() {
  122. this.friendBtn.getComponent(cc.Sprite).spriteFrame = this.friendSpriteFrames[this._currentTab];
  123. this.artistBtn.getComponent(cc.Sprite).spriteFrame = this.artistSpriteFrames[this._currentTab];
  124. },
  125. showAnimation(btn, scrollView) {
  126. let tempBtn = btn;
  127. let tempScrollView = scrollView;
  128. tempBtn.node.zIndex = 2;
  129. let scrollMoveAction = cc.moveTo(0.3, cc.p(0, -250));
  130. let scrollCallback = cc.callFunc(() => {
  131. tempScrollView.node.active = false;
  132. });
  133. tempScrollView.node.runAction(cc.sequence(scrollMoveAction, scrollCallback));
  134. },
  135. hideAnimation(btn, scrollView) {
  136. let tempBtn = btn;
  137. let tempScrollView = scrollView;
  138. tempBtn.node.zIndex = 0;
  139. tempScrollView.node.active = true;
  140. let scrollMoveAction = cc.moveTo(0.3, cc.p(0, -10));
  141. tempScrollView.node.runAction(scrollMoveAction);
  142. },
  143. changeTab(target, eventData) {
  144. if (this._currentTab == eventData) { return; }
  145. this.changeTabEvent(eventData);
  146. },
  147. loadFriendData() {
  148. return new Promise((resolve, reject) => {
  149. HomeApi.getFriends((responseData) => {
  150. resolve(responseData);
  151. }, (error) => {
  152. reject(error);
  153. });
  154. });
  155. },
  156. loadArtists() {
  157. return new Promise((resolve, reject) => {
  158. HomeApi.getFriendManageList((responseData) => {
  159. resolve(responseData);
  160. }, (error) => {
  161. reject(error);
  162. })
  163. });
  164. },
  165. layout() {
  166. this.friendLayout();
  167. // this.artistLayout();
  168. },
  169. friendLayout() {
  170. console.log('friendLayout');
  171. for (let child of this.friendScrollView.content.children) {
  172. child.destroy();
  173. }
  174. for (let i = 0; i < this.friends.length; i++) {
  175. let item = cc.instantiate(this.friendItem);
  176. let scriptComponent = item.getComponent('LevelHomeFriendItem');
  177. scriptComponent.style = Style.Friend;
  178. scriptComponent.setFriendInfo(this.friends[i]);
  179. this.friendScrollView.content.addChild(item);
  180. }
  181. },
  182. artistLayout() {
  183. for (let child of this.artistScrollView.content.children) {
  184. child.destroy();
  185. }
  186. let artistCount = 1;
  187. artistCount += this.artists.length;
  188. for (let i = 0; i < artistCount; i++) {
  189. let item = cc.instantiate(this.friendItem);
  190. let scriptComponent = item.getComponent('LevelHomeFriendItem');
  191. if (i === 0) {
  192. scriptComponent.style = Style.Talent;
  193. } else {
  194. scriptComponent.style = Style.Artist;
  195. scriptComponent.setFriendInfo(this.artists[i-1]);
  196. }
  197. this.artistScrollView.content.addChild(item);
  198. }
  199. },
  200. });