FeiEYun.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. <?php
  2. namespace crmeb\services\printer\storage;
  3. use app\services\activity\table\TableQrcodeServices;
  4. use crmeb\basic\BasePrinter;
  5. class FeiEYun extends BasePrinter
  6. {
  7. /**
  8. * 初始化
  9. * @param array $config
  10. * @return mixed|void
  11. */
  12. protected function initialize(array $config)
  13. {
  14. }
  15. /**
  16. * 开始打印
  17. * @return bool|mixed|string
  18. * @throws \Exception
  19. */
  20. public function startPrinter()
  21. {
  22. if (!$this->printerContent) {
  23. return $this->setError('Missing print');
  24. }
  25. $time = time();
  26. $request = $this->accessToken->postRequest('http://api.feieyun.cn/Api/Open/', [
  27. 'user' => $this->accessToken->feyUser,
  28. 'stime' => $time,
  29. 'sig' => sha1($this->accessToken->feyUser . $this->accessToken->feyUkey . $time),
  30. 'apiname' => 'Open_printMsg',
  31. 'sn' => $this->accessToken->feySn,
  32. 'content' => $this->printerContent,
  33. 'times' => 1
  34. ]);
  35. $res = json_decode($request, true);
  36. if ($res['msg'] == 'ok') {
  37. return $res;
  38. } else {
  39. return $this->setError($res['msg']);
  40. }
  41. }
  42. /**
  43. * 设置打印内容
  44. * @param array $config
  45. * @return YiLianYun
  46. */
  47. public function setPrinterContent(array $config): self
  48. {
  49. $printTime = date('Y-m-d H:i:s', time());
  50. $product = $config['product'];
  51. $orderInfo = $config['orderInfo'];
  52. $orderTime = date('Y-m-d H:i:s', $orderInfo['pay_time']);
  53. $this->printerContent = '<CB>**' . $config['name'] . '**</CB><BR>';
  54. $this->printerContent .= '--------------------------------<BR>';
  55. $this->printerContent .= '订单编号:' . $orderInfo['order_id'] . '<BR>';
  56. $this->printerContent .= '打印时间: ' . $printTime . '<BR>';
  57. $this->printerContent .= '付款时间: ' . $orderTime . '<BR>';
  58. $this->printerContent .= '姓 名: ' . $orderInfo['real_name'] . '<BR>';
  59. $this->printerContent .= '电 话: ' . $orderInfo['user_phone'] . '<BR>';
  60. $this->printerContent .= '地 址: ' . $orderInfo['user_address'] . '<BR>';
  61. $this->printerContent .= '赠送积分: ' . $orderInfo['gain_integral'] . '<BR>';
  62. $this->printerContent .= '订单备注:' . $orderInfo['mark'] . '<BR>';
  63. $this->printerContent .= '**************商品**************<BR>';
  64. $this->printerContent .= '名称 单价 数量 金额<BR>';
  65. $this->printerContent .= '--------------------------------<BR>';
  66. foreach ($product as $item) {
  67. $name = $item['productInfo']['store_name'] . " | " . $item['productInfo']['attrInfo']['suk'];
  68. $price = $item['truePrice'];
  69. $num = $item['cart_num'];
  70. $prices = bcmul((string)$item['cart_num'], (string)$item['truePrice'], 2);
  71. $kw3 = '';
  72. $kw1 = '';
  73. $kw2 = '';
  74. $kw4 = '';
  75. $str = $name;
  76. $blankNum = 14;//名称控制为14个字节
  77. $lan = mb_strlen($str, 'utf-8');
  78. $m = 0;
  79. $j = 1;
  80. $blankNum++;
  81. $result = array();
  82. if (strlen($price) < 6) {
  83. $k1 = 6 - strlen($price);
  84. for ($q = 0; $q < $k1; $q++) {
  85. $kw1 .= ' ';
  86. }
  87. $price = $price . $kw1;
  88. }
  89. if (strlen($num) < 3) {
  90. $k2 = 3 - strlen($num);
  91. for ($q = 0; $q < $k2; $q++) {
  92. $kw2 .= ' ';
  93. }
  94. $num = $num . $kw2;
  95. }
  96. if (strlen($prices) < 6) {
  97. $k3 = 6 - strlen($prices);
  98. for ($q = 0; $q < $k3; $q++) {
  99. $kw4 .= ' ';
  100. }
  101. $prices = $prices . $kw4;
  102. }
  103. for ($i = 0; $i < $lan; $i++) {
  104. $new = mb_substr($str, $m, $j, 'utf-8');
  105. $j++;
  106. if (mb_strwidth($new, 'utf-8') < $blankNum) {
  107. if ($m + $j > $lan) {
  108. $m = $m + $j;
  109. $tail = $new;
  110. $lenght = iconv("UTF-8", "GBK//IGNORE", $new);
  111. $k = 14 - strlen($lenght);
  112. for ($q = 0; $q < $k; $q++) {
  113. $kw3 .= ' ';
  114. }
  115. if ($m == $j) {
  116. $tail .= $kw3 . ' ' . $price . ' ' . $num . ' ' . $prices;
  117. } else {
  118. $tail .= $kw3 . '<BR>';
  119. }
  120. break;
  121. } else {
  122. $next_new = mb_substr($str, $m, $j, 'utf-8');
  123. if (mb_strwidth($next_new, 'utf-8') < $blankNum) {
  124. continue;
  125. } else {
  126. $m = $i + 1;
  127. $result[] = $new;
  128. $j = 1;
  129. }
  130. }
  131. }
  132. }
  133. $head = '';
  134. foreach ($result as $key => $value) {
  135. if ($key < 1) {
  136. $v_lenght = iconv("UTF-8", "GBK//IGNORE", $value);
  137. $v_lenght = strlen($v_lenght);
  138. if ($v_lenght == 13) $value = $value . " ";
  139. $head .= $value . ' ' . $price . ' ' . $num . ' ' . $prices;
  140. } else {
  141. $head .= $value . '<BR>';
  142. }
  143. }
  144. $this->printerContent .= $head . $tail;
  145. unset($price);
  146. }
  147. $this->printerContent .= '--------------------------------<BR>';
  148. $this->printerContent .= '合计:' . number_format($orderInfo['total_price'], 1) . '元<BR>';
  149. $this->printerContent .= '邮费:' . number_format($orderInfo['pay_postage'], 1) . '元<BR>';
  150. $this->printerContent .= '优惠:' . number_format($orderInfo['coupon_price'], 1) . '元<BR>';
  151. $this->printerContent .= '抵扣:' . number_format($orderInfo['deduction_price'], 1) . '元<BR>';
  152. $this->printerContent .= '实际支付:' . number_format($orderInfo['pay_price'], 1) . '元<BR>';
  153. return $this;
  154. }
  155. /**
  156. * 设置桌码打印内容
  157. * @param array $config
  158. * @return YiLianYun
  159. */
  160. public function setPrinterTableContent(array $config): self
  161. {
  162. $timeYmd = date('Y-m-d', time());
  163. $timeHis = date('H:i:s', time());
  164. $product = $config['product'];
  165. $tableInfo = $config['tableInfo'];
  166. $name = $config['name'];
  167. /** @var TableQrcodeServices $qrcodeService */
  168. $qrcodeService = app()->make(TableQrcodeServices::class);
  169. $Info = $qrcodeService->getQrcodeyInfo((int)$tableInfo['qrcode_id'], ['cateName']);
  170. $this->printerContent = '<CB>**' . $config['name'] . '**</CB><BR>';
  171. $this->printerContent .= '--------------------------------<BR>';
  172. $this->printerContent .= '桌码流水:' . $tableInfo['serial_number'] . '<BR>';
  173. $this->printerContent .= '桌码分类: ' . $Info['cateName']['name'] . '<BR>';
  174. $this->printerContent .= '桌码编号: ' . $Info['table_number'] . '<BR>';
  175. $this->printerContent .= '日 期: ' . $timeYmd . '<BR>';
  176. $this->printerContent .= '时 间:' . $timeHis . '<BR>';
  177. $this->printerContent .= '**************商品**************<BR>';
  178. $this->printerContent .= '名称 单价 数量 金额<BR>';
  179. $this->printerContent .= '--------------------------------<BR>';
  180. foreach ($product as $item) {
  181. $name = $item['productInfo']['store_name'] . " | " . $item['productInfo']['attrInfo']['suk'];
  182. $price = $item['truePrice'];
  183. $num = $item['cart_num'];
  184. $prices = bcmul((string)$item['cart_num'], (string)$item['truePrice'], 2);
  185. $kw3 = '';
  186. $kw1 = '';
  187. $kw2 = '';
  188. $kw4 = '';
  189. $str = $name;
  190. $blankNum = 14;//名称控制为14个字节
  191. $lan = mb_strlen($str, 'utf-8');
  192. $m = 0;
  193. $j = 1;
  194. $blankNum++;
  195. $result = array();
  196. if (strlen($price) < 6) {
  197. $k1 = 6 - strlen($price);
  198. for ($q = 0; $q < $k1; $q++) {
  199. $kw1 .= ' ';
  200. }
  201. $price = $price . $kw1;
  202. }
  203. if (strlen($num) < 3) {
  204. $k2 = 3 - strlen($num);
  205. for ($q = 0; $q < $k2; $q++) {
  206. $kw2 .= ' ';
  207. }
  208. $num = $num . $kw2;
  209. }
  210. if (strlen($prices) < 6) {
  211. $k3 = 6 - strlen($prices);
  212. for ($q = 0; $q < $k3; $q++) {
  213. $kw4 .= ' ';
  214. }
  215. $prices = $prices . $kw4;
  216. }
  217. for ($i = 0; $i < $lan; $i++) {
  218. $new = mb_substr($str, $m, $j, 'utf-8');
  219. $j++;
  220. if (mb_strwidth($new, 'utf-8') < $blankNum) {
  221. if ($m + $j > $lan) {
  222. $m = $m + $j;
  223. $tail = $new;
  224. $lenght = iconv("UTF-8", "GBK//IGNORE", $new);
  225. $k = 14 - strlen($lenght);
  226. for ($q = 0; $q < $k; $q++) {
  227. $kw3 .= ' ';
  228. }
  229. if ($m == $j) {
  230. $tail .= $kw3 . ' ' . $price . ' ' . $num . ' ' . $prices;
  231. } else {
  232. $tail .= $kw3 . '<BR>';
  233. }
  234. break;
  235. } else {
  236. $next_new = mb_substr($str, $m, $j, 'utf-8');
  237. if (mb_strwidth($next_new, 'utf-8') < $blankNum) {
  238. continue;
  239. } else {
  240. $m = $i + 1;
  241. $result[] = $new;
  242. $j = 1;
  243. }
  244. }
  245. }
  246. }
  247. $head = '';
  248. foreach ($result as $key => $value) {
  249. if ($key < 1) {
  250. $v_lenght = iconv("UTF-8", "GBK//IGNORE", $value);
  251. $v_lenght = strlen($v_lenght);
  252. if ($v_lenght == 13) $value = $value . " ";
  253. $head .= $value . ' ' . $price . ' ' . $num . ' ' . $prices;
  254. } else {
  255. $head .= $value . '<BR>';
  256. }
  257. }
  258. $this->printerContent .= $head . $tail;
  259. unset($price);
  260. }
  261. $this->printerContent .= '--------------------------------<BR>';
  262. $this->printerContent .= '商品金额:' . number_format(array_sum(array_column($product, 'price')), 1) . '元<BR>';
  263. return $this;
  264. }
  265. /**
  266. * 积分商城打印内容
  267. * @param array $config
  268. * @return YiLianYun
  269. */
  270. public function setIntegralPrinterContent(array $config): self
  271. {
  272. $timeYmd = date('Y-m-d', time());
  273. $timeHis = date('H:i:s', time());
  274. $orderInfo = $config['orderInfo'];
  275. $product = [['store_name' => $orderInfo['store_name'], 'cart_num' => $orderInfo['total_num'], 'price' => $orderInfo['price'], 'total_price' => $orderInfo['total_price']]];
  276. $this->printerContent = '<CB>**' . $config['name'] . '**</CB><BR>';
  277. $this->printerContent .= '--------------------------------<BR>';
  278. $this->printerContent .= '订单编号:' . $orderInfo['order_id'] . '<BR>';
  279. $this->printerContent .= '日 期: ' . $timeYmd . '<BR>';
  280. $this->printerContent .= '时 间: ' . $timeHis . '<BR>';
  281. $this->printerContent .= '姓 名: ' . $orderInfo['real_name'] . '<BR>';
  282. $this->printerContent .= '电 话:' . $orderInfo['user_phone'] . '<BR>';
  283. $this->printerContent .= '地 址:' . $orderInfo['user_address'] . '<BR>';
  284. $this->printerContent .= '订单备注:' . $orderInfo['mark'] . '<BR>';
  285. $this->printerContent .= '**************商品**************<BR>';
  286. $this->printerContent .= '名称 单价 数量 金额<BR>';
  287. $this->printerContent .= '--------------------------------<BR>';
  288. foreach ($product as $item) {
  289. $name = $item['store_name'];
  290. $price = $item['price'];
  291. $num = $item['cart_num'];
  292. $prices = $item['total_price'];
  293. $kw3 = '';
  294. $kw1 = '';
  295. $kw2 = '';
  296. $kw4 = '';
  297. $str = $name;
  298. $blankNum = 14;//名称控制为14个字节
  299. $lan = mb_strlen($str, 'utf-8');
  300. $m = 0;
  301. $j = 1;
  302. $blankNum++;
  303. $result = array();
  304. if (strlen($price) < 6) {
  305. $k1 = 6 - strlen($price);
  306. for ($q = 0; $q < $k1; $q++) {
  307. $kw1 .= ' ';
  308. }
  309. $price = $price . $kw1;
  310. }
  311. if (strlen($num) < 3) {
  312. $k2 = 3 - strlen($num);
  313. for ($q = 0; $q < $k2; $q++) {
  314. $kw2 .= ' ';
  315. }
  316. $num = $num . $kw2;
  317. }
  318. if (strlen($prices) < 6) {
  319. $k3 = 6 - strlen($prices);
  320. for ($q = 0; $q < $k3; $q++) {
  321. $kw4 .= ' ';
  322. }
  323. $prices = $prices . $kw4;
  324. }
  325. for ($i = 0; $i < $lan; $i++) {
  326. $new = mb_substr($str, $m, $j, 'utf-8');
  327. $j++;
  328. if (mb_strwidth($new, 'utf-8') < $blankNum) {
  329. if ($m + $j > $lan) {
  330. $m = $m + $j;
  331. $tail = $new;
  332. $lenght = iconv("UTF-8", "GBK//IGNORE", $new);
  333. $k = 14 - strlen($lenght);
  334. for ($q = 0; $q < $k; $q++) {
  335. $kw3 .= ' ';
  336. }
  337. if ($m == $j) {
  338. $tail .= $kw3 . ' ' . $price . ' ' . $num . ' ' . $prices;
  339. } else {
  340. $tail .= $kw3 . '<BR>';
  341. }
  342. break;
  343. } else {
  344. $next_new = mb_substr($str, $m, $j, 'utf-8');
  345. if (mb_strwidth($next_new, 'utf-8') < $blankNum) {
  346. continue;
  347. } else {
  348. $m = $i + 1;
  349. $result[] = $new;
  350. $j = 1;
  351. }
  352. }
  353. }
  354. }
  355. $head = '';
  356. foreach ($result as $key => $value) {
  357. if ($key < 1) {
  358. $v_lenght = iconv("UTF-8", "GBK//IGNORE", $value);
  359. $v_lenght = strlen($v_lenght);
  360. if ($v_lenght == 13) $value = $value . " ";
  361. $head .= $value . ' ' . $price . ' ' . $num . ' ' . $prices;
  362. } else {
  363. $head .= $value . '<BR>';
  364. }
  365. }
  366. $this->printerContent .= $head . $tail;
  367. unset($price);
  368. }
  369. $this->printerContent .= '--------------------------------<BR>';
  370. $this->printerContent .= '金额:' . $orderInfo['price'] . '元<BR>';
  371. $this->printerContent .= '积分:' . $orderInfo['integral'] . '<BR>';
  372. return $this;
  373. }
  374. }