XhyPrint.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <?php
  2. namespace app\utils;
  3. use app\models\Printer;
  4. use Exception;
  5. class XhyPrint
  6. {
  7. protected int $debug = 0;
  8. protected string $host = 'https://open.xpyun.net';
  9. public string $app_id = '';
  10. public string $app_key = '';
  11. protected array $error = [
  12. '-1' => '请求头错误',
  13. '-2' => '参数不合法',
  14. '-3' => '参数签名失败',
  15. '-4' => '用户未注册',
  16. '1001' => '打印机编号和用户不匹配',
  17. '1002' => '打印机未注册',
  18. '1003' => '打印机不在线',
  19. '1004' => '添加订单失败',
  20. '1005' => '未找到订单信息',
  21. '1006' => '订单日期格式或大小不正确',
  22. '1007' => '打印内容不能超过12K',
  23. '1008' => '用户修改打印机记录失败',
  24. '1009' => '用户添加打印机时,打印机编号或名称不能为空',
  25. '1010' => '打印机设备编号无效',
  26. '1011' => '打印机已存在',
  27. '1012' => '添加打印设备失败',
  28. '1013' => '打印订单时触发幂等性',
  29. '1014' => '幂等因子过长',
  30. '1015' => '',
  31. '1016' => 'LOGO文件格式错误',
  32. '1017' => 'LOGO文件超出规定范围',
  33. '1018' => 'LOGO上传次数超限制',
  34. '1019' => '',
  35. '1020' => 'LOGO删除失败',
  36. '1021' => 'LOGO上传模式错误',
  37. '1022' => '该设备属于定制设备,您当前无权使用',
  38. ];
  39. protected function getError($code): string
  40. {
  41. return $this->error[$code] ?? '';
  42. }
  43. public function __construct($printer_id = 0)
  44. {
  45. $printerModel = Printer::findOne($printer_id);
  46. if ($printerModel) {
  47. $printer_setting = json_decode($printerModel->printer_setting, true);
  48. if ($printer_setting) {
  49. $this->app_id = $printer_setting['app_id'];
  50. $this->app_key = $printer_setting['app_key'];
  51. }
  52. }
  53. }
  54. /**
  55. * @throws Exception
  56. */
  57. protected function request($url, $data)
  58. {
  59. $timestamp = (string)time();
  60. $default = [
  61. 'user' => $this->app_id,
  62. 'timestamp' => $timestamp,
  63. 'sign' => sha1($this->app_id . $this->app_key . $timestamp),
  64. 'debug' => $this->debug,
  65. ];
  66. $data = array_merge($default, $data);
  67. $jsonStr = json_encode($data);
  68. $ch = curl_init();
  69. curl_setopt($ch, CURLOPT_POST, 1);// 发送一个常规的Post请求
  70. curl_setopt($ch, CURLOPT_URL, $url);// 要访问的地址
  71. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检测
  72. curl_setopt($ch, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循
  73. curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr);
  74. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  75. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  76. 'Content-Type: application/json;charset=UTF-8',
  77. 'Content-Length: ' . strlen($jsonStr)
  78. )
  79. );
  80. $response = curl_exec($ch);
  81. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  82. if (curl_errno($ch)) {
  83. throw new Exception(curl_error($ch));
  84. }
  85. curl_close($ch);
  86. if ($httpCode !== 200) {
  87. throw new Exception($httpCode);
  88. }
  89. $result = json_decode($response, true);
  90. if ($result['code'] !== 0) {
  91. $err_msg = $this->getError($result['code']);
  92. throw new Exception($err_msg === '' ? $result['msg'] : $err_msg);
  93. }
  94. return $result['data'];
  95. }
  96. /**
  97. * 添加打印设备
  98. * @param string $sn
  99. * @param string $name
  100. *
  101. * @return boolean
  102. * @throws Exception
  103. */
  104. public function add_printer(string $sn, string $name): bool
  105. {
  106. if (!$sn || !$name) {
  107. throw new Exception('缺少必要参数');
  108. }
  109. $url = $this->host . '/api/openapi/xprinter/addPrinters';
  110. $result = $this->request($url, [
  111. 'items' => [[
  112. 'sn' => $sn,
  113. 'name' => $name,
  114. ]]
  115. ]);
  116. $open = false;
  117. if (isset($result['failMsg']) && $result['failMsg']) {
  118. $error = '';
  119. foreach ($result['failMsg'] as $fail) {
  120. $fail_arr = explode(':', $fail);
  121. $open = true;
  122. if ($fail_arr[1] == 1011) {//打印机已经存在
  123. $open = false;
  124. }
  125. $error .= $this->getError($fail_arr[1]);
  126. }
  127. if (!$open) {
  128. return true;
  129. }
  130. throw new Exception($error);
  131. }
  132. return true;
  133. }
  134. /**
  135. * 更新打印设备信息
  136. *
  137. * @return boolean
  138. * @throws Exception
  139. */
  140. public function update_printer(string $sn, string $name): bool
  141. {
  142. if (!$sn || !$name) {
  143. throw new Exception('缺少必要参数');
  144. }
  145. $url = $this->host . '/api/openapi/xprinter/updPrinter';
  146. return $this->request($url, [
  147. 'sn' => $sn,
  148. 'name' => $name,
  149. ]);
  150. }
  151. /**
  152. * 删除打印设备
  153. * @param string $sn
  154. *
  155. * @return mixed
  156. * @throws Exception
  157. */
  158. public function delete_printer(string $sn)
  159. {
  160. $url = $this->host . '/api/openapi/xprinter/delPrinters';
  161. $result = $this->request($url, [
  162. 'snlist' => [$sn]
  163. ]);
  164. if (isset($result['failMsg']) && $result['failMsg']) {
  165. $error = '';
  166. foreach ($result['failMsg'] as $fail) {
  167. $fail_arr = explode(':', $fail);
  168. $error .= '删除失败:' . $this->getError($fail_arr[1]);
  169. }
  170. throw new Exception($error);
  171. }
  172. return $result;
  173. }
  174. /**
  175. * 设置声音
  176. * @param $sn string SN码
  177. * @param $voice_type int 声音类型
  178. * @param $level int 声音级别
  179. *
  180. * @return boolean
  181. * @throws Exception
  182. */
  183. public function set_voice_type(string $sn, int $voice_type, int $level): bool
  184. {
  185. $url = $this->host . '/api/openapi/xprinter/setVoiceType';
  186. if (!$sn) {
  187. throw new Exception('SN码错误');
  188. }
  189. if (!$voice_type || !in_array($voice_type, [0, 1, 2, 3, 4])) {
  190. throw new Exception('声音类型错误');
  191. }
  192. if (!$level || !in_array($level, [0, 1, 2, 3])) {
  193. throw new Exception('声音大小错误');
  194. }
  195. return $this->request($url, [
  196. 'sn' => $sn,
  197. 'voiceType' => (int)$voice_type,
  198. 'volumeLevel' => (int)$level,
  199. ]);
  200. }
  201. /**
  202. * 批量查询打印设备状态
  203. * @param string $sn
  204. *
  205. * @return mixed
  206. * @throws Exception
  207. */
  208. public function get_status(string $sn)
  209. {
  210. $url = $this->host . '/api/openapi/xprinter/queryPrintersStatus';
  211. return $this->request($url, [
  212. 'snlist' => [$sn]
  213. ])[0];
  214. }
  215. /**
  216. * 打印内容
  217. * @param string $sn SN码
  218. * @param string $content 内容
  219. * @param int $copies 份数
  220. * @param int $voice 声音播放模式,0 为取消订单模式,1 为静音模式,2 为来单播放模式,3 为有用户申请退单了,默认为 2 来单播放模式
  221. * @param int $mode 打印模式:0=不在线不打印;1=不在线加入打印队列
  222. * @param int $expiresIn 缓存有效期(mode=1时生效):0=系统默认,取值范围:0 < expiresIn < 86400
  223. *
  224. * @return mixed
  225. * @throws Exception
  226. * */
  227. public function print(string $sn, string $content, int $copies = 1, int $voice = 2, int $mode = 0, int $expiresIn = 0)
  228. {
  229. $url = $this->host . '/api/openapi/xprinter/print';
  230. return $this->request($url, [
  231. 'sn' => $sn,
  232. 'content' => $content,
  233. 'copies' => $copies,
  234. 'voice' => $voice,
  235. 'mode' => $mode,
  236. 'expiresIn' => $expiresIn,
  237. ]);
  238. }
  239. }