ViewportController.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. //没有session时 判断cookie中的uid
  63. var hasValidCookie = Ext.util.Cookies.get('uid')?me.getAccountCookie():false;
  64. if(!hasValidCookie){
  65. if(window.location.host.indexOf('.usoftchina.com')>-1){
  66. window.location.href = Ext.manifest.server.accountCenter
  67. }else{
  68. me.redirectTo('login', {replace: true});
  69. }
  70. }else{
  71. me.redirectTo('main', {replace: true});
  72. }
  73. return;
  74. }
  75. // There is an authenticated user, so let's simply redirect to the default token.
  76. var target = saas.getApplication().getDefaultToken();
  77. Ext.log.warn('Route unknown: ', route);
  78. if (route !== target) {
  79. me.redirectTo(target, {replace: true});
  80. }
  81. },
  82. getAccountCookie:function(){
  83. var hasValidCookie = false;
  84. Ext.Ajax.request({
  85. url: '/api/auth/info',
  86. withCredentials: true,
  87. async:false,
  88. dataType: 'json',
  89. headers:{
  90. 'Authorization':'',
  91. 'Access-Control-Allow-Origin': '<origin> | *'
  92. },
  93. method: 'GET',
  94. success: function(response, opts) {
  95. var res = Ext.decode(response.responseText);
  96. var data = res.data;
  97. var token = data.token;
  98. data.expire = token.expire;
  99. data.timestamp = token.timestamp;
  100. data.token = token.token;
  101. delete data['token'];
  102. if(data.conpanyId){
  103. session = data? saas.model.Session.loadData(data) : null;
  104. if (session && session.isValid()) {
  105. me.setRequestToken(session.get('token'));
  106. me.saveSession(session);
  107. }
  108. hasValidCookie = true
  109. }else{
  110. window.location.href = Ext.manifest.server.accountCenter
  111. }
  112. },
  113. failure: function(response, opts) {
  114. showErrorToast('解析cookie失败:'+response.message)
  115. }
  116. });
  117. return hasValidCookie;
  118. },
  119. setRequestToken: function(token) {
  120. var headers = Ext.Ajax.getDefaultHeaders() || {};
  121. if (token) {
  122. headers['Authorization'] = token;
  123. } else {
  124. delete headers['Authorization'];
  125. }
  126. Ext.Ajax.setDefaultHeaders(headers);
  127. },
  128. // SESSION MANAGEMENT
  129. restoreSession: function() {
  130. var data = saas.util.State.get('session'),
  131. session = data? saas.model.Session.loadData(data) : null;
  132. if (session && session.isValid()) {
  133. this.initiateSession(session);
  134. } else {
  135. this.terminateSession();
  136. }
  137. return session;
  138. },
  139. initiateSession: function(session) {
  140. this.setRequestToken(session.get('token'));
  141. this.saveSession(session);
  142. this.showMain();
  143. },
  144. terminateSession: function() {
  145. this.setRequestToken(null);
  146. this.saveSession(null);
  147. //this.showAuth();
  148. },
  149. saveSession: function(session) {
  150. saas.util.State.set('session', session && session.getData(true));
  151. this.getViewModel().set('account', session && session.get('account'));
  152. this.session = session;
  153. },
  154. // AUTHENTICATION
  155. onLogin: function(session) {
  156. if (!session || !session.isValid()) {
  157. return false;
  158. }
  159. this.initiateSession(session);
  160. this.redirectTo(this.originalRoute, {replace: true});
  161. },
  162. onLogout: function() {
  163. var me = this,
  164. view = me.getView(),
  165. session = me.session;
  166. if (!session || !session.isValid()) {
  167. return false;
  168. }
  169. view.mask();
  170. session.logout().catch(function(error) {
  171. saas.util.BaseUtil.showErrorToast(error.message);
  172. }).then(function() {
  173. me.originalRoute = Ext.History.getToken();
  174. me.terminateSession();
  175. view.unmask();
  176. //跳转到账户中心
  177. const frame = window.frames[window.frames.length - 1];
  178. frame.postMessage('removeToken','*');
  179. window.location.href = getAccountPage();
  180. //me.redirectTo('login', {replace: true});
  181. });
  182. },
  183. onSelectCompany: function(companyId) {
  184. var me = this, view = me.getView(), viewModel = me.getViewModel(),
  185. oldSession = me.session, company = viewModel.get('company');
  186. if (company.id != companyId) {
  187. view.mask('请稍等...');
  188. saas.model.Session.switchCompany(oldSession, companyId)
  189. .then(function(newSession) {
  190. newSession.get('account').companyId = companyId;
  191. me.initiateSession(newSession);
  192. })
  193. .catch(function(error) {
  194. saas.util.BaseUtil.showErrorToast(error.message);
  195. })
  196. .then(function() {
  197. view.isMasked() && view.unmask();
  198. window.location.reload();
  199. });
  200. }
  201. }
  202. });