Gainshca.Class.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: phperstar
  5. * Date: 2020/6/17
  6. * Time: 3:35 PM
  7. */
  8. namespace Util\CloudPrint;
  9. use JinDouYun\Controller\Common\Logger;
  10. use Mall\Framework\Core\ResultWrapper;
  11. use Mall\Framework\Core\ErrorCode;
  12. class Gainshca
  13. {
  14. /**
  15. * 商户编码
  16. */
  17. private $memberCode;
  18. /**
  19. * 终端编号
  20. */
  21. private $deviceID;
  22. /**
  23. * API密钥
  24. */
  25. private $apiKey;
  26. /**
  27. * 微信小程序接口主域名
  28. * @var string $apiUrl
  29. */
  30. private $apiUrl = 'http://api.poscom.cn/apisc/';
  31. public function __construct($memberCode='', $deviceID='', $apiKey = '')
  32. {
  33. $this->memberCode = $memberCode;
  34. $this->deviceID = $deviceID;
  35. $this->apiKey = $apiKey;
  36. }
  37. /**
  38. * 添加打印机设备
  39. */
  40. public function adddev()
  41. {
  42. $postData = [
  43. 'reqTime' => msectime(),
  44. 'securityCode' => md5($this->memberCode.msectime().$this->apiKey.$this->deviceID),
  45. 'memberCode' => $this->memberCode,
  46. 'deviceID' => $this->deviceID,
  47. 'devName' => '千鸟云商',
  48. ];
  49. $apiUrl = $this->apiUrl.'adddev';
  50. $reponse = request($apiUrl, $postData);
  51. if($reponse['httpcode'] != 200){
  52. return ResultWrapper::fail('请求外部系统接口报错', ErrorCode::$apiNotResult);
  53. }else{
  54. $reponseData = json_decode($reponse['content'], true);
  55. if( isset($reponseData['code']) && $reponseData['code'] != 1){
  56. return ResultWrapper::fail($reponseData['msg'], $reponseData['code']);
  57. }
  58. return ResultWrapper::success(['devID'=>$reponseData['devID']]);
  59. }
  60. }
  61. /**
  62. * 发送信息到打印机
  63. * @param $params
  64. * @return ResultWrapper
  65. */
  66. public function sendMsg($params)
  67. {
  68. $postData = [
  69. 'reqTime' => msectime(),
  70. 'securityCode' => md5($this->memberCode.$this->deviceID.msectime().$this->apiKey),
  71. 'memberCode' => $this->memberCode,
  72. 'deviceID' => $this->deviceID,
  73. 'mode' => 2,
  74. 'msgDetail' => $params
  75. ];
  76. $apiUrl = $this->apiUrl.'sendMsg';
  77. $reponse = request($apiUrl, $postData);
  78. if($reponse['httpcode'] != 200){
  79. return ResultWrapper::fail('请求外部系统接口报错', ErrorCode::$apiNotResult);
  80. }else{
  81. $reponseData = json_decode($reponse['content'], true);
  82. Logger::logs(E_USER_ERROR,'打印机返回信息',__CLASS__,__LINE__,$reponseData);
  83. if( isset($reponseData['code']) && $reponseData['code'] != 0){
  84. return ResultWrapper::fail($reponseData['msg'], $reponseData['code']);
  85. }
  86. return ResultWrapper::success(['devID'=>$reponseData['devID']]);
  87. }
  88. }
  89. /**
  90. * 查询打印机状态
  91. * @param $deviceID
  92. * @param $selectAll
  93. * @return ResultWrapper
  94. */
  95. public function getStatus($deviceID, $selectAll)
  96. {
  97. $postData = [
  98. 'reqTime' => msectime(),
  99. 'securityCode' => md5($this->memberCode.msectime().$this->apiKey),
  100. 'memberCode' => $this->memberCode,
  101. 'deviceID' => $this->deviceID,
  102. ];
  103. if($deviceID) $postData['deviceID'] = $deviceID;
  104. if($selectAll) unset($postData['deviceID']);
  105. $apiUrl = $this->apiUrl.'getStatus';
  106. $reponse = request($apiUrl, $postData);
  107. if($reponse['httpcode'] != 200){
  108. return ResultWrapper::fail('请求外部系统接口报错', ErrorCode::$apiNotResult);
  109. }else{
  110. $reponseData = json_decode($reponse['content'], true);
  111. if( isset($reponseData['code']) && $reponseData['code'] != 1){
  112. return ResultWrapper::fail($reponseData['msg'], $reponseData['code']);
  113. }
  114. return ResultWrapper::success($reponseData['statusList']);
  115. }
  116. }
  117. /**
  118. * 查询打印机状态
  119. * @param $deviceID
  120. * @param $selectAll
  121. * @return ResultWrapper
  122. */
  123. public function setVolume($num)
  124. {
  125. $postData = [
  126. 'reqTime' => msectime(),
  127. 'securityCode' => md5($this->memberCode.msectime().$this->apiKey.$this->deviceID),
  128. 'memberCode' => $this->memberCode,
  129. 'deviceID' => $this->deviceID,
  130. 'volume' => $num
  131. ];
  132. $apiUrl = $this->apiUrl.'sendVolume';
  133. $reponse = request($apiUrl, $postData);
  134. if($reponse['httpcode'] != 200){
  135. return ResultWrapper::fail('请求外部系统接口报错', ErrorCode::$apiNotResult);
  136. }else{
  137. $reponseData = json_decode($reponse['content'], true);
  138. if( isset($reponseData['code']) && $reponseData['code'] != 1){
  139. return ResultWrapper::fail($reponseData['msg'], $reponseData['code']);
  140. }
  141. return ResultWrapper::success(['devID'=>$reponseData['devID']]);
  142. }
  143. }
  144. }