Driver.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\common\library\printer;
  13. use app\common\library\printer\engine\Feie;
  14. use app\common\library\printer\engine\PrintCenter;
  15. use app\common\enum\setting\PrinterType as PrinterTypeEnum;
  16. use cores\exception\BaseException;
  17. /**
  18. * 小票打印机驱动
  19. * Class driver
  20. * @package app\common\library\printer
  21. */
  22. class Driver
  23. {
  24. private $printer; // 当前打印机
  25. private $engine; // 当前打印机引擎类
  26. // 打印机引擎列表
  27. private static array $engineList = [
  28. PrinterTypeEnum::FEI_E_YUN => Feie::class,
  29. PrinterTypeEnum::PRINT_CENTER => PrintCenter::class,
  30. ];
  31. /**
  32. * 构造方法
  33. * @param $printer
  34. * @throws BaseException
  35. */
  36. public function __construct($printer)
  37. {
  38. // 当前打印机
  39. $this->printer = $printer;
  40. // 实例化当前打印机引擎
  41. $this->engine = $this->getEngineClass();
  42. }
  43. /**
  44. * 执行打印请求
  45. * @param string $content
  46. * @return bool
  47. */
  48. public function printTicket(string $content): bool
  49. {
  50. return $this->engine->printTicket($content);
  51. }
  52. /**
  53. * 获取错误信息
  54. * @return mixed
  55. */
  56. public function getError()
  57. {
  58. return $this->engine->getError();
  59. }
  60. /**
  61. * 获取当前的打印机引擎类
  62. * @return mixed
  63. * @throws BaseException
  64. */
  65. private function getEngineClass()
  66. {
  67. $engineClass = self::$engineList[$this->printer['printer_type']];
  68. if (!class_exists($engineClass)) {
  69. throwError("未找到打印机引擎类: {$engineClass}");
  70. }
  71. return new $engineClass($this->printer['printer_config'], (int)$this->printer['print_times']);
  72. }
  73. }