Tab.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. bgProgressNode: cc.Node,
  11. friendSpriteFrames: [cc.SpriteFrame],
  12. artistSpriteFrames: [cc.SpriteFrame],
  13. friendBtn: cc.Button,
  14. artistBtn: cc.Button,
  15. friendItem: cc.Prefab,
  16. friendScrollView: cc.ScrollView,
  17. artistScrollView: cc.ScrollView,
  18. noArtistTipNode: cc.Node,
  19. _currentTab: 0
  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.bgProgressNode.zIndex = 1;
  29. this.friendScrollView.node.zIndex = 1;
  30. this.artistScrollView.node.zIndex = 1;
  31. if (this._currentTab === 0) {
  32. this.friendBtn.node.setScale(1.0, 1.0);
  33. this.artistBtn.node.setScale(0.9, 0.9);
  34. this.friendBtn.node.zIndex = 2;
  35. this.artistBtn.node.zIndex = 0;
  36. this.friendScrollView.node.active = true;
  37. this.artistScrollView.node.active = false;
  38. this.artistScrollView.node.position = cc.v2(0, -250);
  39. } else {
  40. this.friendBtn.node.setScale(0.9, 0.9);
  41. this.artistBtn.node.setScale(1.0, 1.0);
  42. this.friendBtn.node.zIndex = 0;
  43. this.artistBtn.node.zIndex = 2;
  44. this.friendScrollView.node.active = false;
  45. this.artistScrollView.node.active = true;
  46. this.friendScrollView.node.position = cc.v2(0, -250);
  47. }
  48. this.loadFriendData()
  49. .then((result) => { // 好友数据回调
  50. this.friends = result || [];
  51. return this.loadArtists();
  52. }).then((result) => { // 艺人数据回调
  53. if (result != undefined) {
  54. this.artists = result;
  55. this.noArtistTipNode.active = false;
  56. } else {
  57. this.noArtistTipNode.active = true;
  58. }
  59. this.layout();
  60. }).catch((err) => { // 捕获到报错
  61. console.log(err);
  62. });
  63. // 刷新艺人列表
  64. GameEvent.on(Notikey.RefreshFriendList, this, () => {
  65. this.loadFriendData()
  66. .then((result) => {
  67. this.friends = result || [];
  68. this.friendLayout();
  69. }).catch((err) => {
  70. console.log(err);
  71. });
  72. });
  73. GameEvent.on(Notikey.RefreshArtistManagerList, this, () => {
  74. this.loadArtists()
  75. .then((result) => {
  76. if (result != undefined) {
  77. this.artists = result;
  78. this.noArtistTipNode.active = false;
  79. } else {
  80. this.artists = [];
  81. this.noArtistTipNode.active = true;
  82. }
  83. this.artistLayout();
  84. }).catch((err) => {
  85. console.log(err);
  86. });
  87. });
  88. this.changeTabEvent = _.debounce((eventData) => {
  89. this._currentTab = eventData;
  90. this.setBtnSpriteFrames();
  91. let showBtn = (eventData == 0) ? this.friendBtn : this.artistBtn;
  92. let hideBtn = (eventData == 0) ? this.artistBtn : this.friendBtn;
  93. let showScrollView = (eventData == 0) ? this.friendScrollView : this.artistScrollView;
  94. let hideScrollView = (eventData == 0) ? this.artistScrollView : this.friendScrollView;
  95. this.showAnimation(showBtn, hideScrollView);
  96. this.hideAnimation(hideBtn, showScrollView);
  97. }, 500, true);
  98. },
  99. setBtnSpriteFrames() {
  100. this.friendBtn.getComponent(cc.Sprite).spriteFrame = this.friendSpriteFrames[this._currentTab];
  101. this.artistBtn.getComponent(cc.Sprite).spriteFrame = this.artistSpriteFrames[this._currentTab];
  102. },
  103. showAnimation(btn, scrollView) {
  104. let tempBtn = btn;
  105. let tempScrollView = scrollView;
  106. tempBtn.node.zIndex = 2;
  107. let scrollMoveAction = cc.moveTo(0.3, cc.p(0, -250));
  108. let scrollCallback = cc.callFunc(() => {
  109. tempScrollView.node.active = false;
  110. });
  111. tempScrollView.node.runAction(cc.sequence(scrollMoveAction, scrollCallback));
  112. },
  113. hideAnimation(btn, scrollView) {
  114. let tempBtn = btn;
  115. let tempScrollView = scrollView;
  116. tempBtn.node.zIndex = 0;
  117. tempScrollView.node.active = true;
  118. let scrollMoveAction = cc.moveTo(0.3, cc.p(0, -25));
  119. tempScrollView.node.runAction(scrollMoveAction);
  120. },
  121. changeTab(target, eventData) {
  122. if (this._currentTab == eventData) { return; }
  123. this.changeTabEvent(eventData);
  124. },
  125. loadFriendData() {
  126. return new Promise((resolve, reject) => {
  127. HomeApi.getFriends((responseData) => {
  128. resolve(responseData);
  129. }, (error) => {
  130. reject(error);
  131. });
  132. });
  133. },
  134. loadArtists() {
  135. return new Promise((resolve, reject) => {
  136. HomeApi.getFriendManageList( (responseData) => {
  137. resolve(responseData);
  138. }, (error) => {
  139. reject(error);
  140. })
  141. });
  142. },
  143. layout() {
  144. this.friendLayout();
  145. this.artistLayout();
  146. },
  147. friendLayout() {
  148. for (let child of this.friendScrollView.content.children) {
  149. child.destroy();
  150. }
  151. // 默认有全部好友和邀请两个节点
  152. let count = 1;
  153. count += this.friends.length;
  154. for (let i = 0; i < count; i++) {
  155. let item = cc.instantiate(this.friendItem);
  156. let scriptComponent = item.getComponent('LevelHomeFriendItem');
  157. if (i === 0) {
  158. scriptComponent.style = Style.AllFriend;
  159. } else {
  160. scriptComponent.style = Style.Friend;
  161. scriptComponent.setFriendInfo(this.friends[i-1]);
  162. }
  163. this.friendScrollView.content.addChild(item);
  164. }
  165. },
  166. artistLayout() {
  167. for (let child of this.artistScrollView.content.children) {
  168. child.destroy();
  169. }
  170. let artistCount = 1;
  171. artistCount += this.artists.length;
  172. for (let i = 0; i < artistCount; i++) {
  173. let item = cc.instantiate(this.friendItem);
  174. let scriptComponent = item.getComponent('LevelHomeFriendItem');
  175. if (i === 0) {
  176. scriptComponent.style = Style.Talent;
  177. } else {
  178. scriptComponent.style = Style.Artist;
  179. scriptComponent.setFriendInfo(this.artists[i-1]);
  180. }
  181. this.artistScrollView.content.addChild(item);
  182. }
  183. },
  184. });