Commit c3abf07c authored by 马超's avatar 马超

feat(微问诊): 修改网络请求方式

parent 54673991
......@@ -17,7 +17,20 @@ public class CdfortisConstant {
public static String RESULT_SUCC_CODE = "C00010000";
/**
* http请求状态码
*/
public static int HTTP_SUCC = 200;
/**
* post请求类型
*/
public static String METHOD_POST = "post";
/**
* get请求类型
*/
public static String METHOD_GET = "get";
}
......@@ -7,17 +7,17 @@ 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 okhttp3.MediaType;
import okhttp3.RequestBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
......@@ -76,24 +76,25 @@ public class CdfortisServiceImpl implements CdfortisService {
List<CdfortisDrugInfo> cdfortisDrugList = preDrugsList.stream().map(this::fromPreDrugs).collect(Collectors.toList());
param.put("drugInfo", cdfortisDrugList);
RequestBody body = RequestBody.create(MEDIA_TYPE_JSON, param.toJSONString());
Request request = new Request.Builder().url(uploadUrl).post(body).build();
// 获取请求结果
JSONObject retObj = CdfortisResponseUtil.getResponseBody(request, rtnJson);
if (retObj == null) {
// 请求获取数据
JSONObject data = CdfortisResponseUtil.request(uploadUrl, CdfortisConstant.METHOD_POST,
null, null, body, rtnJson);
if (data == null) {
return rtnJson;
}
JSONObject data = retObj.getJSONObject("data");
boolean isAllSuccess = data.getBooleanValue("isAllSuccess");
if (!isAllSuccess) {
JSONArray failedDataArr = data.getJSONArray("failedData");
int size = failedDataArr.size();
for (int i = 0; i < size; i++) {
JSONObject failedData = failedDataArr.getJSONObject(i);
log.debug("{}上传失败原因: {}", failedData.getString("drugName"), failedData.getString("reason"));
}
//TODO 更新药物上传状态
rtnJson.put("errorNo", "0");
rtnJson.put("data", failedDataArr);
return rtnJson;
}
rtnJson.put("errorNo", "0");
rtnJson.put("data", "");
} catch (Exception e) {
handleException(rtnJson, e);
}
......@@ -117,23 +118,21 @@ public class CdfortisServiceImpl implements CdfortisService {
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) {
Map<String, String> urlParam = new HashMap<>(6);
urlParam.put("appid", appid);
urlParam.put("token", cdfortisTokenUtil.getToken());
urlParam.put("page", page + "");
urlParam.put("rows", rows + "");
urlParam.put("startTime", startTime);
urlParam.put("endTime", endTime);
// 请求获取数据
JSONObject data = CdfortisResponseUtil.request(getFbusiListUrl, CdfortisConstant.METHOD_GET, urlParam,
null, null, rtnJson);
if (data == null) {
return rtnJson;
}
rtnJson.put("errorNo", "0");
rtnJson.put("data", retObj.getJSONObject("data"));
rtnJson.put("data", data);
} catch (Exception e) {
handleException(rtnJson, e);
}
......@@ -151,19 +150,19 @@ public class CdfortisServiceImpl implements CdfortisService {
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) {
// 构建URL参数
Map<String, String> urlParam = new HashMap<>(3);
urlParam.put("appid", appid);
urlParam.put("token", cdfortisTokenUtil.getToken());
urlParam.put("presId", presId);
// 请求获取数据
JSONObject data = CdfortisResponseUtil.request(getFbusiInfoUrl, CdfortisConstant.METHOD_GET, urlParam,
null, null, rtnJson);
if (data == null) {
return rtnJson;
}
rtnJson.put("errorNo", "0");
rtnJson.put("data", retObj.getJSONObject("data"));
rtnJson.put("data", data);
} catch (Exception e) {
handleException(rtnJson, e);
}
......@@ -181,19 +180,19 @@ public class CdfortisServiceImpl implements CdfortisService {
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) {
// 构建URL参数
Map<String, String> urlParam = new HashMap<>(3);
urlParam.put("appid", appid);
urlParam.put("token", cdfortisTokenUtil.getToken());
urlParam.put("presId", presId);
// 请求获取数据
JSONObject data = CdfortisResponseUtil.request(getFbusiPicUrl, CdfortisConstant.METHOD_GET, urlParam,
null, null, rtnJson);
if (data == null) {
return rtnJson;
}
rtnJson.put("errorNo", "0");
rtnJson.put("data", retObj.getJSONObject("data"));
rtnJson.put("data", data);
} catch (Exception e) {
handleException(rtnJson, e);
}
......
......@@ -2,11 +2,10 @@ 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;
import java.util.Map;
@Slf4j
public class CdfortisResponseUtil {
......@@ -30,19 +29,54 @@ public class CdfortisResponseUtil {
return client;
}
/**
* 检查结果是否失败
*
* @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))) {
log.error(returnCode.toJSONString());
throw new Exception(returnCode.getString(CdfortisConstant.RETURN_CODE_CONTENT));
}
}
/**
* 获取请求结果
* 发送请求
*
* @param request
* @param url
* @param method
* @param queryParams
* @param headers
* @param body
* @param rtnJson
* @return
* @throws Exception
*/
public static JSONObject getResponseBody(Request request, JSONObject rtnJson) throws Exception {
// 构建请求
public static JSONObject request(String url, String method, Map<String, String> queryParams, Map<String, String> headers, RequestBody body, JSONObject rtnJson) throws Exception {
HttpUrl.Builder urlBuild = HttpUrl.parse(url).newBuilder();
// 处理query参数
if (queryParams != null) {
queryParams.forEach(urlBuild::addQueryParameter);
}
HttpUrl httpUrl = urlBuild.build();
log.debug("request url: {} ", httpUrl.toString());
Request.Builder builder = new Request.Builder();
builder.url(url);
// 处理请求头
if (headers != null) {
builder.headers(Headers.of(headers));
}
// 处理body
if (CdfortisConstant.METHOD_POST.equals(method)) {
builder.post(body);
}
OkHttpClient client = getClient();
try {
Response response = client.newCall(request).execute();
Response response = client.newCall(builder.build()).execute();
// 判断http状态值
if (CdfortisConstant.HTTP_SUCC != response.code()) {
log.error(response.message());
......@@ -53,30 +87,15 @@ public class CdfortisResponseUtil {
}
ResponseBody responseBody = response.body();
String retStr = responseBody.string();
log.debug("接口请求结果: {}", retStr);
log.debug("request result: {}", retStr);
// 转换结果
JSONObject retObj = JSONObject.parseObject(retStr);
// 插件结果
checkResponse(retObj);
return retObj;
return retObj.getJSONObject("data");
} 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))) {
log.error(returnCode.toJSONString());
throw new Exception(returnCode.getString(CdfortisConstant.RETURN_CODE_CONTENT));
}
}
}
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