From d8ed96dff7b0f861923d13c2d13ffebfa7182814 Mon Sep 17 00:00:00 2001 From: wangw <1594593906@qq.com> Date: Fri, 20 Sep 2024 16:10:36 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BA=A4=E7=8F=AD2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cons/rest/ViewHandoverController.java | 51 ++++++++++++------- 1 file changed, 32 insertions(+), 19 deletions(-) diff --git a/eladmin-system/src/main/java/cn/ysk/cashier/cons/rest/ViewHandoverController.java b/eladmin-system/src/main/java/cn/ysk/cashier/cons/rest/ViewHandoverController.java index fb73d613..a25a5df8 100644 --- a/eladmin-system/src/main/java/cn/ysk/cashier/cons/rest/ViewHandoverController.java +++ b/eladmin-system/src/main/java/cn/ysk/cashier/cons/rest/ViewHandoverController.java @@ -1,25 +1,31 @@ package cn.ysk.cashier.cons.rest; -import cn.hutool.core.util.StrUtil; -import cn.hutool.http.HttpResponse; -import cn.hutool.http.HttpUtil; import cn.ysk.cashier.annotation.Log; import cn.ysk.cashier.cons.domain.ViewHandover; import cn.ysk.cashier.cons.service.ViewHandoverService; import cn.ysk.cashier.cons.service.dto.ViewHandoverQueryCriteria; import cn.ysk.cashier.exception.BadRequestException; +import cn.ysk.cashier.utils.JSONUtil; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import lombok.RequiredArgsConstructor; +import org.apache.http.client.methods.CloseableHttpResponse; import lombok.extern.slf4j.Slf4j; +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.springframework.beans.factory.annotation.Value; import org.springframework.data.domain.Pageable; -import lombok.RequiredArgsConstructor; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +import org.springframework.util.CollectionUtils; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; -import io.swagger.annotations.*; -import java.io.IOException; -import java.util.Objects; + import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.util.Map; /** * @author admin @@ -40,18 +46,25 @@ public class ViewHandoverController { @PostMapping("handoverData") @Log("提交交班") @ApiOperation("提交交班") - public ResponseEntity handoverData(@RequestBody ViewHandover resources){ - if(Objects.isNull(resources.getShopId())) throw new BadRequestException("店铺信息不可为空"); - HttpResponse response = HttpUtil.createGet(StrUtil.format(wxServiceHostname,"/cashierService/common/handoverData?shopId={}", resources.getShopId())).execute(); - if (response.getStatus() >= 200 && response.getStatus() < 300) { - String body = response.body(); - return new ResponseEntity<>(HttpStatus.OK); - } else { - // 请求失败,获取状态码和错误信息 - int statusCode = response.getStatus(); - String errorMessage = response.body(); // 可以获取错误信息 - log.error("交班提交失败:{}",errorMessage); - throw new BadRequestException("请求失败,状态码:"+statusCode); + public ResponseEntity handoverData(@RequestBody Map map) throws Exception { + if (CollectionUtils.isEmpty(map) || !map.containsKey("shopId") || !map.containsKey("isprintProduct")) { + throw new BadRequestException("参数缺失"); + } + CloseableHttpClient httpClient = HttpClients.createDefault(); + HttpPost post = new HttpPost(wxServiceHostname + "/cashierService/common/handoverData"); + String jsonString = JSONUtil.toJSONString(map); + StringEntity entity = new StringEntity(jsonString); + post.setEntity(entity); + post.setHeader("Content-Type", "application/json"); + + try (CloseableHttpResponse response = httpClient.execute(post)) { + if (response.getStatusLine().getStatusCode() == 200) { + return new ResponseEntity<>(HttpStatus.OK); + } else { + return new ResponseEntity<>(HttpStatus.valueOf(response.getStatusLine().getStatusCode())); + } + } finally { + httpClient.close(); } }