ViewportController.js 5.0 KB

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