Commit e2cdff7e by golton_gao

update: 优化发送请求时的header

parent 3660b6d0
Pipeline #23917 failed with stage
in 0 seconds
...@@ -78,15 +78,17 @@ export class MAS { ...@@ -78,15 +78,17 @@ export class MAS {
getData (url, params, options) { getData (url, params, options) {
let method = options && options.method || this.method || 'get' let method = options && options.method || this.method || 'get'
let baseUrl = options && options.baseUrl || this.baseUrl let baseUrl = options && options.baseUrl || this.baseUrl
const headers = options && options.headers || {}
if (options && options.Authorization) {
headers['Authorization'] = options.Authorization
}
return axios({ return axios({
url: `${baseUrl}/${url}`, url: `${baseUrl}/${url}`,
method, method,
params: method === 'get' && params, params: method === 'get' && params,
data: method === 'post' && params, data: method === 'post' && params,
...options, ...options,
headers: { headers: headers,
Authorization: options && options.Authorization
}
}).then(result => this.responser(result)) }).then(result => this.responser(result))
} }
...@@ -96,15 +98,17 @@ export class MAS { ...@@ -96,15 +98,17 @@ export class MAS {
userName: this.userName ? this.userName : '' userName: this.userName ? this.userName : ''
},params, options && options.params) },params, options && options.params)
let param = {[this.tokenKey]: this.token}; let param = {[this.tokenKey]: this.token};
const headers = options && options.headers || {}
if (options && options.Authorization) {
headers['Authorization'] = options.Authorization
}
return axios({ return axios({
url: `${baseUrl}/${url}`, url: `${baseUrl}/${url}`,
method:'post', method:'post',
params: param, params: param,
data: data, data: data,
...options, ...options,
headers: { headers: headers,
Authorization: options && options.Authorization
}
}).then(result => this.responser(result)) }).then(result => this.responser(result))
} }
......
...@@ -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,53 @@ function request(opt) { ...@@ -78,35 +83,53 @@ 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)) { }
opt.headers = Object.assign(opt.headers || {}, { else if (isString(data)) {
'Content-Type': 'application/json;charset=UTF-8' if (serializer === 'json') {
}) 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) {
qing.call('request', { 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 || {},
......
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