Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
高东东-金蝶建发
/
jf-yzj-employeeself-develop
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
e2cdff7e
authored
Nov 10, 2023
by
golton_gao
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
update: 优化发送请求时的header
parent
3660b6d0
Pipeline
#23917
failed with stage
in 0 seconds
Changes
2
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
47 additions
and
20 deletions
src/libraries/plugins/MAS/index.js
src/shared/polyfill/request.js
src/libraries/plugins/MAS/index.js
View file @
e2cdff7e
...
...
@@ -78,15 +78,17 @@ export class MAS {
getData
(
url
,
params
,
options
)
{
let
method
=
options
&&
options
.
method
||
this
.
method
||
'get'
let
baseUrl
=
options
&&
options
.
baseUrl
||
this
.
baseUrl
const
headers
=
options
&&
options
.
headers
||
{}
if
(
options
&&
options
.
Authorization
)
{
headers
[
'Authorization'
]
=
options
.
Authorization
}
return
axios
({
url
:
`
${
baseUrl
}
/
${
url
}
`
,
method
,
params
:
method
===
'get'
&&
params
,
data
:
method
===
'post'
&&
params
,
...
options
,
headers
:
{
Authorization
:
options
&&
options
.
Authorization
}
headers
:
headers
,
}).
then
(
result
=>
this
.
responser
(
result
))
}
...
...
@@ -96,15 +98,17 @@ export class MAS {
userName
:
this
.
userName
?
this
.
userName
:
''
},
params
,
options
&&
options
.
params
)
let
param
=
{[
this
.
tokenKey
]:
this
.
token
};
const
headers
=
options
&&
options
.
headers
||
{}
if
(
options
&&
options
.
Authorization
)
{
headers
[
'Authorization'
]
=
options
.
Authorization
}
return
axios
({
url
:
`
${
baseUrl
}
/
${
url
}
`
,
method
:
'post'
,
params
:
param
,
data
:
data
,
...
options
,
headers
:
{
Authorization
:
options
&&
options
.
Authorization
}
headers
:
headers
,
}).
then
(
result
=>
this
.
responser
(
result
))
}
...
...
src/shared/polyfill/request.js
View file @
e2cdff7e
...
...
@@ -12,6 +12,10 @@ function isObject(value) {
return
value
!==
null
&&
typeof
value
===
'object'
}
function
isString
(
fd
)
{
return
Object
.
prototype
.
toString
.
call
(
fd
)
===
'[object String]'
}
function
isFormData
(
fd
)
{
return
Object
.
prototype
.
toString
.
call
(
fd
)
===
'[object FormData]'
}
...
...
@@ -35,7 +39,7 @@ function params2object(url) {
.
forEach
((
n
)
=>
{
n
=
n
.
split
(
'='
)
if
(
n
.
length
===
2
)
{
params
[
n
[
0
]]
=
n
[
1
]
params
[
n
[
0
]]
=
decodeURIComponent
(
n
[
1
])
}
})
}
...
...
@@ -43,6 +47,7 @@ function params2object(url) {
}
function
request
(
opt
)
{
let
serializer
=
opt
.
serializer
||
'form'
opt
=
opt
||
{}
if
(
!
opt
.
url
)
{
return
Promise
.
reject
(
'interface url is required!'
)
...
...
@@ -78,35 +83,53 @@ function request(opt) {
// post非json即是form。
if
(
opt
.
method
.
toLowerCase
()
===
'post'
)
{
var
data
=
opt
.
data
if
(
isFormData
(
data
))
{
opt
.
headers
=
Object
.
assign
(
opt
.
headers
||
{},
{
'Content-Type'
:
'application/x-www-form-urlencoded'
})
const
headers
=
opt
.
headers
const
contentType
=
headers
[
'content-type'
]
||
headers
[
'Content-Type'
]
||
headers
[
'CONTENT-TYPE'
]
||
''
if
(
contentType
.
indexOf
(
'application/json'
)
!==
-
1
)
{
serializer
=
'json'
}
if
((
serializer
===
'form'
)
&&
isFormData
(
data
))
{
opt
.
data
=
formData2obj
(
data
)
}
else
if
(
isObject
(
data
))
{
opt
.
headers
=
Object
.
assign
(
opt
.
headers
||
{},
{
'Content-Type'
:
'application/json;charset=UTF-8'
})
}
else
if
(
isString
(
data
))
{
if
(
serializer
===
'json'
)
{
try
{
opt
.
data
=
JSON
.
parse
(
data
)
}
catch
(
error
)
{
opt
.
data
=
{}
}
}
else
{
opt
.
data
=
params2object
(
data
)
}
}
}
// 处理header
opt
.
headers
=
opt
.
headers
||
{}
const
headers
=
{}
// headers字段值必须是字符串型
Object
.
keys
(
opt
.
headers
).
forEach
(
function
(
key
)
{
var
val
=
opt
.
headers
[
key
]
if
(
val
)
{
var
type
=
typeof
val
if
(
type
===
'undefined'
)
{
delete
opt
.
headers
[
key
]
}
else
if
(
type
!==
'string'
)
{
opt
.
headers
[
key
]
=
val
+
''
if
(
type
!==
'string'
)
{
val
=
val
+
''
}
// 因content-type可能存在大小写不规范,这里先过滤掉,后面根据serializer统一添加
var
_key
=
key
.
toLowerCase
()
if
(
_key
!==
'content-type'
)
{
headers
[
_key
]
=
val
}
}
})
// 根据serializer统一添加content-type
headers
[
'content-type'
]
=
serializer
===
'json'
?
'application/json'
:
'application/x-www-form-urlencoded'
opt
.
headers
=
headers
return
new
Promise
(
function
(
resolve
,
reject
)
{
qing
.
call
(
'request'
,
{
url
:
opt
.
url
,
method
:
opt
.
method
,
method
:
opt
.
method
.
toUpperCase
()
,
headers
:
opt
.
headers
,
header
:
opt
.
headers
,
data
:
opt
.
data
||
{},
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
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 comment