| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- <?php
- namespace common\components;
- use Yii;
- use yii\helpers\Json;
- use linslin\yii2\curl;
- /**
- * 微信和微信开放平台交互接口
- */
- class printUtil
- {
- public $access_token;
- public $user = 'shish@zhhinc.com', $uKey = 'Du4uwgvtAVmcEzbP', $sn, $times = 1;
- const IP = 'api.feieyun.cn'; //接口IP或域名
- const PORT = 80; //接口IP端口
- const PATH = '/Api/Open/'; //接口路径
- public function __construct($sn)
- {
- header("Content-type: text/html; charset=utf-8");
- $fei = Yii::getAlias("@vendor/feie");
- require_once($fei . '/HttpClient.class.php');
- $this->sn = $sn;
- }
- public function printMsg($content)
- {
- $time = time(); //请求时间
- $msgInfo = array(
- 'user' => $this->user,
- 'stime' => $time,
- 'sig' => $this->signature($time),
- 'apiname' => 'Open_printMsg',
- 'sn' => $this->sn,
- 'content' => $content,
- 'times' => $this->times//打印次数
- );
- $client = new \HttpClient(self::IP, self::PORT);
- if (!$client->post(self::PATH, $msgInfo)) {
- //echo 'error';
- } else {
- //服务器返回的JSON字符串,建议要当做日志记录起来
- $result = $client->getContent();
- //echo $result;
- }
- }
- public function signature($time)
- {
- return sha1($this->user . $this->uKey . $time);//公共参数,请求公钥
- }
- //批量添加打印机 shish 20210712
- public function addPrint($key, $shopName)
- {
- $printerContent = "sn{$this->sn}#{$key}#{$shopName}";
- $time = time(); //请求时间
- $msgInfo = array(
- 'user' => $this->user,
- 'stime' => $time,
- 'sig' => $this->signature($time),
- 'apiname' => 'Open_printerAddlist',
- 'printerContent' => $printerContent
- );
- $client = new \HttpClient(self::IP, self::PORT);
- if (!$client->post(self::PATH, $msgInfo)) {
- return false;
- }
- $content = $client->getContent();
- $respond = json_decode($content, true);
- if (isset($respond['data']['ok']) && !empty($respond['data']['ok'])) {
- return true;
- }
- $no = $respond['data']['no'][0] ?? '';
- if (strstr($no, "已被添加过")) {
- return true;
- }
- return false;
- }
- //查询打印机状态 shish 20210714
- public function queryPrinterStatus($sn)
- {
- $time = time(); //请求时间
- $msgInfo = array(
- 'user' => $this->user,
- 'stime' => $time,
- 'sig' => $this->signature($time),
- 'apiname' => 'Open_queryPrinterStatus',
- 'sn' => $sn
- );
- $client = new \HttpClient(self::IP, self::PORT);
- if (!$client->post(self::PATH, $msgInfo)) {
- echo 'error';
- }
- $client->getContent();
- }
- }
|