Authored by 于良

增加接口请求,review by 盖剑秋

... ... @@ -3,11 +3,14 @@ module.exports = {
// baseUrl: 'http://testapi.yoho.cn:28077',
baseUrl: 'http://service.yoho.cn',
privateKey: 'a85bb0674e08986c6b115d5e3a4884fa',
httpTimeout: 30000, //毫秒
},
prd: {
baseUrl: 'http://service.yoho.cn',
privateKey: 'a85bb0674e08986c6b115d5e3a4884fa',
httpTimeout: 30000, //毫秒
},
storeKey: {
SESSION_TOKEN_KEY: 'SESSION_TOKEN_KEY',
... ...
... ... @@ -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() {
... ...
'use strict';
export default function timeoutPromise(ms, promise) {
return new Promise((resolve, reject) => {
const timeoutId = setTimeout(() => {
reject(new Error("promise timeout"))
}, ms);
promise.then(
(res) => {
clearTimeout(timeoutId);
resolve(res);
},
(err) => {
clearTimeout(timeoutId);
reject(err);
}
);
});
}
\ No newline at end of file
... ...