Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
h5
/
vite-integral
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 88994ab9
authored
Aug 19, 2022
by
胡悦
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
修复
1 parent
66d740ee
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
5 additions
and
97 deletions
src/utils/axios.ts
src/utils/request.ts
src/utils/axios.ts
deleted
100644 → 0
View file @
66d740e
import
axios
,
{
AxiosInstance
,
AxiosRequestConfig
,
AxiosResponse
,
AxiosError
}
from
'axios'
import
{
ElMessage
,
ElMessageBox
}
from
'element-plus'
import
{
Toast
}
from
'vant'
;
import
router
,
{
resetRoute
}
from
'../router'
import
qs
from
'qs'
import
{
createSign
}
from
'./util'
import
$mockConfig
from
'./config'
import
{
useUserStore
}
from
'../store/user'
const
{
response
=
{},
request
=
{},
headers
=
{},
alert
,
sign
,
signWay
=
'sha1'
,
secret
}
=
$mockConfig
// 配置新建一个axios 实例
const
service
:
AxiosInstance
=
axios
.
create
({
baseURL
:
request
.
baseURL
,
// baseURL: PATH_URL,
timeout
:
50000
,
headers
:
{
'Content-Type'
:
'application/x-www-form-urlencoded; charset=UTF-8'
}
})
const
customer
=
{
response
,
alert
,
sign
,
secret
,
signWay
}
// 添加请求拦截器
service
.
interceptors
.
request
.
use
(
(
config
:
AxiosRequestConfig
)
=>
{
console
.
log
(
config
)
let
{
data
=
{},
sign
=
true
,
noSignAttr
=
[]}
=
config
if
(
sign
)
{
let
_configSign
=
customer
.
sign
||
{}
data
=
createSign
(
Object
.
assign
({},
_configSign
,
data
),
customer
.
secret
,
noSignAttr
,
customer
.
signWay
)
}
const
method
=
config
.
method
?.
toLowerCase
()
if
(
method
==
'get'
)
{
config
.
params
=
data
}
else
if
(
method
==
'post'
)
{
config
.
data
=
qs
.
stringify
(
data
)
}
// if (getSession('token')) {
// config.headers.common['Authorization'] = `${getSession('token')}`
// }
return
config
},
(
error
:
AxiosError
)
=>
{
return
Promise
.
reject
(
error
)
}
)
// 添加响应拦截器
service
.
interceptors
.
response
.
use
(
({
data
:
res
,
config
:
{
returnAll
}
})
=>
{
console
.
log
(
returnAll
)
const
{
response
:
{
code_key
,
errId
=
'errId'
,
code_success_value
=
'0'
,
data_key
=
'data'
,
errId_logout_value
,
logout_function
=
'logout'
}
}
=
customer
if
(
returnAll
)
{
return
res
}
let
codekey
=
Array
.
isArray
(
code_key
)
?
code_key
.
find
(
key
=>
res
.
hasOwnProperty
(
key
))
:
code_key
let
code
=
res
[
codekey
]
let
errValue
=
res
[
errId
]
console
.
log
(
code
,
errValue
)
// 特殊的code处理,可以增加
switch
(
code
+
''
)
{
case
code_success_value
+
''
:
break
case
errId_logout_value
+
''
:
// logout(logout_function)
console
.
log
(
11
)
break
default
:
if
(
errValue
&&
errValue
==
errId_logout_value
)
{
// return logout(logout_function)
console
.
log
(
2
)
localStorage
.
removeItem
(
'sessionId'
)
router
.
push
(
'/login'
)
console
.
log
(
'session失效,登出'
,
useUserStore
().
getSession
)
}
Toast
(
res
.
message
||
res
.
msg
||
'出错了,请联系管理员'
)
break
}
return
res
[
data_key
]
},
error
=>
{
// const { alert } = customer
Toast
&&
Toast
(
error
.
message
||
'出错了,请联系管理员'
)
// 返回接口返回的错误信息
return
Promise
.
reject
(
error
)
}
)
export
default
service
\ No newline at end of file
\ No newline at end of file
src/utils/request.ts
View file @
88994ab
...
@@ -7,12 +7,13 @@ const req: any = (url: string, data: any = {}, returnAll = false, method = 'POST
...
@@ -7,12 +7,13 @@ const req: any = (url: string, data: any = {}, returnAll = false, method = 'POST
// let sessionId = '2dfdf8ff-fe50-4b07-bfd4-bdaec96ad717'
// let sessionId = '2dfdf8ff-fe50-4b07-bfd4-bdaec96ad717'
if
(
sessionId
)
data
.
loginSessionId
=
sessionId
if
(
sessionId
)
data
.
loginSessionId
=
sessionId
data
.
method
=
url
data
.
method
=
url
return
http
(
{
const
options
=
{
url
:
PATH
,
url
:
PATH
,
method
,
method
,
returnAll
,
data
,
data
returnAll
})
}
return
http
(
options
)
}
}
export
default
req
export
default
req
\ No newline at end of file
\ No newline at end of file
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment