printUtil.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. } else {
  64. $client->getContent();
  65. return true;
  66. }
  67. }
  68. }