diff --git a/packages/fes-plugin-login/src/runtime/runtime.js b/packages/fes-plugin-login/src/runtime/runtime.js index dc98263ee..29a418609 100644 --- a/packages/fes-plugin-login/src/runtime/runtime.js +++ b/packages/fes-plugin-login/src/runtime/runtime.js @@ -1,8 +1,12 @@ +import { request } from '@@/core/pluginExports'; import { ApplyPluginsType, getRouter, plugin } from '@fesjs/fes'; let config; function getLoginConfig() { - if (config) return config; + if (config) { + return config; + } + config = plugin.applyPlugins({ key: 'login', type: ApplyPluginsType.modify, @@ -14,38 +18,63 @@ function getLoginConfig() { return config; } +const defaultExport = { + onRouterCreated({ router }) { + const { hasLogin, loginPath } = getLoginConfig(); + if (hasLogin && loginPath) { + let isAuthenticated; + router.beforeEach(async (to, from, next) => { + if (to.path !== loginPath && !isAuthenticated) { + isAuthenticated = await hasLogin(); + if (!isAuthenticated) { + return next({ path: loginPath }); + } + } + next(); + }); + } + }, +}; + // ACCESS -export function request(memo) { - if (!memo.responseInterceptors) { - memo.responseInterceptors = []; - } - memo.responseInterceptors.push([ - (response) => response, - (error) => { +if (request.version) { + defaultExport.request = (memo) => { + const config = getRunTimeConfig(); + if (config.ignore401Redirect) { + return memo; + } + + const errorHandler = memo.errorHandler; + memo.errorHandler = (error) => { if (error?.response?.status === 401) { const router = getRouter(); const { loginPath } = getLoginConfig(); router.push({ path: loginPath }); } - throw error; - }, - ]); - return memo; + errorHandler && errorHandler(error); + }; + return memo; + }; } - -export function onRouterCreated({ router }) { - const { hasLogin, loginPath } = getLoginConfig(); - if (hasLogin && loginPath) { - let isAuthenticated; - router.beforeEach(async (to, from, next) => { - if (to.path !== loginPath && !isAuthenticated) { - isAuthenticated = await hasLogin(); - if (!isAuthenticated) { - return next({ path: loginPath }); +else { + defaultExport.request = (memo) => { + if (!memo.responseInterceptors) { + memo.responseInterceptors = []; + } + memo.responseInterceptors.push([ + response => response, + (error) => { + if (error?.response?.status === 401) { + const router = getRouter(); + const { loginPath } = getLoginConfig(); + router.push({ path: loginPath }); } - } - next(); - }); - } + throw error; + }, + ]); + return memo; + }; } + +export default defaultExport; diff --git a/packages/fes-plugin-request/package.json b/packages/fes-plugin-request/package.json index eeb8f6cd1..17354c514 100644 --- a/packages/fes-plugin-request/package.json +++ b/packages/fes-plugin-request/package.json @@ -33,7 +33,7 @@ }, "dependencies": { "@fesjs/utils": "^3.0.1", - "@qlin/request": "^0.2.0" + "@qlin/request": "^0.2.3" }, "typings": "./types.d.ts" } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 017b3039c..1dc4cbec7 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -650,8 +650,8 @@ importers: specifier: ^3.0.1 version: link:../fes-utils '@qlin/request': - specifier: ^0.2.0 - version: 0.2.0 + specifier: ^0.2.3 + version: 0.2.3 vue: specifier: ^3.2.37 version: 3.3.4 @@ -3921,8 +3921,8 @@ packages: resolution: {integrity: sha512-2LuNTFBIO0m7kKIQvvPHN6UE63VjpmL9rnEEaOOaiSPbZK+zUOYIzBAWcED+3XYzhYsd/0mD57VdxAEqqV52CQ==} dev: false - /@qlin/request@0.2.0: - resolution: {integrity: sha512-zbCIHy7wBYy4pz/GarBeDTGmtkT2QfsCqudKQMs6wkhZ/ZMMQzAhO7d+REH89d3cdUPvw7qMnpYXYpHEg2eCeg==} + /@qlin/request@0.2.3: + resolution: {integrity: sha512-haKCRgZZWF9cZF7btRNfZQlDtJQ0uVqjwp51mqT+073V7JbL2DpUmVsRODtb3nnw9uVAHOqS9rmq+ZigwftgGg==} dependencies: '@types/lodash-es': 4.17.12 lodash-es: 4.17.21