From 2d6370ba1b0165bf300190e86b0de2073d016a4c Mon Sep 17 00:00:00 2001 From: Changeden Date: Wed, 14 Apr 2021 10:33:57 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84sdk-nodejs-axios=E5=88=86?= =?UTF-8?q?=E6=94=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sop-sdk/sdk-nodejs-axios/common/OpenClient.js | 33 ++++++++++--------- sop-sdk/sdk-nodejs-axios/main.js | 16 ++++----- 2 files changed, 25 insertions(+), 24 deletions(-) diff --git a/sop-sdk/sdk-nodejs-axios/common/OpenClient.js b/sop-sdk/sdk-nodejs-axios/common/OpenClient.js index 0dfd5c0a..fe0d1820 100644 --- a/sop-sdk/sdk-nodejs-axios/common/OpenClient.js +++ b/sop-sdk/sdk-nodejs-axios/common/OpenClient.js @@ -6,7 +6,7 @@ const RequestType = require('./RequestType'); const SignUtil = require('./SignUtil'); const BaseRequest = require('./BaseRequest'); -const IS_RUN_IN_BROWSER = this === window; +const IS_RUN_IN_BROWSER = typeof window !== 'undefined' && this === window; const HEADERS = {'Accept-Encoding': 'identity'}; @@ -47,10 +47,11 @@ const buildParams = (instance, request, token) => { return allParams; }; -const executeRequest = async (instance = {}, request, token, callback, {headers}) => { +const executeRequest = async (instance = {}, request, token, callback, customOptions = {}) => { const params = buildParams(instance, request, token); const {url} = instance; - const options = { + let {headers} = customOptions; + const config = { url, method: 'POST', params: undefined, @@ -60,28 +61,22 @@ const executeRequest = async (instance = {}, request, token, callback, {headers} const requestType = request.getRealRequestType(); switch (requestType) { case RequestType.GET: { - options.method = 'GET'; - options.params = params; + config.method = 'GET'; + config.params = params; } break; case RequestType.POST_FORM: { headers = Object.assign(headers, { 'Content-Type': 'application/x-www-form-urlencoded' }); - options.data = qs.stringify(params); + config.data = qs.stringify(params); } break; case RequestType.POST_JSON: { - options.data = params; + config.data = params; } break; case RequestType.POST_FILE: { - Object.keys(params).forEach(key => { - const value = params[key]; - if (!(typeof key === 'undefined' || typeof value === 'undefined')) { - formData.append(key, params[key]); - } - }); let formData; if (IS_RUN_IN_BROWSER) { formData = new window.FormData() @@ -101,7 +96,13 @@ const executeRequest = async (instance = {}, request, token, callback, {headers} }); headers = Object.assign(headers, formData.getHeaders()); } - options.data = formData; + Object.keys(params).forEach(key => { + const value = params[key]; + if (!(typeof key === 'undefined' || typeof value === 'undefined')) { + formData.append(key, params[key]); + } + }); + config.data = formData; } break; default: { @@ -110,8 +111,8 @@ const executeRequest = async (instance = {}, request, token, callback, {headers} } } try { - options['headers'] = headers; - const response = await axios.request(options); + config['headers'] = headers; + const response = await axios.request(config); callback(parseResponse(undefined, response, request)); } catch (error) { callback(parseResponse(error, undefined, request)); diff --git a/sop-sdk/sdk-nodejs-axios/main.js b/sop-sdk/sdk-nodejs-axios/main.js index 82fdac2f..01a33c1f 100644 --- a/sop-sdk/sdk-nodejs-axios/main.js +++ b/sop-sdk/sdk-nodejs-axios/main.js @@ -25,14 +25,14 @@ const openClient = new OpenClient().setUrl(url).setAppId(appId).setPrivateKey(pr // 添加上传文件 // 批量添加 - const files = [ - // name: 表单名称,path:文件全路径 - {name: 'file1', path: `${__dirname}/aa.txt`}, - {name: 'file2', path: `${__dirname}/bb.txt`} - ]; - request.setFiles(files); - // 单个添加 - request.addFile('file3', `${__dirname}/package.json`); + // const files = [ + // // name: 表单名称,path:文件全路径 + // {name: 'file1', path: `${__dirname}/aa.txt`}, + // {name: 'file2', path: `${__dirname}/bb.txt`} + // ]; + // request.setFiles(files); + // // 单个添加 + // request.addFile('file3', `${__dirname}/package.json`); const data = await openClient.executeSync(request); // 成功