From 472942e93e2b98434c00cacc95260feef4dbd05f Mon Sep 17 00:00:00 2001 From: Changeden Date: Mon, 12 Apr 2021 15:23:19 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96sdk-nodejs=E4=BB=A5=E4=B8=8B?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=EF=BC=9A=201=E3=80=81=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=E5=90=8C=E6=AD=A5=E8=AF=B7=E6=B1=82=E6=96=B9=E5=BC=8F=EF=BC=88?= =?UTF-8?q?executeSync=E3=80=81executeTokenSync=EF=BC=89=EF=BC=9B=202?= =?UTF-8?q?=E3=80=81=E4=BC=98=E5=8C=96=E8=AF=B7=E6=B1=82=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E4=B8=AD=E6=96=AD=E9=97=AE=E9=A2=98=EF=BC=8C=E8=BF=94=E5=9B=9E?= =?UTF-8?q?502=E9=94=99=E8=AF=AF=E7=A0=81=EF=BC=9B=203=E3=80=81=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E7=AD=BE=E5=90=8D=E6=96=B9=E6=B3=95=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sop-sdk/sdk-nodejs/common/OpenClient.js | 83 ++++++++++++++++++------- sop-sdk/sdk-nodejs/common/SignUtil.js | 23 +++---- sop-sdk/sdk-nodejs/main.js | 26 ++++++++ 3 files changed, 98 insertions(+), 34 deletions(-) diff --git a/sop-sdk/sdk-nodejs/common/OpenClient.js b/sop-sdk/sdk-nodejs/common/OpenClient.js index 3da27c36..38aae1bd 100644 --- a/sop-sdk/sdk-nodejs/common/OpenClient.js +++ b/sop-sdk/sdk-nodejs/common/OpenClient.js @@ -33,44 +33,75 @@ const OpenClient = Class.create({ /** * 发送请求 * @param request 请求类 - * @param callback 回调函数,参数json + * @param callback 回调函数,参数json(undefined则使用executeSync) */ 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 token token - * @param callback 回调函数,参数json + * @param callback 回调函数,参数json(undefined则使用executeTokenSync) */ executeToken: function (request, token, callback) { if (!(request instanceof BaseRequest)) { throw 'request类未继承BaseRequest' } - const requestType = request.getRequestType(); - if (request.files) { - this._postFile(request, callback); - } else { - switch (requestType) { - case RequestType.GET: - this._get(request, callback); - break - case RequestType.POST_FORM: - this._postForm(request, callback); - break - case RequestType.POST_JSON: - this._postJson(request, callback); - break - case RequestType.POST_FILE: - this._postFile(request, callback); - break - default :{ - throw 'request.getRequestType()类型不正确' + if (typeof callback == 'function') { + const requestType = request.getRequestType(); + if (request.files) { + this._postFile(request, callback); + } else { + switch (requestType) { + case RequestType.GET: + this._get(request, callback); + break + case RequestType.POST_FORM: + this._postForm(request, callback); + break + case RequestType.POST_JSON: + this._postJson(request, callback); + break + case RequestType.POST_FILE: + this._postFile(request, callback); + break + 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) { const allParams = this._buildParams(request) const that = this @@ -120,7 +151,13 @@ const OpenClient = Class.create({ if (!error && response.statusCode === 200) { return request.parseResponse(response.body) } else { - throw '请求异常:' + error + // throw '请求异常:' + error + return { // 重新封装请求异常回调,以防中断 + msg: '请求异常', + code: '502', + sub_msg: `${error}`, + sub_code: 'isv.invalid-server' + } } }, _buildParams: function (request, token) { diff --git a/sop-sdk/sdk-nodejs/common/SignUtil.js b/sop-sdk/sdk-nodejs/common/SignUtil.js index 03d757a6..d4300aba 100644 --- a/sop-sdk/sdk-nodejs/common/SignUtil.js +++ b/sop-sdk/sdk-nodejs/common/SignUtil.js @@ -68,21 +68,22 @@ exports.SignUtil = { */ getSignContent: function (params) { const paramNames = [] - for(const key in params) { - paramNames.push(key) - } + // 获取对象中的Key + paramNames.push(...Object.keys(params || {}) + // 过滤无效的KeyValue + .filter(paramName => { + // 参数名不为undefined且参数值不为undefined + return !(typeof paramName === undefined || typeof params[paramName] === undefined) + })) paramNames.sort() - const paramNameValue = [] - - for (let i = 0, len = paramNames.length; i < len; i++) { - const paramName = paramNames[i]; + // 合成签名字符串 + const paramNameValue = paramNames.map(paramName => { const val = params[paramName]; - if (paramName && val) { - paramNameValue.push(`${paramName}=${val}`) - } - } + return `${paramName}=${val}` + }) + return paramNameValue.join('&') } } diff --git a/sop-sdk/sdk-nodejs/main.js b/sop-sdk/sdk-nodejs/main.js index f8d8987e..6d9a2370 100644 --- a/sop-sdk/sdk-nodejs/main.js +++ b/sop-sdk/sdk-nodejs/main.js @@ -30,6 +30,7 @@ function test() { // ] openClient.execute(request, data => { + console.log('异步请求') // 成功 if (!data.sub_code) { console.log('成功', data); @@ -37,6 +38,31 @@ function test() { 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() }