Ext.define('school.view.viewport.ViewportController', { extend: 'Ext.app.ViewController', alias: 'controller.viewport', listen: { controller: { '*': { login: 'onLogin', logout: 'onLogout', unmatchedroute: 'handleUnmatchedRoute' } } }, routes: { 'login': 'handleLoginRoute' }, init: function() { var me = this; me.originalRoute = school.getApplication().getDefaultToken(); me.restoreSession(); }, showView: function(xtype) { var view = this.lookup(xtype), viewport = this.getView(); if (!view) { viewport.removeAll(true); view = viewport.add({ xtype: xtype, reference: xtype }); } viewport.getLayout().setActiveItem(view); }, showAuth: function() { // this.showMain();return; var me = this; this.showView('login'); }, showMain: function() { var me = this; var schoolId = school.util.BaseUtil.getCurrentUser().school_id; //读取学校 school.util.BaseUtil.request({ // url:'http://10.1.80.36:9520/api/school/school/read/' + schoolId url:'/api/school/school/read/' + schoolId }) .then(function(res) { if(res.success) { let d = { schoolId: res.data.school_id, schoolName: res.data.school_name, TeacherId: res.data.teacher_id, TeacherName: res.data.teacher_name, schoolPhone: res.data.school_phone, schoolRemarks: res.data.school_remarks, schoolAppId: res.data.school_appid, schoolAddress: res.data.school_address, schoolStatus: res.data.school_status, schoolSecret: res.data.school_secret, }; me.getViewModel().setData(d); } }) .catch(function(e) { console.error(e); school.util.BaseUtil.showErrorToast('读取学校信息失败: ' + e.message); }); me.showView('main'); }, // ROUTING handleLoginRoute: function() { var session = this.session; if (session && session.isValid()) { this.redirectTo('', {replace: true}); return; } this.showAuth(); }, handleUnmatchedRoute: function(route) { var me = this; if (!me.session || !me.session.isValid()) { // There is no authenticated user, let's redirect to the login page but keep track // of the original route to restore the requested route after user authentication. me.originalRoute = route; me.redirectTo('login', {replace: true}); return; } // There is an authenticated user, so let's simply redirect to the default token. var target = school.getApplication().getDefaultToken(); Ext.log.warn('Route unknown: ', route); if (route !== target) { me.redirectTo(target, {replace: true}); } }, setRequestToken: function(token) { var headers = Ext.Ajax.getDefaultHeaders() || {}; if (token) { headers['Authorization'] = token; } else { delete headers['Authorization']; } Ext.Ajax.setDefaultHeaders(headers); }, // SESSION MANAGEMENT restoreSession: function() { var data = school.util.State.get('session'), session = data? school.model.Session.loadData(data) : null; if (session && session.isValid()) { this.initiateSession(session); } else { this.terminateSession(); } return session; }, initiateSession: function(session) { this.setRequestToken(session.get('token')); this.saveSession(session); this.showMain(); }, terminateSession: function() { this.setRequestToken(null); this.saveSession(null); //this.showAuth(); }, saveSession: function(session) { school.util.State.set('session', session && session.getData(true)); this.getViewModel().set('account', session && session.get('account')); this.session = session; }, // AUTHENTICATION onLogin: function(session) { if (!session || !session.isValid()) { return false; } this.initiateSession(session); this.redirectTo(this.originalRoute, {replace: true}); }, onLogout: function() { var me = this, view = me.getView(), session = me.session; if (!session || !session.isValid()) { return false; } view.mask(); session.logout().catch(function(error) { school.util.BaseUtil.showErrorToast(error.message); }).then(function() { me.originalRoute = Ext.History.getToken(); me.terminateSession(); view.unmask(); me.redirectTo('login', {replace: true}); }); } });