YiLianYun.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 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. $arr = [
  41. 'client_id' => $this->accessToken->clientId,
  42. 'access_token' => $this->accessToken->getAccessToken(),
  43. 'machine_code' => $this->accessToken->machineCode,
  44. 'content' => $this->printerContent,
  45. 'origin_id' => 'crmeb' . time(),
  46. 'sign' => strtolower(md5($this->accessToken->clientId . time() . $this->accessToken->apiKey)),
  47. 'id' => $this->accessToken->createUuid(),
  48. 'timestamp' => time()
  49. ];
  50. $request = $this->accessToken->postRequest($this->accessToken->getApiUrl('print/index'), $arr);
  51. $info = \Qiniu\json_decode($request);
  52. if($info->error == 18){
  53. Cache::delete('YLY_access_token_'.$this->accessToken->clientId);
  54. $arr['access_token'] = $this->accessToken->getAccessToken();
  55. $request = $this->accessToken->postRequest($this->accessToken->getApiUrl('print/index'), $arr);
  56. }
  57. if ($request === false) {
  58. return $this->setError('request was aborted');
  59. }
  60. $request = is_string($request) ? json_decode($request, true) : $request;
  61. if (isset($request['error']) && in_array($request['error'], [18, 14])) {
  62. return $this->setError('Accesstoken has expired');
  63. }
  64. return $request;
  65. }
  66. /**
  67. * 设置打印内容
  68. * @param array $config
  69. * @return YiLianYun
  70. */
  71. public function setPrinterContent(array $config): self
  72. {
  73. $timeYmd = date('Y-m-d H:i:s', time());
  74. $goodsStr = '<table><tr><td>商品名称</td><td>数量</td><td>单价</td><td>金额</td></tr>';
  75. $product = $config['product'];
  76. foreach ($product as $item) {
  77. $goodsStr .= '<tr>';
  78. $goodsStr .= "<td>{$item['store_name']}</td><td>{$item['product_num']}</td><td>{$item['price']}</td><td>{$item['product_price']}</td>";
  79. $goodsStr .= '</tr>';
  80. }
  81. $goodsStr .= '</table>';
  82. $orderInfo = $config['orderInfo'];
  83. $name = $config['name'];
  84. $this->printerContent = <<<CONTENT
  85. <FB><center> ** {$name} **</center></FB>
  86. <FH2><FW2>----------------</FW2></FH2>
  87. 订单编号:{$orderInfo['order_sn']}\r
  88. 打印时间: {$timeYmd} \r
  89. 付款时间: {$orderInfo['pay_time']}\r
  90. 姓 名: {$orderInfo['real_name']}\r
  91. 电 话: {$orderInfo['user_phone']}\r
  92. 地 址: {$orderInfo['user_address']}\r
  93. 订单备注:{$orderInfo['mark']}\r
  94. *************商品***************\r
  95. {$goodsStr}
  96. ********************************\r
  97. <FH>
  98. <LR>合计:¥{$orderInfo['total_price']},优惠: ¥{$orderInfo['coupon_price']}</LR>
  99. <LR>邮费:¥{$orderInfo['pay_postage']}</LR>
  100. <right>实际支付:¥{$orderInfo['pay_price']}</right>
  101. </FH>
  102. <FS><center> ** 完 **</center></FS>
  103. CONTENT;
  104. return $this;
  105. }
  106. }