YiLianYun.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. * @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', time());
  56. $timeHis = date('H:i:s', time());
  57. $goodsStr = '<table><tr><td>商品名称</td><td>数量</td><td>单价</td><td>金额</td></tr>';
  58. $product = $config['product'];
  59. foreach ($product as $item) {
  60. $goodsStr .= '<tr>';
  61. $goodsStr .= "<td>{$item['productInfo']['store_name']}</td><td>{$item['cart_num']}</td><td>{$item['productInfo']['price']}</td><td>{$item['truePrice']}</td>";
  62. $goodsStr .= '</tr>';
  63. }
  64. $goodsStr .= '</table>';
  65. $orderInfo = $config['orderInfo'];
  66. $name = $config['name'];
  67. $this->printerContent = <<<CONTENT
  68. <FB><center> ** {$name} **</center></FB>
  69. <FH2><FW2>----------------</FW2></FH2>
  70. 订单编号:{$orderInfo['order_id']}\r
  71. 时 间: {$timeYmd} {$timeHis}\r
  72. 姓 名: {$orderInfo['real_name']}\r
  73. 电 话: {$orderInfo['user_phone']}\r
  74. 订单备注:{$orderInfo['mark']}\r
  75. *************商品***************\r
  76. {$goodsStr}
  77. ********************************\r
  78. <FH>
  79. <LR>合计:¥{$orderInfo['total_price']},优惠: ¥{$orderInfo['coupon_price']}</LR>
  80. <LR>邮费:¥{$orderInfo['pay_postage']},抵扣:¥{$orderInfo['deduction_price']}</LR>
  81. <right>实际支付:¥{$orderInfo['pay_price']}</right>
  82. </FH>
  83. CONTENT;
  84. return $this;
  85. }
  86. }