优化sdk-nodejs以下功能:

1、新增同步请求方式(executeSync、executeTokenSync);
2、优化请求异常中断问题,返回502错误码;
3、优化签名方法;
pull/11/head
Changeden 4 years ago
parent a1fdf71f3e
commit 472942e93e
  1. 83
      sop-sdk/sdk-nodejs/common/OpenClient.js
  2. 23
      sop-sdk/sdk-nodejs/common/SignUtil.js
  3. 26
      sop-sdk/sdk-nodejs/main.js

@ -33,44 +33,75 @@ const OpenClient = Class.create({
/** /**
* 发送请求 * 发送请求
* @param request 请求类 * @param request 请求类
* @param callback 回调函数参数json * @param callback 回调函数参数jsonundefined则使用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 回调函数参数jsonundefined则使用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'
} }
const requestType = request.getRequestType(); if (typeof callback == 'function') {
if (request.files) { const requestType = request.getRequestType();
this._postFile(request, callback); if (request.files) {
} else { this._postFile(request, callback);
switch (requestType) { } else {
case RequestType.GET: switch (requestType) {
this._get(request, callback); case RequestType.GET:
break this._get(request, callback);
case RequestType.POST_FORM: break
this._postForm(request, callback); case RequestType.POST_FORM:
break this._postForm(request, callback);
case RequestType.POST_JSON: break
this._postJson(request, callback); case RequestType.POST_JSON:
break this._postJson(request, callback);
case RequestType.POST_FILE: break
this._postFile(request, callback); case RequestType.POST_FILE:
break this._postFile(request, callback);
default :{ break
throw 'request.getRequestType()类型不正确' default : {
throw 'request.getRequestType()类型不正确'
}
} }
} }
} else {
return this.executeTokenSync(request, token)
} }
}, },
/**
* 发送同步请求
* @param request 请求类
* @param token token
*/
executeTokenSync: function (request, token) {
return new Promise((resolve) => {
this.executeToken(request, token, res => {
resolve(res)
})
})
},
_get: function(request, callback) { _get: function(request, callback) {
const allParams = this._buildParams(request) const allParams = this._buildParams(request)
const that = this const that = this
@ -120,7 +151,13 @@ const OpenClient = Class.create({
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) {

@ -68,21 +68,22 @@ exports.SignUtil = {
*/ */
getSignContent: function (params) { getSignContent: function (params) {
const paramNames = [] const paramNames = []
for(const key in params) { // 获取对象中的Key
paramNames.push(key) paramNames.push(...Object.keys(params || {})
} // 过滤无效的KeyValue
.filter(paramName => {
// 参数名不为undefined且参数值不为undefined
return !(typeof paramName === undefined || typeof params[paramName] === undefined)
}))
paramNames.sort() paramNames.sort()
const paramNameValue = [] // 合成签名字符串
const paramNameValue = paramNames.map(paramName => {
for (let i = 0, len = paramNames.length; i < len; i++) {
const paramName = paramNames[i];
const val = params[paramName]; const val = params[paramName];
if (paramName && val) { return `${paramName}=${val}`
paramNameValue.push(`${paramName}=${val}`) })
}
}
return paramNameValue.join('&') return paramNameValue.join('&')
} }
} }

@ -30,6 +30,7 @@ function test() {
// ] // ]
openClient.execute(request, data => { openClient.execute(request, data => {
console.log('异步请求')
// 成功 // 成功
if (!data.sub_code) { if (!data.sub_code) {
console.log('成功', data); console.log('成功', data);
@ -37,6 +38,31 @@ function test() {
console.error('失败', data) console.error('失败', data)
} }
}) })
// 使用Promise进行封装
openClient.executeSync(request).then(data => {
console.log('同步请求-Promise')
// 成功
if (!data.sub_code) {
console.log('成功', data);
} else {
console.error('失败', data)
}
})
// 使用Async/Await进行封装
async function syncRequest () {
const data = await openClient.execute(request)
console.log('同步请求-Async/Await')
// 成功
if (!data.sub_code) {
console.log('成功', data);
} else {
console.error('失败', data)
}
}
syncRequest()
} }

Loading…
Cancel
Save