Authored by 于良

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

@@ -3,11 +3,14 @@ module.exports = { @@ -3,11 +3,14 @@ module.exports = {
3 // baseUrl: 'http://testapi.yoho.cn:28077', 3 // baseUrl: 'http://testapi.yoho.cn:28077',
4 baseUrl: 'http://service.yoho.cn', 4 baseUrl: 'http://service.yoho.cn',
5 privateKey: 'a85bb0674e08986c6b115d5e3a4884fa', 5 privateKey: 'a85bb0674e08986c6b115d5e3a4884fa',
  6 + httpTimeout: 30000, //毫秒
6 7
7 }, 8 },
8 prd: { 9 prd: {
9 baseUrl: 'http://service.yoho.cn', 10 baseUrl: 'http://service.yoho.cn',
10 privateKey: 'a85bb0674e08986c6b115d5e3a4884fa', 11 privateKey: 'a85bb0674e08986c6b115d5e3a4884fa',
  12 + httpTimeout: 30000, //毫秒
  13 +
11 }, 14 },
12 storeKey: { 15 storeKey: {
13 SESSION_TOKEN_KEY: 'SESSION_TOKEN_KEY', 16 SESSION_TOKEN_KEY: 'SESSION_TOKEN_KEY',
@@ -5,7 +5,7 @@ import CONFIG from '../constants/config'; @@ -5,7 +5,7 @@ import CONFIG from '../constants/config';
5 import DeviceInfo from 'react-native-device-info'; 5 import DeviceInfo from 'react-native-device-info';
6 import queryString from 'query-string'; 6 import queryString from 'query-string';
7 import md5 from 'md5'; 7 import md5 from 'md5';
8 - 8 +import timeoutPromise from '../utils/timeoutPromise';
9 9
10 export default class Request { 10 export default class Request {
11 11
@@ -15,14 +15,29 @@ export default class Request { @@ -15,14 +15,29 @@ export default class Request {
15 15
16 this.baseUrl= this.config.baseUrl; 16 this.baseUrl= this.config.baseUrl;
17 this.privateKey = this.config.privateKey; 17 this.privateKey = this.config.privateKey;
  18 + this.timeout = this.config.httpTimeout;
18 } 19 }
19 20
20 21
  22 +
  23 + /**
  24 + * GET
  25 + *
  26 + * @param {[object]} opts
  27 + *
  28 + * {
  29 + * url: '/operations/api/v6/category/getCategory', //接口地址
  30 + * timeout: 30, //毫秒
  31 + * }
  32 + *
  33 + * @return {[promise]}
  34 + */
21 async get(opts) { 35 async get(opts) {
22 try { 36 try {
23 let response = await this._fetch({ 37 let response = await this._fetch({
24 method: 'GET', 38 method: 'GET',
25 url: opts.url, 39 url: opts.url,
  40 + timeout: opts.timeout | 0,
26 }); 41 });
27 42
28 let data = await this._parseResponse(response); 43 let data = await this._parseResponse(response);
@@ -33,12 +48,29 @@ export default class Request { @@ -33,12 +48,29 @@ export default class Request {
33 } 48 }
34 } 49 }
35 50
  51 + /**
  52 + * POST
  53 + *
  54 + * @param {[object]} opts
  55 + *
  56 + * {
  57 + * url: '/operations/api/v6/category/getCategory', //接口地址
  58 + * body: {
  59 + * user: 'Lily',
  60 + * password: '111111',
  61 + * },
  62 + * timeout: 30, //毫秒
  63 + * }
  64 + *
  65 + * @return {[promise]}
  66 + */
36 async post(opts) { 67 async post(opts) {
37 try { 68 try {
38 let response = await this._fetch({ 69 let response = await this._fetch({
39 method: 'POST', 70 method: 'POST',
40 url: opts.url, 71 url: opts.url,
41 body: opts.body, 72 body: opts.body,
  73 + timeout: opts.timeout | 0,
42 }); 74 });
43 75
44 let data = await this._parseResponse(response); 76 let data = await this._parseResponse(response);
@@ -67,17 +99,16 @@ export default class Request { @@ -67,17 +99,16 @@ export default class Request {
67 * ### _fetch 99 * ### _fetch
68 */ 100 */
69 async _fetch(opts) { 101 async _fetch(opts) {
70 - let defaultOpts = {  
71 - method: 'GET',  
72 - url: null,  
73 - body: null,  
74 - callback: null  
75 - }; 102 + let defaultOpts = this._defaultOpts();
76 opts = { 103 opts = {
77 ...defaultOpts, 104 ...defaultOpts,
78 ...opts 105 ...opts
79 }; 106 };
80 - opts.body = this._createBody(); 107 + opts.body = this._createBody(opts.body);
  108 +
  109 + if (opts.timeout === 0) {
  110 + opts.timeout = this.timeout;
  111 + }
81 112
82 let reqOpts = { 113 let reqOpts = {
83 method: opts.method, 114 method: opts.method,
@@ -87,7 +118,8 @@ export default class Request { @@ -87,7 +118,8 @@ export default class Request {
87 }; 118 };
88 119
89 if (opts.method === 'GET' || opts.method === 'POST') { 120 if (opts.method === 'GET' || opts.method === 'POST') {
90 - reqOpts.headers['Accept'] = 'application/json'; 121 + // reqOpts.headers['Accept'] = 'application/json';
  122 + reqOpts.headers['Content-Type'] = 'application/x-www-form-urlencoded';
91 } 123 }
92 124
93 let queryStrigPair = this._signParam(opts.body); 125 let queryStrigPair = this._signParam(opts.body);
@@ -100,8 +132,20 @@ export default class Request { @@ -100,8 +132,20 @@ export default class Request {
100 reqOpts.body = queryStrigPair; 132 reqOpts.body = queryStrigPair;
101 } 133 }
102 134
  135 + if (opts.timeout && opts.timeout > 0) {
  136 + return await timeoutPromise(opts.timeout, fetch(this.baseUrl + opts.url, reqOpts));
  137 + }
  138 +
103 return await fetch(this.baseUrl + opts.url, reqOpts); 139 return await fetch(this.baseUrl + opts.url, reqOpts);
  140 + }
104 141
  142 + _defaultOpts() {
  143 + return {
  144 + method: 'GET',
  145 + url: null,
  146 + body: null,
  147 + callback: null,
  148 + };
105 } 149 }
106 150
107 _publicParams() { 151 _publicParams() {
  1 +'use strict';
  2 +
  3 +export default function timeoutPromise(ms, promise) {
  4 + return new Promise((resolve, reject) => {
  5 + const timeoutId = setTimeout(() => {
  6 + reject(new Error("promise timeout"))
  7 + }, ms);
  8 + promise.then(
  9 + (res) => {
  10 + clearTimeout(timeoutId);
  11 + resolve(res);
  12 + },
  13 + (err) => {
  14 + clearTimeout(timeoutId);
  15 + reject(err);
  16 + }
  17 + );
  18 + });
  19 +}