| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- <?php
- namespace app\extra\tools;
- use yzh52521\EasyHttp\Http;
- class PrintService
- {
- /**
- * 配置信息
- * @var array
- */
- protected array $config = [];
- /**
- * 网关
- * @var string
- */
- protected string $gateway = "https://cloud.kuaimai.com/";
- /**
- * 发起打印
- * @param int $templateId
- * @param string $sn
- * @param array $data
- * @param int $num
- * [{"express":[{"j_mobile":"180****9980","j_address":"安徽省阜阳市颍州区xxx镇xxx村xxx","express_num":"SF3270714680386","name":"飞天茅台53度500ml","time":"2025-12-04 12:18:09","type":"特快","s_name":"王*二","s_address":"安徽省阜阳市颍州区xxx镇xxx村xxx","s_mobile":"******00982","j_name":"王麻子","express_type":"饰品","order":"1033999288123123"}]}]
- * @return array
- */
- public function printTemplate(int $templateId,string $sn = "",array $data = [],int $num = 1): array
- {
- $param = [
- "appId" => $this->config['appid'],
- "sn" => $sn,
- "templateId" => $templateId,
- "printTimes" => $num,
- "renderDataArray" => json_encode($data),
- "timestamp" => getDateFull()
- ];
- $param['sign'] = $this->getPaySign($param);
- return Http::asJson()->post($this->gateway."api/cloud/print/tsplTemplatePrint", $param)->array();
- }
- /**
- * 打印机状态查询下
- * @param array $data 最多100台
- * @return array
- */
- public function deviceState(array $data = []): array
- {
- $param = [
- "appId" => $this->config['appid'],
- "snsStr" => json_encode($data),
- "timestamp" => getDateFull()
- ];
- $param['sign'] = $this->getPaySign($param);
- return Http::asJson()->post($this->gateway."api/cloud/device/batchStatus", $param)->array();
- }
- /**
- * 绑定设备
- * @param string $sn
- * @param string $name
- * @return array status true为成功,false为失败 message
- */
- public function bindDevice(string $sn = "",string $name = ""): array
- {
- $param = [
- "appId" => $this->config['appid'],
- "sn" => $sn,
- "noteName" => $name,
- "timestamp" => getDateFull()
- ];
- $param['sign'] = $this->getPaySign($param);
- return Http::asJson()->post($this->gateway."api/cloud/device/bindDevice", $param)->array();
- }
- /**
- * 解绑设备
- * @param string $sn
- * @param string $deviceKey
- * @return array status true为成功,false为失败 message
- */
- public function unbindDevice(string $sn = "",string $deviceKey = ""): array
- {
- $param = [
- "appId" => $this->config['appid'],
- "sn" => $sn,
- "deviceKey" => $deviceKey,
- "timestamp" => getDateFull()
- ];
- $param['sign'] = $this->getPaySign($param);
- return Http::asJson()->post($this->gateway."api/cloud/device/unbindDevice", $param)->array();
- }
- /**
- * @param array $data
- * @return $this
- */
- public function config(array $data = [])
- {
- $this->config = $data;
- return $this;
- }
- /**
- * 生成签名
- * @param array $data 参与签名的数据
- * @param string $signType 参与签名的类型
- * @param string $buff 参与签名字符串前缀
- * @return string
- */
- public function getPaySign(array $data, string $signType = 'MD5', string $buff = ''): string
- {
- ksort($data);
- if (isset($data['sign'])) unset($data['sign']);
- foreach ($data as $k => $v) {
- if ('' === $v || null === $v) continue;
- $buff .= "{$k}{$v}";
- }
- $buff .= $this->config['secret'];
- if (strtoupper($signType) === 'MD5') {
- return md5($this->config['secret'].$buff);
- }
- return strtoupper(hash_hmac('SHA256', $buff, $this->config['secret']));
- }
- }
|