初始化
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
package com.dpkj.modules.express.controller;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.dpkj.common.config.FileConfig;
|
||||
import com.dpkj.common.vo.Result;
|
||||
import com.dpkj.modules.express.service.PrinterService;
|
||||
import com.dpkj.modules.express.utils.FileUtils;
|
||||
import com.dpkj.modules.express.utils.PrinterUtil;
|
||||
import com.dpkj.modules.express.vo.Print;
|
||||
import com.dpkj.modules.express.vo.PrinterStatus;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Auther: 萧道子
|
||||
* @Date: 2024/4/28 13:58
|
||||
* @Description: 打印机
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("printer")
|
||||
public class PrinterController {
|
||||
@Autowired
|
||||
private PrinterService printerService;
|
||||
@Autowired
|
||||
private FileConfig fileConfig;
|
||||
|
||||
/**
|
||||
* 获取打印机状态
|
||||
*
|
||||
* @return com.dpkj.common.vo.Result
|
||||
* @author 萧道子 2024/4/28
|
||||
*/
|
||||
@GetMapping("status")
|
||||
public Result getStatus() {
|
||||
try {
|
||||
PrinterStatus val = printerService.getPrinterStatus();
|
||||
return Result.ok("获取成功", val);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
log.info("[printer][PrinterController.getStatus]获取打印机状态失败 {}", e.getMessage());
|
||||
return Result.error("打印机状态获取失败");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 打印文件
|
||||
*
|
||||
* @return com.dpkj.common.vo.Result
|
||||
* @author 萧道子 2024/4/29
|
||||
*/
|
||||
@PostMapping("print")
|
||||
public Result print(@RequestBody Print val) {
|
||||
try {
|
||||
String base64 = val.getBase64();
|
||||
if (StrUtil.isEmpty(base64)) throw new Exception("参数缺失!");
|
||||
|
||||
List<File> files = FileUtils.saveBase64ToBmp(base64);
|
||||
if (files.isEmpty()) throw new Exception("打印失败,面单未生成!");
|
||||
|
||||
File file = files.get(0);
|
||||
String filePath = file.getAbsolutePath();
|
||||
Integer num = printerService.printBmpByPath(filePath);// 打印
|
||||
FileUtil.del(file); // 删除文件
|
||||
|
||||
if (num != 0) throw new Exception("打印失败,请检查打印机是否连接正常!");
|
||||
|
||||
return Result.ok("打印成功");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
log.info("[printer][PrinterController.print] 打印面单 {}", e.getMessage());
|
||||
return Result.error(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user