Jelajahi Sumber

使用Hutool的HTTP

lighter 1 tahun lalu
induk
melakukan
a6f99f7971

+ 1 - 1
src/main/java/thyyxxk/webserver/service/examinations/LisDockService.java

@@ -184,7 +184,7 @@ public class LisDockService {
     }
 
     private String html2image(HealthCardResult param) {
-        Map<String, Object> res = html2ImgSrvc.execute(param);
+        JSONObject res = html2ImgSrvc.execute(param);
         log.info("生成图片结果:{}", res);
         if (null == res || null == res.get("code")) {
             return null;

+ 12 - 5
src/main/java/thyyxxk/webserver/service/externalhttp/CorpWxSrvc.java

@@ -1,10 +1,17 @@
 package thyyxxk.webserver.service.externalhttp;
 
+import cn.hutool.core.util.StrUtil;
 import com.alibaba.fastjson.JSONObject;
-import com.dtflys.forest.annotation.Get;
-import com.dtflys.forest.annotation.Var;
+import org.springframework.stereotype.Service;
+import thyyxxk.webserver.utils.HttpUtil;
+
+@Service
+public class CorpWxSrvc {
+
+    public JSONObject getUserinfo(String token, String userid) {
+        String url = StrUtil.format("https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token={}&userid={}",
+                token, userid);
+        return HttpUtil.get(url);
+    }
 
-public interface CorpWxSrvc {
-    @Get(value = "https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token={token}&userid={userid}", timeout = 1500)
-    JSONObject getUserinfo(@Var("token") String token, @Var("userid") String userid);
 }

+ 24 - 13
src/main/java/thyyxxk/webserver/service/externalhttp/DrgWebServices.java

@@ -1,29 +1,40 @@
 package thyyxxk.webserver.service.externalhttp;
 
 import com.alibaba.fastjson.JSONObject;
-import com.dtflys.forest.annotation.BaseRequest;
-import com.dtflys.forest.annotation.JSONBody;
-import com.dtflys.forest.annotation.Post;
+import org.springframework.scheduling.annotation.Async;
+import org.springframework.stereotype.Service;
 import thyyxxk.webserver.entity.vo.DrgGroupTestVO;
+import thyyxxk.webserver.utils.HttpUtil;
 
 /**
  * @Author:lihong 调用创智 drg第三方接口
  * @Date: 2022/12/8
  * @Description:
  */
-@BaseRequest(timeout = 5000)
-public interface DrgWebServices {
+@Service
+public class DrgWebServices {
 
-    @Post(url = "http://172.16.32.126:8080/drg_web/localHelp/drg_dagns/list.action")
-    String localHelpDrgDagns(@JSONBody DrgGroupTestVO drgGroupTestVO);
+    private static final int TIMEOUT = 5000;
 
-    @Post(url = "http://172.16.32.126:8080/drg_web/drgGroupThird/V2/setListDrg.action")
-    JSONObject drgQuality(@JSONBody JSONObject obj);
+    public String localHelpDrgDagns(DrgGroupTestVO drgGroupTestVO) {
+        String url = "http://172.16.32.126:8080/drg_web/localHelp/drg_dagns/list.action";
+        return HttpUtil.post(url, drgGroupTestVO, TIMEOUT, String.class);
+    }
 
-    @Post(url = "http://172.16.32.126:8082/etlClient/callHisData.action", async = true)
-    void etlClient(@JSONBody JSONObject obj);
+    public JSONObject drgQuality(JSONObject params) {
+        String url = "http://172.16.32.126:8080/drg_web/drgGroupThird/V2/setListDrg.action";
+        return HttpUtil.post(url, params, TIMEOUT);
+    }
 
-    @Post(url = "http://172.16.32.126:8080/drg_web/drgGroupThird/getAllDrgResults.action")
-    JSONObject getDrgCaseQualityControlGroup(@JSONBody JSONObject obj);
+    @Async
+    public void etlClient(JSONObject params) {
+        String url = "http://172.16.32.126:8082/etlClient/callHisData.action";
+        HttpUtil.post(url, params);
+    }
+
+    public JSONObject getDrgCaseQualityControlGroup(JSONObject params) {
+        String url = "http://172.16.32.126:8080/drg_web/drgGroupThird/getAllDrgResults.action";
+        return HttpUtil.post(url, params, TIMEOUT);
+    }
 
 }

+ 9 - 7
src/main/java/thyyxxk/webserver/service/externalhttp/Html2ImgSrvc.java

@@ -1,13 +1,15 @@
 package thyyxxk.webserver.service.externalhttp;
 
-import com.dtflys.forest.annotation.JSONBody;
-import com.dtflys.forest.annotation.Post;
+import com.alibaba.fastjson.JSONObject;
+import org.springframework.stereotype.Service;
 import thyyxxk.webserver.entity.examinations.lisdock.HealthCardResult;
+import thyyxxk.webserver.utils.HttpUtil;
 
-import java.util.Map;
+@Service
+public class Html2ImgSrvc {
+    private static final String URL = "http://192.168.200.3/htmlToImage/healthCardImage/execute";
 
-public interface Html2ImgSrvc {
-
-    @Post("http://192.168.200.3/htmlToImage/healthCardImage/execute")
-    Map<String, Object> execute(@JSONBody HealthCardResult param);
+    public JSONObject execute(HealthCardResult params) {
+        return HttpUtil.post(URL, params);
+    }
 }

+ 47 - 35
src/main/java/thyyxxk/webserver/service/externalhttp/OutpatientBookingHttp.java

@@ -1,42 +1,54 @@
 package thyyxxk.webserver.service.externalhttp;
 
+import cn.hutool.core.util.StrUtil;
+import cn.hutool.http.HttpRequest;
+import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
-import com.dtflys.forest.annotation.*;
-
-//@BaseRequest(baseURL = "http://172.16.30.33:8089/thmz")
-@BaseRequest(baseURL = "http://webhis.thyy.cn:81/thmz")
-public interface OutpatientBookingHttp {
-
-    @Post("/login")
-    JSONObject getToken(@JSONBody JSONObject js);
-
-    @Get("/getAmpmByDateAndDeptCode?date={date}&deptCode={deptCode}")
-    JSONObject getAmpmByDateAndDeptCode(@Header("Authorization") String Authorization,
-                                        @Var("date") String date,
-                                        @Var("deptCode") String deptCode);
-
-    @Get("/getChargeTypeByRequestDayAndDoctor?unitCode={unitCode}&ampm={ampm}&date={date}&apTime={apTime}&doctorCode={}")
-    JSONObject getChargeTypeByRequestDayAndDoctor(@Header("Authorization") String Authorization,
-                                                  @Var("unitCode") String unitCode,
-                                                  @Var("ampm") String ampm,
-                                                  @Var("date") String date,
-                                                  @Var("apTime") String apTime);
-
-    @Get("/getDoctorCodeByRequestDayAndDoctor?unitCode={unitCode}&ampm={ampm}&chargeType={chargeType}&date={date}&apTime={apTime}")
-    JSONObject getDoctorCodeByRequestDayAndDoctor(@Header("Authorization") String Authorization,
-                                                  @Var("unitCode") String unitCode,
-                                                  @Var("ampm") String ampm,
-                                                  @Var("date") String date,
-                                                  @Var("apTime") String apTime,
-                                                  @Var("chargeType") String chargeType);
-
-    @Get("/getMzChargeTypeByMzyRequestId?mzyRequestId={mzyRequestId}&birthDay={birthDay}&patientId={patientId}")
-    JSONObject getMzChargeTypeByMzyRequestId(@Header("Authorization") String Authorization,
-                                             @Var("mzyRequestId") String mzyRequestId,
-                                             @Var("birthDay") String birthDay,
-                                             @Var("patientId") String patientId);
+import org.springframework.stereotype.Service;
+
+@Service
+public class OutpatientBookingHttp {
+    private static final String baseURL = "http://webhis.thyy.cn:81/thmz";
+
+    public JSONObject getAmpmByDateAndDeptCode(String authorization, String date, String deptCode) {
+        String url = baseURL + StrUtil.format("/getAmpmByDateAndDeptCode?date={}&deptCode={}",
+                date, deptCode);
+        String body = HttpRequest.get(url).header("Authorization", authorization)
+                .execute().body();
+        return JSON.parseObject(body);
+    }
+
+    public JSONObject getChargeTypeByRequestDayAndDoctor(String authorization,
+                                                         String unitCode, String ampm,
+                                                         String date, String apTime) {
+        String url = baseURL + StrUtil.format("/getChargeTypeByRequestDayAndDoctor?" +
+                        "unitCode={}&ampm={}&date={}&apTime={}&doctorCode={}", unitCode, ampm, date, apTime);
+        String body = HttpRequest.get(url).header("Authorization", authorization)
+                .execute().body();
+        return JSON.parseObject(body);
+    }
+
+    public JSONObject getDoctorCodeByRequestDayAndDoctor(String authorization,
+                                                  String unitCode, String ampm,
+                                                  String date, String apTime,
+                                                  String chargeType) {
+        String url = baseURL + StrUtil.format("/getDoctorCodeByRequestDayAndDoctor?" +
+                        "unitCode={}&ampm={}&chargeType={}&date={}&apTime={}", unitCode, ampm, chargeType, date, ampm);
+        String body = HttpRequest.get(url).header("Authorization", authorization)
+                .execute().body();
+        return JSON.parseObject(body);
+    }
+
+    public JSONObject getMzChargeTypeByMzyRequestId(String authorization, String mzyRequestId,
+                                                    String birthDay, String patientId) {
+        String url = baseURL + StrUtil.format("/getMzChargeTypeByMzyRequestId?" +
+                "mzyRequestId={}&birthDay={}&patientId={}", mzyRequestId, birthDay, patientId);
+        String body = HttpRequest.get(url).header("Authorization", authorization)
+                .execute().body();
+        return JSON.parseObject(body);
+    }
 
     @Post("/saveMzyReqrec")
-    JSONObject saveMzyReqrec(@Header("Authorization") String Authorization, @JSONBody JSONObject jsonObject);
+    public JSONObject saveMzyReqrec(@Header("Authorization") String Authorization, @JSONBody JSONObject jsonObject);
 
 }

+ 68 - 0
src/main/java/thyyxxk/webserver/utils/HttpUtil.java

@@ -0,0 +1,68 @@
+package thyyxxk.webserver.utils;
+
+import cn.hutool.http.HttpRequest;
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
+
+public class HttpUtil {
+
+    public static JSONObject get(String url) {
+        return get(url, 2000, JSONObject.class);
+    }
+
+    public static JSONObject get(String url, int timeout) {
+        return get(url, timeout, JSONObject.class);
+    }
+
+    public static <t> t get(String url, Class<t> tClass) {
+        return get(url, 2000, tClass);
+    }
+
+    private static <t> t get(String url, int timeout, Class<t> clazz) {
+        String body = HttpRequest.get(url).timeout(timeout)
+                .execute().body();
+        return JSON.parseObject(body, clazz);
+    }
+
+    public static JSONObject post(String url) {
+        return post(url, "", 2000, JSONObject.class);
+    }
+
+    public static JSONObject post(String url, JSONObject params) {
+        return post(url, params.toString(), 2000, JSONObject.class);
+    }
+
+    public static JSONObject post(String url, Object params) {
+        return post(url, JSON.toJSONString(params), 2000, JSONObject.class);
+    }
+
+    public static JSONObject post(String url, int timeout) {
+        return post(url, "", timeout, JSONObject.class);
+    }
+
+    public static JSONObject post(String url, JSONObject params, int timeout) {
+        return post(url, params.toString(), timeout, JSONObject.class);
+    }
+
+    public static <t> t post(String url, JSONObject params, Class<t> tClass) {
+        return post(url, params.toString(), 2000, tClass);
+    }
+
+    public static <t> t post(String url, JSONObject params, int timeout, Class<t> tClass) {
+        return post(url, params.toString(), timeout, tClass);
+    }
+
+    public static <t> t post(String url, Object params, Class<t> tClass) {
+        return post(url, JSON.toJSONString(params), 2000, tClass);
+    }
+
+    public static <t> t post(String url, Object params, int timeout, Class<t> tClass) {
+        return post(url, JSON.toJSONString(params), timeout, tClass);
+    }
+
+    private static <t> t post(String url, String params, int timeout, Class<t> clazz) {
+        String body = HttpRequest.post(url).timeout(timeout)
+                .body(params).execute().body();
+        return JSON.parseObject(body, clazz);
+    }
+}