Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
高东东-金蝶建发
/
jf-yzj-supplier
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
24d2130e
authored
Nov 29, 2023
by
golton_gao
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
update: 优化代码
parent
dc4a15fb
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
38 additions
and
16 deletions
.eslintrc.js
src/polyfill/request.js
src/vuex/store.js
.eslintrc.js
View file @
24d2130e
...
@@ -46,6 +46,6 @@ module.exports = {
...
@@ -46,6 +46,6 @@ module.exports = {
// allow async-await
// allow async-await
'generator-star-spacing'
:
0
,
'generator-star-spacing'
:
0
,
// allow debugger during development
// allow debugger during development
'no-debugger'
:
process
.
env
.
NODE_ENV
===
'production
'
?
2
:
0
'no-debugger'
:
process
.
env
.
NODE_ENV
!==
'development
'
?
2
:
0
}
}
}
}
src/polyfill/request.js
View file @
24d2130e
...
@@ -12,6 +12,10 @@ function isObject (value) {
...
@@ -12,6 +12,10 @@ function isObject (value) {
return
value
!==
null
&&
typeof
value
===
'object'
return
value
!==
null
&&
typeof
value
===
'object'
}
}
function
isString
(
fd
)
{
return
Object
.
prototype
.
toString
.
call
(
fd
)
===
'[object String]'
}
function
isFormData
(
fd
)
{
function
isFormData
(
fd
)
{
return
Object
.
prototype
.
toString
.
call
(
fd
)
===
'[object FormData]'
return
Object
.
prototype
.
toString
.
call
(
fd
)
===
'[object FormData]'
}
}
...
@@ -35,7 +39,7 @@ function params2object (url) {
...
@@ -35,7 +39,7 @@ function params2object (url) {
.
forEach
((
n
)
=>
{
.
forEach
((
n
)
=>
{
n
=
n
.
split
(
'='
)
n
=
n
.
split
(
'='
)
if
(
n
.
length
===
2
)
{
if
(
n
.
length
===
2
)
{
params
[
n
[
0
]]
=
n
[
1
]
params
[
n
[
0
]]
=
decodeURIComponent
(
n
[
1
])
}
}
})
})
}
}
...
@@ -43,6 +47,7 @@ function params2object (url) {
...
@@ -43,6 +47,7 @@ function params2object (url) {
}
}
function
request
(
opt
)
{
function
request
(
opt
)
{
let
serializer
=
opt
.
serializer
||
'form'
opt
=
opt
||
{}
opt
=
opt
||
{}
if
(
!
opt
.
url
)
{
if
(
!
opt
.
url
)
{
return
Promise
.
reject
(
'interface url is required!'
)
return
Promise
.
reject
(
'interface url is required!'
)
...
@@ -78,35 +83,52 @@ function request (opt) {
...
@@ -78,35 +83,52 @@ function request (opt) {
// post非json即是form。
// post非json即是form。
if
(
opt
.
method
.
toLowerCase
()
===
'post'
)
{
if
(
opt
.
method
.
toLowerCase
()
===
'post'
)
{
var
data
=
opt
.
data
var
data
=
opt
.
data
if
(
isFormData
(
data
))
{
const
headers
=
opt
.
headers
opt
.
headers
=
Object
.
assign
(
opt
.
headers
||
{},
{
const
contentType
=
headers
[
'content-type'
]
||
headers
[
'Content-Type'
]
||
headers
[
'CONTENT-TYPE'
]
||
''
'Content-Type'
:
'application/x-www-form-urlencoded'
if
(
contentType
.
indexOf
(
'application/json'
)
!==
-
1
)
{
})
serializer
=
'json'
}
if
((
serializer
===
'form'
)
&&
isFormData
(
data
))
{
opt
.
data
=
formData2obj
(
data
)
opt
.
data
=
formData2obj
(
data
)
}
else
if
(
isObject
(
data
))
{
}
else
if
(
isString
(
data
))
{
opt
.
headers
=
Object
.
assign
(
opt
.
headers
||
{},
{
if
(
serializer
===
'json'
)
{
'Content-Type'
:
'application/json;charset=UTF-8'
try
{
})
opt
.
data
=
JSON
.
parse
(
data
)
}
catch
(
error
)
{
opt
.
data
=
{}
}
}
else
{
opt
.
data
=
params2object
(
data
)
}
}
}
}
}
// 处理header
// 处理header
opt
.
headers
=
opt
.
headers
||
{}
opt
.
headers
=
opt
.
headers
||
{}
const
headers
=
{}
// headers字段值必须是字符串型
// headers字段值必须是字符串型
Object
.
keys
(
opt
.
headers
).
forEach
(
function
(
key
)
{
Object
.
keys
(
opt
.
headers
).
forEach
(
function
(
key
)
{
var
val
=
opt
.
headers
[
key
]
var
val
=
opt
.
headers
[
key
]
if
(
val
)
{
var
type
=
typeof
val
var
type
=
typeof
val
if
(
type
===
'undefined'
)
{
if
(
type
!==
'string'
)
{
delete
opt
.
headers
[
key
]
val
=
val
+
''
}
else
if
(
type
!==
'string'
)
{
}
opt
.
headers
[
key
]
=
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
)
{
return
new
Promise
(
function
(
resolve
,
reject
)
{
window
.
qing
.
call
(
'request'
,
{
window
.
qing
.
call
(
'request'
,
{
url
:
opt
.
url
,
url
:
opt
.
url
,
method
:
opt
.
method
,
method
:
opt
.
method
.
toUpperCase
()
,
headers
:
opt
.
headers
,
headers
:
opt
.
headers
,
header
:
opt
.
headers
,
header
:
opt
.
headers
,
data
:
opt
.
data
||
{},
data
:
opt
.
data
||
{},
...
...
src/vuex/store.js
View file @
24d2130e
...
@@ -11,7 +11,7 @@ import modules from './modules'
...
@@ -11,7 +11,7 @@ import modules from './modules'
Vue
.
use
(
Vuex
)
Vue
.
use
(
Vuex
)
const
debug
=
process
.
env
.
NODE_ENV
!==
'production
'
const
debug
=
process
.
env
.
NODE_ENV
===
'development
'
Vue
.
config
.
debug
=
debug
Vue
.
config
.
debug
=
debug
export
default
new
Vuex
.
Store
({
export
default
new
Vuex
.
Store
({
...
...
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