Commit c3abf07c authored by 马超's avatar 马超

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

parent 54673991
...@@ -17,7 +17,20 @@ public class CdfortisConstant { ...@@ -17,7 +17,20 @@ public class CdfortisConstant {
public static String RESULT_SUCC_CODE = "C00010000"; public static String RESULT_SUCC_CODE = "C00010000";
/**
* http请求状态码
*/
public static int HTTP_SUCC = 200; 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; ...@@ -7,17 +7,17 @@ import com.cftech.cdfortis.model.CdfortisDrugInfo;
import com.cftech.cdfortis.service.CdfortisService; import com.cftech.cdfortis.service.CdfortisService;
import com.cftech.cdfortis.util.CdfortisResponseUtil; import com.cftech.cdfortis.util.CdfortisResponseUtil;
import com.cftech.cdfortis.util.CdfortisTokenUtil; import com.cftech.cdfortis.util.CdfortisTokenUtil;
import com.cftech.core.util.StringUtils;
import com.cftech.core.util.SystemConfig; import com.cftech.core.util.SystemConfig;
import com.cftech.predrugs.model.PreDrugs; import com.cftech.predrugs.model.PreDrugs;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import okhttp3.*; import okhttp3.MediaType;
import org.apache.http.client.methods.RequestBuilder; import okhttp3.RequestBody;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.io.IOException; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/** /**
...@@ -76,24 +76,25 @@ public class CdfortisServiceImpl implements CdfortisService { ...@@ -76,24 +76,25 @@ public class CdfortisServiceImpl implements CdfortisService {
List<CdfortisDrugInfo> cdfortisDrugList = preDrugsList.stream().map(this::fromPreDrugs).collect(Collectors.toList()); List<CdfortisDrugInfo> cdfortisDrugList = preDrugsList.stream().map(this::fromPreDrugs).collect(Collectors.toList());
param.put("drugInfo", cdfortisDrugList); param.put("drugInfo", cdfortisDrugList);
RequestBody body = RequestBody.create(MEDIA_TYPE_JSON, param.toJSONString()); RequestBody body = RequestBody.create(MEDIA_TYPE_JSON, param.toJSONString());
Request request = new Request.Builder().url(uploadUrl).post(body).build(); // 请求获取数据
// 获取请求结果 JSONObject data = CdfortisResponseUtil.request(uploadUrl, CdfortisConstant.METHOD_POST,
JSONObject retObj = CdfortisResponseUtil.getResponseBody(request, rtnJson); null, null, body, rtnJson);
if (retObj == null) { if (data == null) {
return rtnJson; return rtnJson;
} }
JSONObject data = retObj.getJSONObject("data");
boolean isAllSuccess = data.getBooleanValue("isAllSuccess"); boolean isAllSuccess = data.getBooleanValue("isAllSuccess");
if (!isAllSuccess) { if (!isAllSuccess) {
JSONArray failedDataArr = data.getJSONArray("failedData"); JSONArray failedDataArr = data.getJSONArray("failedData");
int size = failedDataArr.size(); //TODO 更新药物上传状态
for (int i = 0; i < size; i++) {
JSONObject failedData = failedDataArr.getJSONObject(i); rtnJson.put("errorNo", "0");
log.debug("{}上传失败原因: {}", failedData.getString("drugName"), failedData.getString("reason")); rtnJson.put("data", failedDataArr);
} return rtnJson;
} }
rtnJson.put("errorNo", "0"); rtnJson.put("errorNo", "0");
rtnJson.put("data", "");
} catch (Exception e) { } catch (Exception e) {
handleException(rtnJson, e); handleException(rtnJson, e);
} }
...@@ -117,23 +118,21 @@ public class CdfortisServiceImpl implements CdfortisService { ...@@ -117,23 +118,21 @@ public class CdfortisServiceImpl implements CdfortisService {
String getFbusiListUrl = SystemConfig.p.getProperty("cdfortis.get_fbusi_list_url"); String getFbusiListUrl = SystemConfig.p.getProperty("cdfortis.get_fbusi_list_url");
try { try {
// 构建URL参数 // 构建URL参数
HttpUrl.Builder urlBuild = HttpUrl.parse(getFbusiListUrl).newBuilder(); Map<String, String> urlParam = new HashMap<>(6);
urlBuild.addQueryParameter("appid", appid); urlParam.put("appid", appid);
urlBuild.addQueryParameter("token", cdfortisTokenUtil.getToken()); urlParam.put("token", cdfortisTokenUtil.getToken());
urlBuild.addQueryParameter("page", page + ""); urlParam.put("page", page + "");
urlBuild.addQueryParameter("rows", rows + ""); urlParam.put("rows", rows + "");
urlBuild.addQueryParameter("startTime", startTime); urlParam.put("startTime", startTime);
urlBuild.addQueryParameter("endTime", endTime); urlParam.put("endTime", endTime);
HttpUrl requestUrl = urlBuild.build(); // 请求获取数据
log.debug("获取图文处方列表地址: {}", requestUrl.toString()); JSONObject data = CdfortisResponseUtil.request(getFbusiListUrl, CdfortisConstant.METHOD_GET, urlParam,
Request request = new Request.Builder().url(requestUrl).get().build(); null, null, rtnJson);
// 获取请求结果 if (data == null) {
JSONObject retObj = CdfortisResponseUtil.getResponseBody(request, rtnJson);
if (retObj == null) {
return rtnJson; return rtnJson;
} }
rtnJson.put("errorNo", "0"); rtnJson.put("errorNo", "0");
rtnJson.put("data", retObj.getJSONObject("data")); rtnJson.put("data", data);
} catch (Exception e) { } catch (Exception e) {
handleException(rtnJson, e); handleException(rtnJson, e);
} }
...@@ -151,19 +150,19 @@ public class CdfortisServiceImpl implements CdfortisService { ...@@ -151,19 +150,19 @@ public class CdfortisServiceImpl implements CdfortisService {
JSONObject rtnJson = new JSONObject(); JSONObject rtnJson = new JSONObject();
String getFbusiInfoUrl = SystemConfig.p.getProperty("cdfortis.get_fbusi_info_url"); String getFbusiInfoUrl = SystemConfig.p.getProperty("cdfortis.get_fbusi_info_url");
try { try {
HttpUrl.Builder urlBuild = HttpUrl.parse(getFbusiInfoUrl).newBuilder(); // 构建URL参数
urlBuild.addQueryParameter("appid", appid); Map<String, String> urlParam = new HashMap<>(3);
urlBuild.addQueryParameter("token", cdfortisTokenUtil.getToken()); urlParam.put("appid", appid);
urlBuild.addQueryParameter("presId", presId); urlParam.put("token", cdfortisTokenUtil.getToken());
HttpUrl requestUrl = urlBuild.build(); urlParam.put("presId", presId);
log.debug("获取图文处方详情地址: {}", requestUrl.toString()); // 请求获取数据
Request request = new Request.Builder().url(requestUrl).get().build(); JSONObject data = CdfortisResponseUtil.request(getFbusiInfoUrl, CdfortisConstant.METHOD_GET, urlParam,
JSONObject retObj = CdfortisResponseUtil.getResponseBody(request, rtnJson); null, null, rtnJson);
if (retObj == null) { if (data == null) {
return rtnJson; return rtnJson;
} }
rtnJson.put("errorNo", "0"); rtnJson.put("errorNo", "0");
rtnJson.put("data", retObj.getJSONObject("data")); rtnJson.put("data", data);
} catch (Exception e) { } catch (Exception e) {
handleException(rtnJson, e); handleException(rtnJson, e);
} }
...@@ -181,19 +180,19 @@ public class CdfortisServiceImpl implements CdfortisService { ...@@ -181,19 +180,19 @@ public class CdfortisServiceImpl implements CdfortisService {
JSONObject rtnJson = new JSONObject(); JSONObject rtnJson = new JSONObject();
String getFbusiPicUrl = SystemConfig.p.getProperty("cdfortis.get_fbusi_pic_url"); String getFbusiPicUrl = SystemConfig.p.getProperty("cdfortis.get_fbusi_pic_url");
try { try {
HttpUrl.Builder urlBuild = HttpUrl.parse(getFbusiPicUrl).newBuilder(); // 构建URL参数
urlBuild.addQueryParameter("appid", appid); Map<String, String> urlParam = new HashMap<>(3);
urlBuild.addQueryParameter("token", cdfortisTokenUtil.getToken()); urlParam.put("appid", appid);
urlBuild.addQueryParameter("presId", presId); urlParam.put("token", cdfortisTokenUtil.getToken());
HttpUrl requestUrl = urlBuild.build(); urlParam.put("presId", presId);
log.debug("获取图文处方图片地址: {}", requestUrl.toString()); // 请求获取数据
Request request = new Request.Builder().url(requestUrl).get().build(); JSONObject data = CdfortisResponseUtil.request(getFbusiPicUrl, CdfortisConstant.METHOD_GET, urlParam,
JSONObject retObj = CdfortisResponseUtil.getResponseBody(request, rtnJson); null, null, rtnJson);
if (retObj == null) { if (data == null) {
return rtnJson; return rtnJson;
} }
rtnJson.put("errorNo", "0"); rtnJson.put("errorNo", "0");
rtnJson.put("data", retObj.getJSONObject("data")); rtnJson.put("data", data);
} catch (Exception e) { } catch (Exception e) {
handleException(rtnJson, e); handleException(rtnJson, e);
} }
......
...@@ -2,11 +2,10 @@ package com.cftech.cdfortis.util; ...@@ -2,11 +2,10 @@ package com.cftech.cdfortis.util;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.cftech.cdfortis.constants.CdfortisConstant; import com.cftech.cdfortis.constants.CdfortisConstant;
import com.cftech.core.util.StringUtils;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import okhttp3.*; import okhttp3.*;
import java.io.IOException; import java.util.Map;
@Slf4j @Slf4j
public class CdfortisResponseUtil { public class CdfortisResponseUtil {
...@@ -30,19 +29,54 @@ public class CdfortisResponseUtil { ...@@ -30,19 +29,54 @@ public class CdfortisResponseUtil {
return client; 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 * @return
* @throws Exception * @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(); OkHttpClient client = getClient();
try { try {
Response response = client.newCall(request).execute(); Response response = client.newCall(builder.build()).execute();
// 判断http状态值 // 判断http状态值
if (CdfortisConstant.HTTP_SUCC != response.code()) { if (CdfortisConstant.HTTP_SUCC != response.code()) {
log.error(response.message()); log.error(response.message());
...@@ -53,30 +87,15 @@ public class CdfortisResponseUtil { ...@@ -53,30 +87,15 @@ public class CdfortisResponseUtil {
} }
ResponseBody responseBody = response.body(); ResponseBody responseBody = response.body();
String retStr = responseBody.string(); String retStr = responseBody.string();
log.debug("接口请求结果: {}", retStr); log.debug("request result: {}", retStr);
// 转换结果 // 转换结果
JSONObject retObj = JSONObject.parseObject(retStr); JSONObject retObj = JSONObject.parseObject(retStr);
// 插件结果 // 插件结果
checkResponse(retObj); checkResponse(retObj);
return retObj; return retObj.getJSONObject("data");
} catch (Exception e) { } catch (Exception e) {
throw new Exception(e.getMessage()); 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