YiLianYun.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. namespace crmeb\services\printer\storage;
  3. use crmeb\services\PrinterService;
  4. use crmeb\services\printer\AccessToken;
  5. /**
  6. * Class YiLianYun
  7. * @package crmeb\services\printer\storage
  8. */
  9. class YiLianYun extends PrinterService
  10. {
  11. /**
  12. * 初始化
  13. * @param array $config
  14. * @return mixed|void
  15. */
  16. protected function initialize(array $config)
  17. {
  18. }
  19. /**
  20. * 开始打印
  21. * @return bool|mixed|string
  22. * @throws \Exception
  23. */
  24. public function startPrinter()
  25. {
  26. if (!$this->printerContent) {
  27. return $this->setError('Missing print');
  28. }
  29. $request = $this->accessToken->postRequest($this->accessToken->getApiUrl('print/index'), [
  30. 'client_id' => $this->accessToken->clientId,
  31. 'access_token' => $this->accessToken->getAccessToken(),
  32. 'machine_code' => $this->accessToken->machineCode,
  33. 'content' => $this->printerContent,
  34. 'origin_id' => 'crmeb' . time(),
  35. 'sign' => strtolower(md5($this->accessToken->clientId . time() . $this->accessToken->apiKey)),
  36. 'id' => $this->accessToken->createUuid(),
  37. 'timestamp' => time()
  38. ]);
  39. if ($request === false) {
  40. return $this->setError('request was aborted');
  41. }
  42. $request = is_string($request) ? json_decode($request, true) : $request;
  43. if (isset($request['error']) && in_array($request['error'], [18, 14])) {
  44. return $this->setError('Accesstoken has expired');
  45. }
  46. return $request;
  47. }
  48. /**
  49. * 设置打印内容
  50. * @param array $config
  51. * @return YiLianYun
  52. */
  53. public function setPrinterContent(array $config): self
  54. {
  55. $timeYmd = date('Y-m-d H:i:s', time());
  56. $goodsStr = '<table><tr><td>商品名称</td><td>数量</td><td>单价</td><td>金额</td></tr>';
  57. $product = $config['product'];
  58. foreach ($product as $item) {
  59. $goodsStr .= '<tr>';
  60. $goodsStr .= "<td>{$item['store_name']}</td><td>{$item['product_num']}</td><td>{$item['price']}</td><td>{$item['product_price']}</td>";
  61. $goodsStr .= '</tr>';
  62. }
  63. $goodsStr .= '</table>';
  64. $orderInfo = $config['orderInfo'];
  65. $name = $config['name'];
  66. $this->printerContent = <<<CONTENT
  67. <FB><center> ** {$name} **</center></FB>
  68. <FH2><FW2>----------------</FW2></FH2>
  69. 订单编号:{$orderInfo['order_sn']}\r
  70. 打印时间: {$timeYmd} \r
  71. 付款时间: {$orderInfo['pay_time']}\r
  72. 姓 名: {$orderInfo['real_name']}\r
  73. 订单备注:{$orderInfo['mark']}\r
  74. *************商品***************\r
  75. {$goodsStr}
  76. ********************************\r
  77. <FH>
  78. <LR>合计:¥{$orderInfo['total_price']},优惠: ¥{$orderInfo['coupon_price']}</LR>
  79. <LR>邮费:¥{$orderInfo['pay_postage']}</LR>
  80. <right>实际支付:¥{$orderInfo['pay_price']}</right>
  81. </FH>
  82. <FS><center> ** 完 **</center></FS>
  83. CONTENT;
  84. return $this;
  85. }
  86. }