|
@@ -2,15 +2,21 @@ package com.platform.api.controller;
|
|
|
|
|
|
|
|
import com.platform.api.request.XpPrintImageReqVo;
|
|
import com.platform.api.request.XpPrintImageReqVo;
|
|
|
import com.platform.api.request.XpPrintReceiptReqVo;
|
|
import com.platform.api.request.XpPrintReceiptReqVo;
|
|
|
|
|
+import com.platform.config.XpCloudProperties;
|
|
|
|
|
+import com.platform.exception.IotException;
|
|
|
import com.platform.result.BaseResult;
|
|
import com.platform.result.BaseResult;
|
|
|
import com.platform.service.XpCloudPrintService;
|
|
import com.platform.service.XpCloudPrintService;
|
|
|
import io.swagger.v3.oas.annotations.Operation;
|
|
import io.swagger.v3.oas.annotations.Operation;
|
|
|
|
|
+import io.swagger.v3.oas.annotations.Parameter;
|
|
|
|
|
+import io.swagger.v3.oas.annotations.enums.ParameterIn;
|
|
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
|
|
import jakarta.validation.Valid;
|
|
import jakarta.validation.Valid;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.validation.annotation.Validated;
|
|
import org.springframework.validation.annotation.Validated;
|
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
|
|
|
+import org.springframework.web.bind.annotation.RequestHeader;
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
|
@@ -19,6 +25,7 @@ import org.springframework.web.bind.annotation.RestController;
|
|
|
*
|
|
*
|
|
|
* @author assistant
|
|
* @author assistant
|
|
|
*/
|
|
*/
|
|
|
|
|
+@Slf4j
|
|
|
@Validated
|
|
@Validated
|
|
|
@RestController
|
|
@RestController
|
|
|
@RequiredArgsConstructor
|
|
@RequiredArgsConstructor
|
|
@@ -26,8 +33,15 @@ import org.springframework.web.bind.annotation.RestController;
|
|
|
@RequestMapping("/kwsPrinter/xp")
|
|
@RequestMapping("/kwsPrinter/xp")
|
|
|
public class XpCloudPrintController {
|
|
public class XpCloudPrintController {
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 小票打印鉴权请求头名(与请求体打印机字段 {@code sn} 区分开)
|
|
|
|
|
+ */
|
|
|
|
|
+ private static final String HEADER_PRINT_RECEIPT_AUTH_SN = "X-Print-Receipt-Auth-Sn";
|
|
|
|
|
+
|
|
|
private final XpCloudPrintService xpCloudPrintService;
|
|
private final XpCloudPrintService xpCloudPrintService;
|
|
|
|
|
|
|
|
|
|
+ private final XpCloudProperties xpCloudProperties;
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 图片打印接口
|
|
* 图片打印接口
|
|
|
*
|
|
*
|
|
@@ -45,12 +59,37 @@ public class XpCloudPrintController {
|
|
|
*
|
|
*
|
|
|
* <p>content示例:\n<CB>四川顺采建筑材料有限公司...</BOLD></p>
|
|
* <p>content示例:\n<CB>四川顺采建筑材料有限公司...</BOLD></p>
|
|
|
*
|
|
*
|
|
|
|
|
+ * @param printReceiptAuthSn 请求头 {@code X-Print-Receipt-Auth-Sn},须与配置的 {@code xp.dev.print-receipt-authorized-sn} 一致方可调用
|
|
|
* @param reqVo 小票订单请求参数(包含公共参数与业务参数)
|
|
* @param reqVo 小票订单请求参数(包含公共参数与业务参数)
|
|
|
* @return 打印任务号
|
|
* @return 打印任务号
|
|
|
*/
|
|
*/
|
|
|
@PostMapping("/printReceipt")
|
|
@PostMapping("/printReceipt")
|
|
|
@Operation(summary = "调用芯烨SDK print 接口打印小票订单")
|
|
@Operation(summary = "调用芯烨SDK print 接口打印小票订单")
|
|
|
- public BaseResult<String> printReceipt(@Valid @RequestBody XpPrintReceiptReqVo reqVo) {
|
|
|
|
|
|
|
+ public BaseResult<String> printReceipt(
|
|
|
|
|
+ @Parameter(
|
|
|
|
|
+ name = HEADER_PRINT_RECEIPT_AUTH_SN,
|
|
|
|
|
+ description = "小票打印接口鉴权,须与服务端 xp.dev.print-receipt-authorized-sn 一致",
|
|
|
|
|
+ in = ParameterIn.HEADER)
|
|
|
|
|
+ @RequestHeader(value = HEADER_PRINT_RECEIPT_AUTH_SN, required = false)
|
|
|
|
|
+ String printReceiptAuthSn,
|
|
|
|
|
+ @Valid @RequestBody XpPrintReceiptReqVo reqVo) {
|
|
|
|
|
+ assertPrintReceiptAuthorized(printReceiptAuthSn);
|
|
|
return BaseResult.success(xpCloudPrintService.printReceipt(reqVo));
|
|
return BaseResult.success(xpCloudPrintService.printReceipt(reqVo));
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 简单鉴权:当配置了允许的凭证时,请求头 {@link #HEADER_PRINT_RECEIPT_AUTH_SN} 的值必须与其一致。
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param headerSn 请求头 {@code X-Print-Receipt-Auth-Sn} 的取值
|
|
|
|
|
+ */
|
|
|
|
|
+ private void assertPrintReceiptAuthorized(String headerSn) {
|
|
|
|
|
+ if (!xpCloudProperties.isPrintReceiptAuthorizationConfigured()) {
|
|
|
|
|
+ log.debug("未配置 xp.dev.print-receipt-authorized-sn,跳过小票打印请求头 {} 鉴权", HEADER_PRINT_RECEIPT_AUTH_SN);
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (xpCloudProperties.isUnauthorizedForPrintReceipt(headerSn)) {
|
|
|
|
|
+ log.warn("小票打印鉴权拒绝:请求头 {} 与配置不一致(已屏蔽具体值)", HEADER_PRINT_RECEIPT_AUTH_SN);
|
|
|
|
|
+ throw new IotException("无权限调用小票打印接口");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|