Feieyun1.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. <?php
  2. namespace Mshop\Lib;
  3. use Dever;
  4. class Feieyun
  5. {
  6. private $config = array
  7. (
  8. //飞鹅云后台注册账号
  9. 'user' => 'liuxuemei@zsma.com.cn',
  10. //飞鹅云后台注册账号后生成的UKEY
  11. 'ukey' => 'pMsXhZWu5F65MYyV',
  12. //打印机编号,必须要在管理后台里添加打印机或调用API接口添加之后,才能调用API
  13. 'sn' => '',
  14. //host地址
  15. 'host' => 'http://api.feieyun.cn:80/Api/Open/',
  16. );
  17. # 生成参数
  18. public function param(&$param)
  19. {
  20. $time = time();
  21. $param['user'] = $this->config['user'];
  22. $param['stime'] = $time;
  23. $param['sig'] = sha1($this->config['user'] . $this->config['ukey'] . $time);
  24. }
  25. # 发送
  26. public function send()
  27. {
  28. $url = $this->config['host'];
  29. $param = $this->param($param);
  30. return Dever::curl($url, $param, 'post');
  31. }
  32. # 添加打印机
  33. public function add()
  34. {
  35. $msgInfo = array(
  36. 'user'=>USER,
  37. 'stime'=>$time,
  38. 'sig'=>signature($time),
  39. 'apiname'=>'Open_printerAddlist',
  40. 'printerContent'=>$printerContent
  41. );
  42. $client = new HttpClient(IP,PORT);
  43. if(!$client->post(PATH,$msgInfo)){
  44. echo 'error';
  45. }else{
  46. $result = $client->getContent();
  47. echo $result;
  48. }
  49. }
  50. <?php
  51. header("Content-type: text/html; charset=utf-8");
  52. include 'HttpClient.class.php';
  53. define('USER', 'xxxxxxxxxxxxxxxxx'); //*必填*:飞鹅云后台注册账号
  54. define('UKEY', 'xxxxxxxxxxxxxxxxx'); //*必填*: 飞鹅云后台注册账号后生成的UKEY 【备注:这不是填打印机的KEY】
  55. define('SN', 'xxxxxxxxxxxxxxxxx'); //*必填*:打印机编号,必须要在管理后台里添加打印机或调用API接口添加之后,才能调用API
  56. //以下参数不需要修改
  57. define('IP','api.feieyun.cn'); //接口IP或域名
  58. define('PORT',80); //接口IP端口
  59. define('PATH','/Api/Open/'); //接口路径
  60. //*************************************方法1 批量添加打印机接口*************************************************
  61. //***接口返回值说明***
  62. //正确例子:{"msg":"ok","ret":0,"data":{"ok":["sn#key#remark#carnum","316500011#abcdefgh#快餐前台"],"no":["316500012#abcdefgh#快餐前台#13688889999 (错误:识别码不正确)"]},"serverExecutedTime":3}
  63. //错误:{"msg":"参数错误 : 该帐号未注册.","ret":-2,"data":null,"serverExecutedTime":37}
  64. //提示:
  65. //$printerConten => 打印机编号sn(必填) # 打印机识别码key(必填) # 备注名称(选填) # 流量卡号码(选填),多台打印机请换行(\n)添加新打印机信息,每次最多100台。
  66. //打开注释可测试
  67. //$printerContent = "sn1#key1#remark1#carnum1\nsn2#key2#remark2#carnum2";
  68. //printerAddlist($printerContent);
  69. //****************************************方法2 小票机打印订单接口****************************************************
  70. //***接口返回值说明***
  71. //正确例子:{"msg":"ok","ret":0,"data":"123456789_20160823165104_1853029628","serverExecutedTime":6}
  72. //错误例子:{"msg":"错误信息.","ret":非零错误码,"data":null,"serverExecutedTime":5}
  73. //标签说明:
  74. //单标签:
  75. //"<BR>"为换行,"<CUT>"为切刀指令(主动切纸,仅限切刀打印机使用才有效果)
  76. //"<LOGO>"为打印LOGO指令(前提是预先在机器内置LOGO图片),"<PLUGIN>"为钱箱或者外置音响指令
  77. //成对标签:
  78. //"<CB></CB>"为居中放大一倍,"<B></B>"为放大一倍,"<C></C>"为居中,<L></L>字体变高一倍
  79. //<W></W>字体变宽一倍,"<QR></QR>"为二维码,"<BOLD></BOLD>"为字体加粗,"<RIGHT></RIGHT>"为右对齐
  80. //拼凑订单内容时可参考如下格式
  81. //根据打印纸张的宽度,自行调整内容的格式,可参考下面的样例格式
  82. $content = '<CB>测试打印</CB><BR>';
  83. $content .= '名称      单价 数量 金额<BR>';
  84. $content .= '--------------------------------<BR>';
  85. $content .= '饭       10.0 10 100.0<BR>';
  86. $content .= '炒饭      10.0 10 100.0<BR>';
  87. $content .= '蛋炒饭     10.0 10 100.0<BR>';
  88. $content .= '鸡蛋炒饭    10.0 10 100.0<BR>';
  89. $content .= '西红柿炒饭   10.0 10 100.0<BR>';
  90. $content .= '西红柿蛋炒饭  10.0 10 100.0<BR>';
  91. $content .= '西红柿鸡蛋炒饭 10.0 10 100.0<BR>';
  92. $content .= '--------------------------------<BR>';
  93. $content .= '备注:加辣<BR>';
  94. $content .= '合计:xx.0元<BR>';
  95. $content .= '送货地点:广州市南沙区xx路xx号<BR>';
  96. $content .= '联系电话:13888888888888<BR>';
  97. $content .= '订餐时间:2014-08-08 08:08:08<BR>';
  98. $content .= '<QR>http://www.feieyun.com</QR>';//把二维码字符串用标签套上即可自动生成二维码
  99. //提示:
  100. //SN => 打印机编号
  101. //$content => 打印内容,不能超过5000字节
  102. //$times => 打印次数,默认为1。
  103. //打开注释可测试
  104. // printMsg(SN,$content,1);//该接口只能是小票机使用,如购买的是标签机请使用下面方法3,调用打印
  105. //****************************************方法3 标签机专用打印订单接口****************************************************
  106. //***接口返回值说明***
  107. //正确例子:{"msg":"ok","ret":0,"data":"123456789_20160823165104_1853029628","serverExecutedTime":6}
  108. //错误例子:{"msg":"错误信息.","ret":非零错误码,"data":null,"serverExecutedTime":5}
  109. //标签说明:
  110. $content = "<DIRECTION>1</DIRECTION>";//设定打印时出纸和打印字体的方向,n 0 或 1,每次设备重启后都会初始化为 0 值设置,1:正向出纸,0:反向出纸,
  111. $content .= "<TEXT x='9' y='10' font='12' w='1' h='2' r='0'>#001 五号桌 1/3</TEXT><TEXT x='80' y='80' font='12' w='2' h='2' r='0'>可乐鸡翅</TEXT><TEXT x='9' y='180' font='12' w='1' h='1' r='0'>张三先生 13800138000</TEXT>";//40mm宽度标签纸打印例子,打开注释调用标签打印接口打印
  112. //提示:
  113. //SN => 打印机编号
  114. //$content => 打印内容,不能超过5000字节
  115. //$times => 打印次数,默认为1。
  116. //打开注释可测试
  117. // printLabelMsg(SN,$content,1);//打开注释调用标签机打印接口进行打印,该接口只能是标签机使用,其它型号打印机请勿使用该接口
  118. //**************************************方法4 批量删除打印机**************************************************
  119. //***接口返回值说明***
  120. //成功:{"msg":"ok","ret":0,"data":{"ok":["123456789成功"],"no":[]},"serverExecutedTime":5}
  121. //错误:{"msg":"ok","ret":0,"data":{"ok":[],"no":["12345678打印机不存在"]},"serverExecutedTime":2}
  122. //错误:{"msg":"ok","ret":0,"data":{"ok":[],"no":["123456789用户UID不匹配"]},"serverExecutedTime":3}
  123. //提示:
  124. //$snlist => 打印机编号,多台打印机请用减号"-"连接起来。
  125. //打开注释可测试
  126. // $snlist = "123456789";
  127. // printerDelList($snlist);
  128. //************************************方法5 修改打印机信息接口************************************************
  129. //***接口返回值说明***
  130. //成功:{"msg":"ok","ret":0,"data":true,"serverExecutedTime":5}
  131. //错误:{"msg":"参数错误 : 参数值不能传空字符,\"\"、\"null\"、\"undefined\".","ret":-2,"data":null,"serverExecutedTime":1}
  132. //提示:
  133. //SN => 打印机编号
  134. //$name => 打印机备注名称
  135. //$phonenum => 打印机流量卡号码
  136. //打开注释可测试
  137. //$name = "飞鹅云打印机";
  138. //$phonenum = "01234567891011121314";
  139. // printerEdit(SN,$name,$phonenum);
  140. //************************************方法6 清空待打印订单接口************************************************
  141. //***接口返回值说明***
  142. //成功:{"msg":"ok","ret":0,"data":true,"serverExecutedTime":4}
  143. //错误:{"msg":"验证失败 : 打印机编号和用户不匹配.","ret":1002,"data":null,"serverExecutedTime":3}
  144. //错误:{"msg":"参数错误 : 参数值不能传空字符,\"\"、\"null\"、\"undefined\".","ret":-2,"data":null,"serverExecutedTime":2}
  145. //提示:
  146. //SN => 打印机编号
  147. //打开注释可测试
  148. // delPrinterSqs(SN);
  149. //*********************************方法7 查询订单是否打印成功接口*********************************************
  150. //***接口返回值说明***
  151. //正确例子:
  152. //已打印:{"msg":"ok","ret":0,"data":true,"serverExecutedTime":6}
  153. //未打印:{"msg":"ok","ret":0,"data":false,"serverExecutedTime":6}
  154. //提示:
  155. //$orderid => 订单ID,由方法1接口Open_printMsg返回。
  156. //打开注释可测试
  157. //$orderid = "123456789_20160823165104_1853029628";//订单ID,从方法1返回值中获取
  158. //queryOrderState($orderid);
  159. //*****************************方法8 查询指定打印机某天的订单统计数接口*****************************************
  160. //***接口返回值说明***
  161. //正确例子:
  162. //{"msg":"ok","ret":0,"data":{"print":6,"waiting":1},"serverExecutedTime":9}
  163. //错误:{"msg":"验证失败 : 打印机编号和用户不匹配.","ret":1002,"data":null,"serverExecutedTime":3}
  164. //提示:
  165. //$date => 查询日期,格式YY-MM-DD,如:2016-09-20
  166. //打开注释可测试
  167. // $date = "2016-09-20";
  168. // queryOrderInfoByDate(SN,$date);
  169. //***********************************方法9 获取某台打印机状态接口***********************************************
  170. //***接口返回值说明***
  171. //正确例子:
  172. //{"msg":"ok","ret":0,"data":"离线","serverExecutedTime":9}
  173. //{"msg":"ok","ret":0,"data":"在线,工作状态正常","serverExecutedTime":9}
  174. //{"msg":"ok","ret":0,"data":"在线,工作状态不正常","serverExecutedTime":9}
  175. //提示:
  176. //SN => 填打印机编号
  177. //打开注释可测试
  178. // queryPrinterStatus(SN);
  179. /**
  180. * [批量添加打印机接口 Open_printerAddlist]
  181. * @param [string] $printerContent [打印机的sn#key]
  182. * @return [string] [接口返回值]
  183. */
  184. function printerAddlist($printerContent){
  185. $time = time(); //请求时间
  186. $msgInfo = array(
  187. 'user'=>USER,
  188. 'stime'=>$time,
  189. 'sig'=>signature($time),
  190. 'apiname'=>'Open_printerAddlist',
  191. 'printerContent'=>$printerContent
  192. );
  193. $client = new HttpClient(IP,PORT);
  194. if(!$client->post(PATH,$msgInfo)){
  195. echo 'error';
  196. }else{
  197. $result = $client->getContent();
  198. echo $result;
  199. }
  200. }
  201. /**
  202. * [打印订单接口 Open_printMsg]
  203. * @param [string] $sn [打印机编号sn]
  204. * @param [string] $content [打印内容]
  205. * @param [string] $times [打印联数]
  206. * @return [string] [接口返回值]
  207. */
  208. function printMsg($sn,$content,$times){
  209. $time = time(); //请求时间
  210. $msgInfo = array(
  211. 'user'=>USER,
  212. 'stime'=>$time,
  213. 'sig'=>signature($time),
  214. 'apiname'=>'Open_printMsg',
  215. 'sn'=>$sn,
  216. 'content'=>$content,
  217. 'times'=>$times//打印次数
  218. );
  219. $client = new HttpClient(IP,PORT);
  220. if(!$client->post(PATH,$msgInfo)){
  221. echo 'error';
  222. }else{
  223. //服务器返回的JSON字符串,建议要当做日志记录起来
  224. $result = $client->getContent();
  225. echo $result;
  226. }
  227. }
  228. /**
  229. * [标签机打印订单接口 Open_printLabelMsg]
  230. * @param [string] $sn [打印机编号sn]
  231. * @param [string] $content [打印内容]
  232. * @param [string] $times [打印联数]
  233. * @return [string] [接口返回值]
  234. */
  235. function printLabelMsg($sn,$content,$times){
  236. $time = time(); //请求时间
  237. $msgInfo = array(
  238. 'user'=>USER,
  239. 'stime'=>$time,
  240. 'sig'=>signature($time),
  241. 'apiname'=>'Open_printLabelMsg',
  242. 'sn'=>$sn,
  243. 'content'=>$content,
  244. 'times'=>$times//打印次数
  245. );
  246. $client = new HttpClient(IP,PORT);
  247. if(!$client->post(PATH,$msgInfo)){
  248. echo 'error';
  249. }else{
  250. //服务器返回的JSON字符串,建议要当做日志记录起来
  251. $result = $client->getContent();
  252. echo $result;
  253. }
  254. }
  255. /**
  256. * [批量删除打印机 Open_printerDelList]
  257. * @param [string] $snlist [打印机编号,多台打印机请用减号“-”连接起来]
  258. * @return [string] [接口返回值]
  259. */
  260. function printerDelList($snlist){
  261. $time = time(); //请求时间
  262. $msgInfo = array(
  263. 'user'=>USER,
  264. 'stime'=>$time,
  265. 'sig'=>signature($time),
  266. 'apiname'=>'Open_printerDelList',
  267. 'snlist'=>$snlist
  268. );
  269. $client = new HttpClient(IP,PORT);
  270. if(!$client->post(PATH,$msgInfo)){
  271. echo 'error';
  272. }else{
  273. $result = $client->getContent();
  274. echo $result;
  275. }
  276. }
  277. /**
  278. * [修改打印机信息接口 Open_printerEdit]
  279. * @param [string] $sn [打印机编号]
  280. * @param [string] $name [打印机备注名称]
  281. * @param [string] $phonenum [打印机流量卡号码,可以不传参,但是不能为空字符串]
  282. * @return [string] [接口返回值]
  283. */
  284. function printerEdit($sn,$name,$phonenum){
  285. $time = time(); //请求时间
  286. $msgInfo = array(
  287. 'user'=>USER,
  288. 'stime'=>$time,
  289. 'sig'=>signature($time),
  290. 'apiname'=>'Open_printerEdit',
  291. 'sn'=>$sn,
  292. 'name'=>$name,
  293. 'phonenum'=>$phonenum
  294. );
  295. $client = new HttpClient(IP,PORT);
  296. if(!$client->post(PATH,$msgInfo)){
  297. echo 'error';
  298. }else{
  299. $result = $client->getContent();
  300. echo $result;
  301. }
  302. }
  303. /**
  304. * [清空待打印订单接口 Open_delPrinterSqs]
  305. * @param [string] $sn [打印机编号]
  306. * @return [string] [接口返回值]
  307. */
  308. function delPrinterSqs($sn){
  309. $time = time(); //请求时间
  310. $msgInfo = array(
  311. 'user'=>USER,
  312. 'stime'=>$time,
  313. 'sig'=>signature($time),
  314. 'apiname'=>'Open_delPrinterSqs',
  315. 'sn'=>$sn
  316. );
  317. $client = new HttpClient(IP,PORT);
  318. if(!$client->post(PATH,$msgInfo)){
  319. echo 'error';
  320. }else{
  321. $result = $client->getContent();
  322. echo $result;
  323. }
  324. }
  325. /**
  326. * [查询订单是否打印成功接口 Open_queryOrderState]
  327. * @param [string] $orderid [调用打印机接口成功后,服务器返回的JSON中的编号 例如:123456789_20190919163739_95385649]
  328. * @return [string] [接口返回值]
  329. */
  330. function queryOrderState($orderid){
  331. $time = time(); //请求时间
  332. $msgInfo = array(
  333. 'user'=>USER,
  334. 'stime'=>$time,
  335. 'sig'=>signature($time),
  336. 'apiname'=>'Open_queryOrderState',
  337. 'orderid'=>$orderid
  338. );
  339. $client = new HttpClient(IP,PORT);
  340. if(!$client->post(PATH,$msgInfo)){
  341. echo 'error';
  342. }else{
  343. $result = $client->getContent();
  344. echo $result;
  345. }
  346. }
  347. /**
  348. * [查询指定打印机某天的订单统计数接口 Open_queryOrderInfoByDate]
  349. * @param [string] $sn [打印机的编号]
  350. * @param [string] $date [查询日期,格式YY-MM-DD,如:2019-09-20]
  351. * @return [string] [接口返回值]
  352. */
  353. function queryOrderInfoByDate($sn,$date){
  354. $time = time(); //请求时间
  355. $msgInfo = array(
  356. 'user'=>USER,
  357. 'stime'=>$time,
  358. 'sig'=>signature($time),
  359. 'apiname'=>'Open_queryOrderInfoByDate',
  360. 'sn'=>$sn,
  361. 'date'=>$date
  362. );
  363. $client = new HttpClient(IP,PORT);
  364. if(!$client->post(PATH,$msgInfo)){
  365. echo 'error';
  366. }else{
  367. $result = $client->getContent();
  368. echo $result;
  369. }
  370. }
  371. /**
  372. * [获取某台打印机状态接口 Open_queryPrinterStatus]
  373. * @param [string] $sn [打印机编号]
  374. * @return [string] [接口返回值]
  375. */
  376. function queryPrinterStatus($sn){
  377. $time = time(); //请求时间
  378. $msgInfo = array(
  379. 'user'=>USER,
  380. 'stime'=>$time,
  381. 'sig'=>signature($time),
  382. 'apiname'=>'Open_queryPrinterStatus',
  383. 'sn'=>$sn
  384. );
  385. $client = new HttpClient(IP,PORT);
  386. if(!$client->post(PATH,$msgInfo)){
  387. echo 'error';
  388. }else{
  389. $result = $client->getContent();
  390. echo $result;
  391. }
  392. }
  393. /**
  394. * [signature 生成签名]
  395. * @param [string] $time [当前UNIX时间戳,10位,精确到秒]
  396. * @return [string] [接口返回值]
  397. */
  398. function signature($time){
  399. return sha1(USER.UKEY.$time);//公共参数,请求公钥
  400. }
  401. ?>
  402. }