Commit 13329924 authored by 赵啸非's avatar 赵啸非

修改配置文件

parent 5e180d6f
package com.mortals.xhx.busiz.web; package com.mortals.xhx.busiz.web;
import cn.hutool.core.codec.Base64;
import cn.hutool.http.HttpUtil; import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.annotation.UnAuth; import com.mortals.framework.annotation.UnAuth;
import com.mortals.framework.common.Rest; import com.mortals.framework.common.Rest;
import com.mortals.xhx.base.system.upload.service.UploadService;
import com.mortals.xhx.busiz.rsp.ApiResp; import com.mortals.xhx.busiz.rsp.ApiResp;
import com.mortals.xhx.common.code.ApiRespCodeEnum; import com.mortals.xhx.common.code.ApiRespCodeEnum;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import okhttp3.*; import okhttp3.*;
import org.apache.http.util.TextUtils; import org.apache.http.util.TextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.util.ObjectUtils; import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
...@@ -17,6 +20,8 @@ import org.springframework.web.bind.annotation.RequestBody; ...@@ -17,6 +20,8 @@ import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.reactive.function.client.WebClient; import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.Map; import java.util.Map;
...@@ -27,6 +32,8 @@ import java.util.Map; ...@@ -27,6 +32,8 @@ import java.util.Map;
public class ProxyController { public class ProxyController {
private final WebClient webClient; private final WebClient webClient;
@Autowired
private UploadService uploadService;
private final OkHttpClient client = new OkHttpClient(); private final OkHttpClient client = new OkHttpClient();
...@@ -177,48 +184,48 @@ public class ProxyController { ...@@ -177,48 +184,48 @@ public class ProxyController {
} }
} else if ("get".equalsIgnoreCase(method)) { } else if ("get".equalsIgnoreCase(method)) {
Map<String, Object> map = req.toJavaObject(Map.class); Map<String, Object> map = req.toJavaObject(Map.class);
/* String bodyParams = getBodyParams(map); /* String bodyParams = getBodyParams(map);
log.info(bodyParams);*/ log.info(bodyParams);*/
// String urlGet = HttpUtil.urlWithForm(url.toString(), map, Charset.forName("utf-8"), false); // String urlGet = HttpUtil.urlWithForm(url.toString(), map, Charset.forName("utf-8"), false);
try { try {
String resp = HttpUtil.get(url.toString(), map, 30 * 1000); String resp = HttpUtil.get(url.toString(), map, 30 * 1000);
//String resp = HttpUtil.get(urlGet); //todo 将文件转换成base64
return Rest.ok("透传请求成功!", resp);
} catch (Exception e) {
rsp.setCode(ApiRespCodeEnum.FAILED.getValue());
rsp.setMsg("Proxy error: " + e.getMessage());
return Rest.fail(rsp.getMsg());
}
/* //url拼接参数 if ("/base/matter/interinfo".equals(path)) {
Request.Builder requestBuilder = new Request.Builder() JSONObject rspJson = JSON.parseObject(resp);
.url(urlGet);
// 透传 Headers(过滤掉 Host 避免冲突) JSONObject data = rspJson.getJSONObject("data");
headers.forEach((key, value) -> {
if (!key.equalsIgnoreCase("host")) { JSONObject interdata = data.getJSONObject("data");
requestBuilder.addHeader(key, value);
String tid = interdata.getString("tid");
if (!ObjectUtils.isEmpty(tid) && "/file".startsWith(tid)) {
String filePath = uploadService.getFilePath(tid);
String base64Str = Base64.encode(new File(filePath));
log.info("base64Str:{}", base64Str);
interdata.put("tid", base64Str);
return Rest.ok("透传请求成功!", rspJson.toJSONString());
} }
});
log.info("get透传请求地址,urlGet:{},rquestBuilder:{}",urlGet, JSON.toJSONString(requestBuilder));
// 发送请求
try (Response response = client.newCall(requestBuilder.build()).execute()) {
if (!response.isSuccessful()) {
return Rest.fail(response.code(), response.message());
//return ResponseEntity.status(response.code()).body(response.message());
} }
return Rest.ok("透传请求成功!", response.body().string());
// return ResponseEntity.status(response.code()).body(response.body().string()); // rspJson.get
} catch (IOException e) {
//String resp = HttpUtil.get(urlGet);
return Rest.ok("透传请求成功!", resp);
} catch (Exception e) {
rsp.setCode(ApiRespCodeEnum.FAILED.getValue()); rsp.setCode(ApiRespCodeEnum.FAILED.getValue());
rsp.setMsg("Proxy error: " + e.getMessage()); rsp.setMsg("Proxy error: " + e.getMessage());
return Rest.fail(rsp.getMsg()); return Rest.fail(rsp.getMsg());
// return ResponseEntity.ok().body(JSON.toJSONString(rsp)); }
}*/
} else { } else {
rsp.setCode(ApiRespCodeEnum.FAILED.getValue()); rsp.setCode(ApiRespCodeEnum.FAILED.getValue());
rsp.setMsg("method is not support!"); rsp.setMsg("method is not support!");
......
...@@ -78,7 +78,7 @@ Content-Type: application/json ...@@ -78,7 +78,7 @@ Content-Type: application/json
{ {
"id": "1", "id": "1",
"path": "/entservice/ent/life/cycle/interinfo", "path": "/base/matter/interinfo",
"method": "get" "method": "get"
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment