YiLianYun.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 app\services\activity\table\TableQrcodeServices;
  13. use crmeb\basic\BasePrinter;
  14. use crmeb\services\printer\AccessToken;
  15. use app\services\activity\collage\UserCollageServices;
  16. /**
  17. * Class YiLianYun
  18. * @package crmeb\services\printer\storage
  19. */
  20. class YiLianYun extends BasePrinter
  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. $time = time();
  41. try {
  42. $request = $this->accessToken->postRequest($this->accessToken->getApiUrl('print/index'), [
  43. 'client_id' => $this->accessToken->clientId,
  44. 'access_token' => $this->accessToken->getAccessToken(),
  45. 'machine_code' => $this->accessToken->machineCode,
  46. 'content' => $this->printerContent,
  47. 'origin_id' => 'crmeb' . $time,
  48. 'sign' => strtolower(md5($this->accessToken->clientId . $time . $this->accessToken->apiKey)),
  49. 'id' => $this->accessToken->createUuid(),
  50. 'timestamp' => $time
  51. ]);
  52. } catch (\Exception $e) {
  53. return $this->setError($e->getMessage());
  54. }
  55. $this->printerContent = null;
  56. if ($request === false) {
  57. return $this->setError('request was aborted');
  58. }
  59. $request = is_string($request) ? json_decode($request, true) : $request;
  60. if (isset($request['error']) && $request['error'] != 0) {
  61. return $this->setError(isset($request['error_description']) ? $request['error_description'] : 'Accesstoken has expired');
  62. }
  63. return $request;
  64. }
  65. /**
  66. * 设置打印内容
  67. * @param array $config
  68. * @return YiLianYun
  69. */
  70. public function setPrinterContent(array $config): self
  71. {
  72. $timeYmd = date('Y-m-d', time());
  73. $timeHis = date('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. if ($item['is_gift']) {
  79. $unit_price = $price = 0;
  80. } else {
  81. if (isset($item['sum_price'])) {
  82. $unit_price = $item['sum_price'];
  83. $price = bcmul((string)$item['cart_num'], (string)$unit_price, 2);
  84. } else {
  85. $unit_price = $item['truePrice'];
  86. $price = bcadd((string)$item['vip_truePrice'], (string)bcmul((string)$item['cart_num'], (string)$unit_price, 4), 2);
  87. }
  88. }
  89. $goodsStr .= "<td>{$item['productInfo']['store_name']} | {$item['productInfo']['attrInfo']['suk']}</td><td>{$item['cart_num']}</td><td>{$unit_price}</td><td>{$price}</td>";
  90. $goodsStr .= '</tr>';
  91. unset($price, $unit_price);
  92. }
  93. $goodsStr .= '</table>';
  94. $orderInfo = $config['orderInfo'];
  95. $name = $config['name'];
  96. $discountPrice = (float)bcsub((string)bcadd((string)$orderInfo['total_price'], $orderInfo['pay_postage'], 2), (string)bcadd((string)$orderInfo['deduction_price'], $orderInfo['pay_price'], 2), 2);
  97. $this->printerContent = <<<CONTENT
  98. <FB><center> ** {$name} **</center></FB>
  99. <FH2><FW2>----------------</FW2></FH2>
  100. 订单编号:{$orderInfo['order_id']}\r
  101. 日 期: {$timeYmd} \r
  102. 时 间: {$timeHis}\r
  103. 姓 名: {$orderInfo['real_name']}\r
  104. 电 话: {$orderInfo['user_phone']}\r
  105. 地 址: {$orderInfo['user_address']}\r
  106. 赠送积分: {$orderInfo['gain_integral']}\r
  107. 订单备注:{$orderInfo['mark']}\r
  108. *************商品***************\r
  109. {$goodsStr}
  110. ********************************\r
  111. <FH>
  112. <LR>合计:¥{$orderInfo['total_price']},优惠: ¥{$discountPrice}</LR>
  113. <LR>邮费:¥{$orderInfo['pay_postage']},抵扣:¥{$orderInfo['deduction_price']}</LR>
  114. <right>实际支付:¥{$orderInfo['pay_price']}</right>
  115. </FH>
  116. <FS><center> ** 完 **</center></FS>
  117. CONTENT;
  118. return $this;
  119. }
  120. /**
  121. * 设置桌码打印内容
  122. * @param array $config
  123. * @return YiLianYun
  124. */
  125. public function setPrinterTableContent(array $config): self
  126. {
  127. $timeYmd = date('Y-m-d', time());
  128. $timeHis = date('H:i:s', time());
  129. $goodsStr = '<table><tr><td>商品名称</td><td>数量</td><td>金额</td><td>小计</td></tr>';
  130. $product = $config['product'];
  131. $sumPrice = 0;
  132. foreach ($product as $item) {
  133. $goodsStr .= '<tr>';
  134. if (isset($item['sum_price'])) {
  135. $unit_price = $item['sum_price'];
  136. $price = bcmul((string)$item['cart_num'], (string)$unit_price, 2);
  137. } else {
  138. $unit_price = $item['truePrice'];
  139. $price = bcadd((string)$item['vip_truePrice'], (string)bcmul((string)$item['cart_num'], (string)$unit_price, 4), 2);
  140. }
  141. $goodsStr .= "<td>{$item['productInfo']['store_name']} | {$item['productInfo']['attrInfo']['suk']}</td><td>{$item['cart_num']}</td><td>{$unit_price}</td><td>{$price}</td>";
  142. $goodsStr .= '</tr>';
  143. $sumPrice = bcadd((string)$sumPrice, (string)$price, 2);
  144. unset($price, $unit_price);
  145. }
  146. $goodsStr .= '</table>';
  147. $tableInfo = $config['tableInfo'];
  148. $name = $config['name'];
  149. /** @var TableQrcodeServices $qrcodeService */
  150. $qrcodeService = app()->make(TableQrcodeServices::class);
  151. $Info = $qrcodeService->getQrcodeyInfo((int)$tableInfo['qrcode_id'], ['cateName']);
  152. $this->printerContent = <<<CONTENT
  153. <FB><center> ** {$name} **</center></FB>
  154. <FH2><FW2>----------------</FW2></FH2>
  155. 桌码流水:{$tableInfo['serial_number']}\r
  156. 桌码分类: {$Info['cateName']['name']} \r
  157. 桌码编号: {$Info['table_number']} \r
  158. 日 期: {$timeYmd}\r
  159. 时 间: {$timeHis}\r
  160. *************商品***************\r
  161. {$goodsStr}
  162. ********************************\r
  163. <FH>
  164. <right>商品金额:¥{$sumPrice}</right>
  165. </FH>
  166. <FS><center> ** 完 **</center></FS>
  167. CONTENT;
  168. return $this;
  169. }
  170. /**
  171. * 积分商城打印内容
  172. * @param array $config
  173. * @return YiLianYun
  174. */
  175. public function setIntegralPrinterContent(array $config): self
  176. {
  177. $timeYmd = date('Y-m-d', time());
  178. $timeHis = date('H:i:s', time());
  179. $goodsStr = '<table><tr><td>商品名称</td><td>数量</td><td>单价</td><td>总积分</td></tr>';
  180. $goodsStr .= '<tr>';
  181. $goodsStr .= "<td>{$config['orderInfo']['store_name']}</td><td>{$config['orderInfo']['total_num']}</td><td>{$config['orderInfo']['price']}</td><td>{$config['orderInfo']['total_price']}</td>";
  182. $goodsStr .= '</tr>';
  183. $goodsStr .= '</table>';
  184. $orderInfo = $config['orderInfo'];
  185. $name = $config['name'];
  186. $this->printerContent = <<<CONTENT
  187. <FB><center> ** {$name} **</center></FB>
  188. <FH2><FW2>----------------</FW2></FH2>
  189. 订单编号:{$orderInfo['order_id']}\r
  190. 日 期: {$timeYmd} \r
  191. 时 间: {$timeHis}\r
  192. 姓 名: {$orderInfo['real_name']}\r
  193. 电 话: {$orderInfo['user_phone']}\r
  194. 地 址: {$orderInfo['user_address']}\r
  195. 订单备注:{$orderInfo['mark']}\r
  196. *************商品***************\r
  197. {$goodsStr}
  198. ********************************\r
  199. <FH>
  200. <right>合计:{$orderInfo['total_price']}积分</right>
  201. </FH>
  202. <FS><center> ** 完 **</center></FS>
  203. CONTENT;
  204. return $this;
  205. }
  206. }