YiLianYun.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. namespace crmeb\services\printer\storage;
  3. use crmeb\basic\BasePrinter;
  4. use crmeb\services\printer\AccessToken;
  5. /**
  6. * Class YiLianYun
  7. * @package crmeb\services\printer\storage
  8. */
  9. class YiLianYun extends BasePrinter
  10. {
  11. /**
  12. * 初始化
  13. * @param array $config
  14. * @return mixed|void
  15. */
  16. protected function initialize(array $config)
  17. {
  18. }
  19. /**
  20. * 开始打印
  21. * @param array|null $config
  22. * @return bool|mixed|string
  23. * @throws \Exception
  24. */
  25. public function startPrinter(?array $config = [])
  26. {
  27. if (!$this->printerContent) {
  28. return $this->setError('Missing print');
  29. }
  30. $request = $this->accessToken->postRequest($this->accessToken->getApiUrl('print/index'), [
  31. 'client_id' => $this->accessToken->clientId,
  32. 'access_token' => $this->accessToken->getAccessToken(),
  33. 'machine_code' => $this->accessToken->machineCode,
  34. 'content' => $this->printerContent,
  35. 'origin_id' => $config['origin_id'] ?? 'crmeb' . time(),
  36. 'sign' => strtolower(md5($this->accessToken->clientId . time() . $this->accessToken->apiKey)),
  37. 'id' => $this->accessToken->createUuid(),
  38. 'timestamp' => time()
  39. ]);
  40. if ($request === false) {
  41. return $this->setError('request was aborted');
  42. }
  43. $request = is_string($request) ? json_decode($request, true) : $request;
  44. if (isset($request['error']) && in_array($request['error'], [18, 14])) {
  45. return $this->setError('Accesstoken has expired');
  46. }
  47. return $request;
  48. }
  49. /**
  50. * 设置打印内容
  51. * @param array $config
  52. * @return YiLianYun
  53. */
  54. public function setPrinterContent(array $config): self
  55. {
  56. $this->printerContent = $config['content'] ?? null;
  57. return $this;
  58. }
  59. }