Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
B
basic-vue-admin
概览
概览
详情
活动
周期分析
版本库
存储库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
Basic
basic-vue-admin
Commits
b6d5b079
提交
b6d5b079
authored
6月 17, 2021
作者:
Vben
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat(axios): added authenticationScheme configuration,fix #774
上级
f6fe1dd6
隐藏空白字符变更
内嵌
并排
正在显示
10 个修改的文件
包含
28 行增加
和
30 行删除
+28
-30
CHANGELOG.zh_CN.md
CHANGELOG.zh_CN.md
+1
-0
upload.ts
src/api/sys/upload.ts
+1
-1
user.ts
src/api/sys/user.ts
+1
-1
user.ts
src/store/modules/user.ts
+1
-6
Axios.ts
src/utils/http/axios/Axios.ts
+3
-5
axiosCancel.ts
src/utils/http/axios/axiosCancel.ts
+0
-1
axiosTransform.ts
src/utils/http/axios/axiosTransform.ts
+6
-2
checkStatus.ts
src/utils/http/axios/checkStatus.ts
+2
-2
index.ts
src/utils/http/axios/index.ts
+12
-11
axios.d.ts
types/axios.d.ts
+1
-1
没有找到文件。
CHANGELOG.zh_CN.md
浏览文件 @
b6d5b079
...
@@ -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
...
...
src/api/sys/upload.ts
浏览文件 @
b6d5b079
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/type
s'
;
import
{
UploadFileParams
}
from
'/
#/axio
s'
;
import
{
useGlobSetting
}
from
'/@/hooks/setting'
;
import
{
useGlobSetting
}
from
'/@/hooks/setting'
;
const
{
uploadUrl
=
''
}
=
useGlobSetting
();
const
{
uploadUrl
=
''
}
=
useGlobSetting
();
...
...
src/api/sys/user.ts
浏览文件 @
b6d5b079
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/type
s'
;
import
{
ErrorMessageMode
}
from
'/
#/axio
s'
;
enum
Api
{
enum
Api
{
Login
=
'/login'
,
Login
=
'/login'
,
...
...
src/store/modules/user.ts
浏览文件 @
b6d5b079
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'
;
...
...
src/utils/http/axios/Axios.ts
浏览文件 @
b6d5b079
import
type
{
AxiosRequestConfig
,
AxiosInstance
,
AxiosResponse
}
from
'axios'
;
import
type
{
AxiosRequestConfig
,
AxiosInstance
,
AxiosResponse
}
from
'axios'
;
import
type
{
RequestOptions
,
Result
,
UploadFileParams
}
from
'.
/type
s'
;
import
type
{
RequestOptions
,
Result
,
UploadFileParams
}
from
'.
./../../../types/axio
s'
;
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
);
...
...
src/utils/http/axios/axiosCancel.ts
浏览文件 @
b6d5b079
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
...
...
src/utils/http/axios/axiosTransform.ts
浏览文件 @
b6d5b079
...
@@ -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
'
./type
s'
;
import
type
{
RequestOptions
,
Result
}
from
'
/#/axio
s'
;
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: 请求之后的拦截器
...
...
src/utils/http/axios/checkStatus.ts
浏览文件 @
b6d5b079
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
,
...
...
src/utils/http/axios/index.ts
浏览文件 @
b6d5b079
...
@@ -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
'
./type
s'
;
import
type
{
RequestOptions
,
Result
}
from
'
/#/axio
s'
;
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
{
isTransformRe
questResult
,
isReturnNativeResponse
}
=
options
;
const
{
isTransformRe
sponse
,
isReturnNativeResponse
}
=
options
;
// 是否返回原生响应头 比如:需要获取响应头时使用该属性
// 是否返回原生响应头 比如:需要获取响应头时使用该属性
if
(
isReturnNativeResponse
)
{
if
(
isReturnNativeResponse
)
{
return
res
;
return
res
;
}
}
// 不进行任何处理,直接返回
// 不进行任何处理,直接返回
// 用于页面代码可能需要直接获取code,data,message这些信息时开启
// 用于页面代码可能需要直接获取code,data,message这些信息时开启
if
(
!
isTransformRe
questResult
)
{
if
(
!
isTransformRe
sponse
)
{
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
,
// 需要对返回数据进行处理
// 需要对返回数据进行处理
isTransformRe
questResult
:
true
,
isTransformRe
sponse
:
true
,
// post请求的时候添加参数到url
// post请求的时候添加参数到url
joinParamsToUrl
:
false
,
joinParamsToUrl
:
false
,
// 格式化提交参数时间
// 格式化提交参数时间
...
...
src/utils/http/axios/types
.ts
→
types/axios.d
.ts
浏览文件 @
b6d5b079
...
@@ -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
isTransformRe
questResult
?:
boolean
;
isTransformRe
sponse
?:
boolean
;
// 是否返回原生响应头 比如:需要获取响应头时使用该属性
// 是否返回原生响应头 比如:需要获取响应头时使用该属性
isReturnNativeResponse
?:
boolean
;
isReturnNativeResponse
?:
boolean
;
// Whether to join url
// Whether to join url
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论