FeiEYun.php 7.3 KB

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