diff --git a/src/utils/request.js b/src/utils/request.js index ed57e5f26..007e91221 100644 --- a/src/utils/request.js +++ b/src/utils/request.js @@ -15,22 +15,32 @@ const service = axios.create({ timeout: 60 * 1000 // request timeout }) +function beforeRequestAddToken(config) { + if (store.getters.token) { + // let each request carry token + // ['X-Token'] is a custom headers key + // please modify it according to the actual situation + config.headers['X-CSRFToken'] = getToken() + if (getCurrentOrg().id !== '') { + config.headers['X-JMS-ORG'] = getCurrentOrg().id + } + } +} + +function beforeRequestAddTimezone(config) { + try { + config.headers['X-TZ'] = Intl.DateTimeFormat().resolvedOptions().timeZone + } catch (e) { + console.log('Current browser not support Intl tools') + } +} + // request interceptor service.interceptors.request.use( config => { // do something before request is sent - if (store.getters.token) { - // let each request carry token - // ['X-Token'] is a custom headers key - // please modify it according to the actual situation - config.headers['X-CSRFToken'] = getToken() - if (getCurrentOrg().id !== '') { config.headers['X-JMS-ORG'] = getCurrentOrg().id } - } - try { - config.headers['X-TZ'] = Intl.DateTimeFormat().resolvedOptions().timeZone - } catch (e) { - console.log('Current browser not support Intl tools') - } + beforeRequestAddToken(config) + beforeRequestAddTimezone(config) return config }, error => { @@ -40,6 +50,31 @@ service.interceptors.request.use( } ) +function ifUnauthorized({ response, error }) { + if (response.status === 401) { + // 未授权重定向到登录页面 + const title = i18n.t('common.Info').String() + const msg = i18n.t('auth.LoginRequiredMsg').String() + MessageBox.confirm(msg, title, { + confirmButtonText: i18n.t('auth.ReLogin'), + cancelButtonText: i18n.t('common.Cancel'), + type: 'warning' + }).then(() => { + window.location = '/core/auth/login/' + }) + } +} + +function flashErrorMsg({ response, error }) { + if (!response.config.disableFlashMsg) { + Message({ + message: error.message, + type: 'error', + duration: 5 * 1000 + }) + } +} + // response interceptor service.interceptors.response.use( /** @@ -64,24 +99,10 @@ service.interceptors.response.use( if (!error.response) { return Promise.reject(error) } + const response = error.response - if (response.status === 401) { - // 未授权重定向到登录页面 - const title = '' - const msg = i18n.t('auth.LoginRequiredMsg') - MessageBox.confirm(msg, title, { - confirmButtonText: i18n.t('auth.ReLogin'), - cancelButtonText: i18n.t('common.Cancel'), - type: 'warning' - }) - } - if (!response.config.disableFlashMsg) { - Message({ - message: error.message, - type: 'error', - duration: 5 * 1000 - }) - } + ifUnauthorized({ response, error }) + flashErrorMsg({ response, error }) return Promise.reject(error) } ) diff --git a/src/utils/startup.js b/src/utils/startup.js index 538ecde56..7b416faeb 100644 --- a/src/utils/startup.js +++ b/src/utils/startup.js @@ -58,6 +58,7 @@ async function getUserRoleAndSetRoutes({ to, from, next }) { console.log('Current org role: ', current_org_roles) current_org_roles = checkRoles(current_org_roles) + console.log('Current org role: ', current_org_roles) // generate accessible routes map based on roles const accessRoutes = await store.dispatch('permission/generateRoutes', current_org_roles) @@ -68,6 +69,7 @@ async function getUserRoleAndSetRoutes({ to, from, next }) { // hack method to ensure that addRoutes is complete // set the replace: true, so the navigation will not leave a history record + console.log('Next to: ', to) next({ ...to, replace: true }) } catch (error) { // remove token and go to login page to re-login @@ -99,18 +101,18 @@ export async function startup({ to, from, next }) { } function checkRoles(val) { - let currentRule = getPermission() - if (currentRule) { - if (val && !val.includes(currentRule)) { + let currentRoles = getPermission() + if (currentRoles) { + if (val && !val.includes(currentRoles)) { // TODO 异常注入处理 - currentRule = val[0] - setPermission(currentRule) + currentRoles = val[0] + setPermission(currentRoles) } } else { // 设置默认路由 - currentRule = val[0] - setPermission(currentRule) + currentRoles = val[0] + setPermission(currentRoles) } - return [currentRule] + return [currentRoles] }