Commit c8bf4e45bda44a064de46b5cd08eadfcaa06b081

Authored by 肖超群
1 parent 051b6220

增加下发任务接口

Showing 15 changed files with 769 additions and 21 deletions
jeecg-boot-master/ant-design-vue-jeecg/src/api/api.js
... ... @@ -108,7 +108,7 @@ export const searchMaterialByCode = (params)=>postAction('/config/material/searc
108 108 export const listReceiveByReceiptId = (params)=>postAction('/receipt/receiveHeader/listReceiveByReceiptId', params);
109 109 export const createTask = (params)=>postAction('/receipt/receiptContainerHeader/createTask', params);
110 110 export const completeTaskByWMS = (params)=>postAction('/task/taskHeader/completeTaskByWMS', params);
111   -
  111 +export const execute = (params)=>postAction('/task/taskHeader/execute', params);
112 112  
113 113 // 中转HTTP请求
114 114 export const transitRESTful = {
... ...
jeecg-boot-master/ant-design-vue-jeecg/src/views/system/task/TaskHeaderList.vue
... ... @@ -132,8 +132,9 @@
132 132 </template>
133 133  
134 134 <span slot="action" slot-scope="text, record">
  135 + <a v-if="record.status == 1" @click="executeTask(record)">下发任务</a>
  136 + <a-divider type="vertical" />
135 137 <a v-if="record.status < 100" @click="completeTask(record)">完成任务</a>
136   -
137 138 <a-divider type="vertical" />
138 139 <a-dropdown>
139 140 <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
... ... @@ -172,6 +173,7 @@
172 173 import {initDictOptions,filterMultiDictText} from '@/components/dict/JDictSelectUtil'
173 174 import '@/assets/less/TableExpand.less'
174 175 import {completeTaskByWMS} from '@/api/api'
  176 + import {execute} from '@/api/api'
175 177  
176 178 export default {
177 179 name: "TaskHeaderList",
... ... @@ -353,7 +355,6 @@
353 355 },
354 356 completeTask(record) {
355 357 this.loading = true;
356   - const that = this;
357 358 this.model = Object.assign({}, record);
358 359 completeTaskByWMS(this.model).then((res) => {
359 360 this.loading = false;
... ... @@ -366,6 +367,20 @@
366 367 this.searchQuery();
367 368 });
368 369 },
  370 + executeTask(record) {
  371 + this.loading = true;
  372 + this.model = Object.assign({}, record);
  373 + execute(this.model).then((res) => {
  374 + this.loading = false;
  375 + if (res.success) {
  376 + this.$message.success(res.message);
  377 + }
  378 + else {
  379 + this.$message.error(res.message);
  380 + }
  381 + this.searchQuery();
  382 + });
  383 + },
369 384 getSuperFieldList(){
370 385 let fieldList=[];
371 386 fieldList.push({type:'int',value:'taskType',text:'任务类型',dictCode:'task_type'})
... ...
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&lt;T&gt; implements Serializable {
33 33 private String message = "";
34 34  
35 35 /**
  36 + * 返回处理消息
  37 + */
  38 + @ApiModelProperty(value = "返回处理消息")
  39 + private String msg = "";
  40 +
  41 + /**
36 42 * 返回代码
37 43 */
38 44 @ApiModelProperty(value = "返回代码")
39 45 private Integer code = 0;
40   -
  46 +
41 47 /**
42 48 * 返回数据对象 data
43 49 */
44 50 @ApiModelProperty(value = "返回数据对象")
45 51 private T result;
46   -
  52 +
47 53 /**
48 54 * 时间戳
49 55 */
... ... @@ -62,7 +68,7 @@ public class Result&lt;T&gt; implements Serializable {
62 68 this.code = code;
63 69 this.message = message;
64 70 }
65   -
  71 +
66 72 public Result<T> success(String message) {
67 73 this.message = message;
68 74 this.code = CommonConstant.SC_OK_200;
... ... @@ -143,7 +149,7 @@ public class Result&lt;T&gt; implements Serializable {
143 149 public static<T> Result<T> error(String msg) {
144 150 return error(CommonConstant.SC_INTERNAL_SERVER_ERROR_500, msg);
145 151 }
146   -
  152 +
147 153 public static<T> Result<T> error(int code, String msg) {
148 154 Result<T> r = new Result<T>();
149 155 r.setCode(code);
... ... @@ -169,4 +175,4 @@ public class Result&lt;T&gt; implements Serializable {
169 175 @JsonIgnore
170 176 private String onlTable;
171 177  
172   -}
173 178 \ No newline at end of file
  179 +}
... ...
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;
2 2  
3 3 import org.jeecg.common.api.vo.Result;
4 4 import org.jeecg.modules.wms.api.wcs.entity.WarecellDomain;
  5 +import org.jeecg.modules.wms.api.wcs.entity.WcsTask;
5 6 import org.jeecg.modules.wms.task.taskHeader.entity.TaskHeader;
6 7  
7 8 /**
... ... @@ -13,4 +14,6 @@ public interface WcsService {
13 14 Result warecellAllocation(WarecellDomain warecellDomain);
14 15  
15 16 Result wcsTaskAssign(TaskHeader taskHeader);
  17 +
  18 + WcsTask switchTaskTypeToWcs(WcsTask wcsTask);
16 19 }
... ...
jeecg-boot-master/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/wms/api/wcs/service/WcsServiceImpl.java
1 1 package org.jeecg.modules.wms.api.wcs.service;
2 2  
3 3  
  4 +import com.alibaba.fastjson.JSON;
  5 +import com.alibaba.fastjson.JSONObject;
4 6 import com.aliyun.oss.ServiceException;
5 7 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
6 8 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
7 9 import org.jeecg.common.api.vo.Result;
8 10 import org.jeecg.modules.wms.api.wcs.entity.WarecellDomain;
9 11 import org.jeecg.modules.wms.api.wcs.entity.WcsTask;
  12 +import org.jeecg.modules.wms.config.address.service.IAddressService;
10 13 import org.jeecg.modules.wms.config.container.entity.Container;
11 14 import org.jeecg.modules.wms.config.container.service.IContainerService;
12 15 import org.jeecg.modules.wms.config.location.entity.Location;
... ... @@ -31,6 +34,8 @@ import org.jeecg.modules.wms.task.taskHeader.service.ITaskDetailService;
31 34 import org.jeecg.modules.wms.task.taskHeader.service.ITaskHeaderService;
32 35 import org.jeecg.utils.StringUtils;
33 36 import org.jeecg.utils.constant.QuantityConstant;
  37 +import org.jeecg.utils.http.OkHttpUtils;
  38 +import org.springframework.http.ResponseEntity;
34 39 import org.springframework.stereotype.Service;
35 40 import org.springframework.transaction.annotation.Transactional;
36 41  
... ... @@ -69,6 +74,8 @@ public class WcsServiceImpl implements WcsService {
69 74 private LocationAllocationService locationAllocationService;
70 75 @Resource
71 76 private ILocationService locationService;
  77 + @Resource
  78 + private IAddressService addressService;
72 79  
73 80 @Override
74 81 @Transactional(rollbackFor = Exception.class)
... ... @@ -358,8 +365,73 @@ public class WcsServiceImpl implements WcsService {
358 365 }
359 366 wcsTask.setPriority(10);
360 367 wcsTask.setPlatform(QuantityConstant.PLATFORM_WMS);
  368 + wcsTask.setRemark(QuantityConstant.EMPTY_STRING);
  369 + Container container = containerService.getContainerByCode(containerCode, warehouseCode);
  370 + if(container == null) {
  371 + throw new ServiceException("下发任务时,容器没有找到");
  372 + }
361 373  
  374 +// String zoneCode = container.getZoneCode();
  375 +// if(StringUtils.isEmpty(zoneCode)) {
  376 +// throw new ServiceException("下发任务时,库区编码为空");
  377 +// }
  378 + String value = parameterConfigurationService.getValueByCode(QuantityConstant.RULE_CONNECT_WCS);
  379 + if(StringUtils.isEmpty(value)) {
  380 + throw new ServiceException("下发任务时,没有找到连接WCS的数据配置");
  381 + }
  382 + int connectWCS = Integer.parseInt(value);
  383 + if (connectWCS == QuantityConstant.RULE_WCS_CONNECT) {
  384 +// String url = addressService.getUrlByParam(QuantityConstant.ADDRESS_WCS_TASK_ASSIGN,
  385 +// warehouseCode, zoneCode);
  386 + String url = addressService.getUrlByParam(QuantityConstant.ADDRESS_WCS_TASK_ASSIGN);
  387 + wcsTask = switchTaskTypeToWcs(wcsTask);
  388 + String jsonParam = JSON.toJSONString(wcsTask);
  389 + System.out.println(jsonParam);
  390 + String body = OkHttpUtils.bodypost(url, jsonParam);
  391 + if(StringUtils.isEmpty(body)) {
  392 + throw new ServiceException("接口地址错误或返回为空");
  393 + }
  394 + Result result = JSON.parseObject(body, Result.class);
  395 + if(result.getCode() == QuantityConstant.HTTP_OK) {
  396 + return Result.error(result.getMsg());
  397 + }
  398 + }
  399 +
  400 + return Result.ok("下发成功");
  401 + }
  402 +
  403 + // wcs任务类型 入库 = 100 , 出库 = 300,分拣出库 = 400 ,移库 = 800,换站=1000
  404 + @Override
  405 + public WcsTask switchTaskTypeToWcs(WcsTask wcsTask) {
  406 + int taskType = wcsTask.getTaskType().intValue();
  407 + switch (taskType) {
  408 + case QuantityConstant.TASK_TYPE_WHOLERECEIPT:
  409 + case QuantityConstant.TASK_TYPE_EMPTYRECEIPT:
  410 + case QuantityConstant.TASK_TYPE_MANY_EMPTYRECEIPT:
  411 + wcsTask.setTaskType(100);
  412 + break;
  413 + case QuantityConstant.TASK_TYPE_WHOLESHIPMENT:
  414 + case QuantityConstant.TASK_TYPE_EMPTYSHIPMENT:
  415 + case QuantityConstant.TASK_TYPE_MANY_EMPTYSHIPMENT:
  416 + wcsTask.setTaskType(300);
  417 + break;
  418 + case QuantityConstant.TASK_TYPE_SUPPLEMENTRECEIPT:
  419 + case QuantityConstant.TASK_TYPE_SORTINGSHIPMENT:
  420 + case QuantityConstant.TASK_TYPE_CYCLECOUNT:
  421 + case QuantityConstant.TASK_TYPE_VIEW:
  422 + wcsTask.setTaskType(400);
  423 + break;
  424 + case QuantityConstant.TASK_TYPE_TRANSFER:
  425 + wcsTask.setTaskType(800);
  426 + break;
  427 + case QuantityConstant.TASK_TYPE_OVER_STATION:
  428 + case QuantityConstant.TASK_TYPE_MANY_OVER_STATION:
  429 + wcsTask.setTaskType(1000);
  430 + break;
  431 + default:
  432 + throw new ServiceException("不支持的任务类型");
362 433  
363   - return null;
  434 + }
  435 + return wcsTask;
364 436 }
365 437 }
... ...
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 {
36 36 @ApiModelProperty(value = "主键")
37 37 private Integer id;
38 38 /**编码*/
39   - @Excel(name = "编码", width = 15)
40   - @ApiModelProperty(value = "编码")
41   - private String code;
  39 + @Excel(name = "参数", width = 15)
  40 + @ApiModelProperty(value = "参数")
  41 + private String param;
42 42 /**仓库编码*/
43 43 @Excel(name = "仓库编码", width = 15)
44 44 @ApiModelProperty(value = "仓库编码")
... ...
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;
12 12 public interface IAddressService extends IService<Address> {
13 13  
14 14 Address getAddressByUrl(String url, String warehouseCode);
  15 +
  16 + String getUrlByParam(String param, String warehouseCode, String zoneCode);
  17 +
  18 + String getUrlByParam(String param);
15 19 }
... ...
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;
2 2  
3 3 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
4 4 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  5 +import org.apache.commons.math3.analysis.function.Add;
5 6 import org.jeecg.modules.wms.config.address.entity.Address;
6 7 import org.jeecg.modules.wms.config.address.mapper.AddressMapper;
7 8 import org.jeecg.modules.wms.config.address.service.IAddressService;
... ... @@ -26,4 +27,32 @@ public class AddressServiceImpl extends ServiceImpl&lt;AddressMapper, Address&gt; impl
26 27 Address address = getOne(addressLambdaQueryWrapper);
27 28 return address;
28 29 }
  30 +
  31 + @Override
  32 + public String getUrlByParam(String param, String warehouseCode, String zoneCode) {
  33 + LambdaQueryWrapper<Address> addressLambdaQueryWrapper = Wrappers.lambdaQuery();
  34 + addressLambdaQueryWrapper.eq(Address::getParam, param)
  35 + .eq(Address::getWarehouseCode, warehouseCode)
  36 + .eq(Address::getZoneCode, zoneCode);
  37 + Address address = getOne(addressLambdaQueryWrapper);
  38 + if(address == null) {
  39 + return null;
  40 + }
  41 + String url = address.getUrl();
  42 + return url;
  43 + }
  44 +
  45 + @Override
  46 + public String getUrlByParam(String param) {
  47 + LambdaQueryWrapper<Address> addressLambdaQueryWrapper = Wrappers.lambdaQuery();
  48 + addressLambdaQueryWrapper.eq(Address::getParam, param);
  49 + Address address = getOne(addressLambdaQueryWrapper);
  50 + if(address == null) {
  51 + return null;
  52 + }
  53 + String url = address.getUrl();
  54 + return url;
  55 + }
  56 +
  57 +
29 58 }
... ...
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
176 176 Address address = addressService.getAddressByUrl(url.toString(), warehouseCode);
177 177 log.setApiName(apiName);
178 178 log.setRequestFrom("WMS");
179   - log.setResponseBy(address.getCode().toUpperCase());
  179 + log.setResponseBy(address.getParam().toUpperCase());
180 180 } catch (Exception e) {
181 181 e.printStackTrace();
182 182 }
... ... @@ -307,7 +307,7 @@ public class ApiLogAspect
307 307 Address address = addressService.getAddressByUrl(url.toString(), QuantityConstant.DEFAULT_WAREHOUSE);
308 308 log.setApiName(apiName);
309 309 log.setRequestFrom("WMS");
310   - log.setResponseBy(address.getCode().toUpperCase());
  310 + log.setResponseBy(address.getParam().toUpperCase());
311 311 }catch (Exception e){
312 312 e.printStackTrace();
313 313 }
... ...
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&lt;ReceiptHeaderMapper, R
129 129 @Override
130 130 public boolean updateReceiptHeaderStatus(Integer id) {
131 131 LambdaQueryWrapper<ReceiptDetail> receiptDetailLambdaQueryWrapper = Wrappers.lambdaQuery();
132   - receiptDetailLambdaQueryWrapper.eq(ReceiptDetail::getId, id);
  132 + receiptDetailLambdaQueryWrapper.eq(ReceiptDetail::getReceiptId, id);
133 133 List<ReceiptDetail> receiptDetailList = receiptDetailService.list(receiptDetailLambdaQueryWrapper);
134 134 ReceiptHeader receiptHeader = receiptHeaderService.getById(id);
135 135 if(receiptHeader == null) {
... ... @@ -141,8 +141,8 @@ public class ReceiptHeaderServiceImpl extends ServiceImpl&lt;ReceiptHeaderMapper, R
141 141 minStatus = QuantityConstant.RECEIPT_HEADER_BUILD;
142 142 maxStatus = QuantityConstant.RECEIPT_HEADER_BUILD;
143 143 } else {
144   - minStatus = QuantityConstant.RECEIPT_HEADER_BUILD;
145   - maxStatus = QuantityConstant.RECEIPT_HEADER_BUILD;
  144 + minStatus = receiptDetailList.get(0).getStatus();
  145 + maxStatus = receiptDetailList.get(0).getStatus();
146 146 for(ReceiptDetail receiptDetail : receiptDetailList) {
147 147 int status = receiptDetail.getStatus();
148 148 if (minStatus > status) {
... ...
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;
5 5 import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
6 6 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
7 7 import org.jeecg.common.api.vo.Result;
  8 +import org.jeecg.modules.wms.api.wcs.service.WcsService;
8 9 import org.jeecg.modules.wms.config.container.entity.Container;
9 10 import org.jeecg.modules.wms.config.container.service.IContainerService;
10 11 import org.jeecg.modules.wms.config.location.entity.Location;
... ... @@ -78,6 +79,8 @@ public class TaskHeaderServiceImpl extends ServiceImpl&lt;TaskHeaderMapper, TaskHea
78 79 private IReceiptContainerHeaderService receiptContainerHeaderService;
79 80 @Resource
80 81 private IMaterialService materialService;
  82 + @Resource
  83 + private WcsService wcsService;
81 84  
82 85 @Override
83 86 @Transactional
... ... @@ -321,7 +324,24 @@ public class TaskHeaderServiceImpl extends ServiceImpl&lt;TaskHeaderMapper, TaskHea
321 324  
322 325 @Override
323 326 public Result sendTaskToWcs(Integer taskId) {
324   - return null;
  327 + TaskHeader taskHeader = taskHeaderService.getById(taskId);
  328 + if(taskHeader == null) {
  329 + return Result.error("下发wcs任务时,没有找到任务, 任务号" + taskId);
  330 + }
  331 + if (taskHeader.getStatus() >= QuantityConstant.TASK_STATUS_RELEASE) {
  332 + return Result.error("任务" + taskId + "已经下发,请不要重复下发,操作中止");
  333 + }
  334 + Result result = wcsService.wcsTaskAssign(taskHeader);
  335 + if(!result.isSuccess()) {
  336 + throw new ServiceException(result.getMessage());
  337 + }
  338 + taskHeader.setStatus(QuantityConstant.TASK_STATUS_RELEASE);
  339 + boolean success = taskHeaderService.updateById(taskHeader);
  340 + if(!success) {
  341 + throw new ServiceException("下发wcs任务时, 更新任务状态失败");
  342 + }
  343 +
  344 + return Result.ok("执行下发任务成功");
325 345 }
326 346  
327 347 @Transactional(rollbackFor = Exception.class)
... ...
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 {
552 552 public static final int STATUS_ENABLE = 1;
553 553 public static final int STATUS_DISABLE = 0;
554 554  
  555 + public static final int HTTP_OK = 200;
  556 +
555 557  
556 558 public static final String EER_TABLE_OTHERSHIPMENT = "STK_MisDelivery";
557 559 /*直接调拨单回传*/
... ...
jeecg-boot-master/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/utils/http/HttpUtils.java 0 → 100644
  1 +package org.jeecg.utils.http;
  2 +
  3 +import org.apache.commons.io.IOUtils;
  4 +import org.apache.http.HttpResponse;
  5 +import org.apache.http.HttpStatus;
  6 +import org.apache.http.client.methods.HttpPost;
  7 +import org.apache.http.entity.StringEntity;
  8 +import org.apache.http.impl.client.CloseableHttpClient;
  9 +import org.apache.http.impl.client.HttpClients;
  10 +import org.jeecg.modules.wms.framework.aspectj.ApiLogAspect;
  11 +import org.jeecg.modules.wms.monitor.apiLog.entity.ApiLog;
  12 +import org.slf4j.Logger;
  13 +import org.slf4j.LoggerFactory;
  14 +
  15 +import javax.net.ssl.*;
  16 +import java.io.*;
  17 +import java.net.*;
  18 +import java.security.cert.X509Certificate;
  19 +
  20 +
  21 +/**
  22 + * 通用http发送方法
  23 + *
  24 + * @author huaheng
  25 + */
  26 +public class HttpUtils
  27 +{
  28 +
  29 + private static final Logger log = LoggerFactory.getLogger(HttpUtils.class);
  30 +
  31 + /**
  32 + * 向指定 URL 发送GET方法的请求
  33 + *
  34 + * @param url 发送请求的 URL
  35 + * @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
  36 + * @return 所代表远程资源的响应结果
  37 + */
  38 + public static String sendGet(String url, String param)
  39 + {
  40 + StringBuilder result = new StringBuilder();
  41 + BufferedReader in = null;
  42 + try
  43 + {
  44 + String urlNameString = url + "?" + param;
  45 + log.info("sendGet - {}", urlNameString);
  46 + URL realUrl = new URL(urlNameString);
  47 + URLConnection connection = realUrl.openConnection();
  48 + connection.setRequestProperty("accept", "*/*");
  49 + connection.setRequestProperty("connection", "Keep-Alive");
  50 + connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
  51 + connection.connect();
  52 + in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  53 + String line;
  54 + while ((line = in.readLine()) != null)
  55 + {
  56 + result.append(line);
  57 + }
  58 + log.info("recv - {}", result);
  59 + }
  60 + catch (ConnectException e)
  61 + {
  62 + log.error("调用HttpUtils.sendGet ConnectException, url=" + url + ",param=" + param, e.getMessage());
  63 + }
  64 + catch (SocketTimeoutException e)
  65 + {
  66 + log.error("调用HttpUtils.sendGet SocketTimeoutException, url=" + url + ",param=" + param, e.getMessage());
  67 + }
  68 + catch (IOException e)
  69 + {
  70 + log.error("调用HttpUtils.sendGet IOException, url=" + url + ",param=" + param, e.getMessage());
  71 + }
  72 + catch (Exception e)
  73 + {
  74 + log.error("调用HttpsUtil.sendGet Exception, url=" + url + ",param=" + param, e.getMessage());
  75 + }
  76 + finally
  77 + {
  78 + try
  79 + {
  80 + if (in != null)
  81 + {
  82 + in.close();
  83 + }
  84 + }
  85 + catch (Exception ex)
  86 + {
  87 + log.error("调用in.close Exception, url=" + url + ",param=" + param, ex.getMessage());
  88 + }
  89 + }
  90 + return result.toString();
  91 + }
  92 +
  93 + //post请求 为erp回传定制
  94 + public static String erppost(String strURL, String params,String key) {
  95 + HttpURLConnection connection = null;
  96 + InputStream is = null;
  97 + OutputStreamWriter out = null;
  98 + String result = null;
  99 + ApiLog log = null;
  100 +
  101 + try {
  102 + URL url = new URL(strURL);// 创建连接
  103 + String ur=""+url;
  104 + connection = (HttpURLConnection) url.openConnection();
  105 + connection.setDoOutput(true);
  106 + connection.setDoInput(true);
  107 + connection.setUseCaches(false);
  108 + connection.setInstanceFollowRedirects(true);
  109 + connection.setRequestMethod("POST");// 设置请求方式
  110 + connection.setRequestProperty("Accept","application/json");// 设置接收数据的格式
  111 + connection.setRequestProperty("Content-Type","application/json");// 设置发送数据的格式
  112 + connection.setRequestProperty("apiKey",key);//测试环境key
  113 + log = ApiLogAspect.initApiLog(connection, params);
  114 + connection.connect();
  115 + out = new OutputStreamWriter( connection.getOutputStream(),"UTF-8");// utf-8编码
  116 + out.append(params);
  117 + out.flush();
  118 + out.close(); // 读取响应
  119 + int length = (int) connection.getContentLength();// 获取长度
  120 + is = connection.getInputStream();
  121 + BufferedReader br = new BufferedReader(new InputStreamReader(is));
  122 + length = 10000;
  123 + if(length > 0) {
  124 + byte[] data = new byte[length];
  125 + byte[] temp = new byte[512];
  126 + int readLen = 0;
  127 + int destPos = 0;
  128 + while ((readLen = is.read(temp)) > 0) {
  129 + System.arraycopy(temp, 0, data, destPos, readLen);
  130 + destPos += readLen;
  131 + }
  132 + result = new String(data, "UTF-8");
  133 + System.out.println(result);
  134 + }
  135 + } catch (Exception e) {
  136 + ApiLogAspect.setApiLogException(log, e);
  137 + e.printStackTrace();
  138 + }finally {
  139 + ApiLogAspect.finishApiLog(log, connection, result);
  140 + if(connection != null)
  141 + connection.disconnect();
  142 + IOUtils.closeQuietly(out, is);
  143 + }
  144 + return result;
  145 + }
  146 +
  147 + /**
  148 + * 向指定 URL 发送POST方法的请求
  149 + *
  150 + * @param url 发送请求的 URL
  151 + * @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
  152 + * @return 所代表远程资源的响应结果
  153 + */
  154 + public static String sendPost(String url, String param)
  155 + {
  156 + PrintWriter out = null;
  157 + BufferedReader in = null;
  158 + StringBuilder result = new StringBuilder();
  159 + try
  160 + {
  161 + String urlNameString = url + "?" + param;
  162 + log.info("sendPost - {}", urlNameString);
  163 + URL realUrl = new URL(urlNameString);
  164 + URLConnection conn = realUrl.openConnection();
  165 + conn.setRequestProperty("accept", "*/*");
  166 + conn.setRequestProperty("connection", "Keep-Alive");
  167 + conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
  168 + conn.setRequestProperty("Accept-Charset", "utf-8");
  169 + conn.setRequestProperty("contentType", "utf-8");
  170 + conn.setDoOutput(true);
  171 + conn.setDoInput(true);
  172 + out = new PrintWriter(conn.getOutputStream());
  173 + out.print(param);
  174 + out.flush();
  175 + if (((HttpURLConnection)conn).getResponseCode() >= 390) {
  176 + in = new BufferedReader(new InputStreamReader(((HttpURLConnection) conn).getErrorStream()));
  177 + } else {
  178 + in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
  179 + }
  180 +// in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
  181 + String line;
  182 + while ((line = in.readLine()) != null)
  183 + {
  184 + result.append(line);
  185 + }
  186 + log.info("recv - {}", result);
  187 + }
  188 + catch (ConnectException e)
  189 + {
  190 + log.error("调用HttpUtils.sendPost ConnectException, url=" + url + ",param=" + param, e.getMessage());
  191 + }
  192 + catch (SocketTimeoutException e)
  193 + {
  194 + log.error("调用HttpUtils.sendPost SocketTimeoutException, url=" + url + ",param=" + param, e.getMessage());
  195 + }
  196 + catch (IOException e)
  197 + {
  198 + log.error("调用HttpUtils.sendPost IOException, url=" + url + ",param=" + param, e.getMessage());
  199 + }
  200 + catch (Exception e)
  201 + {
  202 + log.error("调用HttpsUtil.sendPost Exception, url=" + url + ",param=" + param, e.getMessage());
  203 + }
  204 + finally
  205 + {
  206 + try
  207 + {
  208 + if (out != null)
  209 + {
  210 + out.close();
  211 + }
  212 + if (in != null)
  213 + {
  214 + in.close();
  215 + }
  216 + }
  217 + catch (IOException ex)
  218 + {
  219 + log.error("调用in.close Exception, url=" + url + ",param=" + param, ex.getMessage());
  220 + }
  221 + }
  222 + return result.toString();
  223 + }
  224 +
  225 + public static String sendSSLPost(String url, String param)
  226 + {
  227 + StringBuilder result = new StringBuilder();
  228 + String urlNameString = url + "?" + param;
  229 + try
  230 + {
  231 + log.info("sendSSLPost - {}", urlNameString);
  232 + SSLContext sc = SSLContext.getInstance("SSL");
  233 + sc.init(null, new TrustManager[] { new TrustAnyTrustManager() }, new java.security.SecureRandom());
  234 + URL console = new URL(urlNameString);
  235 + HttpsURLConnection conn = (HttpsURLConnection) console.openConnection();
  236 + conn.setRequestProperty("accept", "*/*");
  237 + conn.setRequestProperty("connection", "Keep-Alive");
  238 + conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
  239 + conn.setRequestProperty("Accept-Charset", "utf-8");
  240 + conn.setRequestProperty("contentType", "utf-8");
  241 + conn.setDoOutput(true);
  242 + conn.setDoInput(true);
  243 +
  244 + conn.setSSLSocketFactory(sc.getSocketFactory());
  245 + conn.setHostnameVerifier(new TrustAnyHostnameVerifier());
  246 + conn.connect();
  247 + InputStream is = conn.getInputStream();
  248 + BufferedReader br = new BufferedReader(new InputStreamReader(is));
  249 + String ret = "";
  250 + while ((ret = br.readLine()) != null)
  251 + {
  252 + if (ret != null && !"".equals(ret.trim()))
  253 + {
  254 + result.append(new String(ret.getBytes("ISO-8859-1"), "utf-8"));
  255 + }
  256 + }
  257 + log.info("recv - {}", result);
  258 + conn.disconnect();
  259 + br.close();
  260 + }
  261 + catch (ConnectException e)
  262 + {
  263 + log.error("调用HttpUtils.sendSSLPost ConnectException, url=" + url + ",param=" + param, e.getMessage());
  264 + }
  265 + catch (SocketTimeoutException e)
  266 + {
  267 + log.error("调用HttpUtils.sendSSLPost SocketTimeoutException, url=" + url + ",param=" + param, e.getMessage());
  268 + }
  269 + catch (IOException e)
  270 + {
  271 + log.error("调用HttpUtils.sendSSLPost IOException, url=" + url + ",param=" + param, e.getMessage());
  272 + }
  273 + catch (Exception e)
  274 + {
  275 + log.error("调用HttpsUtil.sendSSLPost Exception, url=" + url + ",param=" + param, e.getMessage());
  276 + }
  277 + return result.toString();
  278 + }
  279 +
  280 +
  281 + //此方法是将参数以body形式发送post请求
  282 + public static String bodypost(String strURL, String params) {
  283 + System.out.println(strURL);
  284 + System.out.println(params);
  285 + HttpURLConnection connection = null;
  286 + InputStream is = null;
  287 + OutputStreamWriter out = null;
  288 + String result = null;
  289 + ApiLog log = null;
  290 +
  291 + try {
  292 + URL url = new URL(strURL);// 创建连接
  293 + String ur=""+url;
  294 + connection = (HttpURLConnection) url.openConnection();
  295 + connection.setDoOutput(true);
  296 + connection.setDoInput(true);
  297 + connection.setUseCaches(false);
  298 + connection.setInstanceFollowRedirects(true);
  299 + connection.setRequestMethod("POST");// 设置请求方式
  300 + connection.setRequestProperty("Accept","application/json");// 设置接收数据的格式
  301 + connection.setRequestProperty("Content-Type","application/json");// 设置发送数据的格式
  302 + if ((ur.contains("10.0.15.19")))
  303 + {
  304 + connection.setRequestProperty("apiKey","c687ef505557428595e6a596fba5de6c");//测试环境key
  305 + }
  306 + log = ApiLogAspect.initApiLog(connection, params);
  307 + connection.connect();
  308 + out = new OutputStreamWriter( connection.getOutputStream(),"UTF-8");// utf-8编码
  309 + out.append(params);
  310 + out.flush();
  311 + out.close(); // 读取响应
  312 + int length = (int) connection.getContentLength();// 获取长度
  313 + is = connection.getInputStream();
  314 + BufferedReader br = new BufferedReader(new InputStreamReader(is));
  315 + length = 10000;
  316 + if(length > 0) {
  317 + byte[] data = new byte[length];
  318 + byte[] temp = new byte[512];
  319 + int readLen = 0;
  320 + int destPos = 0;
  321 + while ((readLen = is.read(temp)) > 0) {
  322 + System.arraycopy(temp, 0, data, destPos, readLen);
  323 + destPos += readLen;
  324 + }
  325 + result = new String(data, "UTF-8");
  326 + System.out.println(result);
  327 + }
  328 + } catch (Exception e) {
  329 + ApiLogAspect.setApiLogException(log, e);
  330 + e.printStackTrace();
  331 + }finally {
  332 + ApiLogAspect.finishApiLog(log, connection, result);
  333 + if(connection != null)
  334 + connection.disconnect();
  335 + IOUtils.closeQuietly(out, is);
  336 + }
  337 + return result;
  338 + }
  339 +
  340 + /**
  341 + * 发送以json为参数的POST请求
  342 + * @param json
  343 + * @param url
  344 + * @return
  345 + */
  346 + public static String sendJsonPostToken(String json, String url){
  347 + String result = "";
  348 + HttpPost post = new HttpPost(url);
  349 + try{
  350 + CloseableHttpClient httpClient = HttpClients.createDefault();
  351 +
  352 + post.setHeader("Content-Type","application/json;charset=utf-8");
  353 + post.addHeader("Authorization", "Basic YWRtaW46");
  354 + StringEntity postingString = new StringEntity(json,"utf-8");
  355 + post.setEntity(postingString);
  356 + HttpResponse response = httpClient.execute(post);
  357 +
  358 + InputStream in = response.getEntity().getContent();
  359 + BufferedReader br = new BufferedReader(new InputStreamReader(in, "utf-8"));
  360 + StringBuilder strber= new StringBuilder();
  361 + String line = null;
  362 + while((line = br.readLine())!=null){
  363 + strber.append(line+'\n');
  364 + }
  365 + br.close();
  366 + in.close();
  367 + result = strber.toString();
  368 + if(response.getStatusLine().getStatusCode()!= HttpStatus.SC_OK){
  369 + result = "服务器异常";
  370 + }
  371 + } catch (Exception e){
  372 + System.out.println("请求异常");
  373 + throw new RuntimeException(e);
  374 + } finally{
  375 + post.abort();
  376 + }
  377 + return result;
  378 + }
  379 +
  380 + private static class TrustAnyTrustManager implements X509TrustManager
  381 + {
  382 + @Override
  383 + public void checkClientTrusted(X509Certificate[] chain, String authType)
  384 + {
  385 + }
  386 +
  387 + @Override
  388 + public void checkServerTrusted(X509Certificate[] chain, String authType)
  389 + {
  390 + }
  391 +
  392 + @Override
  393 + public X509Certificate[] getAcceptedIssuers()
  394 + {
  395 + return new X509Certificate[] {};
  396 + }
  397 + }
  398 +
  399 + private static class TrustAnyHostnameVerifier implements HostnameVerifier
  400 + {
  401 + @Override
  402 + public boolean verify(String hostname, SSLSession session)
  403 + {
  404 + return true;
  405 + }
  406 + }
  407 +
  408 +}
... ...
jeecg-boot-master/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/utils/http/OkHttpUtils.java 0 → 100644
  1 +package org.jeecg.utils.http;
  2 +
  3 +
  4 +import com.alibaba.fastjson.parser.Feature;
  5 +import com.google.gson.Gson;
  6 +import okhttp3.*;
  7 +import org.jeecg.modules.wms.framework.aspectj.ApiLogAspect;
  8 +import org.jeecg.modules.wms.monitor.apiLog.entity.ApiLog;
  9 +import org.slf4j.Logger;
  10 +import org.slf4j.LoggerFactory;
  11 +
  12 +import java.io.IOException;
  13 +import java.lang.reflect.Type;
  14 +import java.util.HashMap;
  15 +import java.util.Map;
  16 +import java.util.Objects;
  17 +import java.util.concurrent.TimeUnit;
  18 +
  19 +/**
  20 + * OkHttp发送请求
  21 + * @author huaheng
  22 + * @Date 2022-5-30
  23 + */
  24 +public class OkHttpUtils {
  25 +
  26 + private static final Logger log = LoggerFactory.getLogger(OkHttpUtils.class);
  27 +
  28 + /**
  29 + * 最大连接时间
  30 + */
  31 + public final static int CONNECTION_TIMEOUT = 30;
  32 + /**
  33 + * JSON格式
  34 + */
  35 + public static final MediaType MEDIA_TYPE_JSON = MediaType.parse("application/json; charset=utf-8");
  36 + /**
  37 + * OkHTTP线程池最大空闲线程数
  38 + */
  39 + public final static int MAX_IDLE_CONNECTIONS = 100;
  40 + /**
  41 + * OkHTTP线程池空闲线程存活时间
  42 + */
  43 + public final static long KEEP_ALIVE_DURATION = 30L;
  44 +
  45 +
  46 + private static final String CONTENT_TYPE = "Content-Type";
  47 +
  48 +
  49 + /**
  50 + * client
  51 + * 配置重试
  52 + */
  53 + private final static OkHttpClient HTTP_CLIENT = new OkHttpClient.Builder()
  54 + .readTimeout(CONNECTION_TIMEOUT, TimeUnit.SECONDS)
  55 + .writeTimeout(CONNECTION_TIMEOUT, TimeUnit.SECONDS)
  56 + .connectTimeout(CONNECTION_TIMEOUT, TimeUnit.SECONDS)
  57 + .connectionPool(new ConnectionPool(MAX_IDLE_CONNECTIONS, KEEP_ALIVE_DURATION, TimeUnit.MINUTES))
  58 + .build();
  59 + private static final Gson GSON = new Gson();
  60 +
  61 + /**
  62 + * 向指定 URL 发送GET方法的请求
  63 + *
  64 + * @param url 发送请求的 URL
  65 + //* @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
  66 + * @return 所代表远程资源的响应结果
  67 + */
  68 + public static String sendGet(String url,String param)
  69 + {
  70 + //headers 请求头
  71 + Map<String, String> headers=new HashMap<>();
  72 + //请求URI
  73 + String urlNameString = url + "?" + param;
  74 +
  75 + Request.Builder builder = new Request.Builder();
  76 + buildHeader(builder, headers);
  77 +
  78 + Request request = builder.url(urlNameString).get().build();
  79 + Response response = null;
  80 + try {
  81 + response = HTTP_CLIENT.newCall(request).execute();
  82 + if (response.isSuccessful() && Objects.nonNull(response.body())) {
  83 + String result = response.body().string();
  84 + log.info("执行get请求, url: {} 成功,返回数据: {}", url, result);
  85 + return result;
  86 + }
  87 + } catch (IOException e) {
  88 + log.error("执行get请求,url: {} 失败!", url, e);
  89 + }
  90 + return "";
  91 + }
  92 +
  93 +
  94 +
  95 + /**
  96 + * 向指定 URL 发送POST方法的请求
  97 + *
  98 + * @param url 发送请求的 URL
  99 + // * @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
  100 + * @return 所代表远程资源的响应结果
  101 + */
  102 + public static String sendPost(String url, String param){
  103 + FormBody.Builder builder = new FormBody.Builder();
  104 + String urlNameString = url + "?" + param;
  105 + FormBody body = builder.build();
  106 + Request request = new Request
  107 + .Builder()
  108 + .url(urlNameString)
  109 + .post(body)
  110 + .build();
  111 + Response response = null;
  112 + try {
  113 + response = HTTP_CLIENT.newCall(request).execute();
  114 + //调用成功
  115 + if (response.isSuccessful() && response.body() != null) {
  116 + return response.body().string();
  117 + }
  118 + } catch (IOException e) {
  119 + e.printStackTrace();
  120 + }
  121 + return "";
  122 + }
  123 +
  124 +
  125 + //此方法是将参数以body形式发送post请求
  126 + public static String bodypost(String strURL, String json) {
  127 + ApiLog apiLog = null;
  128 + // using above json body as a input to post API call
  129 + RequestBody body = RequestBody.create(MEDIA_TYPE_JSON, json);
  130 + //headers 请求头
  131 + Map<String, String> headers =new HashMap<>();
  132 + headers.put("Accept","application/json");// 设置接收数据的格式
  133 + headers.put("Content-Type","application/json");// 设置发送数据的格式
  134 + Request.Builder builder = new Request.Builder();
  135 + buildHeader(builder, headers);
  136 + Request request = builder.url(strURL).post(body).build();
  137 + Response response = null;
  138 + String result=null;
  139 + try {
  140 + apiLog = ApiLogAspect.initApiLog(request, json);
  141 + response = HTTP_CLIENT.newCall(request).execute();
  142 + if (response.isSuccessful() && Objects.nonNull(response.body())) {
  143 + result = response.body().string();
  144 + log.info("执行post请求,url: {}, header: {} ,参数: {} 成功,返回结果: {}", strURL, headers, json, result);
  145 + }
  146 + } catch (IOException e) {
  147 + ApiLogAspect.setApiLogException(apiLog, e);
  148 + log.error("执行post请求,url: {},参数: {} 失败!", strURL, json, e);
  149 + } finally {
  150 + ApiLogAspect.finishApiLog(apiLog, response, result);
  151 + }
  152 + return result;
  153 + }
  154 +
  155 + /**
  156 + * 设置请求头
  157 + *
  158 + * @param builder .
  159 + * @param headers 请求头
  160 + */
  161 + private static void buildHeader(Request.Builder builder, Map<String, String> headers) {
  162 + if (Objects.nonNull(headers) && headers.size() > 0) {
  163 + headers.forEach((k, v) -> {
  164 + if (Objects.nonNull(k) && Objects.nonNull(v)) {
  165 + builder.addHeader(k, v);
  166 + }
  167 + });
  168 + }
  169 + }
  170 +
  171 + /**
  172 + * 支持嵌套泛型的post请求。
  173 + * <pre>
  174 + * Type type = new TypeToken<Results<User>>() {}.getType();
  175 + * <pre/>
  176 + *
  177 + * @param url 链接
  178 + * @param json 请求json
  179 + * @param type 嵌套泛型
  180 + * @return 响应对象, 可进行强转。
  181 + */
  182 + public static <T> T post(String url, String json, Type type) {
  183 + String result = bodypost(url, json);
  184 + if (Objects.nonNull(result) && Objects.nonNull(type)) {
  185 + return GSON.fromJson(result, type);
  186 + }
  187 + return null;
  188 + }
  189 +
  190 +}
... ...
jeecg-boot-master/jeecg-boot/pom.xml
... ... @@ -4,7 +4,7 @@
4 4 <artifactId>jeecg-boot-parent</artifactId>
5 5 <version>3.1.0</version>
6 6 <packaging>pom</packaging>
7   -
  7 +
8 8 <parent>
9 9 <groupId>org.springframework.boot</groupId>
10 10 <artifactId>spring-boot-starter-parent</artifactId>
... ... @@ -275,7 +275,6 @@
275 275 <artifactId>okhttp</artifactId>
276 276 <version>4.4.1</version>
277 277 </dependency>
278   -
279 278 <dependency>
280 279 <groupId>io.minio</groupId>
281 280 <artifactId>minio</artifactId>
... ... @@ -421,4 +420,4 @@
421 420 </properties>
422 421 </profile>
423 422 </profiles>
424   -</project>
425 423 \ No newline at end of file
  424 +</project>
... ...