PrinterFeie.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. //===========添加打印机接口(支持批量)=============
  3. //***接口返回值说明***
  4. //正确例子:{"msg":"ok","ret":0,"data":{"ok":["sn#key#remark#carnum","316500011#abcdefgh#快餐前台"],"no":["316500012#abcdefgh#快餐前台#13688889999 (错误:识别码不正确)"]},"serverExecutedTime":3}
  5. //错误:{"msg":"参数错误 : 该帐号未注册.","ret":-2,"data":null,"serverExecutedTime":37}
  6. //打开注释可测试
  7. //提示:打印机编号(必填) # 打印机识别码(必填) # 备注名称(选填) # 流量卡号码(选填),多台打印机请换行(\n)添加新打印机信息,每次最多100行(台)。
  8. //$snlist = "sn1#key1#remark1#carnum1\nsn2#key2#remark2#carnum2";
  9. //addprinter($snlist);
  10. //==================方法1.打印订单==================
  11. //***接口返回值说明***
  12. //正确例子:{"msg":"ok","ret":0,"data":"316500004_20160823165104_1853029628","serverExecutedTime":6}
  13. //错误:{"msg":"错误信息.","ret":非零错误码,"data":null,"serverExecutedTime":5}
  14. //标签说明:
  15. //单标签:
  16. //"<BR>"为换行,"<CUT>"为切刀指令(主动切纸,仅限切刀打印机使用才有效果)
  17. //"<LOGO>"为打印LOGO指令(前提是预先在机器内置LOGO图片),"<PLUGIN>"为钱箱或者外置音响指令
  18. //成对标签:
  19. //"<CB></CB>"为居中放大一倍,"<B></B>"为放大一倍,"<C></C>"为居中,<L></L>字体变高一倍
  20. //<W></W>字体变宽一倍,"<QR></QR>"为二维码,"<BOLD></BOLD>"为字体加粗,"<RIGHT></RIGHT>"为右对齐
  21. //拼凑订单内容时可参考如下格式
  22. //根据打印纸张的宽度,自行调整内容的格式,可参考下面的样例格式
  23. //打开注释可测试
  24. //wp_print(SN,$orderInfo,1);
  25. //===========方法2.查询某订单是否打印成功=============
  26. //***接口返回值说明***
  27. //正确例子:
  28. //已打印:{"msg":"ok","ret":0,"data":true,"serverExecutedTime":6}
  29. //未打印:{"msg":"ok","ret":0,"data":false,"serverExecutedTime":6}
  30. //打开注释可测试
  31. //$orderid = "xxxxxxxx_xxxxxxxxxx_xxxxxxxx";//订单ID,从方法1返回值中获取
  32. //queryOrderState($orderid);
  33. //===========方法3.查询指定打印机某天的订单详情============
  34. //***接口返回值说明***
  35. //正确例子:{"msg":"ok","ret":0,"data":{"print":6,"waiting":1},"serverExecutedTime":9}
  36. //打开注释可测试
  37. //$date = "2017-04-02";//注意时间格式为"yyyy-MM-dd",如2016-08-27
  38. //queryOrderInfoByDate(SN,$date);
  39. //===========方法4.查询打印机的状态==========================
  40. //***接口返回值说明***
  41. //正确例子:
  42. //{"msg":"ok","ret":0,"data":"离线","serverExecutedTime":9}
  43. //{"msg":"ok","ret":0,"data":"在线,工作状态正常","serverExecutedTime":9}
  44. //{"msg":"ok","ret":0,"data":"在线,工作状态不正常","serverExecutedTime":9}
  45. //打开注释可测试
  46. //queryPrinterStatus(SN);
  47. namespace app\model;
  48. use think\exception\ValidateException;
  49. class PrinterFeie
  50. {
  51. private $user; //*必填*:飞鹅云后台注册账号
  52. private $ukey; //*必填*: 飞鹅云注册账号后生成的UKEY
  53. private $sn; //打印机编号
  54. private $times; //打印联数
  55. //以下参数不需要修改
  56. private $ip = 'api.feieyun.cn'; //接口IP或域名
  57. private $port = 80; //接口IP端口
  58. private $path = '/Api/Open/'; //接口路径
  59. public function __construct($sid = 0)
  60. {
  61. $Printer = Printer::where(['weid' => weid(), 'sid' => (int) $sid])->order('id desc')->find();
  62. if (!empty($Printer->settings)) {
  63. $settings = iunserializer($Printer->settings);
  64. $this->user = $settings['user'];
  65. $this->ukey = $settings['ukey'];
  66. $this->sn = $settings['sn'];
  67. $this->times = (int) $settings['times'];
  68. if (empty($this->times)) {
  69. $this->times = 1;
  70. }
  71. }
  72. }
  73. //飞鹅打印机
  74. public function printer($data)
  75. {
  76. $order = $data['orderInfo'];
  77. $addtime = $order['create_time'];
  78. $content = "";
  79. $content .= "<CB></CB><BR>";
  80. $content .= "订单号:{$order['order_num_alias']}<BR>";
  81. $content .= "支付方式:" . paymentCode($order['payment_code']) . "<BR>";
  82. $content .= "订单状态:" . OrderStatus::get_order_status_name($order['order_status_id']) . "<BR>";
  83. $content .= "发货方式:" . $order['shipping_method'] . "<BR>";
  84. $content .= "下单时间:{$addtime}<BR>";
  85. $content .= "<BR>";
  86. $content .= "-------------订单商品-----------<BR>";
  87. $content .= "<BR>";
  88. if (isset($data['goods']) && is_array($data['goods'])) {
  89. foreach ($data['goods'] as $goods) {
  90. $content .= "商品名称:{$goods['name']}<BR>";
  91. $content .= "规格:{$goods['label']}<BR>";
  92. $content .= "单价:{$goods['price']}元<BR>";
  93. $content .= "数量:{$goods['quantity']}<BR>";
  94. $content .= "<BR>";
  95. }
  96. }
  97. $content .= "--------------------------------<BR>";
  98. if ($order['express_price']) {
  99. $content .= "运费:{$order['express_price']}元<BR>";
  100. }
  101. $content .= "总计:{$order['total']}元<BR>";
  102. if (isset($order['user_coupon_id'])) {
  103. $content .= "优惠券优惠:{$order['coupon_sub_price']}元<BR>";
  104. }
  105. if (isset($order['integral'])) {
  106. $integral = json_decode($order['integral'], true);
  107. if ($integral['forehead'] != 0) {
  108. $content .= "积分抵扣:{$integral['forehead']}元<BR>";
  109. }
  110. }
  111. if (isset($order['discount']) && $order['discount'] < 10) {
  112. $content .= "会员折扣:{$order['discount']}折<BR>";
  113. }
  114. //$content .= "实付:{$order['pay_price']}元<BR>";
  115. if (!empty($order['shipping_name'])) {
  116. $content .= "联系人:{$order['shipping_name']}<BR>";
  117. } else {
  118. $content .= "联系人:{$order['member']['nickname']}<BR>";
  119. }
  120. if (!empty($order['shipping_tel'])) {
  121. $content .= "联系电话:{$order['shipping_tel']}<BR>";
  122. }else{
  123. $content .= "联系电话:{$order['member']['telephone']}<BR>";
  124. }
  125. $content .= "地址:{$order['shipping_province']}{$order['shipping_city']}{$order['shipping_district']}{$order['address']['address']}<BR>";
  126. $content .= "--------------------------------<BR>";
  127. if ($order['content']) {
  128. $content .= "备注:{$order['content']}<BR>";
  129. }
  130. $resule = $this->wp_print($content);
  131. //$resule = iunserializer($resule);
  132. $result = json_decode($resule, true);
  133. //var_dump($result);
  134. if (!empty($result['ret'])) {
  135. //throw new ValidateException($result['msg']);
  136. Test::create(['title' => '小票打印机错误记录', 'info' => serialize($result)]);
  137. }
  138. return $result;
  139. }
  140. function addprinter($snlist)
  141. {
  142. $time = time(); //请求时间
  143. $content = array(
  144. 'user' => $this->user,
  145. 'stime' => $time,
  146. 'sig' => $this->signature($time),
  147. 'apiname' => 'Open_printerAddlist',
  148. 'printerContent' => $snlist
  149. );
  150. $client = new PrinterFeieHttpClient($this->ip, $this->port);
  151. if (!$client->post($this->path, $content)) {
  152. echo 'error';
  153. } else {
  154. echo $client->getContent();
  155. }
  156. }
  157. /*
  158. * 方法1
  159. 拼凑订单内容时可参考如下格式
  160. 根据打印纸张的宽度,自行调整内容的格式,可参考下面的样例格式
  161. */
  162. function wp_print($orderInfo)
  163. {
  164. $time = time(); //请求时间
  165. $content = array(
  166. 'user' => $this->user,
  167. 'stime' => $time,
  168. 'sig' => $this->signature($time),
  169. 'apiname' => 'Open_printMsg',
  170. 'sn' => $this->sn,
  171. 'content' => $orderInfo,
  172. 'times' => $this->times //打印次数
  173. );
  174. $client = new PrinterFeieHttpClient($this->ip, $this->port);
  175. if (!$client->post($this->path, $content)) {
  176. return 'error';
  177. } else {
  178. //服务器返回的JSON字符串,建议要当做日志记录起来
  179. return $client->getContent();
  180. }
  181. }
  182. /*
  183. * 方法2
  184. 根据订单索引,去查询订单是否打印成功,订单索引由方法1返回
  185. */
  186. function queryOrderState($index)
  187. {
  188. $time = time(); //请求时间
  189. $msgInfo = array(
  190. 'user' => $this->user,
  191. 'stime' => $time,
  192. 'sig' => $this->signature($time),
  193. 'apiname' => 'Open_queryOrderState',
  194. 'orderid' => $index
  195. );
  196. $client = new PrinterFeieHttpClient($this->ip, $this->port);
  197. if (!$client->post($this->path, $msgInfo)) {
  198. echo 'error';
  199. } else {
  200. $result = $client->getContent();
  201. echo $result;
  202. }
  203. }
  204. /*
  205. * 方法3
  206. 查询指定打印机某天的订单详情
  207. */
  208. function queryOrderInfoByDate($printer_sn, $date)
  209. {
  210. $time = time(); //请求时间
  211. $msgInfo = array(
  212. 'user' => $this->user,
  213. 'stime' => $time,
  214. 'sig' => $this->signature($time),
  215. 'apiname' => 'Open_queryOrderInfoByDate',
  216. 'sn' => $printer_sn,
  217. 'date' => $date
  218. );
  219. $client = new PrinterFeieHttpClient($this->ip, $this->port);
  220. if (!$client->post($this->path, $msgInfo)) {
  221. echo 'error';
  222. } else {
  223. $result = $client->getContent();
  224. echo $result;
  225. }
  226. }
  227. /*
  228. * 方法4
  229. 查询打印机的状态
  230. */
  231. function queryPrinterStatus($printer_sn)
  232. {
  233. $time = time(); //请求时间
  234. $msgInfo = array(
  235. 'user' => $this->user,
  236. 'stime' => $time,
  237. 'sig' => $this->signature($time),
  238. 'apiname' => 'Open_queryPrinterStatus',
  239. 'sn' => $printer_sn
  240. );
  241. $client = new PrinterFeieHttpClient($this->ip, $this->port);
  242. if (!$client->post($this->path, $msgInfo)) {
  243. echo 'error';
  244. } else {
  245. $result = $client->getContent();
  246. echo $result;
  247. }
  248. }
  249. //生成签名
  250. function signature($time)
  251. {
  252. return sha1($this->user . $this->ukey . $time); //公共参数,请求公钥
  253. }
  254. }