ViewportController.js 5.3 KB

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