printUtil.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace common\components;
  3. use Yii;
  4. use yii\helpers\Json;
  5. use linslin\yii2\curl;
  6. /**
  7. * 微信和微信开放平台交互接口
  8. */
  9. class printUtil
  10. {
  11. public $access_token;
  12. public $user = 'shish@zhhinc.com', $uKey = 'Du4uwgvtAVmcEzbP', $sn, $times = 1;
  13. const IP = 'api.feieyun.cn'; //接口IP或域名
  14. const PORT = 80; //接口IP端口
  15. const PATH = '/Api/Open/'; //接口路径
  16. public function __construct($sn)
  17. {
  18. header("Content-type: text/html; charset=utf-8");
  19. $fei = Yii::getAlias("@vendor/feie");
  20. require_once($fei . '/HttpClient.class.php');
  21. $this->sn = $sn;
  22. }
  23. public function printMsg($content)
  24. {
  25. $time = time(); //请求时间
  26. $msgInfo = array(
  27. 'user' => $this->user,
  28. 'stime' => $time,
  29. 'sig' => $this->signature($time),
  30. 'apiname' => 'Open_printMsg',
  31. 'sn' => $this->sn,
  32. 'content' => $content,
  33. 'times' => $this->times//打印次数
  34. );
  35. $client = new \HttpClient(self::IP, self::PORT);
  36. if (!$client->post(self::PATH, $msgInfo)) {
  37. //echo 'error';
  38. } else {
  39. //服务器返回的JSON字符串,建议要当做日志记录起来
  40. $result = $client->getContent();
  41. //echo $result;
  42. }
  43. }
  44. public function signature($time)
  45. {
  46. return sha1($this->user . $this->uKey . $time);//公共参数,请求公钥
  47. }
  48. //批量添加打印机 shish 20210712
  49. public function addPrint($key, $shopName)
  50. {
  51. $printerContent = "sn{$this->sn}#{$key}#{$shopName}";
  52. $time = time(); //请求时间
  53. $msgInfo = array(
  54. 'user' => $this->user,
  55. 'stime' => $time,
  56. 'sig' => $this->signature($time),
  57. 'apiname' => 'Open_printerAddlist',
  58. 'printerContent' => $printerContent
  59. );
  60. $client = new \HttpClient(self::IP, self::PORT);
  61. if (!$client->post(self::PATH, $msgInfo)) {
  62. return false;
  63. }
  64. $content = $client->getContent();
  65. $respond = json_decode($content, true);
  66. if (isset($respond['data']['ok']) && !empty($respond['data']['ok'])) {
  67. return true;
  68. }
  69. $no = $respond['data']['no'][0] ?? '';
  70. if (strstr($no, "已被添加过")) {
  71. return true;
  72. }
  73. return false;
  74. }
  75. //查询打印机状态 shish 20210714
  76. public function queryPrinterStatus($sn)
  77. {
  78. $time = time(); //请求时间
  79. $msgInfo = array(
  80. 'user' => $this->user,
  81. 'stime' => $time,
  82. 'sig' => $this->signature($time),
  83. 'apiname' => 'Open_queryPrinterStatus',
  84. 'sn' => $sn
  85. );
  86. $client = new \HttpClient(self::IP, self::PORT);
  87. if (!$client->post(self::PATH, $msgInfo)) {
  88. echo 'error';
  89. }
  90. $client->getContent();
  91. }
  92. }