Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
B
basic-vue-admin
概览
概览
详情
活动
周期分析
版本库
存储库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
Basic
basic-vue-admin
Commits
a821d9a3
提交
a821d9a3
authored
2月 26, 2021
作者:
Vben
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
perf: imporve axios logic
上级
7c2f8516
隐藏空白字符变更
内嵌
并排
正在显示
13 个修改的文件
包含
77 行增加
和
74 行删除
+77
-74
account.ts
src/api/demo/account.ts
+4
-6
error.ts
src/api/demo/error.ts
+4
-6
select.ts
src/api/demo/select.ts
+2
-6
table.ts
src/api/demo/table.ts
+5
-4
menu.ts
src/api/sys/menu.ts
+6
-8
user.ts
src/api/sys/user.ts
+4
-6
Axios.ts
src/utils/http/axios/Axios.ts
+21
-5
axiosCancel.ts
src/utils/http/axios/axiosCancel.ts
+8
-7
axiosTransform.ts
src/utils/http/axios/axiosTransform.ts
+5
-5
const.ts
src/utils/http/axios/const.ts
+1
-1
helper.ts
src/utils/http/axios/helper.ts
+2
-5
index.ts
src/utils/http/axios/index.ts
+1
-1
types.ts
src/utils/http/axios/types.ts
+14
-14
没有找到文件。
src/api/demo/account.ts
浏览文件 @
a821d9a3
import
{
defHttp
}
from
'/@/utils/http/axios'
;
import
{
GetAccountInfoModel
}
from
'./model/accountModel'
;
const
{
get
}
=
defHttp
;
enum
Api
{
ACCOUNT_INFO
=
'/account/getAccountInfo'
,
}
// Get personal center-basic settings
export
function
accountInfoApi
()
{
return
defHttp
.
request
<
GetAccountInfoModel
>
({
url
:
Api
.
ACCOUNT_INFO
,
method
:
'GET'
,
});
}
export
const
accountInfoApi
=
()
=>
get
<
GetAccountInfoModel
>
({
url
:
Api
.
ACCOUNT_INFO
});
src/api/demo/error.ts
浏览文件 @
a821d9a3
import
{
defHttp
}
from
'/@/utils/http/axios'
;
const
{
get
}
=
defHttp
;
enum
Api
{
// The address does not exist
Error
=
'/error'
,
...
...
@@ -8,9 +10,5 @@ enum Api {
/**
* @description: Trigger ajax error
*/
export
function
fireErrorApi
()
{
return
defHttp
.
request
({
url
:
Api
.
Error
,
method
:
'GET'
,
});
}
export
const
fireErrorApi
=
()
=>
get
({
url
:
Api
.
Error
});
src/api/demo/select.ts
浏览文件 @
a821d9a3
import
{
defHttp
}
from
'/@/utils/http/axios'
;
import
{
DemoOptionsGetResultModel
}
from
'./model/optionsModel'
;
const
{
get
}
=
defHttp
;
enum
Api
{
OPTIONS_LIST
=
'/select/getDemoOptions'
,
...
...
@@ -8,9 +9,4 @@ enum Api {
/**
* @description: Get sample options value
*/
export
function
optionsListApi
()
{
return
defHttp
.
request
<
DemoOptionsGetResultModel
>
({
url
:
Api
.
OPTIONS_LIST
,
method
:
'GET'
,
});
}
export
const
optionsListApi
=
()
=>
get
<
DemoOptionsGetResultModel
>
({
url
:
Api
.
OPTIONS_LIST
});
src/api/demo/table.ts
浏览文件 @
a821d9a3
import
{
defHttp
}
from
'/@/utils/http/axios'
;
import
{
DemoParams
,
DemoListGetResultModel
}
from
'./model/tableModel'
;
const
{
get
}
=
defHttp
;
enum
Api
{
DEMO_LIST
=
'/table/getDemoList'
,
}
...
...
@@ -8,13 +10,12 @@ enum Api {
/**
* @description: Get sample list value
*/
export
function
demoListApi
(
params
:
DemoParams
)
{
return
defHttp
.
request
<
DemoListGetResultModel
>
({
export
const
demoListApi
=
(
params
:
DemoParams
)
=>
get
<
DemoListGetResultModel
>
({
url
:
Api
.
DEMO_LIST
,
method
:
'GET'
,
params
,
headers
:
{
ignoreCancelToken
:
true
,
},
});
}
src/api/sys/menu.ts
浏览文件 @
a821d9a3
import
{
defHttp
}
from
'/@/utils/http/axios'
;
import
{
getMenuListByIdParams
,
getMenuListByIdParamsResultModel
}
from
'./model/menuModel'
;
const
{
get
}
=
defHttp
;
enum
Api
{
GetMenuListById
=
'/getMenuListById'
,
}
...
...
@@ -9,10 +10,7 @@ enum Api {
/**
* @description: Get user menu based on id
*/
export
function
getMenuListById
(
params
:
getMenuListByIdParams
)
{
return
defHttp
.
request
<
getMenuListByIdParamsResultModel
>
({
url
:
Api
.
GetMenuListById
,
method
:
'GET'
,
params
,
});
}
export
const
getMenuListById
=
(
params
:
getMenuListByIdParams
)
=>
{
return
get
<
getMenuListByIdParamsResultModel
>
({
url
:
Api
.
GetMenuListById
,
params
});
};
src/api/sys/user.ts
浏览文件 @
a821d9a3
...
...
@@ -7,6 +7,7 @@ import {
}
from
'./model/userModel'
;
import
{
ErrorMessageMode
}
from
'/@/utils/http/axios/types'
;
const
{
post
,
get
}
=
defHttp
;
enum
Api
{
Login
=
'/login'
,
GetUserInfoById
=
'/getUserInfoById'
,
...
...
@@ -17,10 +18,9 @@ enum Api {
* @description: user login api
*/
export
function
loginApi
(
params
:
LoginParams
,
mode
:
ErrorMessageMode
=
'modal'
)
{
return
defHttp
.
reque
st
<
LoginResultModel
>
(
return
po
st
<
LoginResultModel
>
(
{
url
:
Api
.
Login
,
method
:
'POST'
,
params
,
},
{
...
...
@@ -33,17 +33,15 @@ export function loginApi(params: LoginParams, mode: ErrorMessageMode = 'modal')
* @description: getUserInfoById
*/
export
function
getUserInfoById
(
params
:
GetUserInfoByUserIdParams
)
{
return
defHttp
.
reques
t
<
GetUserInfoByUserIdModel
>
({
return
ge
t
<
GetUserInfoByUserIdModel
>
({
url
:
Api
.
GetUserInfoById
,
method
:
'GET'
,
params
,
});
}
export
function
getPermCodeByUserId
(
params
:
GetUserInfoByUserIdParams
)
{
return
defHttp
.
reques
t
<
string
[]
>
({
return
ge
t
<
string
[]
>
({
url
:
Api
.
GetPermCodeByUserId
,
method
:
'GET'
,
params
,
});
}
src/utils/http/axios/Axios.ts
浏览文件 @
a821d9a3
...
...
@@ -165,6 +165,22 @@ export class VAxios {
};
}
get
<
T
=
any
>
(
config
:
AxiosRequestConfig
,
options
?:
RequestOptions
):
Promise
<
T
>
{
return
this
.
request
({
...
config
,
method
:
'GET'
},
options
);
}
post
<
T
=
any
>
(
config
:
AxiosRequestConfig
,
options
?:
RequestOptions
):
Promise
<
T
>
{
return
this
.
request
({
...
config
,
method
:
'POST'
},
options
);
}
put
<
T
=
any
>
(
config
:
AxiosRequestConfig
,
options
?:
RequestOptions
):
Promise
<
T
>
{
return
this
.
request
({
...
config
,
method
:
'PUT'
},
options
);
}
delete
<
T
=
any
>
(
config
:
AxiosRequestConfig
,
options
?:
RequestOptions
):
Promise
<
T
>
{
return
this
.
request
({
...
config
,
method
:
'DELETE'
},
options
);
}
request
<
T
=
any
>
(
config
:
AxiosRequestConfig
,
options
?:
RequestOptions
):
Promise
<
T
>
{
let
conf
:
AxiosRequestConfig
=
cloneDeep
(
config
);
const
transform
=
this
.
getTransform
();
...
...
@@ -173,7 +189,7 @@ export class VAxios {
const
opt
:
RequestOptions
=
Object
.
assign
({},
requestOptions
,
options
);
const
{
beforeRequestHook
,
requestCatch
,
transformRequestData
}
=
transform
||
{};
const
{
beforeRequestHook
,
requestCatch
Hook
,
transformRequestHook
}
=
transform
||
{};
if
(
beforeRequestHook
&&
isFunction
(
beforeRequestHook
))
{
conf
=
beforeRequestHook
(
conf
,
opt
);
}
...
...
@@ -183,16 +199,16 @@ export class VAxios {
this
.
axiosInstance
.
request
<
any
,
AxiosResponse
<
Result
>>
(
conf
)
.
then
((
res
:
AxiosResponse
<
Result
>
)
=>
{
if
(
transformRequest
Data
&&
isFunction
(
transformRequestData
))
{
const
ret
=
transformRequest
Data
(
res
,
opt
);
if
(
transformRequest
Hook
&&
isFunction
(
transformRequestHook
))
{
const
ret
=
transformRequest
Hook
(
res
,
opt
);
ret
!==
errorResult
?
resolve
(
ret
)
:
reject
(
new
Error
(
'request error!'
));
return
;
}
resolve
((
res
as
unknown
)
as
Promise
<
T
>
);
})
.
catch
((
e
:
Error
)
=>
{
if
(
requestCatch
&&
isFunction
(
requestCatch
))
{
reject
(
requestCatch
(
e
));
if
(
requestCatch
Hook
&&
isFunction
(
requestCatchHook
))
{
reject
(
requestCatch
Hook
(
e
));
return
;
}
reject
(
e
);
...
...
src/utils/http/axios/axiosCancel.ts
浏览文件 @
a821d9a3
...
...
@@ -3,14 +3,14 @@ import axios from 'axios';
import
{
isFunction
}
from
'/@/utils/is'
;
//
声明一个 Map 用于存储每个请求的标识 和 取消函数
//
Used to store the identification and cancellation function of each request
let
pendingMap
=
new
Map
<
string
,
Canceler
>
();
export
const
getPendingUrl
=
(
config
:
AxiosRequestConfig
)
=>
[
config
.
method
,
config
.
url
].
join
(
'&'
);
export
class
AxiosCanceler
{
/**
*
添加请求
*
Add request
* @param {Object} config
*/
addPending
(
config
:
AxiosRequestConfig
)
{
...
...
@@ -20,14 +20,14 @@ export class AxiosCanceler {
config
.
cancelToken
||
new
axios
.
CancelToken
((
cancel
)
=>
{
if
(
!
pendingMap
.
has
(
url
))
{
//
如果 pending 中不存在当前请求,则添加进去
//
If there is no current request in pending, add it
pendingMap
.
set
(
url
,
cancel
);
}
});
}
/**
* @description:
清空所有
pending
* @description:
Clear all
pending
*/
removeAllPending
()
{
pendingMap
.
forEach
((
cancel
)
=>
{
...
...
@@ -37,14 +37,15 @@ export class AxiosCanceler {
}
/**
*
移除请求
*
Removal request
* @param {Object} config
*/
removePending
(
config
:
AxiosRequestConfig
)
{
const
url
=
getPendingUrl
(
config
);
if
(
pendingMap
.
has
(
url
))
{
// 如果在 pending 中存在当前请求标识,需要取消当前请求,并且移除
// If there is a current request identifier in pending,
// the current request needs to be cancelled and removed
const
cancel
=
pendingMap
.
get
(
url
);
cancel
&&
cancel
(
url
);
pendingMap
.
delete
(
url
);
...
...
@@ -52,7 +53,7 @@ export class AxiosCanceler {
}
/**
* @description:
重置
* @description:
reset
*/
reset
():
void
{
pendingMap
=
new
Map
<
string
,
Canceler
>
();
...
...
src/utils/http/axios/axiosTransform.ts
浏览文件 @
a821d9a3
/**
*
数据处理类,可以根据项目自行配置
*
Data processing class, can be configured according to the project
*/
import
type
{
AxiosRequestConfig
,
AxiosResponse
}
from
'axios'
;
import
type
{
RequestOptions
,
Result
}
from
'./types'
;
export
abstract
class
AxiosTransform
{
/**
* @description:
请求之前处理配置
* @description:
Process configuration before request
* @description: Process configuration before request
*/
beforeRequestHook
?:
(
config
:
AxiosRequestConfig
,
options
:
RequestOptions
)
=>
AxiosRequestConfig
;
/**
* @description:
请求成功处理
* @description:
Request successfully processed
*/
transformRequest
Data
?:
(
res
:
AxiosResponse
<
Result
>
,
options
:
RequestOptions
)
=>
any
;
transformRequest
Hook
?:
(
res
:
AxiosResponse
<
Result
>
,
options
:
RequestOptions
)
=>
any
;
/**
* @description: 请求失败处理
*/
requestCatch
?:
(
e
:
Error
)
=>
Promise
<
any
>
;
requestCatch
Hook
?:
(
e
:
Error
)
=>
Promise
<
any
>
;
/**
* @description: 请求之前的拦截器
...
...
src/utils/http/axios/const.ts
浏览文件 @
a821d9a3
//
接口返回值data不能为这个,否则会判为请求失败
//
The interface return value data cannot be this, otherwise the request will be judged as a failure
export
const
errorResult
=
'__ERROR_RESULT__'
;
src/utils/http/axios/helper.ts
浏览文件 @
a821d9a3
...
...
@@ -13,15 +13,12 @@ export function createNow(join: boolean, restful = false): string | object {
if
(
restful
)
{
return
`?_t=
${
now
}
`
;
}
return
{
_t
:
now
,
};
return
{
_t
:
now
};
}
const
DATE_TIME_FORMAT
=
'YYYY-MM-DD HH:mm'
;
/**
* @description:
格式化请求参数时间
* @description:
Format request parameter time
*/
export
function
formatRequestDate
(
params
:
any
)
{
for
(
const
key
in
params
)
{
...
...
src/utils/http/axios/index.ts
浏览文件 @
a821d9a3
...
...
@@ -32,7 +32,7 @@ const transform: AxiosTransform = {
/**
* @description: 处理请求数据
*/
transformRequest
Data
:
(
res
:
AxiosResponse
<
Result
>
,
options
:
RequestOptions
)
=>
{
transformRequest
Hook
:
(
res
:
AxiosResponse
<
Result
>
,
options
:
RequestOptions
)
=>
{
const
{
t
}
=
useI18n
();
const
{
isTransformRequestResult
}
=
options
;
// 不进行任何处理,直接返回
...
...
src/utils/http/axios/types.ts
浏览文件 @
a821d9a3
import
type
{
AxiosRequestConfig
}
from
'axios'
;
import
{
AxiosTransform
}
from
'./axiosTransform'
;
import
type
{
AxiosTransform
}
from
'./axiosTransform'
;
export
type
ErrorMessageMode
=
'none'
|
'modal'
|
'message'
|
undefined
;
export
interface
RequestOptions
{
//
请求参数拼接到
url
//
Splicing request parameters to
url
joinParamsToUrl
?:
boolean
;
//
格式化请求参数时间
//
Format request parameter time
formatDate
?:
boolean
;
//
是否处理请求结果
//
Whether to process the request result
isTransformRequestResult
?:
boolean
;
//
是否加入
url
//
Whether to join
url
joinPrefix
?:
boolean
;
//
接口地址, 不填则使用默认apiUrl
//
Interface address, use the default apiUrl if you leave it blank
apiUrl
?:
string
;
//
错误消息提示类型
//
Error message prompt type
errorMessageMode
?:
ErrorMessageMode
;
//
是否加入时间戳
//
Whether to add a timestamp
joinTime
?:
boolean
;
}
...
...
@@ -32,15 +31,16 @@ export interface Result<T = any> {
message
:
string
;
result
:
T
;
}
// multipart/form-data:上传文件
// multipart/form-data: upload file
export
interface
UploadFileParams
{
//
其他参数
//
Other parameters
data
?:
Indexable
;
//
文件参数的接口字段名
//
File parameter interface field name
name
?:
string
;
//
文件
//
file name
file
:
File
|
Blob
;
//
文件名
//
file name
filename
?:
string
;
[
key
:
string
]:
any
;
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论