BAOLH
2026-10-08 6252124eb55b78004ffbfaa720718587b340f125
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package me.zhengjie.modules.api;
 
import com.alibaba.fastjson.JSONObject;
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.utils.HttpUtil;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
 
import java.util.Map;
 
@Component
public class Api {
 
    public Object get(String getUrl, Map<String, Object> getParams) {
        // 发起通信
        ResponseEntity<String> strRes = HttpUtil.get(getUrl, getParams, new HttpHeaders(), String.class);
        // 通信结构
        JSONObject objBody = JSONObject.parseObject(strRes.getBody());
        // 结果集
        return recordSet(objBody);
    }
 
    public Object post(String postUrl, Map<String, Object> postParams) {
        // 发起通信
        ResponseEntity<String> strRes = HttpUtil.post(postUrl, postParams, new HttpHeaders(), String.class);
        // 通信结构
        JSONObject objBody = JSONObject.parseObject(strRes.getBody());
        // 结果集
        return recordSet(objBody);
 
    }
 
    private Object recordSet(JSONObject objBody) {
        // 判断通信错误码
        if (objBody != null && objBody.get("msg") != null) {
            // 错误信息
            throw new BadRequestException(objBody.get("msg").toString());
        } else if (objBody != null && objBody.get("result") != null) {
            // 通信结果
            return objBody.get("result");
        } else {
            // 通信结果
            return null;
        }
    }
}