Feieyun.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. //host地址
  13. 'host' => 'http://api.feieyun.cn:80/Api/Open/',
  14. //可打印的长度
  15. 'length' => 32,
  16. );
  17. private $text = '';
  18. # 生成参数
  19. public function param($api, $send = array())
  20. {
  21. $time = time();
  22. $param['user'] = $this->config['user'];
  23. $param['stime'] = $time;
  24. $param['sig'] = sha1($this->config['user'] . $this->config['ukey'] . $time);
  25. $param['apiname'] = $api;
  26. $param = array_merge($param, $send);
  27. return $param;
  28. }
  29. # 发送
  30. public function send($api, $send)
  31. {
  32. $url = $this->config['host'];
  33. $param = $this->param($api, $send);
  34. $result = Dever::curl($url, $param, 'post');
  35. $result = Dever::json_decode($result);
  36. if (isset($result['data']) && $result['data'] && isset($result['data'])) {
  37. return $result['data'];
  38. }
  39. if (isset($result['msg'])) {
  40. return $result['msg'];
  41. }
  42. return 'error';
  43. }
  44. # 添加打印机
  45. public function add($data)
  46. {
  47. $content = array();
  48. foreach ($data as $k => $v) {
  49. $content[] = $v['number'] . '#' . $v['key'] . '#' . $v['name'] . '#' . $v['phonenum'];
  50. }
  51. $content = implode('\n', $content);
  52. $send['printerContent'] = $content;
  53. $result = $this->send('Open_printerAddlist', $send);
  54. return $result;
  55. }
  56. # 编辑打印机
  57. public function edit($sn, $name, $phonenum)
  58. {
  59. $send['sn'] = $sn;
  60. $send['name'] = $name;
  61. if ($phonenum && $phonenum != 'null') {
  62. $send['phonenum'] = $phonenum;
  63. }
  64. $result = $this->send('Open_printerEdit', $send);
  65. return $result;
  66. }
  67. # 删除打印机
  68. public function del($data)
  69. {
  70. $content = array();
  71. foreach ($data as $k => $v) {
  72. $content[] = $v['number'];
  73. }
  74. $content = implode('-', $content);
  75. $send['snlist'] = $content;
  76. $result = $this->send('Open_printerDelList', $send);
  77. return $result;
  78. }
  79. # 打印订单
  80. public function printOrder($sn, $content, $times)
  81. {
  82. $send['sn'] = $sn;
  83. $send['content'] = $content;
  84. $send['times'] = $times;
  85. $result = $this->send('Open_printMsg', $send);
  86. return $result;
  87. }
  88. # 打印标签机订单
  89. public function printLabelOrder($sn, $content, $times)
  90. {
  91. $send['sn'] = $sn;
  92. $send['content'] = $content;
  93. $send['times'] = $times;
  94. $result = $this->send('Open_printLabelMsg', $send);
  95. return $result;
  96. }
  97. # 清空待打印订单接口
  98. public function delOrder($sn)
  99. {
  100. $send['sn'] = $sn;
  101. $result = $this->send('Open_delPrinterSqs', $send);
  102. return $result;
  103. }
  104. # 查询订单是否打印成功接口
  105. public function order($id)
  106. {
  107. $send['orderid'] = $id;
  108. $result = $this->send('Open_queryOrderState', $send);
  109. return $result;
  110. }
  111. # 查询指定打印机某天的订单统计数接口
  112. public function stat($sn, $date)
  113. {
  114. $send['sn'] = $sn;
  115. $send['date'] = $date;
  116. $result = $this->send('Open_queryOrderInfoByDate', $send);
  117. return $result;
  118. }
  119. # 获取某台打印机状态接口
  120. public function status($sn)
  121. {
  122. $send['sn'] = $sn;
  123. $result = $this->send('Open_queryPrinterStatus', $send);
  124. return $result;
  125. }
  126. public function getText()
  127. {
  128. return $this->text;
  129. }
  130. public function text($text = '', $align = '', $br = 1)
  131. {
  132. if ($align) {
  133. $text = '<'.$align.'>'.$text.'</'.$align.'>';
  134. }
  135. $this->text .= $text;
  136. if ($br > 0) {
  137. $this->br($br);
  138. }
  139. return $this;
  140. }
  141. public function left($text)
  142. {
  143. $this->text($text);
  144. return $this;
  145. }
  146. public function right($text)
  147. {
  148. $this->text($text, 'RIGHT');
  149. return $this;
  150. }
  151. public function center($text)
  152. {
  153. $this->text($text, 'C');
  154. return $this;
  155. }
  156. public function hr($str = '-', $name = '')
  157. {
  158. $text = str_pad($name, $this->config['length'], $str, STR_PAD_BOTH);
  159. $this->text($text);
  160. return $this;
  161. }
  162. public function br($num = 1)
  163. {
  164. for ($i = 0; $i < $num; $i++) {
  165. $this->text .= '<BR>';
  166. }
  167. return $this;
  168. }
  169. # 左右结构
  170. public function lr($s1, $s2)
  171. {
  172. $l1 = strlen(Dever::convert($s1));
  173. $n = '';
  174. $max = $this->config['length'] - 6;
  175. if ($l1 > $max) {
  176. $max = $max/2;
  177. $t = mb_substr($s1, 0, $max, 'utf-8');
  178. $n = mb_substr($s1, $max, $l1, 'utf-8');
  179. $s1 = $t;
  180. $l1 = strlen(Dever::convert($s1));
  181. }
  182. $l2 = strlen(Dever::convert($s2));
  183. $l = $this->config['length'] - ($l1+$l2);
  184. $k = '';
  185. for($i = 0; $i < $l; $i++) {
  186. $k .= ' ';
  187. }
  188. $text = $s1 . $k . $s2;
  189. $this->text($text);
  190. return $n;
  191. }
  192. # 左中右结构
  193. public function lcr($s1, $s2, $s3, $s1_s2 = 80, $s2_s3 = 20)
  194. {
  195. $s1_s2 = round(($s1_s2/100)*$this->config['length']);
  196. $s2_s3 = round(($s2_s3/100)*$this->config['length']);
  197. $l1 = strlen(Dever::convert($s1));
  198. $n = '';
  199. $max = $s1_s2 - ($s1_s2 - $s2_s3)/2;
  200. if ($l1 > $max) {
  201. $max = $max/2;
  202. $t = mb_substr($s1, 0, $max, 'utf-8');
  203. $n = mb_substr($s1, $max, $l1, 'utf-8');
  204. $s1 = $t;
  205. $l1 = strlen(Dever::convert($s1));
  206. }
  207. $l2 = strlen(Dever::convert($s2));
  208. $l3 = strlen(Dever::convert($s3));
  209. $l = $s1_s2 - ($l1+$l2);
  210. $k = '';
  211. for($i = 0; $i < $l; $i++) {
  212. $k .= ' ';
  213. }
  214. $text = $s1 . $k . $s2;
  215. $l = $s2_s3 - $l3;
  216. $k = '';
  217. for($i = 0; $i < $l; $i++) {
  218. $k .= ' ';
  219. }
  220. $text .= $k . $s3;
  221. $this->text($text);
  222. return $n;
  223. }
  224. # 表格
  225. public function table($head, $body, $name, $method = 'lcr')
  226. {
  227. $this->hr();
  228. if ($name) {
  229. $this->center($name);
  230. }
  231. call_user_func_array(array($this, $method), $head);
  232. $next = '';
  233. foreach ($body as $k => $v) {
  234. $next = call_user_func_array(array($this, $method), $v);
  235. if ($next) {
  236. $this->left($next);
  237. }
  238. }
  239. $this->hr();
  240. }
  241. }