mirror of
https://gitee.com/durcframework/SOP.git
synced 2025-08-11 21:57:56 +08:00
2.0
This commit is contained in:
@@ -20,6 +20,10 @@ public class DocItem {
|
||||
/** http method列表 */
|
||||
private Collection<String> httpMethodList;
|
||||
|
||||
private Collection<String> produces;
|
||||
|
||||
|
||||
|
||||
List<DocParameter> requestParameters;
|
||||
List<DocParameter> responseParameters;
|
||||
|
||||
|
@@ -13,6 +13,7 @@ import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.RequestBody;
|
||||
import okhttp3.Response;
|
||||
import okhttp3.ResponseBody;
|
||||
import org.apache.commons.codec.digest.DigestUtils;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
@@ -243,6 +244,51 @@ public class HttpTool {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 请求数据
|
||||
*
|
||||
* @param url 请求url
|
||||
* @param form 请求数据
|
||||
* @param header header
|
||||
* @return 返回Response
|
||||
* @throws IOException
|
||||
*/
|
||||
public Response requestForResponse(String url, Map<String, ?> form, Map<String, String> header, HTTPMethod method) throws IOException {
|
||||
Request.Builder requestBuilder = buildRequestBuilder(url, form, method);
|
||||
// 添加header
|
||||
addHeader(requestBuilder, header);
|
||||
|
||||
Request request = requestBuilder.build();
|
||||
return httpClient
|
||||
.newCall(request)
|
||||
.execute();
|
||||
}
|
||||
|
||||
/**
|
||||
* 下载文件
|
||||
*
|
||||
* @param url 请求url
|
||||
* @param form 请求数据
|
||||
* @param header header
|
||||
* @return 返回文件流
|
||||
* @throws IOException
|
||||
*/
|
||||
public InputStream downloadFile(String url, Map<String, ?> form, Map<String, String> header) throws IOException {
|
||||
Request.Builder requestBuilder = buildRequestBuilder(url, form, HTTPMethod.GET);
|
||||
// 添加header
|
||||
addHeader(requestBuilder, header);
|
||||
|
||||
Request request = requestBuilder.build();
|
||||
Response response = httpClient
|
||||
.newCall(request)
|
||||
.execute();
|
||||
if (response.isSuccessful()) {
|
||||
ResponseBody body = response.body();
|
||||
return body == null ? null : body.byteStream();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private void addHeader(Request.Builder builder, Map<String, String> header) {
|
||||
if (header != null) {
|
||||
Set<Map.Entry<String, String>> entrySet = header.entrySet();
|
||||
|
@@ -6,6 +6,9 @@ import com.gitee.sop.websiteserver.sign.AlipaySignature;
|
||||
import com.gitee.sop.websiteserver.util.UploadUtil;
|
||||
import lombok.Data;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import okhttp3.Headers;
|
||||
import okhttp3.Response;
|
||||
import okhttp3.ResponseBody;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.apache.http.NameValuePair;
|
||||
@@ -25,6 +28,7 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Collection;
|
||||
@@ -59,7 +63,10 @@ public class SandboxController {
|
||||
, @RequestParam String version
|
||||
, @RequestParam String bizContent
|
||||
, @RequestParam(defaultValue = "get") String httpMethod
|
||||
, HttpServletRequest request) throws AlipayApiException {
|
||||
, @RequestParam(defaultValue = "false") boolean isDownloadRequest
|
||||
, HttpServletRequest request
|
||||
, HttpServletResponse response
|
||||
) throws AlipayApiException {
|
||||
|
||||
Assert.isTrue(StringUtils.isNotBlank(appId), "AppId不能为空");
|
||||
Assert.isTrue(StringUtils.isNotBlank(privateKey), "PrivateKey不能为空");
|
||||
@@ -110,7 +117,21 @@ public class SandboxController {
|
||||
|
||||
try {
|
||||
String responseData;
|
||||
if (!CollectionUtils.isEmpty(files)) {
|
||||
if (isDownloadRequest) {
|
||||
Response resp = httpTool.requestForResponse(url, params, Collections.emptyMap(), HttpTool.HTTPMethod.GET);
|
||||
Headers respHeaders = resp.headers();
|
||||
ResponseBody body = resp.body();
|
||||
if (body == null) {
|
||||
return null;
|
||||
}
|
||||
respHeaders
|
||||
.names()
|
||||
.forEach(name -> response.setHeader(name, respHeaders.get(name)));
|
||||
|
||||
IOUtils.copy(body.byteStream(), response.getOutputStream());
|
||||
response.flushBuffer();
|
||||
return null;
|
||||
} else if (!CollectionUtils.isEmpty(files)) {
|
||||
responseData = httpTool.requestFile(url, params, Collections.emptyMap(), files);
|
||||
} else {
|
||||
responseData = httpTool.request(url, params, Collections.emptyMap(), HttpTool.HTTPMethod.fromValue(httpMethod));
|
||||
@@ -118,6 +139,7 @@ public class SandboxController {
|
||||
result.apiResult = responseData;
|
||||
return result;
|
||||
} catch (Exception e) {
|
||||
log.error("请求失败", e);
|
||||
throw new RuntimeException("请求失败");
|
||||
}
|
||||
}
|
||||
|
@@ -92,6 +92,7 @@ public class SwaggerDocParser implements DocParser {
|
||||
docItem.setSummary(docInfo.getString("summary"));
|
||||
docItem.setDescription(docInfo.getString("description"));
|
||||
docItem.setMultiple(docInfo.getString("multiple") != null);
|
||||
docItem.setProduces(docInfo.getJSONArray("produces").toJavaList(String.class));
|
||||
String moduleName = this.buildModuleName(docInfo, docRoot);
|
||||
docItem.setModule(moduleName);
|
||||
List<DocParameter> docParameterList = this.buildRequestParameterList(docInfo, docRoot);
|
||||
|
@@ -92,21 +92,21 @@ function createTreeTable(id, data) {
|
||||
cols: [[
|
||||
{field: 'name', title: '参数',width: 200}
|
||||
,{field: 'val', title: '值', width: 300, templet:function (row) {
|
||||
var id = currentItem.nameVersion + '-' + row.name;
|
||||
var requiredTxt = row.required ? 'required lay-verify="required"' : '';
|
||||
var module = row.module;
|
||||
var type = row.type == 'file' ? 'file' : 'text';
|
||||
var attrs = [
|
||||
'id="' + id + '"'
|
||||
, 'name="'+row.name+'"'
|
||||
, 'class="layui-input test-input"'
|
||||
, 'type="' + type + '"'
|
||||
, requiredTxt
|
||||
, 'module="'+module+'"'
|
||||
];
|
||||
var id = currentItem.nameVersion + '-' + row.name;
|
||||
var requiredTxt = row.required ? 'required lay-verify="required"' : '';
|
||||
var module = row.module;
|
||||
var type = row.type == 'file' ? 'file' : 'text';
|
||||
var attrs = [
|
||||
'id="' + id + '"'
|
||||
, 'name="'+row.name+'"'
|
||||
, 'class="layui-input test-input"'
|
||||
, 'type="' + type + '"'
|
||||
, requiredTxt
|
||||
, 'module="'+module+'"'
|
||||
];
|
||||
|
||||
return !row.refs ? '<input ' + attrs.join(' ') + '/>' : '';
|
||||
}}
|
||||
return !row.refs ? '<input ' + attrs.join(' ') + '/>' : '';
|
||||
}}
|
||||
,{field: 'description', title: '描述'}
|
||||
]]
|
||||
});
|
||||
@@ -138,6 +138,12 @@ function doTest() {
|
||||
}
|
||||
});
|
||||
data.bizContent = JSON.stringify(bizContent);
|
||||
if (isDownloadRequest(currentItem)) {
|
||||
data.isDownloadRequest = true;
|
||||
downloadFile(data);
|
||||
// window.open()
|
||||
return;
|
||||
}
|
||||
// 确定文件数量,并且知道参数名称
|
||||
if (uploadFileObjects.length > 0) {
|
||||
var formData = new FormData();
|
||||
@@ -151,6 +157,20 @@ function doTest() {
|
||||
}
|
||||
}
|
||||
|
||||
function isDownloadRequest(currentItem) {
|
||||
var produces = currentItem.produces;
|
||||
if (!produces) {
|
||||
return false;
|
||||
}
|
||||
for (var i = 0; i < produces.length; i++) {
|
||||
var produce = produces[i];
|
||||
if (produce.indexOf('application/octet-stream') > -1) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function putVal(obj, input) {
|
||||
if (input.type == 'text') {
|
||||
obj[input.name] = input.value;
|
||||
@@ -204,6 +224,16 @@ function postFile(data, formData) {
|
||||
});
|
||||
}
|
||||
|
||||
function downloadFile(data) {
|
||||
$('.dl-form').remove();
|
||||
var url = SopConfig.url + '/sandbox/test';
|
||||
var form = $('<form class="dl-form"></form>').attr("action", url).attr("method", "get");
|
||||
for(var key in data) {
|
||||
form.append($("<input>").attr("type", "hidden").attr("name", key).attr("value", data[key]));
|
||||
}
|
||||
form.appendTo('body').submit();
|
||||
}
|
||||
|
||||
function successHandler(resp) {
|
||||
setReqInfo(resp);
|
||||
showRespnfo(resp.apiResult);
|
||||
|
Reference in New Issue
Block a user