YiLianYun.php 4.4 KB

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