...
|
...
|
@@ -5,7 +5,7 @@ import CONFIG from '../constants/config'; |
|
|
import DeviceInfo from 'react-native-device-info';
|
|
|
import queryString from 'query-string';
|
|
|
import md5 from 'md5';
|
|
|
|
|
|
import timeoutPromise from '../utils/timeoutPromise';
|
|
|
|
|
|
export default class Request {
|
|
|
|
...
|
...
|
@@ -15,14 +15,29 @@ export default class Request { |
|
|
|
|
|
this.baseUrl= this.config.baseUrl;
|
|
|
this.privateKey = this.config.privateKey;
|
|
|
this.timeout = this.config.httpTimeout;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
* GET
|
|
|
*
|
|
|
* @param {[object]} opts
|
|
|
*
|
|
|
* {
|
|
|
* url: '/operations/api/v6/category/getCategory', //接口地址
|
|
|
* timeout: 30, //毫秒
|
|
|
* }
|
|
|
*
|
|
|
* @return {[promise]}
|
|
|
*/
|
|
|
async get(opts) {
|
|
|
try {
|
|
|
let response = await this._fetch({
|
|
|
method: 'GET',
|
|
|
url: opts.url,
|
|
|
timeout: opts.timeout | 0,
|
|
|
});
|
|
|
|
|
|
let data = await this._parseResponse(response);
|
...
|
...
|
@@ -33,12 +48,29 @@ export default class Request { |
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* POST
|
|
|
*
|
|
|
* @param {[object]} opts
|
|
|
*
|
|
|
* {
|
|
|
* url: '/operations/api/v6/category/getCategory', //接口地址
|
|
|
* body: {
|
|
|
* user: 'Lily',
|
|
|
* password: '111111',
|
|
|
* },
|
|
|
* timeout: 30, //毫秒
|
|
|
* }
|
|
|
*
|
|
|
* @return {[promise]}
|
|
|
*/
|
|
|
async post(opts) {
|
|
|
try {
|
|
|
let response = await this._fetch({
|
|
|
method: 'POST',
|
|
|
url: opts.url,
|
|
|
body: opts.body,
|
|
|
timeout: opts.timeout | 0,
|
|
|
});
|
|
|
|
|
|
let data = await this._parseResponse(response);
|
...
|
...
|
@@ -67,17 +99,16 @@ export default class Request { |
|
|
* ### _fetch
|
|
|
*/
|
|
|
async _fetch(opts) {
|
|
|
let defaultOpts = {
|
|
|
method: 'GET',
|
|
|
url: null,
|
|
|
body: null,
|
|
|
callback: null
|
|
|
};
|
|
|
let defaultOpts = this._defaultOpts();
|
|
|
opts = {
|
|
|
...defaultOpts,
|
|
|
...opts
|
|
|
};
|
|
|
opts.body = this._createBody();
|
|
|
opts.body = this._createBody(opts.body);
|
|
|
|
|
|
if (opts.timeout === 0) {
|
|
|
opts.timeout = this.timeout;
|
|
|
}
|
|
|
|
|
|
let reqOpts = {
|
|
|
method: opts.method,
|
...
|
...
|
@@ -87,7 +118,8 @@ export default class Request { |
|
|
};
|
|
|
|
|
|
if (opts.method === 'GET' || opts.method === 'POST') {
|
|
|
reqOpts.headers['Accept'] = 'application/json';
|
|
|
// reqOpts.headers['Accept'] = 'application/json';
|
|
|
reqOpts.headers['Content-Type'] = 'application/x-www-form-urlencoded';
|
|
|
}
|
|
|
|
|
|
let queryStrigPair = this._signParam(opts.body);
|
...
|
...
|
@@ -100,8 +132,20 @@ export default class Request { |
|
|
reqOpts.body = queryStrigPair;
|
|
|
}
|
|
|
|
|
|
if (opts.timeout && opts.timeout > 0) {
|
|
|
return await timeoutPromise(opts.timeout, fetch(this.baseUrl + opts.url, reqOpts));
|
|
|
}
|
|
|
|
|
|
return await fetch(this.baseUrl + opts.url, reqOpts);
|
|
|
}
|
|
|
|
|
|
_defaultOpts() {
|
|
|
return {
|
|
|
method: 'GET',
|
|
|
url: null,
|
|
|
body: null,
|
|
|
callback: null,
|
|
|
};
|
|
|
}
|
|
|
|
|
|
_publicParams() {
|
...
|
...
|
|