From 387577cd02dc658c2658304df02f858a9f0d0382 Mon Sep 17 00:00:00 2001 From: shiziyuan9527 Date: Sat, 18 Sep 2021 15:45:33 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=8B=BC=E6=8E=A5=E8=AF=B7=E6=B1=82?= =?UTF-8?q?=E5=8F=82=E6=95=B0=E3=80=81=E8=AF=B7=E6=B1=82header?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../project/function/ScriptNavMenu.vue | 29 +++++- .../project/function/custom-function.js | 95 +++++++++++-------- 2 files changed, 84 insertions(+), 40 deletions(-) diff --git a/frontend/src/business/components/settings/project/function/ScriptNavMenu.vue b/frontend/src/business/components/settings/project/function/ScriptNavMenu.vue index ba86458320..3c91e0d2a3 100644 --- a/frontend/src/business/components/settings/project/function/ScriptNavMenu.vue +++ b/frontend/src/business/components/settings/project/function/ScriptNavMenu.vue @@ -67,12 +67,26 @@ export default { } let code = ""; if (data.length > 0) { - data.forEach(dt => { + for (let dt of data) { + // 过滤非HTTP接口API + if (dt.protocol !== "HTTP") { + if (!dt.request) { + continue; + } else { + // 是否是HTTP接口CASE + if (dt.request) { + let req = JSON.parse(dt.request); + if (req.protocol !== "HTTP") { + continue; + } + } + } + } let param = this._parseRequestObj(dt); param['host'] = host; param['protocol'] = protocol; code += '\n' + getCodeTemplate(this.language, param); - }) + } } this.handleCodeTemplate(code); this.$refs.apiFuncRelevance.close(); @@ -82,6 +96,7 @@ export default { }, _parseRequestObj(data) { let requestHeaders = new Map(); + let requestArguments = new Map(); let requestMethod = ""; let requestBody = ""; let requestPath = ""; @@ -97,11 +112,19 @@ export default { } }) } + let args = request.arguments; + if (args && args.length) { + args.forEach(arg => { + if (arg.name) { + requestArguments.set(arg.name, arg.value); + } + }) + } let body = request.body; if (body.json) { requestBody = body.raw; } - return {requestPath, requestHeaders, requestMethod, requestBody} + return {requestPath, requestHeaders, requestMethod, requestBody, requestArguments} }, apiClose() { diff --git a/frontend/src/business/components/settings/project/function/custom-function.js b/frontend/src/business/components/settings/project/function/custom-function.js index ab62a1020e..30b4f3453c 100644 --- a/frontend/src/business/components/settings/project/function/custom-function.js +++ b/frontend/src/business/components/settings/project/function/custom-function.js @@ -24,17 +24,15 @@ export function getCodeTemplate(language, requestObj) { } function groovyCode(requestObj) { - let {requestHeaders = new Map(), requestBody = "", requestPath = "", requestMethod = "", host = "", protocol = ""} = requestObj; + let {requestHeaders = new Map(), requestBody = "", requestPath = "", + requestMethod = "", host = "", protocol = "", requestArguments = new Map()} = requestObj; let requestUrl = ""; + requestPath = getRequestPath(requestArguments, requestPath); if (protocol && host && requestPath) { requestUrl = protocol + "://" + host + requestPath; } - let headers = "", body = JSON.stringify(requestBody); - for (let [k, v] of requestHeaders) { - // 拼装映射 - headers += `['${k}':'${v}']`; - } - headers = headers === "" ? "[]" : headers; + let body = JSON.stringify(requestBody); + let headers = getHeaders(requestHeaders); let params = ""; params += `[ 'url': '${requestUrl}', @@ -45,45 +43,38 @@ function groovyCode(requestObj) { return `import groovy.json.JsonOutput import groovy.json.JsonSlurper -def http_post() { - def params = ${params} - - def headers = params['headers'] - // json数据 - def data = params['data'] - def conn = new URL(params['url']).openConnection() - conn.setRequestMethod(params['method']) - if (data) { - headers.each { - k,v -> conn.setRequestProperty(k, v); - } - // 输出请求参数 - log.info(data) - conn.doOutput = true - def writer = new OutputStreamWriter(conn.outputStream) - writer.write(data) - writer.flush() - writer.close() - } - log.info(conn.content.text) +def params = ${params} +def headers = params['headers'] +// json数据 +def data = params['data'] +def conn = new URL(params['url']).openConnection() +conn.setRequestMethod(params['method']) +if (data) { + headers.each { + k,v -> conn.setRequestProperty(k, v); + } + // 输出请求参数 + log.info(data) + conn.doOutput = true + def writer = new OutputStreamWriter(conn.outputStream) + writer.write(data) + writer.flush() + writer.close() } -http_post() +log.info(conn.content.text) `; } function pythonCode(requestObj) { - let {requestHeaders = new Map(), requestBody = "", requestPath = "", requestMethod = "", host = "", protocol = "http"} = requestObj; + let {requestHeaders = new Map(), requestBody = "", requestPath = "/", + requestMethod = "", host = "", protocol = "http", requestArguments = new Map()} = requestObj; let connType = "HTTPConnection"; if (protocol === 'https') { connType = "HTTPSConnection"; } - let headers = ""; - for (let [k, v] of requestHeaders) { - // 拼装 - headers += `{'${k}':'${v}'}`; - } - headers = headers === "" ? "{}" : headers; + let headers = getHeaders(requestHeaders); requestBody = requestBody ? requestBody : "{}"; + requestPath = getRequestPath(requestArguments, requestPath); return `import httplib params = ${requestBody} #例 {'username':'test'} headers = ${headers} #例 {'Content-Type':'application/json'} @@ -96,7 +87,7 @@ conn.request(method, path, params, headers) res = conn.getresponse() data = unicode(res.read(), 'utf-8') log.info(data) - ` + `; } function javaCode(requestObj) { @@ -106,3 +97,33 @@ function javaCode(requestObj) { function jsCode(requestObj) { return ``; } + +function getRequestPath(requestArgs, requestPath) { + if (requestArgs.size > 0) { + requestPath = requestPath + "?" + let index = 1; + for (let [k, v] of requestArgs) { + if (index !== 1) { + requestPath = requestPath + "&"; + } + requestPath = requestPath + k + "=" + v; + index++; + } + } + return requestPath; +} + +function getHeaders(requestHeaders) { + let headers = "{"; + let index = 1; + for (let [k, v] of requestHeaders) { + if (index !== 1) { + headers += ","; + } + // 拼装 + headers += `'${k}':'${v}'`; + index ++; + } + headers = headers + "}" + return headers; +}