PrintService.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. <?php
  2. namespace addon\printer\data\sdk\yilianyun\api;
  3. use addon\printer\model\Printer;
  4. use think\Exception;
  5. class PrintService extends RpcService
  6. {
  7. /**
  8. * 打印接口
  9. *
  10. * @param $machineCode string 机器码
  11. * @param $content string 打印内容
  12. * @param $originId string 商户系统内部订单号,要求32个字符内,只能是数字、大小写字母
  13. * @return mixed
  14. */
  15. public function index($machineCode, $content, $originId, $printer = [])
  16. {
  17. $res = $this->client->call('print/index', array ( 'machine_code' => $machineCode, 'content' => $content, 'origin_id' => $originId ));
  18. if (is_null($res)) {
  19. throw new Exception("invalid response.");
  20. }
  21. if ($res->error == 18 && $printer) {
  22. $printer_model = new Printer();
  23. $config = new YlyConfig($printer[ 'open_id' ], $printer[ 'apikey' ]);
  24. $access_token = $printer_model->getYlyToken($config, $printer[ 'site_id' ], 1);
  25. $this->index($machineCode, $content, $originId, []);
  26. }
  27. if (isset($res->error) && $res->error != 0) {
  28. $errorDescription = isset($res->body) ? $res->error_description . $res->body : $res->error_description;
  29. throw new Exception('Call method print/index error code is ' . $res->error . ' error message is ' . $errorDescription);
  30. }
  31. }
  32. }