Tab.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. GameModule.audioMng.playButton();
  50. }, 1000, true), this);
  51. // 刷新好友列表
  52. GameEvent.on(Notikey.RefreshFriendList, this, () => {
  53. this.loadFriendData().then((data) => {
  54. Global.friendList = this.friends = data.users || [];
  55. this.friendLayout();
  56. }).catch((err) => {
  57. console.log(err);
  58. });
  59. });
  60. // 刷新艺人列表
  61. GameEvent.on(Notikey.RefreshArtistManagerList, this, () => {
  62. this.loadArtists()
  63. .then((result) => {
  64. if (result != undefined) {
  65. this.artists = result;
  66. this.noArtistTipNode.active = false;
  67. } else {
  68. this.artists = [];
  69. this.noArtistTipNode.active = true;
  70. }
  71. this.artistLayout();
  72. }).catch((err) => {
  73. console.log(err);
  74. });
  75. });
  76. this.changeTabEvent = _.debounce((eventData) => {
  77. this._currentTab = eventData;
  78. this.setBtnSpriteFrames();
  79. let showBtn = (eventData == 0) ? this.friendBtn : this.artistBtn;
  80. let hideBtn = (eventData == 0) ? this.artistBtn : this.friendBtn;
  81. let showScrollView = (eventData == 0) ? this.friendScrollView : this.artistScrollView;
  82. let hideScrollView = (eventData == 0) ? this.artistScrollView : this.friendScrollView;
  83. this.showAnimation(showBtn, hideScrollView);
  84. this.hideAnimation(hideBtn, showScrollView);
  85. }, 500, true);
  86. },
  87. init(game) {
  88. this.game = game
  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 = this.friends = _friendList;
  96. this.noArtistTipNode.active = false;
  97. this.friendLayout();
  98. }).catch((err) => { // 捕获到报错
  99. console.log(err);
  100. });
  101. },
  102. setBtnSpriteFrames() {
  103. this.friendBtn.getComponent(cc.Sprite).spriteFrame = this.friendSpriteFrames[this._currentTab];
  104. this.artistBtn.getComponent(cc.Sprite).spriteFrame = this.artistSpriteFrames[this._currentTab];
  105. },
  106. showAnimation(btn, scrollView) {
  107. let tempBtn = btn;
  108. let tempScrollView = scrollView;
  109. tempBtn.node.zIndex = 2;
  110. let scrollMoveAction = cc.moveTo(0.3, cc.p(0, -250));
  111. let scrollCallback = cc.callFunc(() => {
  112. tempScrollView.node.active = false;
  113. });
  114. tempScrollView.node.runAction(cc.sequence(scrollMoveAction, scrollCallback));
  115. },
  116. hideAnimation(btn, scrollView) {
  117. let tempBtn = btn;
  118. let tempScrollView = scrollView;
  119. tempBtn.node.zIndex = 0;
  120. tempScrollView.node.active = true;
  121. let scrollMoveAction = cc.moveTo(0.3, cc.p(0, -10));
  122. tempScrollView.node.runAction(scrollMoveAction);
  123. },
  124. changeTab(target, eventData) {
  125. if (this._currentTab == eventData) { return; }
  126. this.changeTabEvent(eventData);
  127. },
  128. loadFriendData() {
  129. return new Promise((resolve, reject) => {
  130. HomeApi.getFriends((responseData) => {
  131. resolve(responseData);
  132. }, (error) => {
  133. reject(error);
  134. });
  135. });
  136. },
  137. loadArtists() {
  138. return new Promise((resolve, reject) => {
  139. HomeApi.getFriendManageList((responseData) => {
  140. resolve(responseData);
  141. }, (error) => {
  142. reject(error);
  143. })
  144. });
  145. },
  146. layout() {
  147. this.friendLayout();
  148. // this.artistLayout();
  149. },
  150. friendLayout() {
  151. for (let child of this.friendScrollView.content.children) {
  152. child.destroy();
  153. }
  154. for (let i = 0; i < this.friends.length; i++) {
  155. let item = cc.instantiate(this.friendItem);
  156. let scriptComponent = item.getComponent('LevelHomeFriendItem');
  157. scriptComponent.style = Style.Friend;
  158. scriptComponent.setFriendInfo(this.friends[i]);
  159. this.friendScrollView.content.addChild(item);
  160. }
  161. },
  162. artistLayout() {
  163. for (let child of this.artistScrollView.content.children) {
  164. child.destroy();
  165. }
  166. let artistCount = 1;
  167. artistCount += this.artists.length;
  168. for (let i = 0; i < artistCount; i++) {
  169. let item = cc.instantiate(this.friendItem);
  170. let scriptComponent = item.getComponent('LevelHomeFriendItem');
  171. if (i === 0) {
  172. scriptComponent.style = Style.Talent;
  173. } else {
  174. scriptComponent.style = Style.Artist;
  175. scriptComponent.setFriendInfo(this.artists[i-1]);
  176. }
  177. this.artistScrollView.content.addChild(item);
  178. }
  179. },
  180. });