diff --git a/jeecg-boot-master/ant-design-vue-jeecg/src/api/api.js b/jeecg-boot-master/ant-design-vue-jeecg/src/api/api.js index 536bdda..315d182 100644 --- a/jeecg-boot-master/ant-design-vue-jeecg/src/api/api.js +++ b/jeecg-boot-master/ant-design-vue-jeecg/src/api/api.js @@ -108,7 +108,7 @@ export const searchMaterialByCode = (params)=>postAction('/config/material/searc export const listReceiveByReceiptId = (params)=>postAction('/receipt/receiveHeader/listReceiveByReceiptId', params); export const createTask = (params)=>postAction('/receipt/receiptContainerHeader/createTask', params); export const completeTaskByWMS = (params)=>postAction('/task/taskHeader/completeTaskByWMS', params); - +export const execute = (params)=>postAction('/task/taskHeader/execute', params); // 中转HTTP请求 export const transitRESTful = { diff --git a/jeecg-boot-master/ant-design-vue-jeecg/src/views/system/task/TaskHeaderList.vue b/jeecg-boot-master/ant-design-vue-jeecg/src/views/system/task/TaskHeaderList.vue index d59cdd5..b3d41e4 100644 --- a/jeecg-boot-master/ant-design-vue-jeecg/src/views/system/task/TaskHeaderList.vue +++ b/jeecg-boot-master/ant-design-vue-jeecg/src/views/system/task/TaskHeaderList.vue @@ -132,8 +132,9 @@ </template> <span slot="action" slot-scope="text, record"> + <a v-if="record.status == 1" @click="executeTask(record)">下发任务</a> + <a-divider type="vertical" /> <a v-if="record.status < 100" @click="completeTask(record)">完成任务</a> - <a-divider type="vertical" /> <a-dropdown> <a class="ant-dropdown-link">更多 <a-icon type="down" /></a> @@ -172,6 +173,7 @@ import {initDictOptions,filterMultiDictText} from '@/components/dict/JDictSelectUtil' import '@/assets/less/TableExpand.less' import {completeTaskByWMS} from '@/api/api' + import {execute} from '@/api/api' export default { name: "TaskHeaderList", @@ -353,7 +355,6 @@ }, completeTask(record) { this.loading = true; - const that = this; this.model = Object.assign({}, record); completeTaskByWMS(this.model).then((res) => { this.loading = false; @@ -366,6 +367,20 @@ this.searchQuery(); }); }, + executeTask(record) { + this.loading = true; + this.model = Object.assign({}, record); + execute(this.model).then((res) => { + this.loading = false; + if (res.success) { + this.$message.success(res.message); + } + else { + this.$message.error(res.message); + } + this.searchQuery(); + }); + }, getSuperFieldList(){ let fieldList=[]; fieldList.push({type:'int',value:'taskType',text:'任务类型',dictCode:'task_type'}) diff --git a/jeecg-boot-master/jeecg-boot/jeecg-boot-base/jeecg-boot-base-core/src/main/java/org/jeecg/common/api/vo/Result.java b/jeecg-boot-master/jeecg-boot/jeecg-boot-base/jeecg-boot-base-core/src/main/java/org/jeecg/common/api/vo/Result.java index 6b7724e..91f7be3 100644 --- a/jeecg-boot-master/jeecg-boot/jeecg-boot-base/jeecg-boot-base-core/src/main/java/org/jeecg/common/api/vo/Result.java +++ b/jeecg-boot-master/jeecg-boot/jeecg-boot-base/jeecg-boot-base-core/src/main/java/org/jeecg/common/api/vo/Result.java @@ -33,17 +33,23 @@ public class Result<T> implements Serializable { private String message = ""; /** + * 返回处理消息 + */ + @ApiModelProperty(value = "返回处理消息") + private String msg = ""; + + /** * 返回代码 */ @ApiModelProperty(value = "返回代码") private Integer code = 0; - + /** * 返回数据对象 data */ @ApiModelProperty(value = "返回数据对象") private T result; - + /** * 时间戳 */ @@ -62,7 +68,7 @@ public class Result<T> implements Serializable { this.code = code; this.message = message; } - + public Result<T> success(String message) { this.message = message; this.code = CommonConstant.SC_OK_200; @@ -143,7 +149,7 @@ public class Result<T> implements Serializable { public static<T> Result<T> error(String msg) { return error(CommonConstant.SC_INTERNAL_SERVER_ERROR_500, msg); } - + public static<T> Result<T> error(int code, String msg) { Result<T> r = new Result<T>(); r.setCode(code); @@ -169,4 +175,4 @@ public class Result<T> implements Serializable { @JsonIgnore private String onlTable; -} \ No newline at end of file +} diff --git a/jeecg-boot-master/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/wms/api/wcs/service/WcsService.java b/jeecg-boot-master/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/wms/api/wcs/service/WcsService.java index 0e65b4a..abcc403 100644 --- a/jeecg-boot-master/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/wms/api/wcs/service/WcsService.java +++ b/jeecg-boot-master/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/wms/api/wcs/service/WcsService.java @@ -2,6 +2,7 @@ package org.jeecg.modules.wms.api.wcs.service; import org.jeecg.common.api.vo.Result; import org.jeecg.modules.wms.api.wcs.entity.WarecellDomain; +import org.jeecg.modules.wms.api.wcs.entity.WcsTask; import org.jeecg.modules.wms.task.taskHeader.entity.TaskHeader; /** @@ -13,4 +14,6 @@ public interface WcsService { Result warecellAllocation(WarecellDomain warecellDomain); Result wcsTaskAssign(TaskHeader taskHeader); + + WcsTask switchTaskTypeToWcs(WcsTask wcsTask); } diff --git a/jeecg-boot-master/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/wms/api/wcs/service/WcsServiceImpl.java b/jeecg-boot-master/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/wms/api/wcs/service/WcsServiceImpl.java index 1958bf9..2081917 100644 --- a/jeecg-boot-master/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/wms/api/wcs/service/WcsServiceImpl.java +++ b/jeecg-boot-master/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/wms/api/wcs/service/WcsServiceImpl.java @@ -1,12 +1,15 @@ package org.jeecg.modules.wms.api.wcs.service; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; import com.aliyun.oss.ServiceException; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import org.jeecg.common.api.vo.Result; import org.jeecg.modules.wms.api.wcs.entity.WarecellDomain; import org.jeecg.modules.wms.api.wcs.entity.WcsTask; +import org.jeecg.modules.wms.config.address.service.IAddressService; import org.jeecg.modules.wms.config.container.entity.Container; import org.jeecg.modules.wms.config.container.service.IContainerService; import org.jeecg.modules.wms.config.location.entity.Location; @@ -31,6 +34,8 @@ import org.jeecg.modules.wms.task.taskHeader.service.ITaskDetailService; import org.jeecg.modules.wms.task.taskHeader.service.ITaskHeaderService; import org.jeecg.utils.StringUtils; import org.jeecg.utils.constant.QuantityConstant; +import org.jeecg.utils.http.OkHttpUtils; +import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -69,6 +74,8 @@ public class WcsServiceImpl implements WcsService { private LocationAllocationService locationAllocationService; @Resource private ILocationService locationService; + @Resource + private IAddressService addressService; @Override @Transactional(rollbackFor = Exception.class) @@ -358,8 +365,73 @@ public class WcsServiceImpl implements WcsService { } wcsTask.setPriority(10); wcsTask.setPlatform(QuantityConstant.PLATFORM_WMS); + wcsTask.setRemark(QuantityConstant.EMPTY_STRING); + Container container = containerService.getContainerByCode(containerCode, warehouseCode); + if(container == null) { + throw new ServiceException("下发任务时,容器没有找到"); + } +// String zoneCode = container.getZoneCode(); +// if(StringUtils.isEmpty(zoneCode)) { +// throw new ServiceException("下发任务时,库区编码为空"); +// } + String value = parameterConfigurationService.getValueByCode(QuantityConstant.RULE_CONNECT_WCS); + if(StringUtils.isEmpty(value)) { + throw new ServiceException("下发任务时,没有找到连接WCS的数据配置"); + } + int connectWCS = Integer.parseInt(value); + if (connectWCS == QuantityConstant.RULE_WCS_CONNECT) { +// String url = addressService.getUrlByParam(QuantityConstant.ADDRESS_WCS_TASK_ASSIGN, +// warehouseCode, zoneCode); + String url = addressService.getUrlByParam(QuantityConstant.ADDRESS_WCS_TASK_ASSIGN); + wcsTask = switchTaskTypeToWcs(wcsTask); + String jsonParam = JSON.toJSONString(wcsTask); + System.out.println(jsonParam); + String body = OkHttpUtils.bodypost(url, jsonParam); + if(StringUtils.isEmpty(body)) { + throw new ServiceException("接口地址错误或返回为空"); + } + Result result = JSON.parseObject(body, Result.class); + if(result.getCode() == QuantityConstant.HTTP_OK) { + return Result.error(result.getMsg()); + } + } + + return Result.ok("下发成功"); + } + + // wcs任务类型 入库 = 100 , 出库 = 300,分拣出库 = 400 ,移库 = 800,换站=1000 + @Override + public WcsTask switchTaskTypeToWcs(WcsTask wcsTask) { + int taskType = wcsTask.getTaskType().intValue(); + switch (taskType) { + case QuantityConstant.TASK_TYPE_WHOLERECEIPT: + case QuantityConstant.TASK_TYPE_EMPTYRECEIPT: + case QuantityConstant.TASK_TYPE_MANY_EMPTYRECEIPT: + wcsTask.setTaskType(100); + break; + case QuantityConstant.TASK_TYPE_WHOLESHIPMENT: + case QuantityConstant.TASK_TYPE_EMPTYSHIPMENT: + case QuantityConstant.TASK_TYPE_MANY_EMPTYSHIPMENT: + wcsTask.setTaskType(300); + break; + case QuantityConstant.TASK_TYPE_SUPPLEMENTRECEIPT: + case QuantityConstant.TASK_TYPE_SORTINGSHIPMENT: + case QuantityConstant.TASK_TYPE_CYCLECOUNT: + case QuantityConstant.TASK_TYPE_VIEW: + wcsTask.setTaskType(400); + break; + case QuantityConstant.TASK_TYPE_TRANSFER: + wcsTask.setTaskType(800); + break; + case QuantityConstant.TASK_TYPE_OVER_STATION: + case QuantityConstant.TASK_TYPE_MANY_OVER_STATION: + wcsTask.setTaskType(1000); + break; + default: + throw new ServiceException("不支持的任务类型"); - return null; + } + return wcsTask; } } diff --git a/jeecg-boot-master/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/wms/config/address/entity/Address.java b/jeecg-boot-master/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/wms/config/address/entity/Address.java index a55802a..6769e6a 100644 --- a/jeecg-boot-master/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/wms/config/address/entity/Address.java +++ b/jeecg-boot-master/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/wms/config/address/entity/Address.java @@ -36,9 +36,9 @@ public class Address implements Serializable { @ApiModelProperty(value = "主键") private Integer id; /**编码*/ - @Excel(name = "编码", width = 15) - @ApiModelProperty(value = "编码") - private String code; + @Excel(name = "参数", width = 15) + @ApiModelProperty(value = "参数") + private String param; /**仓库编码*/ @Excel(name = "仓库编码", width = 15) @ApiModelProperty(value = "仓库编码") diff --git a/jeecg-boot-master/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/wms/config/address/service/IAddressService.java b/jeecg-boot-master/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/wms/config/address/service/IAddressService.java index eec392d..4645f28 100644 --- a/jeecg-boot-master/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/wms/config/address/service/IAddressService.java +++ b/jeecg-boot-master/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/wms/config/address/service/IAddressService.java @@ -12,4 +12,8 @@ import org.jeecg.modules.wms.config.address.entity.Address; public interface IAddressService extends IService<Address> { Address getAddressByUrl(String url, String warehouseCode); + + String getUrlByParam(String param, String warehouseCode, String zoneCode); + + String getUrlByParam(String param); } diff --git a/jeecg-boot-master/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/wms/config/address/service/impl/AddressServiceImpl.java b/jeecg-boot-master/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/wms/config/address/service/impl/AddressServiceImpl.java index 07a9dd1..de0c83a 100644 --- a/jeecg-boot-master/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/wms/config/address/service/impl/AddressServiceImpl.java +++ b/jeecg-boot-master/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/wms/config/address/service/impl/AddressServiceImpl.java @@ -2,6 +2,7 @@ package org.jeecg.modules.wms.config.address.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; +import org.apache.commons.math3.analysis.function.Add; import org.jeecg.modules.wms.config.address.entity.Address; import org.jeecg.modules.wms.config.address.mapper.AddressMapper; import org.jeecg.modules.wms.config.address.service.IAddressService; @@ -26,4 +27,32 @@ public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> impl Address address = getOne(addressLambdaQueryWrapper); return address; } + + @Override + public String getUrlByParam(String param, String warehouseCode, String zoneCode) { + LambdaQueryWrapper<Address> addressLambdaQueryWrapper = Wrappers.lambdaQuery(); + addressLambdaQueryWrapper.eq(Address::getParam, param) + .eq(Address::getWarehouseCode, warehouseCode) + .eq(Address::getZoneCode, zoneCode); + Address address = getOne(addressLambdaQueryWrapper); + if(address == null) { + return null; + } + String url = address.getUrl(); + return url; + } + + @Override + public String getUrlByParam(String param) { + LambdaQueryWrapper<Address> addressLambdaQueryWrapper = Wrappers.lambdaQuery(); + addressLambdaQueryWrapper.eq(Address::getParam, param); + Address address = getOne(addressLambdaQueryWrapper); + if(address == null) { + return null; + } + String url = address.getUrl(); + return url; + } + + } diff --git a/jeecg-boot-master/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/wms/framework/aspectj/ApiLogAspect.java b/jeecg-boot-master/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/wms/framework/aspectj/ApiLogAspect.java index d6427f9..eb00328 100644 --- a/jeecg-boot-master/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/wms/framework/aspectj/ApiLogAspect.java +++ b/jeecg-boot-master/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/wms/framework/aspectj/ApiLogAspect.java @@ -176,7 +176,7 @@ public class ApiLogAspect Address address = addressService.getAddressByUrl(url.toString(), warehouseCode); log.setApiName(apiName); log.setRequestFrom("WMS"); - log.setResponseBy(address.getCode().toUpperCase()); + log.setResponseBy(address.getParam().toUpperCase()); } catch (Exception e) { e.printStackTrace(); } @@ -307,7 +307,7 @@ public class ApiLogAspect Address address = addressService.getAddressByUrl(url.toString(), QuantityConstant.DEFAULT_WAREHOUSE); log.setApiName(apiName); log.setRequestFrom("WMS"); - log.setResponseBy(address.getCode().toUpperCase()); + log.setResponseBy(address.getParam().toUpperCase()); }catch (Exception e){ e.printStackTrace(); } diff --git a/jeecg-boot-master/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/wms/receipt/receiptHeader/service/impl/ReceiptHeaderServiceImpl.java b/jeecg-boot-master/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/wms/receipt/receiptHeader/service/impl/ReceiptHeaderServiceImpl.java index b832a82..b99221c 100644 --- a/jeecg-boot-master/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/wms/receipt/receiptHeader/service/impl/ReceiptHeaderServiceImpl.java +++ b/jeecg-boot-master/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/wms/receipt/receiptHeader/service/impl/ReceiptHeaderServiceImpl.java @@ -129,7 +129,7 @@ public class ReceiptHeaderServiceImpl extends ServiceImpl<ReceiptHeaderMapper, R @Override public boolean updateReceiptHeaderStatus(Integer id) { LambdaQueryWrapper<ReceiptDetail> receiptDetailLambdaQueryWrapper = Wrappers.lambdaQuery(); - receiptDetailLambdaQueryWrapper.eq(ReceiptDetail::getId, id); + receiptDetailLambdaQueryWrapper.eq(ReceiptDetail::getReceiptId, id); List<ReceiptDetail> receiptDetailList = receiptDetailService.list(receiptDetailLambdaQueryWrapper); ReceiptHeader receiptHeader = receiptHeaderService.getById(id); if(receiptHeader == null) { @@ -141,8 +141,8 @@ public class ReceiptHeaderServiceImpl extends ServiceImpl<ReceiptHeaderMapper, R minStatus = QuantityConstant.RECEIPT_HEADER_BUILD; maxStatus = QuantityConstant.RECEIPT_HEADER_BUILD; } else { - minStatus = QuantityConstant.RECEIPT_HEADER_BUILD; - maxStatus = QuantityConstant.RECEIPT_HEADER_BUILD; + minStatus = receiptDetailList.get(0).getStatus(); + maxStatus = receiptDetailList.get(0).getStatus(); for(ReceiptDetail receiptDetail : receiptDetailList) { int status = receiptDetail.getStatus(); if (minStatus > status) { diff --git a/jeecg-boot-master/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/wms/task/taskHeader/service/impl/TaskHeaderServiceImpl.java b/jeecg-boot-master/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/wms/task/taskHeader/service/impl/TaskHeaderServiceImpl.java index 10a7605..2878f4b 100644 --- a/jeecg-boot-master/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/wms/task/taskHeader/service/impl/TaskHeaderServiceImpl.java +++ b/jeecg-boot-master/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/wms/task/taskHeader/service/impl/TaskHeaderServiceImpl.java @@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import org.jeecg.common.api.vo.Result; +import org.jeecg.modules.wms.api.wcs.service.WcsService; import org.jeecg.modules.wms.config.container.entity.Container; import org.jeecg.modules.wms.config.container.service.IContainerService; import org.jeecg.modules.wms.config.location.entity.Location; @@ -78,6 +79,8 @@ public class TaskHeaderServiceImpl extends ServiceImpl<TaskHeaderMapper, TaskHea private IReceiptContainerHeaderService receiptContainerHeaderService; @Resource private IMaterialService materialService; + @Resource + private WcsService wcsService; @Override @Transactional @@ -321,7 +324,24 @@ public class TaskHeaderServiceImpl extends ServiceImpl<TaskHeaderMapper, TaskHea @Override public Result sendTaskToWcs(Integer taskId) { - return null; + TaskHeader taskHeader = taskHeaderService.getById(taskId); + if(taskHeader == null) { + return Result.error("下发wcs任务时,没有找到任务, 任务号" + taskId); + } + if (taskHeader.getStatus() >= QuantityConstant.TASK_STATUS_RELEASE) { + return Result.error("任务" + taskId + "已经下发,请不要重复下发,操作中止"); + } + Result result = wcsService.wcsTaskAssign(taskHeader); + if(!result.isSuccess()) { + throw new ServiceException(result.getMessage()); + } + taskHeader.setStatus(QuantityConstant.TASK_STATUS_RELEASE); + boolean success = taskHeaderService.updateById(taskHeader); + if(!success) { + throw new ServiceException("下发wcs任务时, 更新任务状态失败"); + } + + return Result.ok("执行下发任务成功"); } @Transactional(rollbackFor = Exception.class) diff --git a/jeecg-boot-master/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/utils/constant/QuantityConstant.java b/jeecg-boot-master/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/utils/constant/QuantityConstant.java index 488316d..232c4a9 100644 --- a/jeecg-boot-master/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/utils/constant/QuantityConstant.java +++ b/jeecg-boot-master/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/utils/constant/QuantityConstant.java @@ -552,6 +552,8 @@ public class QuantityConstant { public static final int STATUS_ENABLE = 1; public static final int STATUS_DISABLE = 0; + public static final int HTTP_OK = 200; + public static final String EER_TABLE_OTHERSHIPMENT = "STK_MisDelivery"; /*直接调拨单回传*/ diff --git a/jeecg-boot-master/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/utils/http/HttpUtils.java b/jeecg-boot-master/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/utils/http/HttpUtils.java new file mode 100644 index 0000000..0657bb8 --- /dev/null +++ b/jeecg-boot-master/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/utils/http/HttpUtils.java @@ -0,0 +1,408 @@ +package org.jeecg.utils.http; + +import org.apache.commons.io.IOUtils; +import org.apache.http.HttpResponse; +import org.apache.http.HttpStatus; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.jeecg.modules.wms.framework.aspectj.ApiLogAspect; +import org.jeecg.modules.wms.monitor.apiLog.entity.ApiLog; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.net.ssl.*; +import java.io.*; +import java.net.*; +import java.security.cert.X509Certificate; + + +/** + * 通用http发送方法 + * + * @author huaheng + */ +public class HttpUtils +{ + + private static final Logger log = LoggerFactory.getLogger(HttpUtils.class); + + /** + * 向指定 URL 发送GET方法的请求 + * + * @param url 发送请求的 URL + * @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。 + * @return 所代表远程资源的响应结果 + */ + public static String sendGet(String url, String param) + { + StringBuilder result = new StringBuilder(); + BufferedReader in = null; + try + { + String urlNameString = url + "?" + param; + log.info("sendGet - {}", urlNameString); + URL realUrl = new URL(urlNameString); + URLConnection connection = realUrl.openConnection(); + connection.setRequestProperty("accept", "*/*"); + connection.setRequestProperty("connection", "Keep-Alive"); + connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); + connection.connect(); + in = new BufferedReader(new InputStreamReader(connection.getInputStream())); + String line; + while ((line = in.readLine()) != null) + { + result.append(line); + } + log.info("recv - {}", result); + } + catch (ConnectException e) + { + log.error("调用HttpUtils.sendGet ConnectException, url=" + url + ",param=" + param, e.getMessage()); + } + catch (SocketTimeoutException e) + { + log.error("调用HttpUtils.sendGet SocketTimeoutException, url=" + url + ",param=" + param, e.getMessage()); + } + catch (IOException e) + { + log.error("调用HttpUtils.sendGet IOException, url=" + url + ",param=" + param, e.getMessage()); + } + catch (Exception e) + { + log.error("调用HttpsUtil.sendGet Exception, url=" + url + ",param=" + param, e.getMessage()); + } + finally + { + try + { + if (in != null) + { + in.close(); + } + } + catch (Exception ex) + { + log.error("调用in.close Exception, url=" + url + ",param=" + param, ex.getMessage()); + } + } + return result.toString(); + } + + //post请求 为erp回传定制 + public static String erppost(String strURL, String params,String key) { + HttpURLConnection connection = null; + InputStream is = null; + OutputStreamWriter out = null; + String result = null; + ApiLog log = null; + + try { + URL url = new URL(strURL);// 创建连接 + String ur=""+url; + connection = (HttpURLConnection) url.openConnection(); + connection.setDoOutput(true); + connection.setDoInput(true); + connection.setUseCaches(false); + connection.setInstanceFollowRedirects(true); + connection.setRequestMethod("POST");// 设置请求方式 + connection.setRequestProperty("Accept","application/json");// 设置接收数据的格式 + connection.setRequestProperty("Content-Type","application/json");// 设置发送数据的格式 + connection.setRequestProperty("apiKey",key);//测试环境key + log = ApiLogAspect.initApiLog(connection, params); + connection.connect(); + out = new OutputStreamWriter( connection.getOutputStream(),"UTF-8");// utf-8编码 + out.append(params); + out.flush(); + out.close(); // 读取响应 + int length = (int) connection.getContentLength();// 获取长度 + is = connection.getInputStream(); + BufferedReader br = new BufferedReader(new InputStreamReader(is)); + length = 10000; + if(length > 0) { + byte[] data = new byte[length]; + byte[] temp = new byte[512]; + int readLen = 0; + int destPos = 0; + while ((readLen = is.read(temp)) > 0) { + System.arraycopy(temp, 0, data, destPos, readLen); + destPos += readLen; + } + result = new String(data, "UTF-8"); + System.out.println(result); + } + } catch (Exception e) { + ApiLogAspect.setApiLogException(log, e); + e.printStackTrace(); + }finally { + ApiLogAspect.finishApiLog(log, connection, result); + if(connection != null) + connection.disconnect(); + IOUtils.closeQuietly(out, is); + } + return result; + } + + /** + * 向指定 URL 发送POST方法的请求 + * + * @param url 发送请求的 URL + * @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。 + * @return 所代表远程资源的响应结果 + */ + public static String sendPost(String url, String param) + { + PrintWriter out = null; + BufferedReader in = null; + StringBuilder result = new StringBuilder(); + try + { + String urlNameString = url + "?" + param; + log.info("sendPost - {}", urlNameString); + URL realUrl = new URL(urlNameString); + URLConnection conn = realUrl.openConnection(); + conn.setRequestProperty("accept", "*/*"); + conn.setRequestProperty("connection", "Keep-Alive"); + conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); + conn.setRequestProperty("Accept-Charset", "utf-8"); + conn.setRequestProperty("contentType", "utf-8"); + conn.setDoOutput(true); + conn.setDoInput(true); + out = new PrintWriter(conn.getOutputStream()); + out.print(param); + out.flush(); + if (((HttpURLConnection)conn).getResponseCode() >= 390) { + in = new BufferedReader(new InputStreamReader(((HttpURLConnection) conn).getErrorStream())); + } else { + in = new BufferedReader(new InputStreamReader(conn.getInputStream())); + } +// in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8")); + String line; + while ((line = in.readLine()) != null) + { + result.append(line); + } + log.info("recv - {}", result); + } + catch (ConnectException e) + { + log.error("调用HttpUtils.sendPost ConnectException, url=" + url + ",param=" + param, e.getMessage()); + } + catch (SocketTimeoutException e) + { + log.error("调用HttpUtils.sendPost SocketTimeoutException, url=" + url + ",param=" + param, e.getMessage()); + } + catch (IOException e) + { + log.error("调用HttpUtils.sendPost IOException, url=" + url + ",param=" + param, e.getMessage()); + } + catch (Exception e) + { + log.error("调用HttpsUtil.sendPost Exception, url=" + url + ",param=" + param, e.getMessage()); + } + finally + { + try + { + if (out != null) + { + out.close(); + } + if (in != null) + { + in.close(); + } + } + catch (IOException ex) + { + log.error("调用in.close Exception, url=" + url + ",param=" + param, ex.getMessage()); + } + } + return result.toString(); + } + + public static String sendSSLPost(String url, String param) + { + StringBuilder result = new StringBuilder(); + String urlNameString = url + "?" + param; + try + { + log.info("sendSSLPost - {}", urlNameString); + SSLContext sc = SSLContext.getInstance("SSL"); + sc.init(null, new TrustManager[] { new TrustAnyTrustManager() }, new java.security.SecureRandom()); + URL console = new URL(urlNameString); + HttpsURLConnection conn = (HttpsURLConnection) console.openConnection(); + conn.setRequestProperty("accept", "*/*"); + conn.setRequestProperty("connection", "Keep-Alive"); + conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); + conn.setRequestProperty("Accept-Charset", "utf-8"); + conn.setRequestProperty("contentType", "utf-8"); + conn.setDoOutput(true); + conn.setDoInput(true); + + conn.setSSLSocketFactory(sc.getSocketFactory()); + conn.setHostnameVerifier(new TrustAnyHostnameVerifier()); + conn.connect(); + InputStream is = conn.getInputStream(); + BufferedReader br = new BufferedReader(new InputStreamReader(is)); + String ret = ""; + while ((ret = br.readLine()) != null) + { + if (ret != null && !"".equals(ret.trim())) + { + result.append(new String(ret.getBytes("ISO-8859-1"), "utf-8")); + } + } + log.info("recv - {}", result); + conn.disconnect(); + br.close(); + } + catch (ConnectException e) + { + log.error("调用HttpUtils.sendSSLPost ConnectException, url=" + url + ",param=" + param, e.getMessage()); + } + catch (SocketTimeoutException e) + { + log.error("调用HttpUtils.sendSSLPost SocketTimeoutException, url=" + url + ",param=" + param, e.getMessage()); + } + catch (IOException e) + { + log.error("调用HttpUtils.sendSSLPost IOException, url=" + url + ",param=" + param, e.getMessage()); + } + catch (Exception e) + { + log.error("调用HttpsUtil.sendSSLPost Exception, url=" + url + ",param=" + param, e.getMessage()); + } + return result.toString(); + } + + + //此方法是将参数以body形式发送post请求 + public static String bodypost(String strURL, String params) { + System.out.println(strURL); + System.out.println(params); + HttpURLConnection connection = null; + InputStream is = null; + OutputStreamWriter out = null; + String result = null; + ApiLog log = null; + + try { + URL url = new URL(strURL);// 创建连接 + String ur=""+url; + connection = (HttpURLConnection) url.openConnection(); + connection.setDoOutput(true); + connection.setDoInput(true); + connection.setUseCaches(false); + connection.setInstanceFollowRedirects(true); + connection.setRequestMethod("POST");// 设置请求方式 + connection.setRequestProperty("Accept","application/json");// 设置接收数据的格式 + connection.setRequestProperty("Content-Type","application/json");// 设置发送数据的格式 + if ((ur.contains("10.0.15.19"))) + { + connection.setRequestProperty("apiKey","c687ef505557428595e6a596fba5de6c");//测试环境key + } + log = ApiLogAspect.initApiLog(connection, params); + connection.connect(); + out = new OutputStreamWriter( connection.getOutputStream(),"UTF-8");// utf-8编码 + out.append(params); + out.flush(); + out.close(); // 读取响应 + int length = (int) connection.getContentLength();// 获取长度 + is = connection.getInputStream(); + BufferedReader br = new BufferedReader(new InputStreamReader(is)); + length = 10000; + if(length > 0) { + byte[] data = new byte[length]; + byte[] temp = new byte[512]; + int readLen = 0; + int destPos = 0; + while ((readLen = is.read(temp)) > 0) { + System.arraycopy(temp, 0, data, destPos, readLen); + destPos += readLen; + } + result = new String(data, "UTF-8"); + System.out.println(result); + } + } catch (Exception e) { + ApiLogAspect.setApiLogException(log, e); + e.printStackTrace(); + }finally { + ApiLogAspect.finishApiLog(log, connection, result); + if(connection != null) + connection.disconnect(); + IOUtils.closeQuietly(out, is); + } + return result; + } + + /** + * 发送以json为参数的POST请求 + * @param json + * @param url + * @return + */ + public static String sendJsonPostToken(String json, String url){ + String result = ""; + HttpPost post = new HttpPost(url); + try{ + CloseableHttpClient httpClient = HttpClients.createDefault(); + + post.setHeader("Content-Type","application/json;charset=utf-8"); + post.addHeader("Authorization", "Basic YWRtaW46"); + StringEntity postingString = new StringEntity(json,"utf-8"); + post.setEntity(postingString); + HttpResponse response = httpClient.execute(post); + + InputStream in = response.getEntity().getContent(); + BufferedReader br = new BufferedReader(new InputStreamReader(in, "utf-8")); + StringBuilder strber= new StringBuilder(); + String line = null; + while((line = br.readLine())!=null){ + strber.append(line+'\n'); + } + br.close(); + in.close(); + result = strber.toString(); + if(response.getStatusLine().getStatusCode()!= HttpStatus.SC_OK){ + result = "服务器异常"; + } + } catch (Exception e){ + System.out.println("请求异常"); + throw new RuntimeException(e); + } finally{ + post.abort(); + } + return result; + } + + private static class TrustAnyTrustManager implements X509TrustManager + { + @Override + public void checkClientTrusted(X509Certificate[] chain, String authType) + { + } + + @Override + public void checkServerTrusted(X509Certificate[] chain, String authType) + { + } + + @Override + public X509Certificate[] getAcceptedIssuers() + { + return new X509Certificate[] {}; + } + } + + private static class TrustAnyHostnameVerifier implements HostnameVerifier + { + @Override + public boolean verify(String hostname, SSLSession session) + { + return true; + } + } + +} diff --git a/jeecg-boot-master/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/utils/http/OkHttpUtils.java b/jeecg-boot-master/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/utils/http/OkHttpUtils.java new file mode 100644 index 0000000..46c2048 --- /dev/null +++ b/jeecg-boot-master/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/utils/http/OkHttpUtils.java @@ -0,0 +1,190 @@ +package org.jeecg.utils.http; + + +import com.alibaba.fastjson.parser.Feature; +import com.google.gson.Gson; +import okhttp3.*; +import org.jeecg.modules.wms.framework.aspectj.ApiLogAspect; +import org.jeecg.modules.wms.monitor.apiLog.entity.ApiLog; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.lang.reflect.Type; +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; +import java.util.concurrent.TimeUnit; + +/** + * OkHttp发送请求 + * @author huaheng + * @Date 2022-5-30 + */ +public class OkHttpUtils { + + private static final Logger log = LoggerFactory.getLogger(OkHttpUtils.class); + + /** + * 最大连接时间 + */ + public final static int CONNECTION_TIMEOUT = 30; + /** + * JSON格式 + */ + public static final MediaType MEDIA_TYPE_JSON = MediaType.parse("application/json; charset=utf-8"); + /** + * OkHTTP线程池最大空闲线程数 + */ + public final static int MAX_IDLE_CONNECTIONS = 100; + /** + * OkHTTP线程池空闲线程存活时间 + */ + public final static long KEEP_ALIVE_DURATION = 30L; + + + private static final String CONTENT_TYPE = "Content-Type"; + + + /** + * client + * 配置重试 + */ + private final static OkHttpClient HTTP_CLIENT = new OkHttpClient.Builder() + .readTimeout(CONNECTION_TIMEOUT, TimeUnit.SECONDS) + .writeTimeout(CONNECTION_TIMEOUT, TimeUnit.SECONDS) + .connectTimeout(CONNECTION_TIMEOUT, TimeUnit.SECONDS) + .connectionPool(new ConnectionPool(MAX_IDLE_CONNECTIONS, KEEP_ALIVE_DURATION, TimeUnit.MINUTES)) + .build(); + private static final Gson GSON = new Gson(); + + /** + * 向指定 URL 发送GET方法的请求 + * + * @param url 发送请求的 URL + //* @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。 + * @return 所代表远程资源的响应结果 + */ + public static String sendGet(String url,String param) + { + //headers 请求头 + Map<String, String> headers=new HashMap<>(); + //请求URI + String urlNameString = url + "?" + param; + + Request.Builder builder = new Request.Builder(); + buildHeader(builder, headers); + + Request request = builder.url(urlNameString).get().build(); + Response response = null; + try { + response = HTTP_CLIENT.newCall(request).execute(); + if (response.isSuccessful() && Objects.nonNull(response.body())) { + String result = response.body().string(); + log.info("执行get请求, url: {} 成功,返回数据: {}", url, result); + return result; + } + } catch (IOException e) { + log.error("执行get请求,url: {} 失败!", url, e); + } + return ""; + } + + + + /** + * 向指定 URL 发送POST方法的请求 + * + * @param url 发送请求的 URL + // * @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。 + * @return 所代表远程资源的响应结果 + */ + public static String sendPost(String url, String param){ + FormBody.Builder builder = new FormBody.Builder(); + String urlNameString = url + "?" + param; + FormBody body = builder.build(); + Request request = new Request + .Builder() + .url(urlNameString) + .post(body) + .build(); + Response response = null; + try { + response = HTTP_CLIENT.newCall(request).execute(); + //调用成功 + if (response.isSuccessful() && response.body() != null) { + return response.body().string(); + } + } catch (IOException e) { + e.printStackTrace(); + } + return ""; + } + + + //此方法是将参数以body形式发送post请求 + public static String bodypost(String strURL, String json) { + ApiLog apiLog = null; + // using above json body as a input to post API call + RequestBody body = RequestBody.create(MEDIA_TYPE_JSON, json); + //headers 请求头 + Map<String, String> headers =new HashMap<>(); + headers.put("Accept","application/json");// 设置接收数据的格式 + headers.put("Content-Type","application/json");// 设置发送数据的格式 + Request.Builder builder = new Request.Builder(); + buildHeader(builder, headers); + Request request = builder.url(strURL).post(body).build(); + Response response = null; + String result=null; + try { + apiLog = ApiLogAspect.initApiLog(request, json); + response = HTTP_CLIENT.newCall(request).execute(); + if (response.isSuccessful() && Objects.nonNull(response.body())) { + result = response.body().string(); + log.info("执行post请求,url: {}, header: {} ,参数: {} 成功,返回结果: {}", strURL, headers, json, result); + } + } catch (IOException e) { + ApiLogAspect.setApiLogException(apiLog, e); + log.error("执行post请求,url: {},参数: {} 失败!", strURL, json, e); + } finally { + ApiLogAspect.finishApiLog(apiLog, response, result); + } + return result; + } + + /** + * 设置请求头 + * + * @param builder . + * @param headers 请求头 + */ + private static void buildHeader(Request.Builder builder, Map<String, String> headers) { + if (Objects.nonNull(headers) && headers.size() > 0) { + headers.forEach((k, v) -> { + if (Objects.nonNull(k) && Objects.nonNull(v)) { + builder.addHeader(k, v); + } + }); + } + } + + /** + * 支持嵌套泛型的post请求。 + * <pre> + * Type type = new TypeToken<Results<User>>() {}.getType(); + * <pre/> + * + * @param url 链接 + * @param json 请求json + * @param type 嵌套泛型 + * @return 响应对象, 可进行强转。 + */ + public static <T> T post(String url, String json, Type type) { + String result = bodypost(url, json); + if (Objects.nonNull(result) && Objects.nonNull(type)) { + return GSON.fromJson(result, type); + } + return null; + } + +} diff --git a/jeecg-boot-master/jeecg-boot/pom.xml b/jeecg-boot-master/jeecg-boot/pom.xml index fd22020..ee201ac 100644 --- a/jeecg-boot-master/jeecg-boot/pom.xml +++ b/jeecg-boot-master/jeecg-boot/pom.xml @@ -4,7 +4,7 @@ <artifactId>jeecg-boot-parent</artifactId> <version>3.1.0</version> <packaging>pom</packaging> - + <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> @@ -275,7 +275,6 @@ <artifactId>okhttp</artifactId> <version>4.4.1</version> </dependency> - <dependency> <groupId>io.minio</groupId> <artifactId>minio</artifactId> @@ -421,4 +420,4 @@ </properties> </profile> </profiles> -</project> \ No newline at end of file +</project>