Feie.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace app\common\library\printer\engine;
  3. use app\common\library\helper;
  4. use app\common\library\printer\party\FeieHttpClient;
  5. /**
  6. * 飞鹅打印机API引擎
  7. * Class Feie
  8. * @package app\common\library\printer\engine
  9. */
  10. class Feie extends Basics
  11. {
  12. /** @const IP 接口IP或域名 */
  13. const IP = 'api.feieyun.cn';
  14. /** @const PORT 接口IP端口 */
  15. const PORT = 80;
  16. /** @const PATH 接口路径 */
  17. const PATH = '/Api/Open/';
  18. /**
  19. * 执行订单打印
  20. * @param $content
  21. * @return bool|mixed
  22. */
  23. public function printTicket($content)
  24. {
  25. // 构建请求参数
  26. $params = $this->getParams($content);
  27. // API请求:开始打印
  28. $client = new FeieHttpClient(self::IP, self::PORT);
  29. if (!$client->post(self::PATH, $params)) {
  30. $this->error = $client->getError();
  31. return false;
  32. }
  33. // 处理返回结果
  34. $result = helper::jsonDecode($client->getContent());
  35. // 记录日志
  36. log_write([
  37. 'describe' => 'Feie PrintTicket',
  38. 'result' => $result
  39. ]);
  40. // 返回状态
  41. if ($result['ret'] != 0) {
  42. $this->error = $result['msg'];
  43. return false;
  44. }
  45. return true;
  46. }
  47. /**
  48. * 构建Api请求参数
  49. * @param $content
  50. * @return array
  51. */
  52. private function getParams(&$content)
  53. {
  54. $time = time();
  55. return [
  56. 'user' => $this->config['USER'],
  57. 'stime' => $time,
  58. 'sig' => sha1("{$this->config['USER']}{$this->config['UKEY']}{$time}"),
  59. 'apiname' => 'Open_printMsg',
  60. 'sn' => $this->config['SN'],
  61. 'content' => $content,
  62. 'times' => $this->times // 打印次数
  63. ];
  64. }
  65. }