PrintService.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace app\extra\tools;
  3. use yzh52521\EasyHttp\Http;
  4. class PrintService
  5. {
  6. /**
  7. * 配置信息
  8. * @var array
  9. */
  10. protected array $config = [];
  11. /**
  12. * 网关
  13. * @var string
  14. */
  15. protected string $gateway = "https://cloud.kuaimai.com/";
  16. /**
  17. * 发起打印
  18. * @param int $templateId
  19. * @param string $sn
  20. * @param array $data
  21. * @param int $num
  22. * [{"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"}]}]
  23. * @return array
  24. */
  25. public function printTemplate(int $templateId,string $sn = "",array $data = [],int $num = 1): array
  26. {
  27. $param = [
  28. "appId" => $this->config['appid'],
  29. "sn" => $sn,
  30. "templateId" => $templateId,
  31. "printTimes" => $num,
  32. "renderDataArray" => json_encode($data),
  33. "timestamp" => getDateFull()
  34. ];
  35. $param['sign'] = $this->getPaySign($param);
  36. return Http::asJson()->post($this->gateway."api/cloud/print/tsplTemplatePrint", $param)->array();
  37. }
  38. /**
  39. * 打印机状态查询下
  40. * @param array $data 最多100台
  41. * @return array
  42. */
  43. public function deviceState(array $data = []): array
  44. {
  45. $param = [
  46. "appId" => $this->config['appid'],
  47. "snsStr" => json_encode($data),
  48. "timestamp" => getDateFull()
  49. ];
  50. $param['sign'] = $this->getPaySign($param);
  51. return Http::asJson()->post($this->gateway."api/cloud/device/batchStatus", $param)->array();
  52. }
  53. /**
  54. * 绑定设备
  55. * @param string $sn
  56. * @param string $name
  57. * @return array status true为成功,false为失败 message
  58. */
  59. public function bindDevice(string $sn = "",string $name = ""): array
  60. {
  61. $param = [
  62. "appId" => $this->config['appid'],
  63. "sn" => $sn,
  64. "noteName" => $name,
  65. "timestamp" => getDateFull()
  66. ];
  67. $param['sign'] = $this->getPaySign($param);
  68. return Http::asJson()->post($this->gateway."api/cloud/device/bindDevice", $param)->array();
  69. }
  70. /**
  71. * 解绑设备
  72. * @param string $sn
  73. * @param string $deviceKey
  74. * @return array status true为成功,false为失败 message
  75. */
  76. public function unbindDevice(string $sn = "",string $deviceKey = ""): array
  77. {
  78. $param = [
  79. "appId" => $this->config['appid'],
  80. "sn" => $sn,
  81. "deviceKey" => $deviceKey,
  82. "timestamp" => getDateFull()
  83. ];
  84. $param['sign'] = $this->getPaySign($param);
  85. return Http::asJson()->post($this->gateway."api/cloud/device/unbindDevice", $param)->array();
  86. }
  87. /**
  88. * @param array $data
  89. * @return $this
  90. */
  91. public function config(array $data = [])
  92. {
  93. $this->config = $data;
  94. return $this;
  95. }
  96. /**
  97. * 生成签名
  98. * @param array $data 参与签名的数据
  99. * @param string $signType 参与签名的类型
  100. * @param string $buff 参与签名字符串前缀
  101. * @return string
  102. */
  103. public function getPaySign(array $data, string $signType = 'MD5', string $buff = ''): string
  104. {
  105. ksort($data);
  106. if (isset($data['sign'])) unset($data['sign']);
  107. foreach ($data as $k => $v) {
  108. if ('' === $v || null === $v) continue;
  109. $buff .= "{$k}{$v}";
  110. }
  111. $buff .= $this->config['secret'];
  112. if (strtoupper($signType) === 'MD5') {
  113. return md5($this->config['secret'].$buff);
  114. }
  115. return strtoupper(hash_hmac('SHA256', $buff, $this->config['secret']));
  116. }
  117. }