LoginCtr.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. const SceneCtrlBase = require('SceneCtrlBase');
  2. const JMLoadProgressBar = require('JMLoadProgressBar');
  3. const JMNetImage = require('JMNetImage');
  4. const LoginPanel = require('../view/login/LoginPanel');
  5. const RegisterPanel = require('../view/login/RegisterPanel');
  6. const TagType = {
  7. LOGIN: 0,
  8. REGISTER: 1
  9. };
  10. cc.Class({
  11. extends: SceneCtrlBase,
  12. editor: {
  13. menu: 'Ctrl/LoginCtrl'
  14. },
  15. properties: {
  16. loginPanel: {
  17. default: undefined,
  18. type: LoginPanel,
  19. },
  20. registerPanel: {
  21. default: undefined,
  22. type: RegisterPanel,
  23. },
  24. loadProgressBar: JMLoadProgressBar,
  25. },
  26. onLoad () {
  27. this._super();
  28. this._tagType = TagType.LOGIN;
  29. // 进行授权、登录、注册等操作
  30. this.initAuth();
  31. this.initUI();
  32. cc.game.on('e_socket_on_logined', this._handleSocketOnLogined, this);
  33. cc.game.on('e_socket_on_closed', this._handleSocketOnClosed, this);
  34. },
  35. onDestroy () {
  36. cc.game.targetOff(this);
  37. this._super();
  38. },
  39. initAuth () {
  40. },
  41. initUI () {
  42. this.showpanel(TagType.LOGIN);
  43. this.loginPanel.reloadData(()=> {
  44. this.showpanel(TagType.REGISTER);
  45. });
  46. this.registerPanel.reloadData(()=> {
  47. this.showpanel(TagType.LOGIN);
  48. });
  49. // 隐藏进度
  50. this.showDownloadProgress(false);
  51. },
  52. /**
  53. * 是否显示下载进度。不显示下载进度时,展示授权按钮
  54. *
  55. * @param {Boolean} isShow
  56. */
  57. showDownloadProgress (isShow) {
  58. // 更新布局
  59. this.loadProgressBar.node.active = isShow;
  60. if (isShow) {
  61. this.loadProgressBar.progressBar.progress = 0;
  62. this.loadProgressBar.setProgress(15, 1);
  63. }
  64. },
  65. showpanel (tagType) {
  66. this._tagType = tagType;
  67. this.loginPanel.node.active = tagType == TagType.LOGIN;
  68. this.registerPanel.node.active = tagType == TagType.REGISTER;
  69. },
  70. /**
  71. * 登录成功
  72. *
  73. */
  74. _handleSocketOnLogined () {
  75. this.didLoginSuccessful();
  76. },
  77. /**
  78. * 登录成功
  79. *
  80. */
  81. _handleSocketOnClosed () {
  82. this.didLoginFailed();
  83. },
  84. /**
  85. * 登录成功
  86. *
  87. */
  88. didLoginSuccessful () {
  89. G.LogUtils.log('didLoginSuccessful');
  90. this.enterMainScene();
  91. },
  92. /**
  93. * 登录失败
  94. *
  95. */
  96. didLoginFailed () {
  97. G.LogUtils.log('didLoginFailed');
  98. },
  99. // 进入主界面
  100. enterMainScene () {
  101. // 避免反复进入
  102. if (this._alreadyEnterScene)
  103. return;
  104. // 提前预加载大厅场景
  105. if (!this.didLoadMainScene) {
  106. this.preLoadMainScene();
  107. }
  108. this._alreadyEnterScene = true;
  109. this.scheduleOnce(() => {
  110. G.AppUtils.runScene('scene_main');
  111. }, 0.1);
  112. this.showDownloadProgress(true);
  113. this.loadProgressBar.setProgress(0.1, 1);
  114. },
  115. // 预加载主界面
  116. preLoadMainScene () {
  117. // 加载中、或者加载成功时,不再加载
  118. if (this.isPreLoading || this.didLoadHomeScene) {
  119. return;
  120. }
  121. this.isPreLoading = true;
  122. // 开始预加载资源
  123. let temp = undefined;
  124. cc.director.preloadScene('scene_main', (completedCount, totalCount, item)=>{
  125. temp = G.AppUtils.getLoadProgressInfo(temp, completedCount, totalCount);
  126. this.downloadPercent = Math.floor(temp.precent * 100);
  127. }, (error, asset) => {
  128. this.isPreLoading = false;
  129. if (error) {
  130. this.addToast('网络异常!场景加载失败');
  131. return;
  132. }
  133. this.didLoadMainScene = true;
  134. this.enterMainScene();
  135. });
  136. },
  137. });