ViewportController.js 4.8 KB

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