FlowFormHttpReqUtils.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package jnpf.base.util;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONObject;
  4. import jakarta.servlet.http.HttpServletRequest;
  5. import jnpf.base.ActionResult;
  6. import jnpf.base.ActionResultCode;
  7. import jnpf.base.entity.VisualdevEntity;
  8. import jnpf.config.ConfigValueUtil;
  9. import jnpf.constant.MsgCode;
  10. import jnpf.exception.WorkFlowException;
  11. import jnpf.util.JsonUtil;
  12. import jnpf.util.ServletUtil;
  13. import jnpf.util.UserProvider;
  14. import jnpf.util.wxutil.HttpUtil;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.stereotype.Component;
  17. import java.util.HashMap;
  18. import java.util.Map;
  19. /**
  20. * 流程表单 http请求处理表单
  21. *
  22. * @author JNPF开发平台组
  23. * @version V3.4.5
  24. * @copyright 引迈信息技术有限公司(https://www.jnpfsoft.com)
  25. * @date 2022/10/21
  26. */
  27. @Component
  28. public class FlowFormHttpReqUtils {
  29. private static ConfigValueUtil configValueUtil;
  30. @Autowired
  31. public void setConfigValueUtil(ConfigValueUtil configValueUtil) {
  32. FlowFormHttpReqUtils.configValueUtil = configValueUtil;
  33. }
  34. public Map<String, Object> info(VisualdevEntity visualdevEntity, String id, String token) {
  35. String requestURL = this.getReqURL(visualdevEntity, id);
  36. JSONObject jsonObject = HttpUtil.httpRequest(requestURL, "GET", null, token);
  37. ActionResult actionResult = JSON.toJavaObject(jsonObject, ActionResult.class);
  38. if (actionResult == null) {
  39. return new HashMap<>();
  40. }
  41. Object data = actionResult.getData();
  42. return data != null ? JsonUtil.entityToMap(data) : new HashMap<>();
  43. }
  44. public boolean isUpdate(VisualdevEntity visualdevEntity, String id, String token) {
  45. String requestURL = this.getReqURL(visualdevEntity, id);
  46. JSONObject jsonObject = HttpUtil.httpRequest(requestURL, "GET", null, token);
  47. ActionResult actionResult = JSON.toJavaObject(jsonObject, ActionResult.class);
  48. return actionResult != null && actionResult.getData() != null;
  49. }
  50. public void create(VisualdevEntity visualdevEntity, String id, String token, Map<String, Object> map) throws WorkFlowException {
  51. String requestURL = this.getReqURL(visualdevEntity, id);
  52. map.remove("id");
  53. JSONObject jsonObject = HttpUtil.httpRequest(requestURL, "POST", JsonUtil.getObjectToString(map), token);
  54. ActionResult actionResult = JSON.toJavaObject(jsonObject, ActionResult.class);
  55. boolean b = actionResult != null && ActionResultCode.Success.getCode().equals(actionResult.getCode());
  56. if (!b) {
  57. String msg = actionResult != null ? actionResult.getMsg() : MsgCode.FM001.get();
  58. throw new WorkFlowException(msg);
  59. }
  60. }
  61. public void update(VisualdevEntity visualdevEntity, String id, String token, Map<String, Object> map) throws WorkFlowException {
  62. String requestURL = this.getReqURL(visualdevEntity, id);
  63. JSONObject jsonObject = HttpUtil.httpRequest(requestURL, "PUT", JsonUtil.getObjectToString(map), token);
  64. ActionResult actionResult = JSON.toJavaObject(jsonObject, ActionResult.class);
  65. boolean b = actionResult != null && ActionResultCode.Success.getCode().equals(actionResult.getCode());
  66. if (!b) {
  67. String msg = actionResult != null ? actionResult.getMsg() : MsgCode.FM001.get();
  68. throw new WorkFlowException(msg);
  69. }
  70. }
  71. public void saveOrUpdate(VisualdevEntity visualdevEntity, String id, String token, Map<String, Object> map) throws WorkFlowException {
  72. boolean update = this.isUpdate(visualdevEntity, id, token);
  73. if (update) {
  74. this.update(visualdevEntity, id, token, map);
  75. } else {
  76. this.create(visualdevEntity, id, token, map);
  77. }
  78. }
  79. private String getReqURL(VisualdevEntity visualdevEntity, String id) {
  80. HttpServletRequest request = ServletUtil.getRequest();
  81. //请求来源
  82. String requestURL = visualdevEntity.getInterfaceUrl();
  83. boolean isHttp = requestURL.toLowerCase().startsWith("http");
  84. if (!isHttp) {
  85. //补全(内部)
  86. requestURL = configValueUtil.getApiDomain() + requestURL;
  87. }
  88. return requestURL + "/" + id;
  89. }
  90. /**
  91. * 删除数据
  92. *
  93. * @param visualdevEntity
  94. * @param id
  95. * @param token
  96. * @throws WorkFlowException
  97. */
  98. public void delete(VisualdevEntity visualdevEntity, String id, String token) throws WorkFlowException {
  99. String requestURL = this.getReqURL(visualdevEntity, id) + "?forceDel=true";
  100. JSONObject jsonObject = HttpUtil.httpRequest(requestURL, "DELETE", null, token);
  101. ActionResult actionResult = JSON.toJavaObject(jsonObject, ActionResult.class);
  102. boolean b = actionResult != null && ActionResultCode.Success.getCode().equals(actionResult.getCode());
  103. if (!b) {
  104. String msg = actionResult != null ? actionResult.getMsg() : MsgCode.FM001.get();
  105. throw new WorkFlowException(msg);
  106. }
  107. }
  108. /**
  109. * 流程状态修改
  110. *
  111. * @param visualdevEntity
  112. * @param flowTaskId
  113. * @param flowState
  114. */
  115. public void saveState(VisualdevEntity visualdevEntity, String flowTaskId, Integer flowState) {
  116. //请求来源
  117. String requestURL = visualdevEntity.getInterfaceUrl();
  118. boolean isHttp = requestURL.toLowerCase().startsWith("http");
  119. if (!isHttp) {
  120. //补全(内部)
  121. requestURL = configValueUtil.getApiDomain() + requestURL;
  122. }
  123. requestURL += "/saveState?flowTaskId=" + flowTaskId + "&flowState=" + flowState;
  124. JSONObject jsonObject = HttpUtil.httpRequest(requestURL, "POST", null, UserProvider.getToken());
  125. }
  126. }