ny
昨天 b6f169fe43a2b13f351aefc152374fc7f0bc8cb7
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package jnpf.base.util;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jakarta.servlet.http.HttpServletRequest;
import jnpf.base.ActionResult;
import jnpf.base.ActionResultCode;
import jnpf.base.entity.VisualdevEntity;
import jnpf.config.ConfigValueUtil;
import jnpf.constant.MsgCode;
import jnpf.exception.WorkFlowException;
import jnpf.util.JsonUtil;
import jnpf.util.ServletUtil;
import jnpf.util.UserProvider;
import jnpf.util.wxutil.HttpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.util.HashMap;
import java.util.Map;
 
/**
 * 流程表单 http请求处理表单
 *
 * @author JNPF开发平台组
 * @version V3.4.5
 * @copyright 引迈信息技术有限公司(https://www.jnpfsoft.com)
 * @date 2022/10/21
 */
@Component
public class FlowFormHttpReqUtils {
 
    private static ConfigValueUtil configValueUtil;
 
    @Autowired
    public void setConfigValueUtil(ConfigValueUtil configValueUtil) {
        FlowFormHttpReqUtils.configValueUtil = configValueUtil;
    }
 
    public Map<String, Object> info(VisualdevEntity visualdevEntity, String id, String token) {
        String requestURL = this.getReqURL(visualdevEntity, id);
        JSONObject jsonObject = HttpUtil.httpRequest(requestURL, "GET", null, token);
        ActionResult actionResult = JSON.toJavaObject(jsonObject, ActionResult.class);
        if (actionResult == null) {
            return new HashMap<>();
        }
        Object data = actionResult.getData();
        return data != null ? JsonUtil.entityToMap(data) : new HashMap<>();
    }
 
    public boolean isUpdate(VisualdevEntity visualdevEntity, String id, String token) {
        String requestURL = this.getReqURL(visualdevEntity, id);
        JSONObject jsonObject = HttpUtil.httpRequest(requestURL, "GET", null, token);
        ActionResult actionResult = JSON.toJavaObject(jsonObject, ActionResult.class);
        return actionResult != null && actionResult.getData() != null;
    }
 
    public void create(VisualdevEntity visualdevEntity, String id, String token, Map<String, Object> map) throws WorkFlowException {
        String requestURL = this.getReqURL(visualdevEntity, id);
        map.remove("id");
        JSONObject jsonObject = HttpUtil.httpRequest(requestURL, "POST", JsonUtil.getObjectToString(map), token);
        ActionResult actionResult = JSON.toJavaObject(jsonObject, ActionResult.class);
        boolean b = actionResult != null && ActionResultCode.Success.getCode().equals(actionResult.getCode());
        if (!b) {
            String msg = actionResult != null ? actionResult.getMsg() : MsgCode.FM001.get();
            throw new WorkFlowException(msg);
        }
    }
 
    public void update(VisualdevEntity visualdevEntity, String id, String token, Map<String, Object> map) throws WorkFlowException {
        String requestURL = this.getReqURL(visualdevEntity, id);
        JSONObject jsonObject = HttpUtil.httpRequest(requestURL, "PUT", JsonUtil.getObjectToString(map), token);
        ActionResult actionResult = JSON.toJavaObject(jsonObject, ActionResult.class);
        boolean b = actionResult != null && ActionResultCode.Success.getCode().equals(actionResult.getCode());
        if (!b) {
            String msg = actionResult != null ? actionResult.getMsg() : MsgCode.FM001.get();
            throw new WorkFlowException(msg);
        }
    }
 
    public void saveOrUpdate(VisualdevEntity visualdevEntity, String id, String token, Map<String, Object> map) throws WorkFlowException {
        boolean update = this.isUpdate(visualdevEntity, id, token);
        if (update) {
            this.update(visualdevEntity, id, token, map);
        } else {
            this.create(visualdevEntity, id, token, map);
        }
    }
 
 
    private String getReqURL(VisualdevEntity visualdevEntity, String id) {
        HttpServletRequest request = ServletUtil.getRequest();
        //请求来源
        String requestURL = visualdevEntity.getInterfaceUrl();
        boolean isHttp = requestURL.toLowerCase().startsWith("http");
        if (!isHttp) {
            //补全(内部)
            requestURL = configValueUtil.getApiDomain() + requestURL;
        }
        return requestURL + "/" + id;
    }
 
    /**
     * 删除数据
     *
     * @param visualdevEntity
     * @param id
     * @param token
     * @throws WorkFlowException
     */
    public void delete(VisualdevEntity visualdevEntity, String id, String token) throws WorkFlowException {
        String requestURL = this.getReqURL(visualdevEntity, id) + "?forceDel=true";
        JSONObject jsonObject = HttpUtil.httpRequest(requestURL, "DELETE", null, token);
        ActionResult actionResult = JSON.toJavaObject(jsonObject, ActionResult.class);
        boolean b = actionResult != null && ActionResultCode.Success.getCode().equals(actionResult.getCode());
        if (!b) {
            String msg = actionResult != null ? actionResult.getMsg() : MsgCode.FM001.get();
            throw new WorkFlowException(msg);
        }
    }
 
    /**
     * 流程状态修改
     *
     * @param visualdevEntity
     * @param flowTaskId
     * @param flowState
     */
    public void saveState(VisualdevEntity visualdevEntity, String flowTaskId, Integer flowState) {
        //请求来源
        String requestURL = visualdevEntity.getInterfaceUrl();
        boolean isHttp = requestURL.toLowerCase().startsWith("http");
        if (!isHttp) {
            //补全(内部)
            requestURL = configValueUtil.getApiDomain() + requestURL;
        }
        requestURL += "/saveState?flowTaskId=" + flowTaskId + "&flowState=" + flowState;
        JSONObject jsonObject = HttpUtil.httpRequest(requestURL, "POST", null, UserProvider.getToken());
    }
}