Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
uuai
test_git
提交
8679ce08
T
test_git
项目概览
uuai
/
test_git
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
T
test_git
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
8679ce08
编写于
3月 16, 2023
作者:
W
wuyb@phxg.cn
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
vite 路由
上级
426d9ae9
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
289 addition
and
0 deletion
+289
-0
vite-demo/src/utils/axios/axios.js
vite-demo/src/utils/axios/axios.js
+289
-0
未找到文件。
vite-demo/src/utils/axios/axios.js
0 → 100644
浏览文件 @
8679ce08
import
axios
from
'
axios
'
import
Encrypt
from
'
utils/crypto
'
import
config
from
'
config/defaultSettings
'
import
httpErrorConfig
from
'
config/httpErrorConfig
'
import
Message
from
'
ant-design-vue/es/message
'
import
notification
from
'
ant-design-vue/es/notification
'
// import { setErrorLog } from 'api/pubApi';
import
{
sessionClear
,
sessionRead
,
getDate
,
unzip
}
from
'
utils/util
'
import
{
resetRouter
}
from
'
@/router
'
import
store
from
'
@/store
'
/* const addErrorLog = errorInfo => {
setErrorLog(errorInfo).then(() => {});
}; */
class
HttpRequest
{
constructor
()
{
this
.
queue
=
{}
this
.
encrypt
=
new
Encrypt
()
this
.
key
=
''
this
.
outLoginTimer
=
null
this
.
args
=
null
// 未加密参数
this
.
argsOnSecret
=
null
// 加密参数
// 来源平台和版本
this
.
platform
=
config
.
platform
this
.
version
=
config
.
version
// 初始化配置全局提示框
notification
.
config
({
placement
:
'
bottomRight
'
})
}
getInsideConfig
()
{
const
config
=
{
// 请求头信息
headers
:
{
// 'Content-Type': 'application/json',
Accept
:
'
*/*
'
}
}
return
config
}
destroy
(
option
)
{
delete
this
.
queue
[
option
.
url
]
}
interceptors
(
instance
,
options
)
{
// 请求拦截
instance
.
interceptors
.
request
.
use
(
config
=>
{
const
{
isToken
,
isEncrypt
,
otherToken
,
isRequestTip
,
isZip
}
=
options
config
.
isRequestTip
=
isRequestTip
const
token
=
store
.
getters
.
token
if
(
!
token
&&
isToken
!==
false
)
{
this
.
outLogin
()
console
.
error
(
'
token不能为空
'
)
return
}
// if (otherHeaders) {
// config.headers['Content-Type'] = 'application/x-www-form-urlencoded'
// }
// 请求头,拼接otherToken
if
(
otherToken
)
{
config
.
headers
.
_OtherToken_
=
otherToken
}
// 请求头,拼接token
if
(
isToken
!==
false
)
{
config
.
headers
.
_ImpToken_
=
token
}
// 控制后台返回参数是否压缩
if
(
isZip
)
{
config
.
headers
.
_CompressType_
=
'
gzip
'
}
const
currHours
=
getDate
(
new
Date
().
getTime
(),
'
hours
'
)
if
(
sessionRead
(
currHours
)
===
currHours
)
{
delete
config
.
headers
.
_CompressType_
}
/**
* 拦截上行参数,加密
*/
// 生成key 加密传入后台
if
(
isEncrypt
!==
false
&&
sessionRead
(
currHours
)
!==
currHours
)
{
this
.
key
=
this
.
encrypt
.
rndStr
(
16
)
const
secretKey
=
this
.
encrypt
.
keyJsencrypt
(
this
.
key
)
config
.
headers
.
_secretKey_
=
secretKey
}
// 加密上行参数
let
params
=
config
.
data
if
(
params
)
{
params
.
platform
=
store
.
state
.
user
.
platform
||
this
.
platform
params
.
version
=
this
.
version
if
(
isEncrypt
!==
false
&&
sessionRead
(
currHours
)
!==
currHours
)
{
params
=
this
.
cloneParams
(
params
)
}
config
.
data
=
params
this
.
argsOnSecret
=
params
}
this
.
queue
[
options
.
url
]
=
true
return
config
},
error
=>
{
return
Promise
.
reject
(
error
)
}
)
// 响应拦截
instance
.
interceptors
.
response
.
use
(
res
=>
{
const
{
isRequestTip
,
isZip
}
=
res
.
config
this
.
destroy
(
options
)
// 拦截响应,弹出对应信息
const
jsonData
=
res
.
data
if
(
jsonData
&&
jsonData
.
code
&&
jsonData
.
code
!==
200
&&
jsonData
.
code
!==
430
&&
jsonData
.
code
!==
4000
&&
isRequestTip
!==
false
)
{
const
codeLen
=
jsonData
.
code
.
length
const
message
=
jsonData
.
message
||
jsonData
.
data
.
detail
// 新版本业务代码
if
(
codeLen
===
6
)
{
if
(
Number
(
jsonData
.
code
)
>
0
)
{
// 业务成功
Message
.
info
(
message
)
}
else
{
Message
.
warning
(
message
)
}
}
else
{
// 老版本业务代码
if
(
Number
(
jsonData
.
code
)
===
508
||
Number
(
jsonData
.
code
)
===
401
||
Number
(
jsonData
.
code
)
===
510
||
Number
(
jsonData
.
code
)
===
607
)
{
/*
* 508 TOKEN无效
* 401 访问未授权 跳转到登录页面
* 510 授权过期 跳转到登录页面
*/
notification
.
warning
({
message
:
'
警告
'
,
description
:
Number
(
jsonData
.
code
)
===
607
?
`
${
jsonData
.
data
.
detail
}
,3秒钟后退出系统!`
:
`
${
message
}
,3秒钟后退出系统!`
})
if
(
this
.
outLoginTimer
)
{
clearTimeout
(
this
.
outLoginTimer
)
}
this
.
outLoginTimer
=
setTimeout
(()
=>
{
this
.
outLogin
()
},
3000
)
}
else
{
if
(
jsonData
&&
jsonData
.
code
&&
jsonData
.
code
===
413
&&
isZip
)
{
// jsonData.data = JSON.parse(unzip(jsonData.data.gzip));
Message
.
info
(
'
操作失败
'
)
// return false
}
else
{
Message
.
info
(
message
)
}
}
}
}
const
currHours
=
getDate
(
new
Date
().
getTime
(),
'
hours
'
)
if
(
jsonData
&&
jsonData
.
code
&&
jsonData
.
code
===
200
&&
isZip
&&
sessionRead
(
currHours
)
!==
currHours
)
{
jsonData
.
data
=
JSON
.
parse
(
unzip
(
jsonData
.
data
.
gzip
))
}
return
jsonData
},
error
=>
{
this
.
destroy
(
options
)
const
errorInfo
=
error
.
response
if
(
errorInfo
)
{
// 添加404错误日志
if
(
errorInfo
.
status
===
404
)
{
/* const args = this.args;
const argsOnSecret = this.argsOnSecret;
const headers = errorInfo.headers;
const reqUrl = error.config.url;
const secretKey = error.config.headers._secretKey_;
const token = error.config.headers._ImpToken_;
const params = {
args: JSON.stringify(args),
argsOnSecret: JSON.stringify(argsOnSecret),
headers: JSON.stringify(headers),
reqUrl,
secretKey,
token
}; */
// addErrorLog(params);
}
// http请求错误弹窗显示
httpErrorConfig
.
content
.
content
.
forEach
(
item
=>
{
if
(
item
.
code
===
errorInfo
.
status
)
{
notification
.
info
({
message
:
'
提示
'
,
description
:
item
.
tip
})
}
})
}
else
{
console
.
error
(
error
)
}
return
Promise
.
reject
(
error
)
}
)
}
request
(
options
)
{
// 请求配置
// if (process.env.NODE_ENV === 'development')
axios
.
defaults
.
timeout
=
300000
const
instance
=
axios
.
create
()
options
=
Object
.
assign
(
this
.
getInsideConfig
(),
options
)
this
.
interceptors
(
instance
,
options
)
return
instance
(
options
)
}
// 上行参数 clone
cloneParams
(
params
)
{
this
.
args
=
params
const
tempParams
=
JSON
.
parse
(
JSON
.
stringify
(
params
))
return
this
.
isArrayOrMap
(
tempParams
)
}
// 判断数据对象是MAP还是ARRAY
isArrayOrMap
(
data
)
{
if
(
Object
.
prototype
.
toString
.
call
(
data
)
===
'
[object Array]
'
)
{
return
this
.
dataArray
(
data
)
}
else
if
(
Object
.
prototype
.
toString
.
call
(
data
)
===
'
[object Object]
'
)
{
return
this
.
dataMap
(
data
)
}
}
// MAP类型分解加密
dataMap
(
dataMap
)
{
for
(
const
item
in
dataMap
)
{
if
(
Object
.
prototype
.
toString
.
call
(
dataMap
[
item
])
===
'
[object Array]
'
)
{
this
.
dataArray
(
dataMap
[
item
])
}
else
if
(
Object
.
prototype
.
toString
.
call
(
dataMap
[
item
])
===
'
[object Object]
'
)
{
this
.
dataMap
(
dataMap
[
item
])
}
else
{
dataMap
[
item
]
=
dataMap
[
item
]
===
null
||
dataMap
[
item
]
===
undefined
?
''
:
dataMap
[
item
]
dataMap
[
item
]
=
this
.
encrypt
.
encrypt
(
dataMap
[
item
],
this
.
key
)
}
}
return
dataMap
};
// ARRAY类型分解加密
dataArray
(
dataArray
)
{
for
(
let
i
=
0
;
i
<
dataArray
.
length
;
i
++
)
{
if
(
Object
.
prototype
.
toString
.
call
(
dataArray
[
i
])
===
'
[object Array]
'
)
{
this
.
dataArray
(
dataArray
[
i
])
}
else
if
(
Object
.
prototype
.
toString
.
call
(
dataArray
[
i
])
===
'
[object Object]
'
)
{
this
.
dataMap
(
dataArray
[
i
])
}
else
{
dataArray
[
i
]
=
dataArray
[
i
]
===
null
||
dataArray
[
i
]
===
undefined
?
''
:
dataArray
[
i
]
dataArray
[
i
]
=
this
.
encrypt
.
encrypt
(
dataArray
[
i
],
this
.
key
)
}
}
return
dataArray
};
outLogin
()
{
window
.
location
.
hash
=
'
#/user/login
'
sessionClear
()
resetRouter
()
// 清空vuex中的路由
store
.
commit
(
'
CLRAR_ROUTERS
'
)
};
}
export
default
HttpRequest
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录