YiLianYun.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace crmeb\services\printer\storage;
  12. use crmeb\services\printer\BasePrinter;
  13. /**
  14. * Class YiLianYun
  15. * @package crmeb\services\printer\storage
  16. */
  17. class YiLianYun extends BasePrinter
  18. {
  19. /**
  20. * 初始化
  21. * @param array $config
  22. * @return mixed|void
  23. */
  24. protected function initialize(array $config)
  25. {
  26. }
  27. /**
  28. * 开始打印
  29. * @return bool|mixed|string
  30. * @throws \Exception
  31. */
  32. public function startPrinter()
  33. {
  34. if (!$this->printerContent) {
  35. return $this->setError('Missing print');
  36. }
  37. $request = $this->accessToken->postRequest('https://open-api.10ss.net/print/index', [
  38. 'client_id' => $this->accessToken->clientId,
  39. 'access_token' => $this->accessToken->getAccessToken(),
  40. 'machine_code' => $this->accessToken->machineCode,
  41. 'content' => $this->printerContent,
  42. 'origin_id' => 'crmeb' . time(),
  43. 'sign' => strtolower(md5($this->accessToken->clientId . time() . $this->accessToken->apiKey)),
  44. 'id' => $this->accessToken->createUuid(),
  45. 'timestamp' => time()
  46. ]);
  47. if ($request === false) {
  48. return $this->setError('request was aborted');
  49. }
  50. $request = is_string($request) ? json_decode($request, true) : $request;
  51. if (isset($request['error']) && in_array($request['error'], [18, 14])) {
  52. return $this->setError('Accesstoken has expired');
  53. }
  54. return $request;
  55. }
  56. /**
  57. * 设置打印内容
  58. * @param array $config
  59. * @return YiLianYun
  60. */
  61. public function setPrinterContent(array $config): self
  62. {
  63. $printTime = date('Y-m-d H:i:s', time());
  64. $goodsStr = '<table><tr><td>商品名称</td><td>数量</td><td>单价</td><td>金额</td></tr>';
  65. $product = $config['product'];
  66. foreach ($product as $item) {
  67. $goodsStr .= '<tr>';
  68. $price = bcmul((string)$item['cart_num'], (string)$item['truePrice'], 2);
  69. $goodsStr .= "<td>{$item['productInfo']['store_name']} | {$item['productInfo']['attrInfo']['suk']}</td><td>{$item['cart_num']}</td><td>{$item['truePrice']}</td><td>{$price}</td>";
  70. $goodsStr .= '</tr>';
  71. unset($price);
  72. }
  73. $goodsStr .= '</table>';
  74. $orderInfo = $config['orderInfo'];
  75. $orderTime = date('Y-m-d H:i:s',$orderInfo['pay_time']);
  76. $name = $config['name'];
  77. $this->printerContent = <<<CONTENT
  78. <FB><center> ** {$name} **</center></FB>
  79. <FH2><FW2>----------------</FW2></FH2>
  80. 订单编号:{$orderInfo['order_id']}\r
  81. 打印时间: {$printTime} \r
  82. 付款时间: {$orderTime}\r
  83. 姓 名: {$orderInfo['real_name']}\r
  84. 电 话: {$orderInfo['user_phone']}\r
  85. 地 址: {$orderInfo['user_address']}\r
  86. 赠送积分: {$orderInfo['gain_integral']}\r
  87. 订单备注:{$orderInfo['mark']}\r
  88. *************商品***************\r
  89. {$goodsStr}
  90. ********************************\r
  91. <FH>
  92. <LR>合计:¥{$orderInfo['total_price']},优惠: ¥{$orderInfo['coupon_price']}</LR>
  93. <LR>邮费:¥{$orderInfo['pay_postage']},抵扣:¥{$orderInfo['deduction_price']}</LR>
  94. <right>实际支付:¥{$orderInfo['pay_price']}</right>
  95. </FH>
  96. <FS><center> ** 完 **</center></FS>
  97. CONTENT;
  98. return $this;
  99. }
  100. }