Feie.php 1.7 KB

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