printUtil.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. namespace common\components;
  3. use Yii;
  4. use yii\helpers\Json;
  5. use linslin\yii2\curl;
  6. class printUtil
  7. {
  8. public $access_token;
  9. public $user = 'shish@zhhinc.com', $uKey = 'Du4uwgvtAVmcEzbP', $sn, $times = 1;
  10. const IP = 'api.feieyun.cn'; //接口IP或域名
  11. const PORT = 80; //接口IP端口
  12. const PATH = '/Api/Open/'; //接口路径
  13. public function __construct($sn)
  14. {
  15. header("Content-type: text/html; charset=utf-8");
  16. $fei = Yii::getAlias("@vendor/feie");
  17. require_once($fei . '/HttpClient.class.php');
  18. $this->sn = $sn;
  19. }
  20. public function printLabelMsg($content)
  21. {
  22. $time = time(); //请求时间
  23. $msgInfo = array(
  24. 'user' => $this->user,
  25. 'stime' => $time,
  26. 'sig' => $this->signature($time),
  27. 'apiname' => 'Open_printLabelMsg',
  28. 'sn' => $this->sn,
  29. 'content' => $content,
  30. 'times' => $this->times//打印次数
  31. );
  32. $client = new \HttpClient(self::IP, self::PORT);
  33. if (!$client->post(self::PATH, $msgInfo)) {
  34. //echo 'error';
  35. } else {
  36. //服务器返回的JSON字符串,建议要当做日志记录起来
  37. $result = $client->getContent();
  38. //echo $result;
  39. }
  40. }
  41. public function printMsg($content)
  42. {
  43. $time = time(); //请求时间
  44. $msgInfo = array(
  45. 'user' => $this->user,
  46. 'stime' => $time,
  47. 'sig' => $this->signature($time),
  48. 'apiname' => 'Open_printMsg',
  49. 'sn' => $this->sn,
  50. 'content' => $content,
  51. 'times' => $this->times//打印次数
  52. );
  53. $client = new \HttpClient(self::IP, self::PORT);
  54. if (!$client->post(self::PATH, $msgInfo)) {
  55. //echo 'error';
  56. } else {
  57. //服务器返回的JSON字符串,建议要当做日志记录起来
  58. $result = $client->getContent();
  59. //echo $result;
  60. Yii::info('打印日志:' . $result);
  61. }
  62. }
  63. public function signature($time)
  64. {
  65. return sha1($this->user . $this->uKey . $time);//公共参数,请求公钥
  66. }
  67. //批量添加打印机 ssh 20210712
  68. public function addPrint($key, $shopName)
  69. {
  70. $printerContent = "sn{$this->sn}#{$key}#{$shopName}";
  71. $time = time(); //请求时间
  72. $msgInfo = array(
  73. 'user' => $this->user,
  74. 'stime' => $time,
  75. 'sig' => $this->signature($time),
  76. 'apiname' => 'Open_printerAddlist',
  77. 'printerContent' => $printerContent
  78. );
  79. $client = new \HttpClient(self::IP, self::PORT);
  80. if (!$client->post(self::PATH, $msgInfo)) {
  81. return false;
  82. }
  83. $content = $client->getContent();
  84. $respond = json_decode($content, true);
  85. if (isset($respond['data']['ok']) && !empty($respond['data']['ok'])) {
  86. return true;
  87. }
  88. $no = $respond['data']['no'][0] ?? '';
  89. if (strstr($no, "已被添加过")) {
  90. return true;
  91. }
  92. return false;
  93. }
  94. //查询打印机状态 ssh 20210714
  95. public function queryPrinterStatus($sn)
  96. {
  97. $time = time(); //请求时间
  98. $msgInfo = array(
  99. 'user' => $this->user,
  100. 'stime' => $time,
  101. 'sig' => $this->signature($time),
  102. 'apiname' => 'Open_queryPrinterStatus',
  103. 'sn' => $sn
  104. );
  105. $client = new \HttpClient(self::IP, self::PORT);
  106. if (!$client->post(self::PATH, $msgInfo)) {
  107. echo 'error';
  108. }
  109. $client->getContent();
  110. }
  111. //飞鹅自带函数 ssh 20220601
  112. public function bar_code($strnum)
  113. {
  114. $chr = '';
  115. $codeB = array("\x30", "\x31", "\x32", "\x33", "\x34", "\x35", "\x36", "\x37", "\x38", "\x39");//匹配字符集B
  116. $codeC = array("\x00", "\x01", "\x02", "\x03", "\x04", "\x05", "\x06", "\x07", "\x08", "\x09", "\x0A", "\x0B", "\x0C", "\x0D", "\x0E", "\x0F", "\x10", "\x11", "\x12", "\x13", "\x14", "\x15", "\x16", "\x17", "\x18", "\x19", "\x1A", "\x1B", "\x1C", "\x1D", "\x1E", "\x1F", "\x20", "\x21", "\x22", "\x23", "\x24", "\x25", "\x26", "\x27", "\x28", "\x29", "\x2A", "\x2B", "\x2C", "\x2D", "\x2E", "\x2F", "\x30", "\x31", "\x32", "\x33", "\x34", "\x35", "\x36", "\x37", "\x38", "\x39", "\x3A", "\x3B", "\x3C", "\x3D", "\x3E", "\x3F", "\x40", "\x41", "\x42", "\x43", "\x44", "\x45", "\x46", "\x47", "\x48", "\x49", "\x4A", "\x4B", "\x4C", "\x4D", "\x4E", "\x4F", "\x50", "\x51", "\x52", "\x53", "\x54", "\x55", "\x56", "\x57", "\x58", "\x59", "\x5A", "\x5B", "\x5C", "\x5D", "\x5E", "\x5F", "\x60", "\x61", "\x62", "\x63");//匹配字符集C
  117. $length = strlen($strnum);
  118. $b = array();
  119. $b[0] = "\x1b";
  120. $b[1] = "\x64";
  121. $b[2] = "\x02";
  122. $b[3] = "\x1d";
  123. $b[4] = "\x48";
  124. $b[5] = "\x32";//条形码显示控制,\x32上图下字,\x31上字下图,\x30只显示条形码
  125. $b[6] = "\x1d";
  126. $b[7] = "\x68";
  127. $b[8] = "\x50";// \x30 设置条形码高度,7F是最大的高度
  128. $b[9] = "\x1d";
  129. $b[10] = "\x77";
  130. $b[11] = "\x02";// \x01 设置条形码宽度,1-6
  131. $b[12] = "\x1d";
  132. $b[13] = "\x6b";
  133. $b[14] = "\x49";//选择条形码类型code128,code39,codabar等等
  134. $b[15] = chr($length + 2);
  135. $b[16] = "\x7b";
  136. $b[17] = "\x42";//选择字符集
  137. if ($length > 14 && is_numeric($strnum)) {//大于14个字符,且为纯数字的进来这个区间
  138. $b[17] = "\x43";
  139. $j = 0;
  140. $key = 18;
  141. $ss = $length / 2;//初始化数组长度
  142. if ($length % 2 == 1) {//判断条形码为单数
  143. $ss = $ss - 0.5;
  144. }
  145. for ($i = 0; $i < $ss; $i++) {
  146. $temp = substr($strnum, $j, 2);
  147. $iindex = intval($temp);
  148. $j = $j + 2;
  149. if ($iindex == 0) {
  150. $chr = '';
  151. if ($b[$key + $i - 1] == '0' && $b[$key + $i - 2] == '0') {//判断前面的为字符集B,此时不需要转换字符集
  152. $b[$key + $i] = $codeB[0];
  153. $b[$key + $i + 1] = $codeB[0];
  154. $key += 1;
  155. } else {
  156. if ($b[$key + $i - 1] == 'C' && $b[$key + $i - 2] == '{') {//判断前面的为字符集C时转换字符集B
  157. $b[$key + $i - 2] = "\x7b";
  158. $b[$key + $i - 1] = "\x42";
  159. $b[$key + $i] = $codeB[0];
  160. $b[$key + $i + 1] = $codeB[0];
  161. $key += 1;
  162. } else {
  163. $b[$key + $i] = "\x7b";
  164. $b[$key + $i + 1] = "\x42";
  165. $b[$key + $i + 2] = $codeB[0];
  166. $b[$key + $i + 3] = $codeB[0];
  167. $key += 3;
  168. }
  169. }
  170. } else {
  171. if ($b[$key + $i - 1] == '0' && $b[$key + $i - 2] == '0' && $chr != 'chr') {//判断前面的为字符集B,此时要转换字符集C
  172. $b[$key + $i] = "\x7b";
  173. $b[$key + $i + 1] = "\x43";
  174. $b[$key + $i + 2] = $codeC[$iindex];
  175. $key += 2;
  176. } else {
  177. $chr = '';
  178. $b[$key + $i] = $codeC[$iindex];
  179. if ($iindex == 48) $chr = 'chr';//判断chr(48)等于0的情况
  180. }
  181. }
  182. }
  183. @$lastkey = end(array_keys($b));//取得数组的最后一个元素的键
  184. if ($length % 2 > 0) {
  185. $lastnum = substr($strnum, -1);//取得字符串的最后一个数字
  186. if ($b[$lastkey] == '0' && $b[$lastkey - 1] == '0') {//判断前面的为字符集B,此时不需要转换字符集
  187. $b[$lastkey + 1] = $codeB[$lastnum];
  188. } else {
  189. $b[$lastkey + 1] = "\x7b";
  190. $b[$lastkey + 2] = "\x42";
  191. $b[$lastkey + 3] = $codeB[$lastnum];
  192. }
  193. }
  194. @$b[15] = chr(end(array_keys($b)) - 15);//得出条形码长度
  195. $str = implode("", $b);
  196. } else {//1-14个字符的纯数字和非纯数字的条形码进来这个区间,支持数字,大小写字母,特殊字符例如: !@#$%^&*()-=+_
  197. $str = "\x1b\x64\x02\x1d\x48\x32\x1d\x68\x50\x1d\x77\x02\x1d\x6b\x49" . chr($length + 2) . "\x7b\x42" . $strnum;
  198. }
  199. return $str;
  200. }
  201. }