FeiEYun.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. namespace crmeb\services\printer\storage;
  3. use crmeb\services\printer\BasePrinter;
  4. class FeiEYun extends BasePrinter
  5. {
  6. /**
  7. * 初始化
  8. * @param array $config
  9. * @return mixed|void
  10. */
  11. protected function initialize(array $config)
  12. {
  13. }
  14. /**
  15. * 开始打印
  16. * @return bool|mixed|string
  17. * @throws \Exception
  18. */
  19. public function startPrinter()
  20. {
  21. if (!$this->printerContent) {
  22. return $this->setError('Missing print');
  23. }
  24. $time = time();
  25. $request = $this->accessToken->postRequest('http://api.feieyun.cn/Api/Open/', [
  26. 'user' => $this->accessToken->feyUser,
  27. 'stime' => $time,
  28. 'sig' => sha1($this->accessToken->feyUser . $this->accessToken->feyUkey . $time),
  29. 'apiname' => 'Open_printMsg',
  30. 'sn' => $this->accessToken->feySn,
  31. 'content' => $this->printerContent,
  32. 'times' => 1
  33. ]);
  34. $res = json_decode($request, true);
  35. if ($res['msg'] == 'ok') {
  36. return $res;
  37. } else {
  38. return $this->setError($res['msg']);
  39. }
  40. }
  41. /**
  42. * 设置打印内容
  43. * @param array $config
  44. * @return YiLianYun
  45. */
  46. public function setPrinterContent(array $config): self
  47. {
  48. $printTime = date('Y-m-d H:i:s', time());
  49. $product = $config['product'];
  50. $orderInfo = $config['orderInfo'];
  51. $orderTime = date('Y-m-d H:i:s', $orderInfo['pay_time']);
  52. $this->printerContent = '<CB>**' . $config['name'] . '**</CB><BR>';
  53. $this->printerContent .= '--------------------------------<BR>';
  54. $this->printerContent .= '订单编号:' . $orderInfo['order_id'] . '<BR>';
  55. $this->printerContent .= '打印时间: ' . $printTime . '<BR>';
  56. $this->printerContent .= '付款时间: ' . $orderTime . '<BR>';
  57. $this->printerContent .= '姓 名: ' . $orderInfo['real_name'] . '<BR>';
  58. $this->printerContent .= '电 话: ' . $orderInfo['user_phone'] . '<BR>';
  59. $this->printerContent .= '地 址: ' . $orderInfo['user_address'] . '<BR>';
  60. $this->printerContent .= '赠送积分: ' . $orderInfo['gain_integral'] . '<BR>';
  61. $this->printerContent .= '订单备注:' . $orderInfo['mark'] . '<BR>';
  62. $this->printerContent .= '**************商品**************<BR>';
  63. $this->printerContent .= '名称 单价 数量 金额<BR>';
  64. $this->printerContent .= '--------------------------------<BR>';
  65. foreach ($product as $item) {
  66. $name = $item['productInfo']['store_name'] . " | " . $item['productInfo']['attrInfo']['suk'];
  67. $price = $item['truePrice'];
  68. $num = $item['cart_num'];
  69. $prices = bcmul((string)$item['cart_num'], (string)$item['truePrice'], 2);
  70. $kw3 = '';
  71. $kw1 = '';
  72. $kw2 = '';
  73. $kw4 = '';
  74. $str = $name;
  75. $blankNum = 14;//名称控制为14个字节
  76. $lan = mb_strlen($str, 'utf-8');
  77. $m = 0;
  78. $j = 1;
  79. $blankNum++;
  80. $result = array();
  81. if (strlen($price) < 6) {
  82. $k1 = 6 - strlen($price);
  83. for ($q = 0; $q < $k1; $q++) {
  84. $kw1 .= ' ';
  85. }
  86. $price = $price . $kw1;
  87. }
  88. if (strlen($num) < 3) {
  89. $k2 = 3 - strlen($num);
  90. for ($q = 0; $q < $k2; $q++) {
  91. $kw2 .= ' ';
  92. }
  93. $num = $num . $kw2;
  94. }
  95. if (strlen($prices) < 6) {
  96. $k3 = 6 - strlen($prices);
  97. for ($q = 0; $q < $k3; $q++) {
  98. $kw4 .= ' ';
  99. }
  100. $prices = $prices . $kw4;
  101. }
  102. for ($i = 0; $i < $lan; $i++) {
  103. $new = mb_substr($str, $m, $j, 'utf-8');
  104. $j++;
  105. if (mb_strwidth($new, 'utf-8') < $blankNum) {
  106. if ($m + $j > $lan) {
  107. $m = $m + $j;
  108. $tail = $new;
  109. $lenght = iconv("UTF-8", "GBK//IGNORE", $new);
  110. $k = 14 - strlen($lenght);
  111. for ($q = 0; $q < $k; $q++) {
  112. $kw3 .= ' ';
  113. }
  114. if ($m == $j) {
  115. $tail .= $kw3 . ' ' . $price . ' ' . $num . ' ' . $prices;
  116. } else {
  117. $tail .= $kw3 . '<BR>';
  118. }
  119. break;
  120. } else {
  121. $next_new = mb_substr($str, $m, $j, 'utf-8');
  122. if (mb_strwidth($next_new, 'utf-8') < $blankNum) {
  123. continue;
  124. } else {
  125. $m = $i + 1;
  126. $result[] = $new;
  127. $j = 1;
  128. }
  129. }
  130. }
  131. }
  132. $head = '';
  133. foreach ($result as $key => $value) {
  134. if ($key < 1) {
  135. $v_lenght = iconv("UTF-8", "GBK//IGNORE", $value);
  136. $v_lenght = strlen($v_lenght);
  137. if ($v_lenght == 13) $value = $value . " ";
  138. $head .= $value . ' ' . $price . ' ' . $num . ' ' . $prices;
  139. } else {
  140. $head .= $value . '<BR>';
  141. }
  142. }
  143. $this->printerContent .= $head . $tail;
  144. unset($price);
  145. }
  146. $this->printerContent .= '--------------------------------<BR>';
  147. $this->printerContent .= '合计:' . number_format($orderInfo['total_price'], 2) . '元<BR>';
  148. $this->printerContent .= '邮费:' . number_format($orderInfo['pay_postage'], 2) . '元<BR>';
  149. $this->printerContent .= '优惠:' . number_format($orderInfo['coupon_price'], 2) . '元<BR>';
  150. $this->printerContent .= '抵扣:' . number_format($orderInfo['deduction_price'], 2) . '元<BR>';
  151. $this->printerContent .= '实际支付:' . number_format($orderInfo['pay_price'], 2) . '元<BR>';
  152. $this->printerContent .= '<QR>' . $config['url'] . '</QR>';//把解析后的二维码生成的字符串用标签套上即可自动生成二维码
  153. return $this;
  154. }
  155. }