Feie.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\common\library\printer\engine;
  13. use app\common\library\helper;
  14. use app\common\library\printer\party\FeieHttpClient;
  15. /**
  16. * 飞鹅打印机API引擎
  17. * Class Feie
  18. * @package app\common\library\printer\engine
  19. */
  20. class Feie extends Basics
  21. {
  22. /** @const IP 接口IP或域名 */
  23. const IP = 'api.feieyun.cn';
  24. /** @const PORT 接口IP端口 */
  25. const PORT = 80;
  26. /** @const PATH 接口路径 */
  27. const PATH = '/Api/Open/';
  28. /**
  29. * 执行订单打印
  30. * @param string $content
  31. * @return bool
  32. */
  33. public function printTicket(string $content): bool
  34. {
  35. // 构建请求参数
  36. $params = $this->getParams($content);
  37. // API请求:开始打印
  38. $client = new FeieHttpClient(self::IP, self::PORT);
  39. if (!$client->post(self::PATH, $params)) {
  40. $this->error = $client->getError();
  41. return false;
  42. }
  43. // 处理返回结果
  44. $result = helper::jsonDecode($client->getContent());
  45. // 记录日志
  46. log_record($result);
  47. // 返回状态
  48. if ($result['ret'] != 0) {
  49. $this->error = $result['msg'];
  50. return false;
  51. }
  52. return true;
  53. }
  54. /**
  55. * 构建Api请求参数
  56. * @param $content
  57. * @return array
  58. */
  59. private function getParams(&$content): array
  60. {
  61. $time = time();
  62. return [
  63. 'user' => $this->config['USER'],
  64. 'stime' => $time,
  65. 'sig' => sha1("{$this->config['USER']}{$this->config['UKEY']}{$time}"),
  66. 'apiname' => 'Open_printMsg',
  67. 'sn' => $this->config['SN'],
  68. 'content' => $content,
  69. 'times' => $this->times // 打印次数
  70. ];
  71. }
  72. }