DqznPrint.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace app\utils;
  3. use app\models\Printer;
  4. use Exception;
  5. class DqznPrint
  6. {
  7. protected string $host = 'https://printer.juhesaas.com';
  8. public string $app_id = '';
  9. public string $app_secret = '';
  10. public function __construct($printer_id = 0)
  11. {
  12. $printerModel = Printer::findOne($printer_id);
  13. if ($printerModel) {
  14. $printer_setting = json_decode($printerModel->printer_setting, true);
  15. if ($printer_setting) {
  16. $this->app_id = $printer_setting['app_id'];
  17. $this->app_secret = $printer_setting['app_secret'];
  18. }
  19. }
  20. }
  21. protected function request($url, $data)
  22. {
  23. $data = json_encode($data);
  24. $uid = uniqid('rxid');
  25. $time = time();
  26. $md5 = md5($uid . $this->app_id . $time . $this->app_secret . $data);
  27. $header = [
  28. "Content-Type:application/json",
  29. "appid:" . $this->app_id,
  30. "uid:" . $uid,
  31. "stime:" . $time,
  32. "sign:" . $md5
  33. ];
  34. $ci = curl_init();
  35. curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
  36. curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 30);
  37. curl_setopt($ci, CURLOPT_TIMEOUT, 30);
  38. curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);
  39. curl_setopt($ci, CURLINFO_HEADER_OUT, true);
  40. curl_setopt($ci, CURLOPT_HTTPHEADER, $header); // 设置通用传参
  41. curl_setopt($ci, CURLOPT_POST, true);
  42. curl_setopt($ci, CURLOPT_POSTFIELDS, $data);
  43. curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
  44. curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, false);// 从证书中检查SSL加密算法是否存在
  45. curl_setopt($ci, CURLOPT_URL, $url);
  46. $response = curl_exec($ci);
  47. $http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
  48. curl_close($ci);
  49. if (!$http_code || !$response || $http_code !== 200) {
  50. throw new Exception('请求失败:' . $http_code);
  51. }
  52. $result = json_decode($response, true);
  53. if ($result['code'] !== 0) {
  54. throw new Exception($result['message']);
  55. }
  56. return $result['data'];
  57. }
  58. /**
  59. * 添加打印设备
  60. */
  61. public function add_printer($sn, $key, $name)
  62. {
  63. $url = $this->host . '/openapi/addPrinter';
  64. return $this->request($url, [
  65. [
  66. "sn" => $sn,
  67. "key" => $key,
  68. "name" => $name
  69. ]
  70. ]);
  71. }
  72. /**
  73. * 修改打印设备
  74. */
  75. public function edit_printer($sn, $name)
  76. {
  77. $url = $this->host . '/openapi/editPrinter';
  78. return $this->request($url, [
  79. [
  80. "sn" => $sn,
  81. "name" => $name
  82. ]
  83. ]);
  84. }
  85. /**
  86. * 删除打印设备
  87. */
  88. public function delete_printer($sn)
  89. {
  90. $url = $this->host . '/openapi/delPrinter';
  91. return $this->request($url, [
  92. $sn
  93. ]);
  94. }
  95. /**
  96. * 查询打印机状态
  97. */
  98. public function status($sn)
  99. {
  100. $url = $this->host . '/openapi/getDeviceStatus';
  101. return $this->request($url, [
  102. 'sn' => $sn
  103. ]);
  104. }
  105. /**
  106. * 打印小票
  107. */
  108. public function print($sn, $content, $copies, $voice = 4)
  109. {
  110. $url = $this->host . '/openapi/print';
  111. return $this->request($url, [
  112. "sn" => $sn,
  113. "content" => $content,
  114. "copies" => $copies,
  115. "voice" => $voice,
  116. ]);
  117. }
  118. }