Commit b6d5b0796de4d0b66c0f33c335ec991d44f64ef2
1 parent
f6fe1dd6
feat(axios): added authenticationScheme configuration,fix #774
Showing
10 changed files
with
28 additions
and
30 deletions
CHANGELOG.zh_CN.md
@@ -16,6 +16,7 @@ | @@ -16,6 +16,7 @@ | ||
16 | - **Preview** 新增`createImgPreview`图片预览函数 | 16 | - **Preview** 新增`createImgPreview`图片预览函数 |
17 | - **Setup** 新增引导页示例 | 17 | - **Setup** 新增引导页示例 |
18 | - **Tests** 添加 jest 测试套件,暂不支持 Vue 组件单测 | 18 | - **Tests** 添加 jest 测试套件,暂不支持 Vue 组件单测 |
19 | +- **Axios** 新增`authenticationScheme`配置,用于指定认证方案 | ||
19 | 20 | ||
20 | ### 🐛 Bug Fixes | 21 | ### 🐛 Bug Fixes |
21 | 22 |
src/api/sys/upload.ts
1 | import { UploadApiResult } from './model/uploadModel'; | 1 | import { UploadApiResult } from './model/uploadModel'; |
2 | import { defHttp } from '/@/utils/http/axios'; | 2 | import { defHttp } from '/@/utils/http/axios'; |
3 | -import { UploadFileParams } from '/@/utils/http/axios/types'; | 3 | +import { UploadFileParams } from '/#/axios'; |
4 | import { useGlobSetting } from '/@/hooks/setting'; | 4 | import { useGlobSetting } from '/@/hooks/setting'; |
5 | 5 | ||
6 | const { uploadUrl = '' } = useGlobSetting(); | 6 | const { uploadUrl = '' } = useGlobSetting(); |
src/api/sys/user.ts
1 | import { defHttp } from '/@/utils/http/axios'; | 1 | import { defHttp } from '/@/utils/http/axios'; |
2 | import { LoginParams, LoginResultModel, GetUserInfoModel } from './model/userModel'; | 2 | import { LoginParams, LoginResultModel, GetUserInfoModel } from './model/userModel'; |
3 | 3 | ||
4 | -import { ErrorMessageMode } from '/@/utils/http/axios/types'; | 4 | +import { ErrorMessageMode } from '/#/axios'; |
5 | 5 | ||
6 | enum Api { | 6 | enum Api { |
7 | Login = '/login', | 7 | Login = '/login', |
src/store/modules/user.ts
1 | import type { UserInfo } from '/#/store'; | 1 | import type { UserInfo } from '/#/store'; |
2 | -import type { ErrorMessageMode } from '/@/utils/http/axios/types'; | ||
3 | - | 2 | +import type { ErrorMessageMode } from '/#/axios'; |
4 | import { defineStore } from 'pinia'; | 3 | import { defineStore } from 'pinia'; |
5 | import { store } from '/@/store'; | 4 | import { store } from '/@/store'; |
6 | - | ||
7 | import { RoleEnum } from '/@/enums/roleEnum'; | 5 | import { RoleEnum } from '/@/enums/roleEnum'; |
8 | import { PageEnum } from '/@/enums/pageEnum'; | 6 | import { PageEnum } from '/@/enums/pageEnum'; |
9 | import { ROLES_KEY, TOKEN_KEY, USER_INFO_KEY } from '/@/enums/cacheEnum'; | 7 | import { ROLES_KEY, TOKEN_KEY, USER_INFO_KEY } from '/@/enums/cacheEnum'; |
10 | - | ||
11 | import { getAuthCache, setAuthCache } from '/@/utils/auth'; | 8 | import { getAuthCache, setAuthCache } from '/@/utils/auth'; |
12 | import { GetUserInfoModel, LoginParams } from '/@/api/sys/model/userModel'; | 9 | import { GetUserInfoModel, LoginParams } from '/@/api/sys/model/userModel'; |
13 | - | ||
14 | import { getUserInfo, loginApi } from '/@/api/sys/user'; | 10 | import { getUserInfo, loginApi } from '/@/api/sys/user'; |
15 | - | ||
16 | import { useI18n } from '/@/hooks/web/useI18n'; | 11 | import { useI18n } from '/@/hooks/web/useI18n'; |
17 | import { useMessage } from '/@/hooks/web/useMessage'; | 12 | import { useMessage } from '/@/hooks/web/useMessage'; |
18 | import { router } from '/@/router'; | 13 | import { router } from '/@/router'; |
src/utils/http/axios/Axios.ts
1 | import type { AxiosRequestConfig, AxiosInstance, AxiosResponse } from 'axios'; | 1 | import type { AxiosRequestConfig, AxiosInstance, AxiosResponse } from 'axios'; |
2 | -import type { RequestOptions, Result, UploadFileParams } from './types'; | 2 | +import type { RequestOptions, Result, UploadFileParams } from '../../../../types/axios'; |
3 | import type { CreateAxiosOptions } from './axiosTransform'; | 3 | import type { CreateAxiosOptions } from './axiosTransform'; |
4 | - | ||
5 | import axios from 'axios'; | 4 | import axios from 'axios'; |
6 | import qs from 'qs'; | 5 | import qs from 'qs'; |
7 | import { AxiosCanceler } from './axiosCancel'; | 6 | import { AxiosCanceler } from './axiosCancel'; |
8 | import { isFunction } from '/@/utils/is'; | 7 | import { isFunction } from '/@/utils/is'; |
9 | import { cloneDeep } from 'lodash-es'; | 8 | import { cloneDeep } from 'lodash-es'; |
10 | - | ||
11 | import { ContentTypeEnum } from '/@/enums/httpEnum'; | 9 | import { ContentTypeEnum } from '/@/enums/httpEnum'; |
12 | -import { RequestEnum } from '../../../enums/httpEnum'; | 10 | +import { RequestEnum } from '/@/enums/httpEnum'; |
13 | 11 | ||
14 | export * from './axiosTransform'; | 12 | export * from './axiosTransform'; |
15 | 13 | ||
@@ -93,7 +91,7 @@ export class VAxios { | @@ -93,7 +91,7 @@ export class VAxios { | ||
93 | 91 | ||
94 | !ignoreCancel && axiosCanceler.addPending(config); | 92 | !ignoreCancel && axiosCanceler.addPending(config); |
95 | if (requestInterceptors && isFunction(requestInterceptors)) { | 93 | if (requestInterceptors && isFunction(requestInterceptors)) { |
96 | - config = requestInterceptors(config); | 94 | + config = requestInterceptors(config, this.options); |
97 | } | 95 | } |
98 | return config; | 96 | return config; |
99 | }, undefined); | 97 | }, undefined); |
src/utils/http/axios/axiosCancel.ts
1 | import type { AxiosRequestConfig, Canceler } from 'axios'; | 1 | import type { AxiosRequestConfig, Canceler } from 'axios'; |
2 | import axios from 'axios'; | 2 | import axios from 'axios'; |
3 | - | ||
4 | import { isFunction } from '/@/utils/is'; | 3 | import { isFunction } from '/@/utils/is'; |
5 | 4 | ||
6 | // Used to store the identification and cancellation function of each request | 5 | // Used to store the identification and cancellation function of each request |
src/utils/http/axios/axiosTransform.ts
@@ -2,9 +2,10 @@ | @@ -2,9 +2,10 @@ | ||
2 | * Data processing class, can be configured according to the project | 2 | * Data processing class, can be configured according to the project |
3 | */ | 3 | */ |
4 | import type { AxiosRequestConfig, AxiosResponse } from 'axios'; | 4 | import type { AxiosRequestConfig, AxiosResponse } from 'axios'; |
5 | -import type { RequestOptions, Result } from './types'; | 5 | +import type { RequestOptions, Result } from '/#/axios'; |
6 | 6 | ||
7 | export interface CreateAxiosOptions extends AxiosRequestConfig { | 7 | export interface CreateAxiosOptions extends AxiosRequestConfig { |
8 | + authenticationScheme?: string; | ||
8 | urlPrefix?: string; | 9 | urlPrefix?: string; |
9 | transform?: AxiosTransform; | 10 | transform?: AxiosTransform; |
10 | requestOptions?: RequestOptions; | 11 | requestOptions?: RequestOptions; |
@@ -30,7 +31,10 @@ export abstract class AxiosTransform { | @@ -30,7 +31,10 @@ export abstract class AxiosTransform { | ||
30 | /** | 31 | /** |
31 | * @description: 请求之前的拦截器 | 32 | * @description: 请求之前的拦截器 |
32 | */ | 33 | */ |
33 | - requestInterceptors?: (config: AxiosRequestConfig) => AxiosRequestConfig; | 34 | + requestInterceptors?: ( |
35 | + config: AxiosRequestConfig, | ||
36 | + options: CreateAxiosOptions | ||
37 | + ) => AxiosRequestConfig; | ||
34 | 38 | ||
35 | /** | 39 | /** |
36 | * @description: 请求之后的拦截器 | 40 | * @description: 请求之后的拦截器 |
src/utils/http/axios/checkStatus.ts
1 | -import type { ErrorMessageMode } from './types'; | ||
2 | - | 1 | +import type { ErrorMessageMode } from '/#/axios'; |
3 | import { useMessage } from '/@/hooks/web/useMessage'; | 2 | import { useMessage } from '/@/hooks/web/useMessage'; |
4 | import { useI18n } from '/@/hooks/web/useI18n'; | 3 | import { useI18n } from '/@/hooks/web/useI18n'; |
5 | // import router from '/@/router'; | 4 | // import router from '/@/router'; |
@@ -9,6 +8,7 @@ import { useUserStoreWidthOut } from '/@/store/modules/user'; | @@ -9,6 +8,7 @@ import { useUserStoreWidthOut } from '/@/store/modules/user'; | ||
9 | const { createMessage, createErrorModal } = useMessage(); | 8 | const { createMessage, createErrorModal } = useMessage(); |
10 | 9 | ||
11 | const error = createMessage.error!; | 10 | const error = createMessage.error!; |
11 | + | ||
12 | export function checkStatus( | 12 | export function checkStatus( |
13 | status: number, | 13 | status: number, |
14 | msg: string, | 14 | msg: string, |
src/utils/http/axios/index.ts
@@ -2,22 +2,17 @@ | @@ -2,22 +2,17 @@ | ||
2 | // The axios configuration can be changed according to the project, just change the file, other files can be left unchanged | 2 | // The axios configuration can be changed according to the project, just change the file, other files can be left unchanged |
3 | 3 | ||
4 | import type { AxiosResponse } from 'axios'; | 4 | import type { AxiosResponse } from 'axios'; |
5 | -import type { RequestOptions, Result } from './types'; | 5 | +import type { RequestOptions, Result } from '/#/axios'; |
6 | import type { AxiosTransform, CreateAxiosOptions } from './axiosTransform'; | 6 | import type { AxiosTransform, CreateAxiosOptions } from './axiosTransform'; |
7 | - | ||
8 | import { VAxios } from './Axios'; | 7 | import { VAxios } from './Axios'; |
9 | import { checkStatus } from './checkStatus'; | 8 | import { checkStatus } from './checkStatus'; |
10 | - | ||
11 | import { useGlobSetting } from '/@/hooks/setting'; | 9 | import { useGlobSetting } from '/@/hooks/setting'; |
12 | import { useMessage } from '/@/hooks/web/useMessage'; | 10 | import { useMessage } from '/@/hooks/web/useMessage'; |
13 | - | ||
14 | import { RequestEnum, ResultEnum, ContentTypeEnum } from '/@/enums/httpEnum'; | 11 | import { RequestEnum, ResultEnum, ContentTypeEnum } from '/@/enums/httpEnum'; |
15 | - | ||
16 | import { isString } from '/@/utils/is'; | 12 | import { isString } from '/@/utils/is'; |
17 | import { getToken } from '/@/utils/auth'; | 13 | import { getToken } from '/@/utils/auth'; |
18 | import { setObjToUrlParams, deepMerge } from '/@/utils'; | 14 | import { setObjToUrlParams, deepMerge } from '/@/utils'; |
19 | import { useErrorLogStoreWithOut } from '/@/store/modules/errorLog'; | 15 | import { useErrorLogStoreWithOut } from '/@/store/modules/errorLog'; |
20 | - | ||
21 | import { useI18n } from '/@/hooks/web/useI18n'; | 16 | import { useI18n } from '/@/hooks/web/useI18n'; |
22 | import { joinTimestamp, formatRequestDate } from './helper'; | 17 | import { joinTimestamp, formatRequestDate } from './helper'; |
23 | 18 | ||
@@ -34,14 +29,14 @@ const transform: AxiosTransform = { | @@ -34,14 +29,14 @@ const transform: AxiosTransform = { | ||
34 | */ | 29 | */ |
35 | transformRequestHook: (res: AxiosResponse<Result>, options: RequestOptions) => { | 30 | transformRequestHook: (res: AxiosResponse<Result>, options: RequestOptions) => { |
36 | const { t } = useI18n(); | 31 | const { t } = useI18n(); |
37 | - const { isTransformRequestResult, isReturnNativeResponse } = options; | 32 | + const { isTransformResponse, isReturnNativeResponse } = options; |
38 | // 是否返回原生响应头 比如:需要获取响应头时使用该属性 | 33 | // 是否返回原生响应头 比如:需要获取响应头时使用该属性 |
39 | if (isReturnNativeResponse) { | 34 | if (isReturnNativeResponse) { |
40 | return res; | 35 | return res; |
41 | } | 36 | } |
42 | // 不进行任何处理,直接返回 | 37 | // 不进行任何处理,直接返回 |
43 | // 用于页面代码可能需要直接获取code,data,message这些信息时开启 | 38 | // 用于页面代码可能需要直接获取code,data,message这些信息时开启 |
44 | - if (!isTransformRequestResult) { | 39 | + if (!isTransformResponse) { |
45 | return res.data; | 40 | return res.data; |
46 | } | 41 | } |
47 | // 错误的时候返回 | 42 | // 错误的时候返回 |
@@ -124,12 +119,14 @@ const transform: AxiosTransform = { | @@ -124,12 +119,14 @@ const transform: AxiosTransform = { | ||
124 | /** | 119 | /** |
125 | * @description: 请求拦截器处理 | 120 | * @description: 请求拦截器处理 |
126 | */ | 121 | */ |
127 | - requestInterceptors: (config) => { | 122 | + requestInterceptors: (config, options) => { |
128 | // 请求之前处理config | 123 | // 请求之前处理config |
129 | const token = getToken(); | 124 | const token = getToken(); |
130 | if (token) { | 125 | if (token) { |
131 | // jwt token | 126 | // jwt token |
132 | - config.headers.Authorization = token; | 127 | + config.headers.Authorization = options.authenticationScheme |
128 | + ? `${options.authenticationScheme} ${token}` | ||
129 | + : token; | ||
133 | } | 130 | } |
134 | return config; | 131 | return config; |
135 | }, | 132 | }, |
@@ -183,6 +180,10 @@ function createAxios(opt?: Partial<CreateAxiosOptions>) { | @@ -183,6 +180,10 @@ function createAxios(opt?: Partial<CreateAxiosOptions>) { | ||
183 | return new VAxios( | 180 | return new VAxios( |
184 | deepMerge( | 181 | deepMerge( |
185 | { | 182 | { |
183 | + // See https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication#authentication_schemes | ||
184 | + // authentication schemes,e.g: Bearer | ||
185 | + // authenticationScheme: 'Bearer', | ||
186 | + authenticationScheme: '', | ||
186 | timeout: 10 * 1000, | 187 | timeout: 10 * 1000, |
187 | // 基础接口地址 | 188 | // 基础接口地址 |
188 | // baseURL: globSetting.apiUrl, | 189 | // baseURL: globSetting.apiUrl, |
@@ -200,7 +201,7 @@ function createAxios(opt?: Partial<CreateAxiosOptions>) { | @@ -200,7 +201,7 @@ function createAxios(opt?: Partial<CreateAxiosOptions>) { | ||
200 | // 是否返回原生响应头 比如:需要获取响应头时使用该属性 | 201 | // 是否返回原生响应头 比如:需要获取响应头时使用该属性 |
201 | isReturnNativeResponse: false, | 202 | isReturnNativeResponse: false, |
202 | // 需要对返回数据进行处理 | 203 | // 需要对返回数据进行处理 |
203 | - isTransformRequestResult: true, | 204 | + isTransformResponse: true, |
204 | // post请求的时候添加参数到url | 205 | // post请求的时候添加参数到url |
205 | joinParamsToUrl: false, | 206 | joinParamsToUrl: false, |
206 | // 格式化提交参数时间 | 207 | // 格式化提交参数时间 |
src/utils/http/axios/types.ts renamed to types/axios.d.ts
@@ -6,7 +6,7 @@ export interface RequestOptions { | @@ -6,7 +6,7 @@ export interface RequestOptions { | ||
6 | // Format request parameter time | 6 | // Format request parameter time |
7 | formatDate?: boolean; | 7 | formatDate?: boolean; |
8 | // Whether to process the request result | 8 | // Whether to process the request result |
9 | - isTransformRequestResult?: boolean; | 9 | + isTransformResponse?: boolean; |
10 | // 是否返回原生响应头 比如:需要获取响应头时使用该属性 | 10 | // 是否返回原生响应头 比如:需要获取响应头时使用该属性 |
11 | isReturnNativeResponse?: boolean; | 11 | isReturnNativeResponse?: boolean; |
12 | // Whether to join url | 12 | // Whether to join url |