Commit b6d5b0796de4d0b66c0f33c335ec991d44f64ef2

Authored by Vben
1 parent f6fe1dd6

feat(axios): added authenticationScheme configuration,fix #774

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