YilianyunConfig.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. /**
  3. * @link:http://www.gdqijianshi.com/
  4. * @copyright: Copyright (c) 2020 广东七件事集团
  5. * Created by PhpStorm
  6. * Author: zal
  7. * Date: 2020-04-01
  8. * Time: 21:49
  9. */
  10. namespace addons\Printer\common\config;
  11. class YilianyunConfig extends BaseConfig
  12. {
  13. public $client_id; // 用户ID
  14. public $machine_code; // 打印机终端号
  15. public $client_key; // API密钥
  16. public $key; // 打印机密钥
  17. public $time;
  18. private $url = 'http://open.10ss.net:8888';
  19. public function print($content)
  20. {
  21. date_default_timezone_set("Asia/Shanghai");
  22. $result = $this->actionPrint($this->client_id, $this->machine_code, $content, $this->client_key, $this->key);
  23. if (!$result) {
  24. throw new \Exception('请求数据为空');
  25. }
  26. $result = json_decode($result, true);
  27. switch ($result['state']) {
  28. case 1:
  29. return true;
  30. break;
  31. case 2:
  32. throw new \Exception('提交时间超时。验证你所提交的时间戳超过3分钟后拒绝接受,参数:' . json_encode($result));
  33. break;
  34. case 3:
  35. throw new \Exception('参数有误,参数:' . json_encode($result));
  36. break;
  37. case 4:
  38. throw new \Exception('sign加密验证失败,参数:' . json_encode($result));
  39. break;
  40. default:
  41. throw new \Exception(isset($result['error']) ? $result['error'] : '易联云配置有问题,参数:' . json_encode($result));
  42. break;
  43. }
  44. }
  45. /**
  46. * 发起请求
  47. * @param string $url 请求地址
  48. * @param string $data 请求数据包
  49. * @return string 请求返回数据
  50. */
  51. public function sendCmd($url, $data)
  52. {
  53. $curl = curl_init(); // 启动一个CURL会话
  54. curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
  55. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检测
  56. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2); // 从证书中检查SSL加密算法是否存在
  57. curl_setopt($curl, CURLOPT_HTTPHEADER, array('Expect:')); //解决数据包大不能提交
  58. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
  59. curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
  60. curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
  61. curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
  62. curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循
  63. curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
  64. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
  65. $tmpInfo = curl_exec($curl); // 执行操作
  66. curl_close($curl); // 关键CURL会话
  67. return $tmpInfo; // 返回数据
  68. }
  69. /**
  70. * 生成字符串参数
  71. * @param array $param 参数
  72. * @return string 参数字符串
  73. */
  74. public function getStr($param)
  75. {
  76. $str = '';
  77. foreach ($param as $key => $value) {
  78. $str = $str . $key . '=' . $value . '&';
  79. }
  80. $str = rtrim($str, '&');
  81. return $str;
  82. }
  83. /**
  84. * 生成签名sign
  85. * @param array $params 参数
  86. * @param string $apiKey API密钥
  87. * @param string $msign 打印机密钥
  88. * @return string sign
  89. */
  90. private function generateSign($params, $apiKey, $msign)
  91. {
  92. //所有请求参数按照字母先后顺序排
  93. ksort($params);
  94. //定义字符串开始所包括的字符串
  95. $stringToBeSigned = $apiKey;
  96. //把所有参数名和参数值串在一起
  97. foreach ($params as $k => $v) {
  98. $stringToBeSigned .= urldecode($k . $v);
  99. }
  100. unset($k, $v);
  101. //定义字符串结尾所包括的字符串
  102. $stringToBeSigned .= $msign;
  103. //使用MD5进行加密,再转化成大写
  104. return strtoupper(md5($stringToBeSigned));
  105. }
  106. /**
  107. * 打印接口
  108. * @param int $partner 用户ID
  109. * @param string $machine_code 打印机终端号
  110. * @param string $content 打印内容
  111. * @param string $apiKey API密钥
  112. * @param string $msign 打印机密钥
  113. * @return string
  114. */
  115. public function actionPrint($partner, $machine_code, $content, $apiKey, $msign)
  116. {
  117. $param = array(
  118. "partner" => $partner,
  119. 'machine_code' => $machine_code,
  120. 'time' => time(),
  121. );
  122. //获取签名
  123. $param['sign'] = $this->generateSign($param, $apiKey, $msign);
  124. $param['content'] = urlencode($content);
  125. $str = $this->getStr($param);
  126. return $this->sendCmd('http://open.10ss.net:8888/index.php', $str);
  127. }
  128. /**
  129. * 添加打印机
  130. * @param int $partner 用户ID1
  131. * @param string $machine_code 打印机终端号
  132. * @param string $username 用户名
  133. * @param string $printname 打印机名称
  134. * @param string $mobilephone 打印机卡号
  135. * @param string $apiKey API密钥
  136. * @param string $msign 打印机密钥
  137. * @return string
  138. */
  139. public function actionAddprint($partner, $machine_code, $username, $printname, $mobilephone, $apiKey, $msign)
  140. {
  141. $param = array(
  142. 'partner' => $partner,
  143. 'machine_code' => $machine_code,
  144. 'username' => $username,
  145. 'printname' => $printname,
  146. 'mobilephone' => $mobilephone,
  147. );
  148. $param['sign'] = $this->generateSign($param, $apiKey, $msign);
  149. $param['msign'] = $msign;
  150. $str = $this->getStr($param);
  151. return $this->sendCmd('http://open.10ss.net:8888/addprint.php', $str);
  152. }
  153. /**
  154. * 删除打印机
  155. * @param int $partner 用户ID
  156. * @param string $machine_code 打印机终端号
  157. * @param string $apiKey API密钥
  158. * @param string $msign 打印机密钥
  159. * @return string
  160. */
  161. public function actionRemoveprinter($partner, $machine_code, $apiKey, $msign)
  162. {
  163. $param = array(
  164. 'partner' => $partner,
  165. 'machine_code' => $machine_code,
  166. );
  167. $param['sign'] = $this->generateSign($param, $apiKey, $msign);
  168. $str = $this->getStr($param);
  169. return $this->sendCmd('http://open.10ss.net:8888/removeprint.php', $str);
  170. }
  171. }