YiLianYun.php 3.4 KB

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