Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
江名洋
/
linkwe-scrm
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 724dfd20
authored
Jul 22, 2023
by
xinla
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
扩展 axios request
1 parent
8095096d
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
135 additions
and
75 deletions
vue3-lw-pc/src/api/common.js
vue3-lw-pc/src/utils/request.js
vue3-lw-pc/src/api/common.js
View file @
724dfd2
// import { request } from '@/utils/request'
// const { get, post, put, del: _del } = request
// /**
// * 列表
// * @param {*} data
// */
// export function getList(data) {
// get(service + '/statistic', data)
// }
// // 详情
// export function getDetail(id) {
// return get(service + '/get/' + id)
// }
// // 删除
// export function del(id) {
// return _del(service + '/delete/' + id)
// }
// /**
// * 新增
// * @param {*} data
// * @returns
// */
// export function add(data) {
// return post(service + '/add', data)
// }
// // 修改
// export function update(data) {
// return put(service + '/update/' + id, data)
// }
import
{
decryptAES
}
from
'@/utils/jsencrypt'
import
{
decryptAES
}
from
'@/utils/jsencrypt'
import
request
from
'@/utils/request'
import
request
from
'@/utils/request'
import
{
dataURLtoFile
}
from
'@/utils/common'
import
{
dataURLtoFile
}
from
'@/utils/common'
...
...
vue3-lw-pc/src/utils/request.js
View file @
724dfd2
...
@@ -8,58 +8,42 @@ axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8'
...
@@ -8,58 +8,42 @@ axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8'
// setToken('eyJhbGciOiJIUzUxMiJ9.eyJ0ZW5hbnRfaWQiOjI2LCJ1c2VyX3R5cGUiOiI1IiwidXNlcl9pZCI6MjU5LCJ3ZUFwcFNlY3JldCI6IjY0YjRjNWRiMzg3MjA2Nzc2ZDRmYWJlMjEwNWMwNTc1IiwibG9naW5fdHlwZSI6IkxpbmtXZUNoYXRBUEkiLCJ1c2VyX25hbWUiOiLlp5zpuY_lh68iLCJ1c2VyX2tleSI6ImRmNTdmYzY4LWI2ZTYtNDVmNS04N2UzLWY4YzA5ZTZiNzFiOSIsIndlQXBwSWQiOiJ3eDg2M2MwMzFhNzRmNTg5YTAiLCJjb3JwX25hbWUiOiLku5_lvq7np5HmioAiLCJjb3JwX2lkIjoid3c2MjJmYzg1MmY3OWMzZjEzIn0.X5To1L7fJRBEIxmP6Dvp2XTpvWHwz8Ey6ODAeyk_owlFj_JywVjfpodDk0B7bGjRXE16nNpab0L9f3eF_-WGPQ')
// setToken('eyJhbGciOiJIUzUxMiJ9.eyJ0ZW5hbnRfaWQiOjI2LCJ1c2VyX3R5cGUiOiI1IiwidXNlcl9pZCI6MjU5LCJ3ZUFwcFNlY3JldCI6IjY0YjRjNWRiMzg3MjA2Nzc2ZDRmYWJlMjEwNWMwNTc1IiwibG9naW5fdHlwZSI6IkxpbmtXZUNoYXRBUEkiLCJ1c2VyX25hbWUiOiLlp5zpuY_lh68iLCJ1c2VyX2tleSI6ImRmNTdmYzY4LWI2ZTYtNDVmNS04N2UzLWY4YzA5ZTZiNzFiOSIsIndlQXBwSWQiOiJ3eDg2M2MwMzFhNzRmNTg5YTAiLCJjb3JwX25hbWUiOiLku5_lvq7np5HmioAiLCJjb3JwX2lkIjoid3c2MjJmYzg1MmY3OWMzZjEzIn0.X5To1L7fJRBEIxmP6Dvp2XTpvWHwz8Ey6ODAeyk_owlFj_JywVjfpodDk0B7bGjRXE16nNpab0L9f3eF_-WGPQ')
// 创建axios实例
// 创建axios实例
const
service
=
axios
.
create
({
function
requestFactory
(
baseURL
)
{
// axios中请求配置有baseURL选项,表示请求URL公共部分
const
service
=
axios
.
create
({
baseURL
:
process
.
env
.
NODE_ENV
===
'development'
?
'/api'
:
window
.
lwConfig
.
DOMAIN
,
// axios中请求配置有baseURL选项,表示请求URL公共部分
// 超时
baseURL
:
process
.
env
.
NODE_ENV
===
'development'
?
'/api'
:
baseURL
,
timeout
:
100000
,
// 超时
})
timeout
:
100000
,
})
// request拦截器
// request拦截器
service
.
interceptors
.
request
.
use
(
service
.
interceptors
.
request
.
use
(
(
config
)
=>
{
(
config
)
=>
{
// 是否需要设置 token
// 是否需要设置 token
const
isToken
=
(
config
.
headers
||
{}).
isToken
===
false
const
isToken
=
(
config
.
headers
||
{}).
isToken
===
false
if
(
getToken
()
&&
!
isToken
)
{
if
(
getToken
()
&&
!
isToken
)
{
config
.
headers
=
window
.
lwConfig
.
headers
// 让每个请求携带自定义token 请根据实际情况自行修改
config
.
headers
=
window
.
lwConfig
.
headers
// 让每个请求携带自定义token 请根据实际情况自行修改
}
}
return
config
return
config
},
},
(
error
)
=>
{
(
error
)
=>
{
console
.
log
(
error
)
console
.
log
(
error
)
Promise
.
reject
(
error
)
Promise
.
reject
(
error
)
},
},
)
)
// 响应拦截器
// 响应拦截器
service
.
interceptors
.
response
.
use
(
service
.
interceptors
.
response
.
use
(
(
res
)
=>
{
(
res
)
=>
{
let
store
=
stores
()
let
store
=
stores
()
// 未设置状态码则默认成功状态
// 未设置状态码则默认成功状态
const
code
=
res
.
data
.
code
||
200
const
code
=
res
.
data
.
code
||
200
// 获取错误信息
// 获取错误信息
const
msg
=
errorCode
[
code
]
||
res
.
data
.
msg
||
errorCode
[
'default'
]
const
msg
=
errorCode
[
code
]
||
res
.
data
.
msg
||
errorCode
[
'default'
]
if
(
code
===
200
||
code
===
433
||
code
===
226
)
{
if
(
code
===
200
||
code
===
433
||
code
===
226
)
{
return
res
.
data
return
res
.
data
}
else
if
(
code
===
401
)
{
}
else
if
(
code
===
401
)
{
MessageBox
.
confirm
(
'登录状态已过期,您可以继续留在该页面,或者重新登录'
,
'系统提示'
,
{
MessageBox
.
confirm
(
'登录状态已过期,您可以继续留在该页面,或者重新登录'
,
'系统提示'
,
{
confirmButtonText
:
'重新登录'
,
cancelButtonText
:
'取消'
,
type
:
'warning'
,
}).
then
(()
=>
{
store
.
LogOut
().
then
(()
=>
{
location
.
href
=
window
.
lwConfig
.
BASE_URL
})
})
}
else
{
if
(
process
.
env
.
NODE_ENV
===
'development'
)
{
Message
({
message
:
`后端错误:接口:
${
res
.
config
.
url
}
,
${
code
}
错误:
${
msg
}
`
,
type
:
'error'
,
})
}
else
if
([
40029
,
-
10002
].
includes
(
code
))
{
// 不合法的oauth_code,租户未授权等
MessageBox
.
confirm
(
msg
,
'系统提示'
,
{
confirmButtonText
:
'重新登录'
,
confirmButtonText
:
'重新登录'
,
cancelButtonText
:
'取消'
,
cancelButtonText
:
'取消'
,
type
:
'warning'
,
type
:
'warning'
,
...
@@ -69,31 +53,73 @@ service.interceptors.response.use(
...
@@ -69,31 +53,73 @@ service.interceptors.response.use(
})
})
})
})
}
else
{
}
else
{
Notification
.
error
({
if
(
process
.
env
.
NODE_ENV
===
'development'
)
{
title
:
msg
,
Message
({
})
message
:
`后端错误:接口:
${
res
.
config
.
url
}
,
${
code
}
错误:
${
msg
}
`
,
type
:
'error'
,
})
}
else
if
([
40029
,
-
10002
].
includes
(
code
))
{
// 不合法的oauth_code,租户未授权等
MessageBox
.
confirm
(
msg
,
'系统提示'
,
{
confirmButtonText
:
'重新登录'
,
cancelButtonText
:
'取消'
,
type
:
'warning'
,
}).
then
(()
=>
{
store
.
LogOut
().
then
(()
=>
{
location
.
href
=
window
.
lwConfig
.
BASE_URL
})
})
}
else
{
Notification
.
error
({
title
:
msg
,
})
}
return
Promise
.
reject
()
}
}
},
(
error
)
=>
{
console
.
log
(
'err: '
+
error
)
let
{
message
:
msg
,
response
,
config
}
=
error
if
(
msg
==
'Network Error'
)
{
msg
=
'后端接口连接异常'
}
else
if
(
msg
.
includes
(
'timeout'
))
{
msg
=
'系统接口请求超时'
}
else
if
(
response
)
{
let
status
=
response
.
status
msg
=
'系统接口:'
+
status
+
'异常'
}
Message
({
message
:
`
${
msg
}
:
${
config
.
url
}
`
,
type
:
'error'
,
duration
:
5
*
1000
,
})
return
Promise
.
reject
()
return
Promise
.
reject
()
}
},
},
)
(
error
)
=>
{
console
.
log
(
'err: '
+
error
)
// 重写方法,统一传参格式
let
{
message
:
msg
,
response
,
config
}
=
error
service
.
get
=
(
url
,
params
,
config
=
{})
=>
{
if
(
msg
==
'Network Error'
)
{
return
service
({
url
,
params
,
...
config
})
msg
=
'后端接口连接异常'
}
}
else
if
(
msg
.
includes
(
'timeout'
))
{
msg
=
'系统接口请求超时'
let
post
=
service
.
post
}
else
if
(
response
)
{
service
.
post
=
(
url
,
data
,
config
=
{})
=>
{
let
status
=
response
.
status
return
post
(
url
,
data
,
config
)
msg
=
'系统接口:'
+
status
+
'异常'
}
}
Message
({
let
put
=
service
.
put
message
:
`
${
msg
}
:
${
config
.
url
}
`
,
service
.
put
=
(
url
,
data
,
config
=
{})
=>
{
type
:
'error'
,
return
put
(
url
,
data
,
config
)
duration
:
5
*
1000
,
}
})
return
Promise
.
reject
()
let
del
=
service
.
del
},
service
.
del
=
(
url
,
params
,
config
=
{})
=>
{
)
return
del
(
url
,
{
params
,
...
config
})
}
return
service
}
export
const
request
=
requestFactory
(
window
.
lwConfig
.
BASE_API
)
export
default
service
export
default
requestFactory
(
window
.
lwConfig
.
DOMAIN
)
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