Commit aafaec45 by zqm

update: 网关换成云之家的

parent aa0d5d30
This diff could not be displayed because it is too large.
......@@ -65,7 +65,7 @@ export default class MAS {
* @memberof MAS
*/
getData(url, params, options = {}) {
console.log('getData');
console.log('mas getData>>',{url, params, options});
let method = (options && options.method) || this.method || 'get';
// let baseUrl = (options && options.baseUrl) || this.baseUrl;
return axios({
......
import axios from 'axios'
import axios from 'axios';
var originAdapter = axios.defaults.adapter
axios.defaults.adapter = function (config) {
var originAdapter = axios.defaults.adapter;
axios.defaults.adapter = function(config) {
if (!/^https?/.test(config.url)) {
return originAdapter(config)
return originAdapter(config);
}
return request(config)
}
return request(config);
};
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]'
return Object.prototype.toString.call(fd) === '[object String]';
}
function isFormData(fd) {
return Object.prototype.toString.call(fd) === '[object FormData]'
return Object.prototype.toString.call(fd) === '[object FormData]';
}
function formData2obj(fd) {
var data = {}
var data = {};
for (var p of fd.entries()) {
data[p[0]] = p[1]
data[ p[ 0 ] ] = p[ 1 ];
}
return data
return data;
}
function params2object(url) {
var params = {}
var params = {};
if (typeof url === 'object') {
return url
return url;
} else if (typeof url === 'string') {
url
.replace(/^.*\?|#.*$/g, '')
.split('&')
.forEach((n) => {
n = n.split('=')
n = n.split('=');
if (n.length === 2) {
params[n[0]] = decodeURIComponent(n[1])
params[ n[ 0 ] ] = decodeURIComponent(n[ 1 ]);
}
})
});
}
return params
return params;
}
function request(opt) {
let serializer = opt.serializer || 'form'
opt = opt || {}
let serializer = opt.serializer || 'form';
opt = opt || {};
if (!opt.url) {
return Promise.reject('interface url is required!')
return Promise.reject('interface url is required!');
}
// 默认get请求
opt.method = opt.method || 'get'
opt.method = opt.method || 'get';
// 处理params
var urlParams = opt.params || {}
var urlParams = opt.params || {};
// 合并url和params字段的数据,如果params字段与url中有重名参数,以params中的为准
urlParams = Object.assign(params2object(opt.url), urlParams)
urlParams = Object.assign(params2object(opt.url), urlParams);
// 处理url
// url如果包含空格,会在iOS中报错
opt.url = opt.url.replace(/^\s+|\s+$/g, '')
opt.url = opt.url.replace(/^\s+|\s+$/g, '');
// 将params拼接到url中
opt.url =
opt.url.split('?')[0] +
opt.url.split('?')[ 0 ] +
'?' +
Object.keys(urlParams)
.map((key) => {
var val = urlParams[key]
var val = urlParams[ key ];
if (isObject(val)) {
val = JSON.stringify(val)
val = encodeURIComponent(val)
val = JSON.stringify(val);
val = encodeURIComponent(val);
} else if (/[^\x00-\xff]/.test(val)) {
val = encodeURIComponent(val)
val = encodeURIComponent(val);
}
return key + '=' + val
return key + '=' + val;
})
.join('&')
.join('&');
// post非json即是form。
if (opt.method.toLowerCase() === 'post') {
var data = opt.data
const headers = opt.headers
const contentType = headers['content-type'] || headers['Content-Type'] || headers['CONTENT-TYPE'] || ''
var data = opt.data;
const headers = opt.headers;
const contentType = headers[ 'content-type' ] || headers[ 'Content-Type' ] || headers[ 'CONTENT-TYPE' ] || '';
if (contentType.indexOf('application/json') !== -1) {
serializer = 'json'
serializer = 'json';
}
if ((serializer === 'form') && isFormData(data)) {
opt.data = formData2obj(data)
}
else if (isString(data)) {
opt.data = formData2obj(data);
} else if (isString(data)) {
if (serializer === 'json') {
try {
opt.data = JSON.parse(data)
opt.data = JSON.parse(data);
} catch (error) {
opt.data = {}
opt.data = {};
}
} else {
opt.data = params2object(data)
opt.data = params2object(data);
}
}
}
// 处理header
opt.headers = opt.headers || {}
const headers = {}
opt.headers = opt.headers || {};
const headers = {};
// headers字段值必须是字符串型
Object.keys(opt.headers).forEach(function (key) {
var val = opt.headers[key]
Object.keys(opt.headers)
.forEach(function(key) {
var val = opt.headers[ key ];
if (val) {
var type = typeof val
var type = typeof val;
if (type !== 'string') {
val = val + ''
val = val + '';
}
// 因content-type可能存在大小写不规范,这里先过滤掉,后面根据serializer统一添加
var _key = key.toLowerCase()
var _key = key.toLowerCase();
if (_key !== 'content-type') {
headers[_key] = val
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) {
headers[ 'content-type' ] = serializer === 'json' ? 'application/json' : 'application/x-www-form-urlencoded';
opt.headers = headers;
console.log('请求到这 opt>>', opt);
return new Promise(function(resolve, reject) {
qing.call('request', {
url: opt.url,
method: opt.method.toUpperCase(),
......@@ -134,35 +134,41 @@ function request(opt) {
header: opt.headers,
data: opt.data || {},
dataType: opt.dataType,
success: function (resp) {
success: function(resp) {
console.log('qing success>>', resp);
if (resp.data.errMsg || resp.data.error) {
alert(resp.data.errMsg + ':' + resp.data.error);
}
var response = {
data: resp.data,
status: resp.statusCode,
headers: resp.header,
config: opt
}
var validateStatus = response.config.validateStatus
};
var validateStatus = response.config.validateStatus;
// Note: status is not exposed by XDomainRequest
if (
!response.status ||
!validateStatus ||
validateStatus(response.status)
) {
resolve(response)
resolve(response);
} else {
var error = new Error(
'Request failed with status code ' + response.status
)
error.config = response.config
error.response = response
reject(error)
);
error.config = response.config;
error.response = response;
reject(error);
}
},
error: function (e) {
var error = new Error(e)
error.config = opt
reject(error)
}
})
})
error: function(e) {
console.log('qing error>>', e);
var error = new Error(e);
error.config = opt;
reject(error);
}
});
});
}
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