提交 b6d5b079 作者: Vben

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

上级 f6fe1dd6
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
- **Preview** 新增`createImgPreview`图片预览函数 - **Preview** 新增`createImgPreview`图片预览函数
- **Setup** 新增引导页示例 - **Setup** 新增引导页示例
- **Tests** 添加 jest 测试套件,暂不支持 Vue 组件单测 - **Tests** 添加 jest 测试套件,暂不支持 Vue 组件单测
- **Axios** 新增`authenticationScheme`配置,用于指定认证方案
### 🐛 Bug Fixes ### 🐛 Bug Fixes
......
import { UploadApiResult } from './model/uploadModel'; import { UploadApiResult } from './model/uploadModel';
import { defHttp } from '/@/utils/http/axios'; import { defHttp } from '/@/utils/http/axios';
import { UploadFileParams } from '/@/utils/http/axios/types'; import { UploadFileParams } from '/#/axios';
import { useGlobSetting } from '/@/hooks/setting'; import { useGlobSetting } from '/@/hooks/setting';
const { uploadUrl = '' } = useGlobSetting(); const { uploadUrl = '' } = useGlobSetting();
......
import { defHttp } from '/@/utils/http/axios'; import { defHttp } from '/@/utils/http/axios';
import { LoginParams, LoginResultModel, GetUserInfoModel } from './model/userModel'; import { LoginParams, LoginResultModel, GetUserInfoModel } from './model/userModel';
import { ErrorMessageMode } from '/@/utils/http/axios/types'; import { ErrorMessageMode } from '/#/axios';
enum Api { enum Api {
Login = '/login', Login = '/login',
......
import type { UserInfo } from '/#/store'; import type { UserInfo } from '/#/store';
import type { ErrorMessageMode } from '/@/utils/http/axios/types'; import type { ErrorMessageMode } from '/#/axios';
import { defineStore } from 'pinia'; import { defineStore } from 'pinia';
import { store } from '/@/store'; import { store } from '/@/store';
import { RoleEnum } from '/@/enums/roleEnum'; import { RoleEnum } from '/@/enums/roleEnum';
import { PageEnum } from '/@/enums/pageEnum'; import { PageEnum } from '/@/enums/pageEnum';
import { ROLES_KEY, TOKEN_KEY, USER_INFO_KEY } from '/@/enums/cacheEnum'; import { ROLES_KEY, TOKEN_KEY, USER_INFO_KEY } from '/@/enums/cacheEnum';
import { getAuthCache, setAuthCache } from '/@/utils/auth'; import { getAuthCache, setAuthCache } from '/@/utils/auth';
import { GetUserInfoModel, LoginParams } from '/@/api/sys/model/userModel'; import { GetUserInfoModel, LoginParams } from '/@/api/sys/model/userModel';
import { getUserInfo, loginApi } from '/@/api/sys/user'; import { getUserInfo, loginApi } from '/@/api/sys/user';
import { useI18n } from '/@/hooks/web/useI18n'; import { useI18n } from '/@/hooks/web/useI18n';
import { useMessage } from '/@/hooks/web/useMessage'; import { useMessage } from '/@/hooks/web/useMessage';
import { router } from '/@/router'; import { router } from '/@/router';
......
import type { AxiosRequestConfig, AxiosInstance, AxiosResponse } from 'axios'; import type { AxiosRequestConfig, AxiosInstance, AxiosResponse } from 'axios';
import type { RequestOptions, Result, UploadFileParams } from './types'; import type { RequestOptions, Result, UploadFileParams } from '../../../../types/axios';
import type { CreateAxiosOptions } from './axiosTransform'; import type { CreateAxiosOptions } from './axiosTransform';
import axios from 'axios'; import axios from 'axios';
import qs from 'qs'; import qs from 'qs';
import { AxiosCanceler } from './axiosCancel'; import { AxiosCanceler } from './axiosCancel';
import { isFunction } from '/@/utils/is'; import { isFunction } from '/@/utils/is';
import { cloneDeep } from 'lodash-es'; import { cloneDeep } from 'lodash-es';
import { ContentTypeEnum } from '/@/enums/httpEnum'; import { ContentTypeEnum } from '/@/enums/httpEnum';
import { RequestEnum } from '../../../enums/httpEnum'; import { RequestEnum } from '/@/enums/httpEnum';
export * from './axiosTransform'; export * from './axiosTransform';
...@@ -93,7 +91,7 @@ export class VAxios { ...@@ -93,7 +91,7 @@ export class VAxios {
!ignoreCancel && axiosCanceler.addPending(config); !ignoreCancel && axiosCanceler.addPending(config);
if (requestInterceptors && isFunction(requestInterceptors)) { if (requestInterceptors && isFunction(requestInterceptors)) {
config = requestInterceptors(config); config = requestInterceptors(config, this.options);
} }
return config; return config;
}, undefined); }, undefined);
......
import type { AxiosRequestConfig, Canceler } from 'axios'; import type { AxiosRequestConfig, Canceler } from 'axios';
import axios from 'axios'; import axios from 'axios';
import { isFunction } from '/@/utils/is'; import { isFunction } from '/@/utils/is';
// Used to store the identification and cancellation function of each request // Used to store the identification and cancellation function of each request
......
...@@ -2,9 +2,10 @@ ...@@ -2,9 +2,10 @@
* Data processing class, can be configured according to the project * Data processing class, can be configured according to the project
*/ */
import type { AxiosRequestConfig, AxiosResponse } from 'axios'; import type { AxiosRequestConfig, AxiosResponse } from 'axios';
import type { RequestOptions, Result } from './types'; import type { RequestOptions, Result } from '/#/axios';
export interface CreateAxiosOptions extends AxiosRequestConfig { export interface CreateAxiosOptions extends AxiosRequestConfig {
authenticationScheme?: string;
urlPrefix?: string; urlPrefix?: string;
transform?: AxiosTransform; transform?: AxiosTransform;
requestOptions?: RequestOptions; requestOptions?: RequestOptions;
...@@ -30,7 +31,10 @@ export abstract class AxiosTransform { ...@@ -30,7 +31,10 @@ export abstract class AxiosTransform {
/** /**
* @description: 请求之前的拦截器 * @description: 请求之前的拦截器
*/ */
requestInterceptors?: (config: AxiosRequestConfig) => AxiosRequestConfig; requestInterceptors?: (
config: AxiosRequestConfig,
options: CreateAxiosOptions
) => AxiosRequestConfig;
/** /**
* @description: 请求之后的拦截器 * @description: 请求之后的拦截器
......
import type { ErrorMessageMode } from './types'; import type { ErrorMessageMode } from '/#/axios';
import { useMessage } from '/@/hooks/web/useMessage'; import { useMessage } from '/@/hooks/web/useMessage';
import { useI18n } from '/@/hooks/web/useI18n'; import { useI18n } from '/@/hooks/web/useI18n';
// import router from '/@/router'; // import router from '/@/router';
...@@ -9,6 +8,7 @@ import { useUserStoreWidthOut } from '/@/store/modules/user'; ...@@ -9,6 +8,7 @@ import { useUserStoreWidthOut } from '/@/store/modules/user';
const { createMessage, createErrorModal } = useMessage(); const { createMessage, createErrorModal } = useMessage();
const error = createMessage.error!; const error = createMessage.error!;
export function checkStatus( export function checkStatus(
status: number, status: number,
msg: string, msg: string,
......
...@@ -2,22 +2,17 @@ ...@@ -2,22 +2,17 @@
// The axios configuration can be changed according to the project, just change the file, other files can be left unchanged // The axios configuration can be changed according to the project, just change the file, other files can be left unchanged
import type { AxiosResponse } from 'axios'; import type { AxiosResponse } from 'axios';
import type { RequestOptions, Result } from './types'; import type { RequestOptions, Result } from '/#/axios';
import type { AxiosTransform, CreateAxiosOptions } from './axiosTransform'; import type { AxiosTransform, CreateAxiosOptions } from './axiosTransform';
import { VAxios } from './Axios'; import { VAxios } from './Axios';
import { checkStatus } from './checkStatus'; import { checkStatus } from './checkStatus';
import { useGlobSetting } from '/@/hooks/setting'; import { useGlobSetting } from '/@/hooks/setting';
import { useMessage } from '/@/hooks/web/useMessage'; import { useMessage } from '/@/hooks/web/useMessage';
import { RequestEnum, ResultEnum, ContentTypeEnum } from '/@/enums/httpEnum'; import { RequestEnum, ResultEnum, ContentTypeEnum } from '/@/enums/httpEnum';
import { isString } from '/@/utils/is'; import { isString } from '/@/utils/is';
import { getToken } from '/@/utils/auth'; import { getToken } from '/@/utils/auth';
import { setObjToUrlParams, deepMerge } from '/@/utils'; import { setObjToUrlParams, deepMerge } from '/@/utils';
import { useErrorLogStoreWithOut } from '/@/store/modules/errorLog'; import { useErrorLogStoreWithOut } from '/@/store/modules/errorLog';
import { useI18n } from '/@/hooks/web/useI18n'; import { useI18n } from '/@/hooks/web/useI18n';
import { joinTimestamp, formatRequestDate } from './helper'; import { joinTimestamp, formatRequestDate } from './helper';
...@@ -34,14 +29,14 @@ const transform: AxiosTransform = { ...@@ -34,14 +29,14 @@ const transform: AxiosTransform = {
*/ */
transformRequestHook: (res: AxiosResponse<Result>, options: RequestOptions) => { transformRequestHook: (res: AxiosResponse<Result>, options: RequestOptions) => {
const { t } = useI18n(); const { t } = useI18n();
const { isTransformRequestResult, isReturnNativeResponse } = options; const { isTransformResponse, isReturnNativeResponse } = options;
// 是否返回原生响应头 比如:需要获取响应头时使用该属性 // 是否返回原生响应头 比如:需要获取响应头时使用该属性
if (isReturnNativeResponse) { if (isReturnNativeResponse) {
return res; return res;
} }
// 不进行任何处理,直接返回 // 不进行任何处理,直接返回
// 用于页面代码可能需要直接获取code,data,message这些信息时开启 // 用于页面代码可能需要直接获取code,data,message这些信息时开启
if (!isTransformRequestResult) { if (!isTransformResponse) {
return res.data; return res.data;
} }
// 错误的时候返回 // 错误的时候返回
...@@ -124,12 +119,14 @@ const transform: AxiosTransform = { ...@@ -124,12 +119,14 @@ const transform: AxiosTransform = {
/** /**
* @description: 请求拦截器处理 * @description: 请求拦截器处理
*/ */
requestInterceptors: (config) => { requestInterceptors: (config, options) => {
// 请求之前处理config // 请求之前处理config
const token = getToken(); const token = getToken();
if (token) { if (token) {
// jwt token // jwt token
config.headers.Authorization = token; config.headers.Authorization = options.authenticationScheme
? `${options.authenticationScheme} ${token}`
: token;
} }
return config; return config;
}, },
...@@ -183,6 +180,10 @@ function createAxios(opt?: Partial<CreateAxiosOptions>) { ...@@ -183,6 +180,10 @@ function createAxios(opt?: Partial<CreateAxiosOptions>) {
return new VAxios( return new VAxios(
deepMerge( deepMerge(
{ {
// See https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication#authentication_schemes
// authentication schemes,e.g: Bearer
// authenticationScheme: 'Bearer',
authenticationScheme: '',
timeout: 10 * 1000, timeout: 10 * 1000,
// 基础接口地址 // 基础接口地址
// baseURL: globSetting.apiUrl, // baseURL: globSetting.apiUrl,
...@@ -200,7 +201,7 @@ function createAxios(opt?: Partial<CreateAxiosOptions>) { ...@@ -200,7 +201,7 @@ function createAxios(opt?: Partial<CreateAxiosOptions>) {
// 是否返回原生响应头 比如:需要获取响应头时使用该属性 // 是否返回原生响应头 比如:需要获取响应头时使用该属性
isReturnNativeResponse: false, isReturnNativeResponse: false,
// 需要对返回数据进行处理 // 需要对返回数据进行处理
isTransformRequestResult: true, isTransformResponse: true,
// post请求的时候添加参数到url // post请求的时候添加参数到url
joinParamsToUrl: false, joinParamsToUrl: false,
// 格式化提交参数时间 // 格式化提交参数时间
......
...@@ -6,7 +6,7 @@ export interface RequestOptions { ...@@ -6,7 +6,7 @@ export interface RequestOptions {
// Format request parameter time // Format request parameter time
formatDate?: boolean; formatDate?: boolean;
// Whether to process the request result // Whether to process the request result
isTransformRequestResult?: boolean; isTransformResponse?: boolean;
// 是否返回原生响应头 比如:需要获取响应头时使用该属性 // 是否返回原生响应头 比如:需要获取响应头时使用该属性
isReturnNativeResponse?: boolean; isReturnNativeResponse?: boolean;
// Whether to join url // Whether to join url
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论