ViewportController.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. Ext.define('school.view.viewport.ViewportController', {
  2. extend: 'Ext.app.ViewController',
  3. alias: 'controller.viewport',
  4. listen: {
  5. controller: {
  6. '*': {
  7. login: 'onLogin',
  8. logout: 'onLogout',
  9. unmatchedroute: 'handleUnmatchedRoute'
  10. }
  11. }
  12. },
  13. routes: {
  14. 'login': 'handleLoginRoute'
  15. },
  16. init: function() {
  17. var me = this;
  18. me.originalRoute = school.getApplication().getDefaultToken();
  19. me.restoreSession();
  20. },
  21. showView: function(xtype) {
  22. var view = this.lookup(xtype),
  23. viewport = this.getView();
  24. if (!view) {
  25. viewport.removeAll(true);
  26. view = viewport.add({
  27. xtype: xtype,
  28. reference: xtype
  29. });
  30. }
  31. viewport.getLayout().setActiveItem(view);
  32. },
  33. showAuth: function() {
  34. var me = this;
  35. this.showView('login');
  36. },
  37. showMain: function() {
  38. var me = this;
  39. //读取学校
  40. school.util.BaseUtil.request({
  41. // url:'http://10.1.80.35:9560/school/read/1'
  42. url:'/school/read/1'
  43. })
  44. .then(function(res) {
  45. if(res.success) {
  46. let d = {
  47. schoolId: res.data.school_id,
  48. schoolName: res.data.school_name,
  49. schoolPhone: res.data.school_phone,
  50. schoolRemarks: res.data.school_remarks,
  51. schoolAppId: res.data.school_appid,
  52. schoolAddress: res.data.school_address,
  53. schoolStatus: res.data.school_status,
  54. schoolSecret: res.data.school_secret,
  55. };
  56. me.getViewModel().setData(d)
  57. }
  58. })
  59. .catch(function(e) {
  60. console.error(e);
  61. });
  62. me.showView('main');
  63. },
  64. // ROUTING
  65. handleLoginRoute: function() {
  66. var session = this.session;
  67. if (session && session.isValid()) {
  68. this.redirectTo('', {replace: true});
  69. return;
  70. }
  71. this.showAuth();
  72. },
  73. handleUnmatchedRoute: function(route) {
  74. var me = this;
  75. if (!me.session || !me.session.isValid()) {
  76. // There is no authenticated user, let's redirect to the login page but keep track
  77. // of the original route to restore the requested route after user authentication.
  78. me.originalRoute = route;
  79. me.redirectTo('login', {replace: true});
  80. return;
  81. }
  82. // There is an authenticated user, so let's simply redirect to the default token.
  83. var target = school.getApplication().getDefaultToken();
  84. Ext.log.warn('Route unknown: ', route);
  85. if (route !== target) {
  86. me.redirectTo(target, {replace: true});
  87. }
  88. },
  89. setRequestToken: function(token) {
  90. var headers = Ext.Ajax.getDefaultHeaders() || {};
  91. if (token) {
  92. headers['Authorization'] = token;
  93. } else {
  94. delete headers['Authorization'];
  95. }
  96. Ext.Ajax.setDefaultHeaders(headers);
  97. },
  98. // SESSION MANAGEMENT
  99. restoreSession: function() {
  100. var data = school.util.State.get('session'),
  101. session = data? school.model.Session.loadData(data) : null;
  102. if (session && session.isValid()) {
  103. this.initiateSession(session);
  104. } else {
  105. this.terminateSession();
  106. }
  107. return session;
  108. },
  109. initiateSession: function(session) {
  110. this.setRequestToken(session.get('token'));
  111. this.saveSession(session);
  112. this.showMain();
  113. },
  114. terminateSession: function() {
  115. this.setRequestToken(null);
  116. this.saveSession(null);
  117. //this.showAuth();
  118. },
  119. saveSession: function(session) {
  120. school.util.State.set('session', session && session.getData(true));
  121. this.getViewModel().set('account', session && session.get('account'));
  122. this.session = session;
  123. },
  124. // AUTHENTICATION
  125. onLogin: function(session) {
  126. if (!session || !session.isValid()) {
  127. return false;
  128. }
  129. this.initiateSession(session);
  130. this.redirectTo(this.originalRoute, {replace: true});
  131. },
  132. onLogout: function() {
  133. var me = this,
  134. view = me.getView(),
  135. session = me.session;
  136. if (!session || !session.isValid()) {
  137. return false;
  138. }
  139. view.mask();
  140. session.logout().catch(function(error) {
  141. school.util.BaseUtil.showErrorToast(error.message);
  142. }).then(function() {
  143. me.originalRoute = Ext.History.getToken();
  144. me.terminateSession();
  145. view.unmask();
  146. me.redirectTo('login', {replace: true});
  147. });
  148. }
  149. });