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