Commit 54673991 authored by 马超's avatar 马超

feat(微问诊): 获取图文处方列表、详情、图片接口

parent 2535fc5a
......@@ -17,4 +17,7 @@ public class CdfortisConstant {
public static String RESULT_SUCC_CODE = "C00010000";
public static int HTTP_SUCC = 200;
}
......@@ -29,4 +29,31 @@ public interface CdfortisService {
* @return
*/
JSONObject uploadDrugInfo(List<PreDrugs> preDrugsList);
/**
* 获取图文处方列表
*
* @param page
* @param rows
* @param startTime
* @param endTime
* @return
*/
JSONObject getFbusiList(int page, int rows, String startTime, String endTime);
/**
* 获取图文处方详情
*
* @param presId
* @return
*/
JSONObject getFbusiDetail(String presId);
/**
* 获取图文处方图片
*
* @param presId
* @return
*/
JSONObject getFbusiPicture(String presId);
}
......@@ -7,18 +7,21 @@ import com.cftech.cdfortis.model.CdfortisDrugInfo;
import com.cftech.cdfortis.service.CdfortisService;
import com.cftech.cdfortis.util.CdfortisResponseUtil;
import com.cftech.cdfortis.util.CdfortisTokenUtil;
import com.cftech.core.util.StringUtils;
import com.cftech.core.util.SystemConfig;
import com.cftech.predrugs.model.PreDrugs;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import org.apache.http.client.methods.RequestBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;
/**
* 获取微问诊token Service
* 微问诊Service
*
* @author :machao
* @date :Created in 2021/08/19 9:30
......@@ -30,30 +33,35 @@ public class CdfortisServiceImpl implements CdfortisService {
@Autowired
CdfortisTokenUtil cdfortisTokenUtil;
private OkHttpClient client;
private static final MediaType MEDIA_TYPE_JSON
= MediaType.parse("application/json; charset=utf-8");
private static String appid = SystemConfig.p.getProperty("cdfortis.appid");
/**
* 获取token
*
* @return
*/
@Override
public JSONObject getTokenResult() {
JSONObject rtnJson = new JSONObject();
try {
String token = cdfortisTokenUtil.getToken();
rtnJson.put("errorNo", "0");
rtnJson.put("token", token);
rtnJson.put("data", token);
} catch (Exception e) {
e.printStackTrace();
rtnJson.put("errorNo", "1");
rtnJson.put("errorMsg", e.getMessage());
rtnJson.put("errorEnMsg", e.getMessage());
handleException(rtnJson, e);
}
return rtnJson;
}
/**
* 上传药品清单
*
* @param preDrugsList
* @return
*/
@Override
public JSONObject uploadDrugInfo(List<PreDrugs> preDrugsList) {
JSONObject rtnJson = new JSONObject();
......@@ -67,24 +75,13 @@ public class CdfortisServiceImpl implements CdfortisService {
// 转换药品类型为微问诊需要的结构
List<CdfortisDrugInfo> cdfortisDrugList = preDrugsList.stream().map(this::fromPreDrugs).collect(Collectors.toList());
param.put("drugInfo", cdfortisDrugList);
// 构建请求
OkHttpClient client = getClient();
RequestBody body = RequestBody.create(MEDIA_TYPE_JSON, param.toJSONString());
Request request = new Request.Builder().url(uploadUrl).post(body).build();
Response response = client.newCall(request).execute();
// 判断http状态值
if (200 != response.code()) {
rtnJson.put("errorNo", "1");
rtnJson.put("errorMsg", "微问诊请求失败");
rtnJson.put("errorEnMsg", "request failure");
// 获取请求结果
JSONObject retObj = CdfortisResponseUtil.getResponseBody(request, rtnJson);
if (retObj == null) {
return rtnJson;
}
ResponseBody responseBody = response.body();
String retStr = responseBody.string();
log.debug("上传接口请求结果: {}", retStr);
// 解析结果
JSONObject retObj = JSONObject.parseObject(retStr);
CdfortisResponseUtil.checkResponse(retObj);
JSONObject data = retObj.getJSONObject("data");
boolean isAllSuccess = data.getBooleanValue("isAllSuccess");
if (!isAllSuccess) {
......@@ -98,17 +95,115 @@ public class CdfortisServiceImpl implements CdfortisService {
rtnJson.put("errorNo", "0");
} catch (Exception e) {
log.error(e.getMessage(), e);
rtnJson.put("errorNo", "1");
rtnJson.put("errorMsg", e.getMessage());
rtnJson.put("errorEnMsg", e.getMessage());
handleException(rtnJson, e);
}
return rtnJson;
}
/**
* 获取图文处方列表
*
* @param page
* @param rows
* @param startTime
* @param endTime
* @return
*/
@Override
public JSONObject getFbusiList(int page, int rows, String startTime, String endTime) {
JSONObject rtnJson = new JSONObject();
String getFbusiListUrl = SystemConfig.p.getProperty("cdfortis.get_fbusi_list_url");
try {
// 构建URL参数
HttpUrl.Builder urlBuild = HttpUrl.parse(getFbusiListUrl).newBuilder();
urlBuild.addQueryParameter("appid", appid);
urlBuild.addQueryParameter("token", cdfortisTokenUtil.getToken());
urlBuild.addQueryParameter("page", page + "");
urlBuild.addQueryParameter("rows", rows + "");
urlBuild.addQueryParameter("startTime", startTime);
urlBuild.addQueryParameter("endTime", endTime);
HttpUrl requestUrl = urlBuild.build();
log.debug("获取图文处方列表地址: {}", requestUrl.toString());
Request request = new Request.Builder().url(requestUrl).get().build();
// 获取请求结果
JSONObject retObj = CdfortisResponseUtil.getResponseBody(request, rtnJson);
if (retObj == null) {
return rtnJson;
}
rtnJson.put("errorNo", "0");
rtnJson.put("data", retObj.getJSONObject("data"));
} catch (Exception e) {
handleException(rtnJson, e);
}
return rtnJson;
}
/**
* 获取图文处方详情
*
* @param presId
* @return
*/
@Override
public JSONObject getFbusiDetail(String presId) {
JSONObject rtnJson = new JSONObject();
String getFbusiInfoUrl = SystemConfig.p.getProperty("cdfortis.get_fbusi_info_url");
try {
HttpUrl.Builder urlBuild = HttpUrl.parse(getFbusiInfoUrl).newBuilder();
urlBuild.addQueryParameter("appid", appid);
urlBuild.addQueryParameter("token", cdfortisTokenUtil.getToken());
urlBuild.addQueryParameter("presId", presId);
HttpUrl requestUrl = urlBuild.build();
log.debug("获取图文处方详情地址: {}", requestUrl.toString());
Request request = new Request.Builder().url(requestUrl).get().build();
JSONObject retObj = CdfortisResponseUtil.getResponseBody(request, rtnJson);
if (retObj == null) {
return rtnJson;
}
rtnJson.put("errorNo", "0");
rtnJson.put("data", retObj.getJSONObject("data"));
} catch (Exception e) {
handleException(rtnJson, e);
}
return rtnJson;
}
/**
* 获取图文处方图片
*
* @param presId
* @return
*/
@Override
public JSONObject getFbusiPicture(String presId) {
JSONObject rtnJson = new JSONObject();
String getFbusiPicUrl = SystemConfig.p.getProperty("cdfortis.get_fbusi_pic_url");
try {
HttpUrl.Builder urlBuild = HttpUrl.parse(getFbusiPicUrl).newBuilder();
urlBuild.addQueryParameter("appid", appid);
urlBuild.addQueryParameter("token", cdfortisTokenUtil.getToken());
urlBuild.addQueryParameter("presId", presId);
HttpUrl requestUrl = urlBuild.build();
log.debug("获取图文处方图片地址: {}", requestUrl.toString());
Request request = new Request.Builder().url(requestUrl).get().build();
JSONObject retObj = CdfortisResponseUtil.getResponseBody(request, rtnJson);
if (retObj == null) {
return rtnJson;
}
rtnJson.put("errorNo", "0");
rtnJson.put("data", retObj.getJSONObject("data"));
} catch (Exception e) {
handleException(rtnJson, e);
}
return rtnJson;
}
/**
* 转换药品信息
*
* @return
* @para preDrugs
*/
......@@ -124,16 +219,18 @@ public class CdfortisServiceImpl implements CdfortisService {
}
private OkHttpClient getClient() {
if (client == null) {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
Dispatcher dispatcher = new Dispatcher();
dispatcher.setMaxRequests(100);
dispatcher.setMaxRequestsPerHost(50);
builder.dispatcher(dispatcher);
client = builder.build();
}
return client;
/**
* 处理异常
*
* @param rtnJson
* @param e
*/
private void handleException(JSONObject rtnJson, Exception e) {
log.error(e.getMessage(), e);
rtnJson.put("errorNo", "1");
rtnJson.put("errorMsg", e.getMessage());
rtnJson.put("errorEnMsg", e.getMessage());
}
}
......@@ -2,11 +2,75 @@ package com.cftech.cdfortis.util;
import com.alibaba.fastjson.JSONObject;
import com.cftech.cdfortis.constants.CdfortisConstant;
import com.cftech.core.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import java.io.IOException;
@Slf4j
public class CdfortisResponseUtil {
private static OkHttpClient client;
/**
* 获取请求client
*
* @return
*/
public static OkHttpClient getClient() {
if (client == null) {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
Dispatcher dispatcher = new Dispatcher();
dispatcher.setMaxRequests(100);
dispatcher.setMaxRequestsPerHost(50);
builder.dispatcher(dispatcher);
client = builder.build();
}
return client;
}
/**
* 获取请求结果
*
* @param request
* @return
* @throws Exception
*/
public static JSONObject getResponseBody(Request request, JSONObject rtnJson) throws Exception {
// 构建请求
OkHttpClient client = getClient();
try {
Response response = client.newCall(request).execute();
// 判断http状态值
if (CdfortisConstant.HTTP_SUCC != response.code()) {
log.error(response.message());
rtnJson.put("errorNo", "1");
rtnJson.put("errorMsg", "微问诊请求失败");
rtnJson.put("errorEnMsg", "request failure");
return null;
}
ResponseBody responseBody = response.body();
String retStr = responseBody.string();
log.debug("接口请求结果: {}", retStr);
// 转换结果
JSONObject retObj = JSONObject.parseObject(retStr);
// 插件结果
checkResponse(retObj);
return retObj;
} catch (Exception e) {
throw new Exception(e.getMessage());
}
}
/**
* 检查结果是否失败
*
* @param retObj
* @throws Exception
*/
public static void checkResponse(JSONObject retObj) throws Exception {
JSONObject returnCode = retObj.getJSONObject(CdfortisConstant.RETURN_CODE);
if (!CdfortisConstant.RESULT_SUCC_CODE.equals(returnCode.getString(CdfortisConstant.RETURN_CODE_KEY))) {
......
......@@ -22,7 +22,7 @@ import java.util.List;
public class CdfortisController {
@Autowired
CdfortisService tokenService;
CdfortisService cdfortisService;
/**
* 获取微问诊token
......@@ -31,7 +31,7 @@ public class CdfortisController {
*/
@GetMapping("/get/token")
public JSONObject getToken() {
return tokenService.getTokenResult();
return cdfortisService.getTokenResult();
}
......@@ -43,7 +43,47 @@ public class CdfortisController {
*/
@PostMapping("/upload/druginfo")
public JSONObject uploadDrugInfo(@RequestBody List<PreDrugs> preDrugsList) {
return tokenService.uploadDrugInfo(preDrugsList);
return cdfortisService.uploadDrugInfo(preDrugsList);
}
/**
* 微问诊图文处方列表
*
* @param iDisplayStart 查询的页数
* @param iDisplayLength 一次查询几条(最大50)
* @param startTime 开始时间
* @param endTime 结束时间
* @return
*/
@GetMapping("/get/fbusi/list")
public JSONObject getFbusiList(int iDisplayStart, int iDisplayLength, @RequestParam String startTime, @RequestParam String endTime) {
return cdfortisService.getFbusiList(iDisplayStart, iDisplayLength, startTime, endTime);
}
/**
* 获取图文处方详情
*
* @param presId 处方编号
* @return
*/
@GetMapping("/get/fbusi/detail")
public JSONObject getFbusiDetail(@RequestParam String presId) {
return cdfortisService.getFbusiDetail(presId);
}
/**
* 获取图文处方图片
*
* @param presId 处方编号
* @return
*/
@GetMapping("/get/fbusi/picture")
public JSONObject getFbusiPicture(@RequestParam String presId) {
return cdfortisService.getFbusiPicture(presId);
}
}
......@@ -14,3 +14,6 @@ Content-Type: application/json
}]
###
GET http://localhost:8080/aidea/mobile/auth/cdfortis/get/fbusi/list?iDisplayStart=1&iDisplayLength=10&startTime=2020-09-07 00:00:00&endTime=2020-09-14 23:59:59
###
\ No newline at end of file
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