ViewportController.js 5.1 KB

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