...
|
...
|
@@ -3,30 +3,53 @@ |
|
|
* @author: xuqi<qi.xu@yoho.cn>
|
|
|
* @date: 2016/3/28
|
|
|
*/
|
|
|
'use strict';
|
|
|
var rp = require('request-promise');
|
|
|
var errUtil = require('../util/error');
|
|
|
var sign = require('./sign');
|
|
|
|
|
|
const API_URL = 'http://testapi.yoho.cn:28078';
|
|
|
var API_CONFIG = {
|
|
|
API_URL: 'http://testapi.yoho.cn:28078',
|
|
|
SERVICE_URL: 'http://testservice.yoho.cn:28077'
|
|
|
};
|
|
|
|
|
|
// 最好有后端提供的各接口对应什么服务器说明,接口文档
|
|
|
// const DEV_API_URL = 'http://devapi.yoho.cn:58078';
|
|
|
// const SERVICE_URL = 'http://testservice.yoho.cn:28077';
|
|
|
// const YOHOBUY_URL = 'http://www.yohobuy.com';
|
|
|
// const API_OLD = 'http://test2.open.yohobuy.com';
|
|
|
if (process.env.NODE_ENV === 'production') {
|
|
|
|
|
|
// 正式环境接口地址
|
|
|
API_CONFIG = {
|
|
|
API_URL: 'http://apih5.yoho.cn/',
|
|
|
SERVICE_URL: 'http://serviceh5.yoho.cn/'
|
|
|
};
|
|
|
}
|
|
|
|
|
|
class API {
|
|
|
constructor() {
|
|
|
|
|
|
// var api = new API(); 默认调用新版接口
|
|
|
// var api = new API('SERVICE_URL'); 服务调用方法
|
|
|
|
|
|
constructor(type) {
|
|
|
this.type = type || 'API_URL';
|
|
|
this.headers = {
|
|
|
'User-Agent': 'YOHO WEB NODE' // TODO: 请求的服务端是否有要求格式,是否添加
|
|
|
'User-Agent': 'YOHO WEB NODE'
|
|
|
};
|
|
|
this.timeout = 5000;
|
|
|
this.server = API_CONFIG[type];
|
|
|
this.data = {};
|
|
|
|
|
|
// TODO: 后端接口是否需求签名校验未知,如果需要需要添加
|
|
|
if (this.type === 'API_URL') {
|
|
|
this.data.v = 7;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
get(url, data) {
|
|
|
data = Object.assign(this.data, data);
|
|
|
|
|
|
if (this.type === 'API_URL') {
|
|
|
data = sign.apiSign(data);
|
|
|
}
|
|
|
|
|
|
return rp({
|
|
|
url: `${API_URL}${url}`,
|
|
|
url: `${this.server}${url}`,
|
|
|
headers: this.headers,
|
|
|
timeout: this.timeout,
|
|
|
qs: data
|
...
|
...
|
@@ -41,8 +64,14 @@ class API { |
|
|
var rps = [];
|
|
|
|
|
|
for (let el of urls.values()) {
|
|
|
el.data = Object.assign(this.data, el.data);
|
|
|
|
|
|
if (this.type === 'API_URL') {
|
|
|
el.data = sign.apiSign(el.data);
|
|
|
}
|
|
|
|
|
|
rps.push(rp({
|
|
|
url: `${API_URL}${el.url}`,
|
|
|
url: `${this.server}${el.url}`,
|
|
|
headers: this.headers,
|
|
|
timeout: this.timeout,
|
|
|
qs: el.data
|
...
|
...
|
@@ -55,12 +84,18 @@ class API { |
|
|
}
|
|
|
|
|
|
post(url, data) {
|
|
|
data = Object.assign(this.data, data);
|
|
|
|
|
|
if (this.type === 'API_URL') {
|
|
|
data = sign.apiSign(data);
|
|
|
}
|
|
|
|
|
|
return rp({
|
|
|
url: `${API_URL}${url}`,
|
|
|
url: `${this.server}${url}`,
|
|
|
method: 'post',
|
|
|
headers: this.headers,
|
|
|
timeout: this.timeout,
|
|
|
form: data // TODO: post 请求格式不知后端要求 json 还是 form 提交,需要后续完善
|
|
|
form: data
|
|
|
}).catch(errUtil.apiError);
|
|
|
}
|
|
|
}
|
...
|
...
|
|