Commit 99216db5 by golton_gao

update: 优化代码

parent 61d0692d
Showing with 40 additions and 17 deletions
......@@ -5,11 +5,11 @@
"author": "liyj31 <yujun2.li@meicloud.com>",
"private": true,
"scripts": {
"dev": "cross-env PACK_ENV=dev node build/dev-server.js",
"dev": "cross-env PACK_ENV=dev && node build/dev-server.js",
"start": "npm run dev",
"build": "npm run build:test",
"build:test": "cross-env PACK_ENV=test node build/build.js",
"build:prod": "cross-env PACK_ENV=product node build/build.js",
"build:test": "cross-env PACK_ENV=test && node build/build.js",
"build:prod": "cross-env PACK_ENV=product && node build/build.js",
"build:report": "npm run build --report"
},
"dependencies": {
......
......@@ -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 || {},
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment