PrinterOrder.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * =========================================================
  9. */
  10. namespace addon\printer\model;
  11. use addon\printer\data\sdk\yilianyun\api\PrintService;
  12. use addon\printer\data\sdk\yilianyun\config\YlyConfig;
  13. use app\model\BaseModel;
  14. use think\facade\Log;
  15. class PrinterOrder extends BaseModel
  16. {
  17. /************************************************ 正式打印 start ******************************************************************/
  18. /**
  19. * 打印
  20. */
  21. public function printer($data)
  22. {
  23. // Log::write('printer' . json_encode($data));
  24. $items = event('PrinterContent', $data, true);
  25. // Log::write('data:' . json_encode($items));
  26. if (empty($items)) return $this->error('', '没有可用的打印机');
  27. if (isset($items[ 'code' ]) && $items[ 'code' ] != 0) return $items;
  28. $printer_model = new Printer();
  29. foreach ($items as $k => $v) {
  30. $printer = $v[ 'printer_info' ] ?? [];
  31. switch ( $printer[ 'brand' ] ) {
  32. case '365'://365打印机
  33. break;
  34. case 'feie'://飞鹅打印机
  35. break;
  36. case 'yilianyun'://易联云打印机
  37. try {
  38. $config = new YlyConfig($printer[ 'open_id' ], $printer[ 'apikey' ]);
  39. $access_token = $printer_model->getYlyToken($config, $printer[ 'site_id' ]);
  40. $machine_code = $v[ 'printer_code' ];//商户授权机器码
  41. $origin_id = $v[ 'origin_id' ]; //内部订单号(32位以内)
  42. $print = new PrintService($access_token, $config);
  43. $content = $v[ 'content' ];
  44. $print->index($machine_code, $content, $origin_id, $printer);
  45. } catch (\Exception $e) {
  46. return $this->error('', $e->getMessage());
  47. }
  48. break;
  49. }
  50. }
  51. return $this->success();
  52. }
  53. /**
  54. * 订单打印
  55. */
  56. public function printOrder($data)
  57. {
  58. // //获取订单详情
  59. // $order_common_model = new OrderCommonModel();
  60. // $order_detail_result = $order_common_model->getOrderDetail($order_id);
  61. // $order_info = $order_detail_result['data'];
  62. //
  63. // if (empty($order_info)) {
  64. // return $this->success();
  65. // }
  66. //
  67. // //获取店铺信息
  68. // $shop_model = new Shop();
  69. // $shop = $shop_model->getShopInfo([['site_id', '=', $order_info['site_id']]]);
  70. // $shop_info = $shop['data'];
  71. //
  72. // //获取打印机列表
  73. // $print_model = new Printer();
  74. // $printer_data = $print_model->getPrinterList([['site_id', '=', $order_info['site_id']],['store_id','=',0]]);
  75. //
  76. // if($order_info['delivery_store_id'] == 0){
  77. // $store_printer_data['data'] = [];
  78. // }else{
  79. // $store_printer_data = $print_model->getPrinterList([['site_id', '=', $order_info['site_id']], ['order_type', 'like', '%,' . $order_info['order_type'] . ',%'],['store_id','=',$order_info['delivery_store_id']]]);
  80. // }
  81. // if (empty($printer_data['data']) && empty($store_printer_data['data'])) {
  82. // return $this->success();
  83. // }
  84. //
  85. // $printer_list = array_merge($printer_data['data'],$store_printer_data['data']);
  86. // foreach ($printer_list as $v) {
  87. //
  88. // switch ($v['brand']) {
  89. //
  90. // case '365'://365打印机
  91. //
  92. // break;
  93. // case 'feie'://飞鹅打印机
  94. //
  95. // break;
  96. // case 'yilianyun'://易联云打印机
  97. //
  98. // $this->Ylyprint($order_info, $v, $shop_info);
  99. // break;
  100. // }
  101. // }
  102. // return $this->success();
  103. }
  104. /**
  105. * 易联云打印
  106. * @param $order_info
  107. * @param $printer
  108. * @param $shop_info
  109. */
  110. public function Ylyprint($order_info, $printer, $shop_info)
  111. {
  112. //打印模板
  113. $print_template_model = new PrinterTemplate();
  114. $print_template_data = $print_template_model->getPrinterTemplateInfo([ [ 'template_id', '=', $printer[ 'template_id' ] ] ]);
  115. $print_template = $print_template_data[ 'data' ];
  116. $config = new YlyConfig($printer[ 'open_id' ], $printer[ 'apikey' ]);
  117. $printer_model = new Printer();
  118. $access_token = $printer_model->getYlyToken($config, $printer[ 'site_id' ]);
  119. $machine_code = $printer[ 'printer_code' ]; //商户授权机器码
  120. $origin_id = $order_info[ 'order_no' ]; //内部订单号(32位以内)
  121. /**文本接口开始**/
  122. $print = new PrintService($access_token, $config);
  123. $content = "<MN>" . $printer[ 'print_num' ] . "</MN>";
  124. //小票名称
  125. if ($print_template[ 'title' ] != '') {
  126. $content .= "<center>" . $print_template[ 'title' ] . "</center>";
  127. $content .= str_repeat('.', 32);
  128. }
  129. //商城名称
  130. if ($print_template[ 'head' ] == 1) {
  131. $content .= "<FH2><FS><center>" . $print_template[ 'site_name' ] . "</center></FS></FH2>";
  132. $content .= str_repeat('.', 32);
  133. }
  134. if(!empty($order_info[ 'pay_time' ])) {
  135. $content .= "订单时间:" . date("Y-m-d H:i", $order_info[ 'pay_time' ]) . "\n";
  136. }else{
  137. $content .= "订单时间:" . date("Y-m-d H:i", time()) . "\n";
  138. }
  139. $content .= "订单编号:" . $order_info[ 'order_no' ] . "\n";
  140. $content .= str_repeat('.', 32);
  141. $content .= "<table>";
  142. $content .= "<tr><td>商品名称</td><td></td><td>数量</td><td>金额</td></tr>";
  143. $content .= "</table>";
  144. $content .= str_repeat('.', 32);
  145. $content .= "<table>";
  146. foreach ($order_info[ 'order_goods' ] as $goods) {
  147. $sku_name_list = $this->r_str_pad_1($goods[ 'sku_name' ], 7);
  148. foreach ($sku_name_list as $index => $value) {
  149. if ($index == 0) {
  150. $content .= "<tr><td>" . $value . "</td><td></td><td>x" . numberFormat($goods[ 'num' ]) . "</td><td>¥" . $goods[ 'price' ] . "</td></tr>";
  151. } else {
  152. $content .= "<tr><td>" . $value . "</td></tr>";
  153. }
  154. }
  155. }
  156. $content .= "</table>";
  157. $content .= str_repeat('.', 32);
  158. if ($order_info[ "goods_money" ] > 0) {
  159. $content .= "商品总额:¥" . $order_info[ "goods_money" ] . "\n";
  160. }
  161. if ($order_info[ "coupon_money" ] > 0) {
  162. $content .= "店铺优惠券:¥" . $order_info[ "coupon_money" ] . "\n";
  163. }
  164. if ($order_info[ "promotion_money" ] > 0) {
  165. $content .= "店铺优惠:¥" . $order_info[ "promotion_money" ] . "\n";
  166. }
  167. if ($order_info[ "point_money" ] > 0) {
  168. $content .= "积分抵扣:¥" . $order_info[ "point_money" ] . "\n";
  169. }
  170. if ($order_info[ "adjust_money" ] > 0) {
  171. $content .= "订单调价:¥" . $order_info[ "adjust_money" ] . "\n";
  172. }
  173. if ($order_info[ "delivery_money" ] > 0) {
  174. $content .= "配送费用:¥" . $order_info[ "delivery_money" ] . "\n";
  175. }
  176. if ($order_info[ "invoice_money" ] > 0) {
  177. $content .= "发票费用:¥" . $order_info[ "invoice_money" ] . "\n";
  178. }
  179. if ($order_info[ "invoice_delivery_money" ] > 0) {
  180. $content .= "发票邮寄费用:¥" . $order_info[ "invoice_delivery_money" ] . "\n";
  181. }
  182. if ($order_info[ "goods_num" ] > 0) {
  183. $content .= "订单共" . numberFormat($order_info[ 'goods_num' ]) . "件商品,总计: ¥" . $order_info[ 'order_money' ] . " \n";
  184. }
  185. $content .= str_repeat('.', 32);
  186. /******************** 备注信息 **************************/
  187. //买家留言
  188. if ($print_template[ 'buy_notes' ] == 1) {
  189. $content .= "<FH2>买家留言:" . $order_info[ "buyer_message" ] . "</FH2>\n";
  190. $content .= str_repeat('.', 32);
  191. }
  192. //卖家留言
  193. if ($print_template[ 'seller_notes' ] == 1) {
  194. $content .= "<FH2>卖家留言:" . $order_info[ "remark" ] . "</FH2>\n";
  195. $content .= str_repeat('.', 32);
  196. }
  197. /******************** 买家信息 **************************/
  198. //买家姓名
  199. if ($print_template[ 'buy_name' ] == 1) {
  200. $content .= "" . $order_info[ "name" ] . "\n";
  201. }
  202. //联系方式
  203. if ($print_template[ 'buy_mobile' ] == 1) {
  204. $content .= "" . $order_info[ "mobile" ] . "\n";
  205. }
  206. //地址
  207. if ($print_template[ 'buy_address' ] == 1) {
  208. $content .= "" . $order_info[ 'full_address' ] . "-" . $order_info[ 'address' ] . "\n";
  209. }
  210. if ($print_template[ 'buy_name' ] == 1 || $print_template[ 'buy_mobile' ] == 1 || $print_template[ 'buy_address' ] == 1) {
  211. $content .= str_repeat('.', 32);
  212. }
  213. /******************** 商城信息 **************************/
  214. //联系方式
  215. if ($print_template[ 'shop_mobile' ] == 1) {
  216. $content .= "" . $shop_info[ "mobile" ] . "\n";
  217. }
  218. //地址
  219. if ($print_template[ 'shop_address' ] == 1) {
  220. $content .= "" . $shop_info[ 'province_name' ] . $shop_info[ 'city_name' ] . $shop_info[ 'district_name' ] . $shop_info[ 'address' ] . "\n";
  221. }
  222. if ($print_template[ 'shop_mobile' ] == 1 || $print_template[ 'shop_address' ] == 1) {
  223. $content .= str_repeat('.', 32);
  224. }
  225. //二维码
  226. if ($print_template[ 'shop_qrcode' ] == 1) {
  227. $content .= "<QR>" . $print_template[ 'qrcode_url' ] . "</QR>";
  228. $content .= str_repeat('.', 32);
  229. }
  230. /******************** 门店信息 **************************/
  231. if ($order_info[ 'delivery_store_id' ] > 0 && !empty($order_info[ 'delivery_store_name' ]) && !empty($order_info[ 'delivery_store_info' ])) {
  232. $store_info = json_decode($order_info[ 'delivery_store_info' ], true);
  233. $content .= "" . $order_info[ "delivery_store_name" ] . "\n";//门店名称
  234. $content .= "" . $store_info[ "telphone" ] . "\n";//门店电话
  235. $content .= "" . $store_info[ "full_address" ] . "\n";//门店地址
  236. $content .= str_repeat('.', 32);
  237. }
  238. //底部内容
  239. if (!empty($print_template[ 'bottom' ])) {
  240. $content .= "<center>" . $print_template[ 'bottom' ] . "</center>";
  241. }
  242. try {
  243. $print->index($machine_code, $content, $origin_id);
  244. } catch (\Exception $e) {
  245. echo $e->getMessage();
  246. }
  247. }
  248. /**
  249. * 补齐空格
  250. * 每n个中文字符长度为一个数组元素
  251. */
  252. public function r_str_pad_1($input, $n = 7)
  253. {
  254. $string = "";
  255. $count = 0;
  256. $c_count = 0;
  257. $arr = array ();
  258. for ($i = 0; $i < mb_strlen($input, 'UTF-8'); $i++) {
  259. $char = mb_substr($input, $i, 1, 'UTF-8');
  260. $string .= $char;
  261. if (strlen($char) == 3) {
  262. $count += 2;
  263. $c_count++;
  264. } else {
  265. $count += 1;
  266. }
  267. if ($count >= $n * 2) {
  268. $arr[] = $string;
  269. $string = '';
  270. $count = 0;
  271. $c_count = 0;
  272. }
  273. }
  274. if ($count < $n * 2) {
  275. $string = str_pad($string, $n * 2 + $c_count);
  276. $arr[] = $string;
  277. }
  278. return $arr;
  279. }
  280. /************************************************ 正式打印 end ******************************************************************/
  281. /************************************************ 测试打印 start ******************************************************************/
  282. /**
  283. * 测试打印
  284. * @param $printer_id
  285. * @param $site_id
  286. * @return array
  287. */
  288. public function testPrint($printer_id, $site_id)
  289. {
  290. //获取打印机列表
  291. $printer_info = model('printer')->getInfo([ [ 'site_id', '=', $site_id ], [ 'printer_id', '=', $printer_id ] ]);
  292. if (empty($printer_info)) {
  293. return $this->success();
  294. }
  295. switch ( $printer_info[ 'brand' ] ) {
  296. case '365'://365打印机
  297. break;
  298. case 'feie'://飞鹅打印机
  299. break;
  300. case 'yilianyun'://易联云打印机
  301. $res = $this->testYlyprint($printer_info);
  302. break;
  303. }
  304. return $res;
  305. }
  306. /**
  307. *
  308. * @param $printer_info
  309. */
  310. public function testYlyprint($printer)
  311. {
  312. $config = new YlyConfig($printer[ 'open_id' ], $printer[ 'apikey' ]);
  313. $printer_model = new Printer();
  314. $access_token = $printer_model->getYlyToken($config, $printer[ 'site_id' ]);
  315. $machine_code = $printer[ 'printer_code' ]; //商户授权机器码
  316. $origin_id = date('YmdHis') . rand(1, 999); //内部订单号(32位以内)
  317. /**文本接口开始**/
  318. $print = new PrintService($access_token, $config);
  319. $content = "<MN>" . $printer[ 'print_num' ] . "</MN>";
  320. $content .= "<center>小票名称</center>";
  321. $content .= str_repeat('.', 32);
  322. $content .= "<FH2><FS><center>商城名称</center></FS></FH2>";
  323. $content .= str_repeat('.', 32);
  324. $content .= "订单时间:" . date("Y-m-d H:i") . "\n";
  325. $content .= "订单编号:" . $origin_id . "\n";
  326. $content .= str_repeat('.', 32);
  327. $content .= "<table>";
  328. $content .= "<tr><td>商品名称</td><td></td><td>数量</td><td>金额</td></tr>";
  329. $content .= "</table>";
  330. $content .= str_repeat('.', 32);
  331. $content .= "<table>";
  332. $content .= "<tr><td>烤土豆(超级辣)</td><td></td><td>x3</td><td>5</td></tr>";
  333. $content .= "<tr><td>烤豆干(超级辣)</td><td></td><td>x2</td><td>10</td></tr>";
  334. $content .= "<tr><td>烤鸡翅(超级辣)</td><td></td><td>x3</td><td>15</td></tr>";
  335. $content .= "</table>";
  336. $content .= str_repeat('.', 32);
  337. $content .= "商品总额:¥30 \n";
  338. $content .= "订单共8件商品,总计: ¥30 \n";
  339. $content .= str_repeat('.', 32);
  340. /******************** 备注信息 **************************/
  341. //买家留言
  342. $content .= "<FH2>买家留言:微辣,多放孜然</FH2>\n";
  343. $content .= str_repeat('.', 32);
  344. $content .= "<center>谢谢惠顾,欢迎下次光临</center>";
  345. try {
  346. $res = $print->index($machine_code, $content, $origin_id);
  347. if ($res->error == 0) {
  348. return $this->success();
  349. } else {
  350. return $this->error('', $res->error_description);
  351. }
  352. } catch (\Exception $e) {
  353. return $this->error('', $e->getMessage());
  354. }
  355. }
  356. }