printUtil.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <?php
  2. namespace common\components;
  3. use biz\shop\classes\ShopAdminClass;
  4. use Yii;
  5. use yii\helpers\Json;
  6. use linslin\yii2\curl;
  7. class printUtil
  8. {
  9. public $access_token;
  10. public $user = 'shish@zhhinc.com', $uKey = 'Du4uwgvtAVmcEzbP', $sn, $times = 1;
  11. const IP = 'api.feieyun.cn'; //接口IP或域名
  12. const PORT = 80; //接口IP端口
  13. const PATH = '/Api/Open/'; //接口路径
  14. public function __construct($sn)
  15. {
  16. header("Content-type: text/html; charset=utf-8");
  17. $fei = Yii::getAlias("@vendor/feie");
  18. require_once($fei . '/HttpClient.class.php');
  19. $this->sn = $sn;
  20. }
  21. public function printLabelMsg($content)
  22. {
  23. $time = time(); //请求时间
  24. $msgInfo = array(
  25. 'user' => $this->user,
  26. 'stime' => $time,
  27. 'sig' => $this->signature($time),
  28. 'apiname' => 'Open_printLabelMsg',
  29. 'sn' => $this->sn,
  30. 'content' => $content,
  31. 'times' => $this->times//打印次数
  32. );
  33. try {
  34. $client = new \HttpClient(self::IP, self::PORT);
  35. $client->useGzip(false); //禁用 Gzip
  36. $rs = $client->post(self::PATH, $msgInfo);
  37. //服务器返回的JSON字符串,建议要当做日志记录起来
  38. $result = $client->getContent();
  39. //echo $result;
  40. } catch (\Exception $e) {
  41. Yii::error('打印日志:' . $e->getMessage());
  42. noticeUtil::push("出现打印标签,内容:{$content},原因:" . $e->getMessage(), '15280215347');
  43. return;
  44. }
  45. if (!$rs) {
  46. //echo 'error';
  47. } else {
  48. //服务器返回的JSON字符串,建议要当做日志记录起来
  49. $result = $client->getContent();
  50. //echo $result;
  51. }
  52. }
  53. /**
  54. * 打印订单
  55. */
  56. public function printMsg($content, $shop = null, $orderSn = '')
  57. {
  58. $contentLength = strlen($content);
  59. if ($contentLength > 5000) {
  60. $contents = explode("<BR>", $content);
  61. $newContent = '';
  62. foreach ($contents as $line) {
  63. if (strlen($newContent . $line . "<BR>") > 5000) {
  64. $this->doPrint($newContent, $shop, $orderSn);
  65. $newContent = $line . "<BR>";
  66. } else {
  67. $newContent .= $line . "<BR>";
  68. }
  69. }
  70. if (!empty($newContent)) {
  71. $this->doPrint($newContent, $shop, $orderSn);
  72. }
  73. } else {
  74. $this->doPrint($content, $shop, $orderSn);
  75. }
  76. }
  77. private function doPrint($content, $shop = null, $orderSn = '')
  78. {
  79. $time = time(); //请求时间
  80. $msgInfo = array(
  81. 'user' => $this->user,
  82. 'stime' => $time,
  83. 'sig' => $this->signature($time),
  84. 'apiname' => 'Open_printMsg',
  85. 'sn' => $this->sn,
  86. 'content' => $content,
  87. 'times' => $this->times//打印次数
  88. );
  89. try {
  90. $client = new \HttpClient(self::IP, self::PORT);
  91. $client->useGzip(false); // 禁用 Gzip
  92. $rs = $client->post(self::PATH, $msgInfo);
  93. } catch (\Exception $e) {
  94. Yii::error('打印日志:' . $e->getMessage());
  95. noticeUtil::push($shop->merchantName . $shop->shopName . " 出现订单打印未成功,订单号:{$orderSn},原因:" . $e->getMessage(), '15280215347');
  96. return;
  97. }
  98. if (!$rs) {
  99. //echo 'error';
  100. } else {
  101. //服务器返回的JSON字符串,建议要当做日志记录起来
  102. $result = $client->getContent();
  103. Yii::info('打印日志:' . $result);
  104. $arr = json_decode($result, true);
  105. if (isset($arr['ret']) && $arr['ret'] == 1001) {
  106. $msg = $arr['msg'] ?? '';
  107. $sjName = $shop->merchantName ?? '';
  108. $mainId = $shop->mainId ?? 0;
  109. $shopName = $shop->shopName ?? '';
  110. noticeUtil::push($sjName . $shopName . " 出现订单打印未成功,订单号:{$orderSn},原因:" . $msg, '15280215347');
  111. $staffList = ShopAdminClass::getAllByCondition(['mainId' => $mainId], null, '*', null, true);
  112. if (!empty($staffList)) {
  113. foreach ($staffList as $staff) {
  114. $remind = $staff->remind ?? 0;
  115. $isPt = $staff->isPt ?? 0;
  116. $delStatus = $staff->delStatus ?? 0;
  117. if ($isPt == 0 && $remind == 1 && $delStatus == 0) {
  118. // $mobile = $staff->mobile ?? '';
  119. // sms::send($mobile . ',' . $shopName, '请注意,您有一个订单花材很多,小票没有打出来。门店:{$var}');
  120. // noticeUtil::push('已经通知手机号:' . $mobile, '15280215347');
  121. }
  122. }
  123. }
  124. }
  125. }
  126. }
  127. public function signature($time)
  128. {
  129. return sha1($this->user . $this->uKey . $time);//公共参数,请求公钥
  130. }
  131. //批量添加打印机 ssh 20210712
  132. public function addPrint($key, $shopName)
  133. {
  134. $printerContent = "sn{$this->sn}#{$key}#{$shopName}";
  135. $time = time(); //请求时间
  136. $msgInfo = array(
  137. 'user' => $this->user,
  138. 'stime' => $time,
  139. 'sig' => $this->signature($time),
  140. 'apiname' => 'Open_printerAddlist',
  141. 'printerContent' => $printerContent
  142. );
  143. $client = new \HttpClient(self::IP, self::PORT);
  144. if (!$client->post(self::PATH, $msgInfo)) {
  145. return false;
  146. }
  147. $content = $client->getContent();
  148. $respond = json_decode($content, true);
  149. if (isset($respond['data']['ok']) && !empty($respond['data']['ok'])) {
  150. return true;
  151. }
  152. $no = $respond['data']['no'][0] ?? '';
  153. if (strstr($no, "已被添加过")) {
  154. return true;
  155. }
  156. return false;
  157. }
  158. //查询打印机状态 ssh 20210714
  159. public function queryPrinterStatus($sn)
  160. {
  161. $time = time(); //请求时间
  162. $msgInfo = array(
  163. 'user' => $this->user,
  164. 'stime' => $time,
  165. 'sig' => $this->signature($time),
  166. 'apiname' => 'Open_queryPrinterStatus',
  167. 'sn' => $sn
  168. );
  169. $client = new \HttpClient(self::IP, self::PORT);
  170. if (!$client->post(self::PATH, $msgInfo)) {
  171. echo 'error';
  172. }
  173. $client->getContent();
  174. }
  175. //飞鹅自带函数 ssh 20220601
  176. public function bar_code($strnum)
  177. {
  178. $chr = '';
  179. $codeB = array("\x30", "\x31", "\x32", "\x33", "\x34", "\x35", "\x36", "\x37", "\x38", "\x39");//匹配字符集B
  180. $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
  181. $length = strlen($strnum);
  182. $b = array();
  183. $b[0] = "\x1b";
  184. $b[1] = "\x64";
  185. $b[2] = "\x02";
  186. $b[3] = "\x1d";
  187. $b[4] = "\x48";
  188. $b[5] = "\x32";//条形码显示控制,\x32上图下字,\x31上字下图,\x30只显示条形码
  189. $b[6] = "\x1d";
  190. $b[7] = "\x68";
  191. $b[8] = "\x50";// \x30 设置条形码高度,7F是最大的高度
  192. $b[9] = "\x1d";
  193. $b[10] = "\x77";
  194. $b[11] = "\x02";// \x01 设置条形码宽度,1-6
  195. $b[12] = "\x1d";
  196. $b[13] = "\x6b";
  197. $b[14] = "\x49";//选择条形码类型code128,code39,codabar等等
  198. $b[15] = chr($length + 2);
  199. $b[16] = "\x7b";
  200. $b[17] = "\x42";//选择字符集
  201. if ($length > 14 && is_numeric($strnum)) {//大于14个字符,且为纯数字的进来这个区间
  202. $b[17] = "\x43";
  203. $j = 0;
  204. $key = 18;
  205. $ss = $length / 2;//初始化数组长度
  206. if ($length % 2 == 1) {//判断条形码为单数
  207. $ss = $ss - 0.5;
  208. }
  209. for ($i = 0; $i < $ss; $i++) {
  210. $temp = substr($strnum, $j, 2);
  211. $iindex = intval($temp);
  212. $j = $j + 2;
  213. if ($iindex == 0) {
  214. $chr = '';
  215. if ($b[$key + $i - 1] == '0' && $b[$key + $i - 2] == '0') {//判断前面的为字符集B,此时不需要转换字符集
  216. $b[$key + $i] = $codeB[0];
  217. $b[$key + $i + 1] = $codeB[0];
  218. $key += 1;
  219. } else {
  220. if ($b[$key + $i - 1] == 'C' && $b[$key + $i - 2] == '{') {//判断前面的为字符集C时转换字符集B
  221. $b[$key + $i - 2] = "\x7b";
  222. $b[$key + $i - 1] = "\x42";
  223. $b[$key + $i] = $codeB[0];
  224. $b[$key + $i + 1] = $codeB[0];
  225. $key += 1;
  226. } else {
  227. $b[$key + $i] = "\x7b";
  228. $b[$key + $i + 1] = "\x42";
  229. $b[$key + $i + 2] = $codeB[0];
  230. $b[$key + $i + 3] = $codeB[0];
  231. $key += 3;
  232. }
  233. }
  234. } else {
  235. if ($b[$key + $i - 1] == '0' && $b[$key + $i - 2] == '0' && $chr != 'chr') {//判断前面的为字符集B,此时要转换字符集C
  236. $b[$key + $i] = "\x7b";
  237. $b[$key + $i + 1] = "\x43";
  238. $b[$key + $i + 2] = $codeC[$iindex];
  239. $key += 2;
  240. } else {
  241. $chr = '';
  242. $b[$key + $i] = $codeC[$iindex];
  243. if ($iindex == 48) $chr = 'chr';//判断chr(48)等于0的情况
  244. }
  245. }
  246. }
  247. @$lastkey = end(array_keys($b));//取得数组的最后一个元素的键
  248. if ($length % 2 > 0) {
  249. $lastnum = substr($strnum, -1);//取得字符串的最后一个数字
  250. if ($b[$lastkey] == '0' && $b[$lastkey - 1] == '0') {//判断前面的为字符集B,此时不需要转换字符集
  251. $b[$lastkey + 1] = $codeB[$lastnum];
  252. } else {
  253. $b[$lastkey + 1] = "\x7b";
  254. $b[$lastkey + 2] = "\x42";
  255. $b[$lastkey + 3] = $codeB[$lastnum];
  256. }
  257. }
  258. @$b[15] = chr(end(array_keys($b)) - 15);//得出条形码长度
  259. $str = implode("", $b);
  260. } else {//1-14个字符的纯数字和非纯数字的条形码进来这个区间,支持数字,大小写字母,特殊字符例如: !@#$%^&*()-=+_
  261. $str = "\x1b\x64\x02\x1d\x48\x32\x1d\x68\x50\x1d\x77\x02\x1d\x6b\x49" . chr($length + 2) . "\x7b\x42" . $strnum;
  262. }
  263. return $str;
  264. }
  265. }