ViewportController.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. Ext.define('saas.view.viewport.ViewportController', {
  2. extend: 'Ext.app.ViewController',
  3. alias: 'controller.viewport',
  4. listen: {
  5. controller: {
  6. '*': {
  7. login: 'onLogin',
  8. logout: 'onLogout',
  9. selectCompany: 'onSelectCompany',
  10. unmatchedroute: 'handleUnmatchedRoute'
  11. }
  12. }
  13. },
  14. routes: {
  15. 'login': 'handleLoginRoute'
  16. },
  17. init: function() {
  18. this.originalRoute = saas.getApplication().getDefaultToken();
  19. this.restoreSession();
  20. },
  21. mainviewboxready: function() {
  22. //初始化accountPage地址
  23. document.getElementsByName('accountPage')[0].setAttribute('src',getTokenPage());
  24. if(!Ext.isChrome && !Ext.isFirefox) {
  25. saas.util.BaseUtil.showConfirm('温馨提示', '为了更好地呈现页面效果,推荐使用Chrome浏览器或火狐浏览器');
  26. }
  27. },
  28. showView: function(xtype) {
  29. var view = this.lookup(xtype),
  30. viewport = this.getView();
  31. if (!view) {
  32. viewport.removeAll(true);
  33. view = viewport.add({
  34. xtype: xtype,
  35. reference: xtype
  36. });
  37. }
  38. viewport.getLayout().setActiveItem(view);
  39. },
  40. showAuth: function() {
  41. this.showView('login');
  42. },
  43. showMain: function() {
  44. var me = this;
  45. me.showView('main');
  46. },
  47. // ROUTING
  48. handleLoginRoute: function() {
  49. var session = this.session;
  50. if (session && session.isValid()) {
  51. this.redirectTo('', {replace: true});
  52. return;
  53. }
  54. this.showAuth();
  55. },
  56. handleUnmatchedRoute: function(route) {
  57. var me = this;
  58. if (!me.session || !me.session.isValid()) {
  59. // There is no authenticated user, let's redirect to the login page but keep track
  60. // of the original route to restore the requested route after user authentication.
  61. me.originalRoute = route;
  62. me.redirectTo('login', {replace: true});
  63. return;
  64. }
  65. // There is an authenticated user, so let's simply redirect to the default token.
  66. var target = saas.getApplication().getDefaultToken();
  67. Ext.log.warn('Route unknown: ', route);
  68. if (route !== target) {
  69. me.redirectTo(target, {replace: true});
  70. }
  71. },
  72. setRequestToken: function(token) {
  73. var headers = Ext.Ajax.getDefaultHeaders() || {};
  74. if (token) {
  75. headers['Authorization'] = token;
  76. } else {
  77. delete headers['Authorization'];
  78. }
  79. Ext.Ajax.setDefaultHeaders(headers);
  80. },
  81. // SESSION MANAGEMENT
  82. restoreSession: function() {
  83. var data = saas.util.State.get('session'),
  84. session = data? saas.model.Session.loadData(data) : null;
  85. if (session && session.isValid()) {
  86. this.initiateSession(session);
  87. } else {
  88. this.terminateSession();
  89. }
  90. return session;
  91. },
  92. initiateSession: function(session) {
  93. this.setRequestToken(session.get('token'));
  94. this.saveSession(session);
  95. this.showMain();
  96. },
  97. terminateSession: function() {
  98. this.setRequestToken(null);
  99. this.saveSession(null);
  100. //this.showAuth();
  101. },
  102. saveSession: function(session) {
  103. saas.util.State.set('session', session && session.getData(true));
  104. this.getViewModel().set('account', session && session.get('account'));
  105. this.session = session;
  106. },
  107. // AUTHENTICATION
  108. onLogin: function(session) {
  109. if (!session || !session.isValid()) {
  110. return false;
  111. }
  112. this.initiateSession(session);
  113. this.redirectTo(this.originalRoute, {replace: true});
  114. },
  115. onLogout: function() {
  116. var me = this,
  117. view = me.getView(),
  118. session = me.session;
  119. if (!session || !session.isValid()) {
  120. return false;
  121. }
  122. view.mask();
  123. session.logout().catch(function(error) {
  124. saas.util.BaseUtil.showErrorToast(error.message);
  125. }).then(function() {
  126. me.originalRoute = Ext.History.getToken();
  127. me.terminateSession();
  128. view.unmask();
  129. //跳转到账户中心
  130. const frame = window.frames[window.frames.length - 1];
  131. frame.postMessage('removeToken','*');
  132. window.location.href = getAccountPage();
  133. //me.redirectTo('login', {replace: true});
  134. });
  135. },
  136. onSelectCompany: function(companyId) {
  137. var me = this, view = me.getView(), viewModel = me.getViewModel(),
  138. oldSession = me.session, company = viewModel.get('company');
  139. if (company.id != companyId) {
  140. view.mask('请稍等...');
  141. saas.model.Session.switchCompany(oldSession, companyId)
  142. .then(function(newSession) {
  143. newSession.get('account').companyId = companyId;
  144. me.initiateSession(newSession);
  145. })
  146. .catch(function(error) {
  147. saas.util.BaseUtil.showErrorToast(error.message);
  148. })
  149. .then(function() {
  150. view.isMasked() && view.unmask();
  151. window.location.reload();
  152. });
  153. }
  154. }
  155. });