|
|
@ -1,22 +1,22 @@ |
|
|
|
const needle = require('needle') |
|
|
|
const needle = require('needle'); |
|
|
|
const moment = require('moment') |
|
|
|
const moment = require('moment'); |
|
|
|
|
|
|
|
|
|
|
|
const {Class} = require("./Class"); |
|
|
|
const {Class} = require('./Class'); |
|
|
|
const {RequestType} = require('./RequestType') |
|
|
|
const {RequestType} = require('./RequestType'); |
|
|
|
const {SignUtil} = require('./SignUtil') |
|
|
|
const {SignUtil} = require('./SignUtil'); |
|
|
|
const {BaseRequest} = require('../request/BaseRequest') |
|
|
|
const {BaseRequest} = require('../request/BaseRequest'); |
|
|
|
|
|
|
|
|
|
|
|
const HEADERS = {'Accept-Encoding': 'identity'} |
|
|
|
const HEADERS = {'Accept-Encoding': 'identity'}; |
|
|
|
|
|
|
|
|
|
|
|
const getHeaders = function (headers) { |
|
|
|
const getHeaders = function (headers) { |
|
|
|
if (!headers) { |
|
|
|
if (!headers) { |
|
|
|
return HEADERS |
|
|
|
return HEADERS; |
|
|
|
} |
|
|
|
} |
|
|
|
for (const key in HEADERS) { |
|
|
|
for (const key in HEADERS) { |
|
|
|
headers[key] = HEADERS[key]; |
|
|
|
headers[key] = HEADERS[key]; |
|
|
|
} |
|
|
|
} |
|
|
|
return headers |
|
|
|
return headers; |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
const OpenClient = Class.create({ |
|
|
|
const OpenClient = Class.create({ |
|
|
|
/** |
|
|
|
/** |
|
|
@ -33,21 +33,37 @@ const OpenClient = Class.create({ |
|
|
|
/** |
|
|
|
/** |
|
|
|
* 发送请求 |
|
|
|
* 发送请求 |
|
|
|
* @param request 请求类 |
|
|
|
* @param request 请求类 |
|
|
|
* @param callback 回调函数,参数json |
|
|
|
* @param callback 回调函数,参数json(undefined则使用executeSync) |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
execute: function (request, callback) { |
|
|
|
execute: function (request, callback) { |
|
|
|
this.executeToken(request, null, callback) |
|
|
|
if (typeof callback == 'function') { |
|
|
|
|
|
|
|
this.executeToken(request, null, callback); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
return this.executeSync(request); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 发送同步请求 |
|
|
|
|
|
|
|
* @param request 请求类 |
|
|
|
|
|
|
|
* */ |
|
|
|
|
|
|
|
executeSync: function (request) { |
|
|
|
|
|
|
|
return new Promise((resolve) => { |
|
|
|
|
|
|
|
this.execute(request, res => { |
|
|
|
|
|
|
|
resolve(res); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
}); |
|
|
|
}, |
|
|
|
}, |
|
|
|
/** |
|
|
|
/** |
|
|
|
* 发送请求 |
|
|
|
* 发送请求 |
|
|
|
* @param request 请求类 |
|
|
|
* @param request 请求类 |
|
|
|
* @param token token |
|
|
|
* @param token token |
|
|
|
* @param callback 回调函数,参数json |
|
|
|
* @param callback 回调函数,参数json(undefined则使用executeTokenSync) |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
executeToken: function (request, token, callback) { |
|
|
|
executeToken: function (request, token, callback) { |
|
|
|
if (!(request instanceof BaseRequest)) { |
|
|
|
if (!(request instanceof BaseRequest)) { |
|
|
|
throw 'request类未继承BaseRequest' |
|
|
|
throw 'request类未继承BaseRequest'; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if (typeof callback == 'function') { |
|
|
|
const requestType = request.getRequestType(); |
|
|
|
const requestType = request.getRequestType(); |
|
|
|
if (request.files) { |
|
|
|
if (request.files) { |
|
|
|
this._postFile(request, callback); |
|
|
|
this._postFile(request, callback); |
|
|
@ -55,72 +71,93 @@ const OpenClient = Class.create({ |
|
|
|
switch (requestType) { |
|
|
|
switch (requestType) { |
|
|
|
case RequestType.GET: |
|
|
|
case RequestType.GET: |
|
|
|
this._get(request, callback); |
|
|
|
this._get(request, callback); |
|
|
|
break |
|
|
|
break; |
|
|
|
case RequestType.POST_FORM: |
|
|
|
case RequestType.POST_FORM: |
|
|
|
this._postForm(request, callback); |
|
|
|
this._postForm(request, callback); |
|
|
|
break |
|
|
|
break; |
|
|
|
case RequestType.POST_JSON: |
|
|
|
case RequestType.POST_JSON: |
|
|
|
this._postJson(request, callback); |
|
|
|
this._postJson(request, callback); |
|
|
|
break |
|
|
|
break; |
|
|
|
case RequestType.POST_FILE: |
|
|
|
case RequestType.POST_FILE: |
|
|
|
this._postFile(request, callback); |
|
|
|
this._postFile(request, callback); |
|
|
|
break |
|
|
|
break; |
|
|
|
default :{ |
|
|
|
default: { |
|
|
|
throw 'request.getRequestType()类型不正确' |
|
|
|
throw 'request.getRequestType()类型不正确'; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
return this.executeTokenSync(request, token); |
|
|
|
|
|
|
|
} |
|
|
|
}, |
|
|
|
}, |
|
|
|
_get: function(request, callback) { |
|
|
|
/** |
|
|
|
const allParams = this._buildParams(request) |
|
|
|
* 发送同步请求 |
|
|
|
const that = this |
|
|
|
* @param request 请求类 |
|
|
|
|
|
|
|
* @param token token |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
executeTokenSync: function (request, token) { |
|
|
|
|
|
|
|
return new Promise((resolve) => { |
|
|
|
|
|
|
|
this.executeToken(request, token, res => { |
|
|
|
|
|
|
|
resolve(res); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
_get: function (request, callback) { |
|
|
|
|
|
|
|
const allParams = this._buildParams(request); |
|
|
|
|
|
|
|
const that = this; |
|
|
|
// needle.request(method, url, data[, options][, callback])
|
|
|
|
// needle.request(method, url, data[, options][, callback])
|
|
|
|
needle.request('GET',this.url, allParams, { |
|
|
|
needle.request('GET', this.url, allParams, { |
|
|
|
headers: getHeaders() |
|
|
|
headers: getHeaders() |
|
|
|
}, function(error, response) { |
|
|
|
}, function (error, response) { |
|
|
|
callback(that._parseResponse(error, response, request)) |
|
|
|
callback(that._parseResponse(error, response, request)); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}, |
|
|
|
}, |
|
|
|
_postForm: function (request, callback) { |
|
|
|
_postForm: function (request, callback) { |
|
|
|
const allParams = this._buildParams(request) |
|
|
|
const allParams = this._buildParams(request); |
|
|
|
const that = this |
|
|
|
const that = this; |
|
|
|
needle.request('POST',this.url, allParams, { |
|
|
|
needle.request('POST', this.url, allParams, { |
|
|
|
headers: getHeaders({ |
|
|
|
headers: getHeaders({ |
|
|
|
'Content-Type': 'application/x-www-form-urlencoded' |
|
|
|
'Content-Type': 'application/x-www-form-urlencoded' |
|
|
|
}) |
|
|
|
}) |
|
|
|
}, function(error, response) { |
|
|
|
}, function (error, response) { |
|
|
|
callback(that._parseResponse(error, response, request)) |
|
|
|
callback(that._parseResponse(error, response, request)); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}, |
|
|
|
}, |
|
|
|
_postJson: function (request, callback) { |
|
|
|
_postJson: function (request, callback) { |
|
|
|
const allParams = this._buildParams(request) |
|
|
|
const allParams = this._buildParams(request); |
|
|
|
const that = this |
|
|
|
const that = this; |
|
|
|
needle.request('POST',this.url, allParams, { |
|
|
|
needle.request('POST', this.url, allParams, { |
|
|
|
headers: getHeaders(), json: true |
|
|
|
headers: getHeaders(), json: true |
|
|
|
}, function(error, response) { |
|
|
|
}, function (error, response) { |
|
|
|
callback(that._parseResponse(error, response, request)) |
|
|
|
callback(that._parseResponse(error, response, request)); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}, |
|
|
|
}, |
|
|
|
_postFile: function (request, callback) { |
|
|
|
_postFile: function (request, callback) { |
|
|
|
const allParams = this._buildParams(request) |
|
|
|
const allParams = this._buildParams(request); |
|
|
|
const files = request.files; |
|
|
|
const files = request.files; |
|
|
|
files.forEach(row => { |
|
|
|
files.forEach(row => { |
|
|
|
// 设置成{ file: row.path, content_type: 'application/octet-stream' }格式
|
|
|
|
// 设置成{ file: row.path, content_type: 'application/octet-stream' }格式
|
|
|
|
// needle会认为是上传文件
|
|
|
|
// needle会认为是上传文件
|
|
|
|
allParams[row.name] = { file: row.path, content_type: 'application/octet-stream' } |
|
|
|
allParams[row.name] = {file: row.path, content_type: 'application/octet-stream'}; |
|
|
|
}) |
|
|
|
}); |
|
|
|
const that = this |
|
|
|
const that = this; |
|
|
|
needle.request('POST',this.url, allParams, { |
|
|
|
needle.request('POST', this.url, allParams, { |
|
|
|
headers: getHeaders(), multipart: true |
|
|
|
headers: getHeaders(), multipart: true |
|
|
|
}, function(error, response) { |
|
|
|
}, function (error, response) { |
|
|
|
callback(that._parseResponse(error, response, request)) |
|
|
|
callback(that._parseResponse(error, response, request)); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}, |
|
|
|
}, |
|
|
|
_parseResponse: function (error, response, request) { |
|
|
|
_parseResponse: function (error, response, request) { |
|
|
|
if (!error && response.statusCode === 200) { |
|
|
|
if (!error && response.statusCode === 200) { |
|
|
|
return request.parseResponse(response.body) |
|
|
|
return request.parseResponse(response.body); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
throw '请求异常:' + error |
|
|
|
// throw '请求异常:' + error
|
|
|
|
|
|
|
|
return { // 重新封装请求异常回调,以防中断
|
|
|
|
|
|
|
|
msg: '请求异常', |
|
|
|
|
|
|
|
code: '502', |
|
|
|
|
|
|
|
sub_msg: `${error}`, |
|
|
|
|
|
|
|
sub_code: 'isv.invalid-server' |
|
|
|
|
|
|
|
}; |
|
|
|
} |
|
|
|
} |
|
|
|
}, |
|
|
|
}, |
|
|
|
_buildParams: function (request, token) { |
|
|
|
_buildParams: function (request, token) { |
|
|
@ -132,16 +169,16 @@ const OpenClient = Class.create({ |
|
|
|
'timestamp': moment().format('YYYY-MM-DD HH:mm:ss'), |
|
|
|
'timestamp': moment().format('YYYY-MM-DD HH:mm:ss'), |
|
|
|
'version': request.getVersion(), |
|
|
|
'version': request.getVersion(), |
|
|
|
'biz_content': JSON.stringify(request.bizModel) |
|
|
|
'biz_content': JSON.stringify(request.bizModel) |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
if (token) { |
|
|
|
if (token) { |
|
|
|
allParams['app_auth_token'] = token |
|
|
|
allParams['app_auth_token'] = token; |
|
|
|
} |
|
|
|
} |
|
|
|
// 创建签名
|
|
|
|
// 创建签名
|
|
|
|
const sign = SignUtil.createSign(allParams, this.privateKey, 'RSA2') |
|
|
|
const sign = SignUtil.createSign(allParams, this.privateKey, 'RSA2'); |
|
|
|
allParams.sign = sign |
|
|
|
allParams.sign = sign; |
|
|
|
return allParams; |
|
|
|
return allParams; |
|
|
|
} |
|
|
|
} |
|
|
|
}) |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
module.exports = OpenClient |
|
|
|
module.exports = OpenClient; |
|
|
|