printUtil.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. $client = new \HttpClient(self::IP, self::PORT);
  34. if (!$client->post(self::PATH, $msgInfo)) {
  35. //echo 'error';
  36. } else {
  37. //服务器返回的JSON字符串,建议要当做日志记录起来
  38. $result = $client->getContent();
  39. //echo $result;
  40. }
  41. }
  42. public function printMsg($content, $shop = null)
  43. {
  44. $time = time(); //请求时间
  45. $msgInfo = array(
  46. 'user' => $this->user,
  47. 'stime' => $time,
  48. 'sig' => $this->signature($time),
  49. 'apiname' => 'Open_printMsg',
  50. 'sn' => $this->sn,
  51. 'content' => $content,
  52. 'times' => $this->times//打印次数
  53. );
  54. $client = new \HttpClient(self::IP, self::PORT);
  55. if (!$client->post(self::PATH, $msgInfo)) {
  56. //echo 'error';
  57. } else {
  58. //服务器返回的JSON字符串,建议要当做日志记录起来
  59. $result = $client->getContent();
  60. Yii::info('打印日志:' . $result);
  61. $arr = json_decode($result, true);
  62. if (isset($arr['ret']) && $arr['ret'] == 1001) {
  63. $msg = $arr['msg'] ?? '';
  64. noticeUtil::push('有订单打印报错了!!请注意查看,报错内容:' . $msg, '15280215347');
  65. $mainId = $shop->mainId ?? 0;
  66. $staffList = ShopAdminClass::getAllByCondition(['mainId' => $mainId], null, '*', null, true);
  67. if (!empty($staffList)) {
  68. foreach ($staffList as $staff) {
  69. $remind = $staff->remind ?? 0;
  70. $isPt = $staff->isPt ?? 0;
  71. $delStatus = $staff->delStatus ?? 0;
  72. if ($isPt == 0 && $remind == 1 && $delStatus == 0) {
  73. $mobile = $staff->mobile ?? '';
  74. sms::send($mobile, '请注意,您有一个订单花材很多,小票没有打出来。');
  75. }
  76. }
  77. }
  78. }
  79. }
  80. $mainId = $shop->mainId ?? 0;
  81. $staffList = ShopAdminClass::getAllByCondition(['mainId' => $mainId], null, '*', null, true);
  82. if (!empty($staffList)) {
  83. foreach ($staffList as $staff) {
  84. $remind = $staff->remind ?? 0;
  85. $isPt = $staff->isPt ?? 0;
  86. $delStatus = $staff->delStatus ?? 0;
  87. noticeUtil::push("isPt:{$isPt} remind:{$remind} delStatus:{$delStatus}", '15280215347');
  88. if ($isPt == 0 && $remind == 1 && $delStatus == 0) {
  89. $mobile = $staff->mobile ?? '';
  90. sms::send($mobile, '请注意,您有一个订单花材很多,小票没有打出来。');
  91. }
  92. }
  93. }
  94. }
  95. public function signature($time)
  96. {
  97. return sha1($this->user . $this->uKey . $time);//公共参数,请求公钥
  98. }
  99. //批量添加打印机 ssh 20210712
  100. public function addPrint($key, $shopName)
  101. {
  102. $printerContent = "sn{$this->sn}#{$key}#{$shopName}";
  103. $time = time(); //请求时间
  104. $msgInfo = array(
  105. 'user' => $this->user,
  106. 'stime' => $time,
  107. 'sig' => $this->signature($time),
  108. 'apiname' => 'Open_printerAddlist',
  109. 'printerContent' => $printerContent
  110. );
  111. $client = new \HttpClient(self::IP, self::PORT);
  112. if (!$client->post(self::PATH, $msgInfo)) {
  113. return false;
  114. }
  115. $content = $client->getContent();
  116. $respond = json_decode($content, true);
  117. if (isset($respond['data']['ok']) && !empty($respond['data']['ok'])) {
  118. return true;
  119. }
  120. $no = $respond['data']['no'][0] ?? '';
  121. if (strstr($no, "已被添加过")) {
  122. return true;
  123. }
  124. return false;
  125. }
  126. //查询打印机状态 ssh 20210714
  127. public function queryPrinterStatus($sn)
  128. {
  129. $time = time(); //请求时间
  130. $msgInfo = array(
  131. 'user' => $this->user,
  132. 'stime' => $time,
  133. 'sig' => $this->signature($time),
  134. 'apiname' => 'Open_queryPrinterStatus',
  135. 'sn' => $sn
  136. );
  137. $client = new \HttpClient(self::IP, self::PORT);
  138. if (!$client->post(self::PATH, $msgInfo)) {
  139. echo 'error';
  140. }
  141. $client->getContent();
  142. }
  143. //飞鹅自带函数 ssh 20220601
  144. public function bar_code($strnum)
  145. {
  146. $chr = '';
  147. $codeB = array("\x30", "\x31", "\x32", "\x33", "\x34", "\x35", "\x36", "\x37", "\x38", "\x39");//匹配字符集B
  148. $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
  149. $length = strlen($strnum);
  150. $b = array();
  151. $b[0] = "\x1b";
  152. $b[1] = "\x64";
  153. $b[2] = "\x02";
  154. $b[3] = "\x1d";
  155. $b[4] = "\x48";
  156. $b[5] = "\x32";//条形码显示控制,\x32上图下字,\x31上字下图,\x30只显示条形码
  157. $b[6] = "\x1d";
  158. $b[7] = "\x68";
  159. $b[8] = "\x50";// \x30 设置条形码高度,7F是最大的高度
  160. $b[9] = "\x1d";
  161. $b[10] = "\x77";
  162. $b[11] = "\x02";// \x01 设置条形码宽度,1-6
  163. $b[12] = "\x1d";
  164. $b[13] = "\x6b";
  165. $b[14] = "\x49";//选择条形码类型code128,code39,codabar等等
  166. $b[15] = chr($length + 2);
  167. $b[16] = "\x7b";
  168. $b[17] = "\x42";//选择字符集
  169. if ($length > 14 && is_numeric($strnum)) {//大于14个字符,且为纯数字的进来这个区间
  170. $b[17] = "\x43";
  171. $j = 0;
  172. $key = 18;
  173. $ss = $length / 2;//初始化数组长度
  174. if ($length % 2 == 1) {//判断条形码为单数
  175. $ss = $ss - 0.5;
  176. }
  177. for ($i = 0; $i < $ss; $i++) {
  178. $temp = substr($strnum, $j, 2);
  179. $iindex = intval($temp);
  180. $j = $j + 2;
  181. if ($iindex == 0) {
  182. $chr = '';
  183. if ($b[$key + $i - 1] == '0' && $b[$key + $i - 2] == '0') {//判断前面的为字符集B,此时不需要转换字符集
  184. $b[$key + $i] = $codeB[0];
  185. $b[$key + $i + 1] = $codeB[0];
  186. $key += 1;
  187. } else {
  188. if ($b[$key + $i - 1] == 'C' && $b[$key + $i - 2] == '{') {//判断前面的为字符集C时转换字符集B
  189. $b[$key + $i - 2] = "\x7b";
  190. $b[$key + $i - 1] = "\x42";
  191. $b[$key + $i] = $codeB[0];
  192. $b[$key + $i + 1] = $codeB[0];
  193. $key += 1;
  194. } else {
  195. $b[$key + $i] = "\x7b";
  196. $b[$key + $i + 1] = "\x42";
  197. $b[$key + $i + 2] = $codeB[0];
  198. $b[$key + $i + 3] = $codeB[0];
  199. $key += 3;
  200. }
  201. }
  202. } else {
  203. if ($b[$key + $i - 1] == '0' && $b[$key + $i - 2] == '0' && $chr != 'chr') {//判断前面的为字符集B,此时要转换字符集C
  204. $b[$key + $i] = "\x7b";
  205. $b[$key + $i + 1] = "\x43";
  206. $b[$key + $i + 2] = $codeC[$iindex];
  207. $key += 2;
  208. } else {
  209. $chr = '';
  210. $b[$key + $i] = $codeC[$iindex];
  211. if ($iindex == 48) $chr = 'chr';//判断chr(48)等于0的情况
  212. }
  213. }
  214. }
  215. @$lastkey = end(array_keys($b));//取得数组的最后一个元素的键
  216. if ($length % 2 > 0) {
  217. $lastnum = substr($strnum, -1);//取得字符串的最后一个数字
  218. if ($b[$lastkey] == '0' && $b[$lastkey - 1] == '0') {//判断前面的为字符集B,此时不需要转换字符集
  219. $b[$lastkey + 1] = $codeB[$lastnum];
  220. } else {
  221. $b[$lastkey + 1] = "\x7b";
  222. $b[$lastkey + 2] = "\x42";
  223. $b[$lastkey + 3] = $codeB[$lastnum];
  224. }
  225. }
  226. @$b[15] = chr(end(array_keys($b)) - 15);//得出条形码长度
  227. $str = implode("", $b);
  228. } else {//1-14个字符的纯数字和非纯数字的条形码进来这个区间,支持数字,大小写字母,特殊字符例如: !@#$%^&*()-=+_
  229. $str = "\x1b\x64\x02\x1d\x48\x32\x1d\x68\x50\x1d\x77\x02\x1d\x6b\x49" . chr($length + 2) . "\x7b\x42" . $strnum;
  230. }
  231. return $str;
  232. }
  233. }