李林
2023-10-07 658d4927d468c47208fd012d9128b09249c07eff
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
48
package com.chinaztt.mes.production.util;
 
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
 
import java.util.Map;
 
/**
 * @Description:
 * @Author: shz
 * @Date: 2022/6/1 11:09
 */
@Component
public class MouldUtils {
    @Value("${mould.url}")
    private String mouldUrl;
 
    public JSONArray get(Object param, String url) {
        Map<String, Object> params = BeanUtil.beanToMap(param);
        String result = HttpRequest.get(mouldUrl + url).form(params).execute().body();
        JSONObject jsonObject = JSONObject.parseObject(result);
        JSONArray data = jsonObject.getJSONArray("data");
        Boolean success = jsonObject.getBoolean("success");
        if (success == null) {
            throw new RuntimeException("请求模具处理失败");
        } else if (!success) {
            throw new RuntimeException("请求模具处理失败:" + jsonObject.getString("msg"));
        }
        return data;
    }
 
    public boolean send(Object param, String url) {
        Map<String, Object> params = BeanUtil.beanToMap(param);
        String result = HttpRequest.post(mouldUrl + url).form(params).execute().body();
        JSONObject jsonObject = JSONObject.parseObject(result);
        Boolean success = jsonObject.getBoolean("success");
        if (success == null) {
            throw new RuntimeException("请求模具处理失败");
        } else if (!success) {
            throw new RuntimeException("请求模具处理失败:" + jsonObject.getString("msg"));
        }
        return true;
    }
}