ai请求接口

master
chenfei 2 years ago
parent 5292c35a78
commit 2ed5c1be95

@ -1,6 +1,8 @@
package com.ruoyi.web.controller.api;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
@ -12,15 +14,13 @@ import com.ruoyi.common.utils.RequestParamsUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.system.domain.THospital;
import com.ruoyi.system.domain.TPatient;
import com.ruoyi.system.domain.TPatientHospitalDoctor;
import com.ruoyi.system.domain.TRecord;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.domain.req.*;
import com.ruoyi.system.domain.resp.TRecordResp;
import com.ruoyi.system.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
@ -29,8 +29,7 @@ import sun.misc.BASE64Decoder;
import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.*;
/**
* @author zhangcl
@ -318,12 +317,14 @@ public class AppController extends BaseController
@ApiOperation("ai接口")
public AjaxResult aiPost(@PathVariable("recordId") Long recordId, @RequestBody AiPostReq aiPostReq)
{
saveImage(aiPostReq,recordId);
ObjectMapper mapper = new ObjectMapper();
String URL = RequestParamsUtil.HTTP + ip + RequestParamsUtil.SEPARATOR + port + url;
JSONObject jsonObject = new JSONObject();
String result;
try
{
result = HttpUtils.sendPost(URL, RequestParamsUtil.buildParams(aiPostReq));
result = HttpUtils.sendPost(URL, mapper.writeValueAsString(aiPostReq));
}
catch (Exception e)
{
@ -331,7 +332,8 @@ public class AppController extends BaseController
}
if (!StringUtils.isEmpty(result))
{
jsonObject = noBase64Image(JSONObject.parseObject(result));
jsonObject = removeBase64Image(JSONObject.parseObject(result),recordId);
TRecord tRecord = new TRecord();
tRecord.setId(recordId);
tRecord.setAiResult(jsonObject.toJSONString());
@ -341,24 +343,54 @@ public class AppController extends BaseController
return AjaxResult.success(jsonObject);
}
@GetMapping("/image/{recordId}")
@ApiOperation("获取图片接口")
private AjaxResult getAiImage(@PathVariable("recordId") Long recordId){
if(recordId == null){
return AjaxResult.error("未获取到参数");
}
TImage tImage = new TImage();
tImage.setThirdId(recordId);
return AjaxResult.success(imageService.selectTImageList(tImage));
}
public JSONObject noBase64Image(JSONObject jsonObject)
private void saveImage(AiPostReq aiPostReq,Long recordId)
{
try
{
jsonObject.getJSONObject("data").getJSONObject("shezhi").getJSONObject("shemian").getJSONObject("image_sz").put("v", null);
jsonObject.getJSONObject("data").getJSONObject("shezhi").getJSONObject("yudian").getJSONObject("image_sz_yudian").put("v", null);
jsonObject.getJSONObject("data").getJSONObject("shezhi").getJSONObject("yuban").getJSONObject("image_sz_yuban").put("v", null);
jsonObject.getJSONObject("data").getJSONObject("shezhi").getJSONObject("liewen").getJSONObject("image_sz_liewen").put("v", null);
jsonObject.getJSONObject("data").getJSONObject("shetai").getJSONObject("image_st").put("v", null);
jsonObject.getJSONObject("data").getJSONObject("shexia").getJSONObject("image_sx").put("v", null);
jsonObject.getJSONObject("data").getJSONObject("shexia").getJSONObject("image_sx_vein").put("v", null);
Base64ResizedRmgSm base64_resized_img_sm = aiPostReq.getBase64_resized_img_sm();
Base64ResizedRmgSm base64_resized_img_sx = aiPostReq.getBase64_resized_img_sx();
Map<String, String> smMap = imageKey(base64_resized_img_sm,"base64_resized_img_sm");
Map<String, String> sxMap = imageKey(base64_resized_img_sx, "base64_resized_img_sx");
smMap.putAll(sxMap);
sxMap.forEach((name,value)->{
TImage tImage = new TImage();
tImage.setThirdId(recordId);
tImage.setCreateTime(new Date());
tImage.setName(name);
String path=GenerateImage(value);
tImage.setPath(path);
imageService.insertTImage(tImage);
});
}
public Map<String,String> imageKey(Base64ResizedRmgSm param,String key){
Map<String,String> map = new HashMap<>();
if(!StringUtils.isEmpty(param.getFront())){
map.put(key+"-front",param.getFront());
}
catch (Exception e)
{
throw new ServiceException("JSON解析错误:" + jsonObject.toJSONString());
if(!StringUtils.isEmpty(param.getLeft())){
map.put(key+"-left",param.getLeft());
}
return jsonObject;
if(!StringUtils.isEmpty(param.getFront())){
map.put(key+"-right",param.getRight());
}
if(!CollectionUtils.isEmpty(param.getOther())){
for (int i = 0; i < param.getOther().size(); i++)
{
map.put(key+"-other"+i,param.getOther().get(i));
}
}
return map;
}
public JSONObject removeBase64Image(JSONObject jsonObject,Long recordId)
@ -381,7 +413,12 @@ public class AppController extends BaseController
String imgStr=parents.get(i).getJSONObject(imageKey).getString("v");
String path=GenerateImage(imgStr);
parents.get(i).getJSONObject(keys[i]).put("v",null);
TImage tImage = new TImage();
tImage.setName(imageKey);
tImage.setPath(path);
tImage.setCreateTime(new Date());
tImage.setThirdId(recordId);
imageService.insertTImage(tImage);
//保存图片到数据库,thirdId(recordId),path,name(imageKey)
}

@ -92,6 +92,8 @@ public class RequestParamsUtil
return params;
}
private static String createLinkStringByGet(Map<String, Object> params) throws UnsupportedEncodingException {
List<String> keys = new ArrayList<>(params.keySet());
Collections.sort(keys);

@ -29,6 +29,15 @@ public class AiPostReq
{
this.base64_resized_img_sx = base64_resized_img_sx;
}
@Override
public String toString()
{
return "{" +
"base64_resized_img_sm=" + base64_resized_img_sm +
", base64_resized_img_sx=" + base64_resized_img_sx +
'}';
}
}

@ -48,4 +48,15 @@ public class Base64ResizedRmgSm
{
this.other = other;
}
@Override
public String toString()
{
return "{" +
"front='" + front + '\'' +
", left='" + left + '\'' +
", right='" + right + '\'' +
", other=" + other +
'}';
}
}

@ -199,9 +199,9 @@ public class TRecordServiceImpl implements ITRecordService
Integer value3 ;
try
{
value1 = jsonObject.getJSONObject("shezhi").getJSONObject(keys0[0]).getInteger(keys0[1]);
value2 = jsonObject.getJSONObject("shetai").getInteger(key1);
value3 = jsonObject.getJSONObject("shexia").getInteger(key2);
value1 = jsonObject.getJSONObject("data").getJSONObject("shezhi").getJSONObject(keys0[0]).getInteger(keys0[1]);
value2 = jsonObject.getJSONObject("data").getJSONObject("shetai").getInteger(key1);
value3 = jsonObject.getJSONObject("data").getJSONObject("shexia").getInteger(key2);
}catch (Exception e){
throw new ServiceException("JSON格式错误");
}

Loading…
Cancel
Save