Feie.Class.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: 清闲
  5. * Date: 2020/6/23
  6. * Time: 14:06 PM
  7. */
  8. namespace Util\CloudPrint;
  9. use Mall\Framework\Core\ResultWrapper;
  10. use Mall\Framework\Core\ErrorCode;
  11. class Feie
  12. {
  13. /**
  14. * 飞鹅云后台注册账号
  15. */
  16. private $user = 'phperstar@163.com';
  17. /**
  18. * 飞鹅云后台注册账号后生成的UKEY 【备注:这不是填打印机的KEY】
  19. */
  20. private $ukey = '7CW8fsUTR6dULwsS';
  21. /**
  22. * 打印机编号,必须要在管理后台里添加打印机或调用API接口添加之后,才能调用API
  23. */
  24. private $sn;
  25. /**
  26. * 打印机识别码
  27. */
  28. private $code;
  29. /**
  30. * 请求域名
  31. */
  32. private $apiUrl = 'api.feieyun.cn';
  33. /**
  34. * 请求地址
  35. * @var string
  36. */
  37. private $path = '/Api/Open/';
  38. /**
  39. * Feie constructor.
  40. * @param string $sn
  41. * @param string $code
  42. */
  43. public function __construct($sn = '', $code = '')
  44. {
  45. $this->sn = $sn;
  46. $this->code = $code;
  47. }
  48. /**
  49. * 添加打印机设备
  50. * @return ResultWrapper
  51. */
  52. public function adddev()
  53. {
  54. $time = time();
  55. $postData = [
  56. 'user' => $this->user,
  57. 'stime' => $time,
  58. 'sig' => sha1($this->user.$this->ukey.$time),
  59. 'apiname' => 'Open_printerAddlist',
  60. 'printerContent' => $this->sn.'#'.$this->code,//打印机编号(必填) # 打印机识别码(必填)
  61. ];
  62. $apiUrl = $this->apiUrl.$this->path;
  63. $reponse = request($apiUrl, $postData);
  64. if($reponse['httpcode'] != 200){
  65. return ResultWrapper::fail('请求外部系统接口报错', ErrorCode::$apiNotResult);
  66. }else{
  67. $reponseData = json_decode($reponse['content'], true);
  68. if( isset($reponseData['ret']) && $reponseData['ret'] != 0){
  69. return ResultWrapper::fail($reponseData['msg'],$reponseData['ret']);
  70. }
  71. return ResultWrapper::success($reponseData['data']);
  72. }
  73. }
  74. /**
  75. * 发送打印机信息
  76. * @param $params
  77. * @return ResultWrapper
  78. */
  79. public function sendMsg($params)
  80. {
  81. $time = time();
  82. $postData = [
  83. 'user' => $this->user,
  84. 'stime' => $time,
  85. 'sig' => sha1($this->user.$this->ukey.$time),
  86. 'apiname' => 'Open_printMsg',
  87. ];
  88. $postData['sn'] = $this->sn;
  89. $postData['content'] = $params;
  90. $apiUrl = $this->apiUrl.$this->path;
  91. $reponse = request($apiUrl, $postData);
  92. if($reponse['httpcode'] != 200){
  93. return ResultWrapper::fail('请求外部系统接口报错', ErrorCode::$apiNotResult);
  94. }else{
  95. $reponseData = json_decode($reponse['content'], true);
  96. if( isset($reponseData['ret']) && $reponseData['ret'] != 0){
  97. return ResultWrapper::fail($reponseData['msg'],$reponseData['ret']);
  98. }
  99. return ResultWrapper::success($reponseData['data']);
  100. }
  101. }
  102. }