Feie.php 1.8 KB

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