Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
一臭打代码的 aka 潘先生です
vue-vben-admin
提交
a821d9a3
V
vue-vben-admin
项目概览
一臭打代码的 aka 潘先生です
/
vue-vben-admin
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
V
vue-vben-admin
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
a821d9a3
编写于
2月 26, 2021
作者:
V
Vben
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
perf: imporve axios logic
上级
7c2f8516
变更
13
隐藏空白更改
内联
并排
Showing
13 changed file
with
77 addition
and
74 deletion
+77
-74
src/api/demo/account.ts
src/api/demo/account.ts
+4
-6
src/api/demo/error.ts
src/api/demo/error.ts
+4
-6
src/api/demo/select.ts
src/api/demo/select.ts
+2
-6
src/api/demo/table.ts
src/api/demo/table.ts
+5
-4
src/api/sys/menu.ts
src/api/sys/menu.ts
+6
-8
src/api/sys/user.ts
src/api/sys/user.ts
+4
-6
src/utils/http/axios/Axios.ts
src/utils/http/axios/Axios.ts
+21
-5
src/utils/http/axios/axiosCancel.ts
src/utils/http/axios/axiosCancel.ts
+8
-7
src/utils/http/axios/axiosTransform.ts
src/utils/http/axios/axiosTransform.ts
+5
-5
src/utils/http/axios/const.ts
src/utils/http/axios/const.ts
+1
-1
src/utils/http/axios/helper.ts
src/utils/http/axios/helper.ts
+2
-5
src/utils/http/axios/index.ts
src/utils/http/axios/index.ts
+1
-1
src/utils/http/axios/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
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录