JRFIDServerService.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. namespace App\Service;
  3. use Illuminate\Support\Facades\Config;
  4. use Illuminate\Support\Facades\Redis;
  5. class JRFIDServerService extends Service
  6. {
  7. public function loginRule($data){
  8. if(empty($data['name'])) return [false, '请输入账号!'];
  9. if(empty($data['password'])) return [false, '请输入密码!'];
  10. list($status, $msg) = $this->getToken($data);
  11. if(! $status) return [false, $msg];
  12. return [true, ['data' => $msg]];
  13. }
  14. public function getToken($data){
  15. $account = $data['name'];
  16. $password = $data['password'];
  17. $url = config("j_rfid.login");
  18. $post = [
  19. "name" => $account,
  20. "password" => $password,
  21. "rememberMe" => true
  22. ];
  23. $header = ['Content-Type:application/json'];
  24. list($status, $result) = $this->post_helper($url,json_encode($post), $header);
  25. if(! $status) return [false, $result];
  26. //登录失败
  27. if(! empty($result['errorMessage'])) return [false, $result['errorMessage']];
  28. return [true, $result];
  29. }
  30. public function getSite($data){
  31. $url = config("j_rfid.site");
  32. list($status,$result) = $this->get_helper($url);
  33. if(! $status) return [false, $result];
  34. if(! empty($result['errorMessage'])) return [false, $result['errorMessage']];
  35. return [true, $result];
  36. }
  37. public function getFlowByProduce($data,$param){
  38. if(empty($data['produce_no'])) return [false, '订单号不能为空'];
  39. if(empty($data['site'])) return [false, '站点不能为空'];
  40. $url = config("j_rfid.get_flow_by_produce");
  41. $post = [
  42. 'produce_no' => $data['produce_no'],
  43. 'site' => $data['site'],
  44. ];
  45. list($status,$result) = $this->post_helper($url,json_encode($post),$param['header']);
  46. if(! $status) return [false, $result];
  47. if(! empty($result['status']) && $result['status'] == 'error') return [false, $result['msg']];
  48. return [true, $result];
  49. }
  50. public function getProduceByContract($data,$param){
  51. if(empty($data['contract_no'])) return [false, '合同不能为空'];
  52. if(empty($data['site'])) return [false, '站点不能为空'];
  53. $url = config("j_rfid.get_produce_by_contract");
  54. $post = [
  55. 'contract_no' => $data['contract_no'],
  56. 'site' => $data['site'],
  57. ];
  58. list($status,$result) = $this->post_helper($url,json_encode($post),$param['header']);
  59. if(! $status) return [false, $result];
  60. if(! empty($result['status']) && $result['status'] == 'error') return [false, $result['msg']];
  61. return [true, $result];
  62. }
  63. public function getPrintData($data){
  64. if(empty($data['id'])) return [false, '数据ID不能为空'];
  65. if(empty($data['type'])) return [false, '打印数据类型不能为空'];
  66. $rsaService = new RsaEncryptionService();
  67. $dataToEncrypt = [
  68. 'id' => [$data['id']],
  69. 'type' => $data['type'],
  70. ];
  71. $encryptedData = $rsaService->encrypt(json_encode($dataToEncrypt));
  72. $url = config("j_rfid.get_print_data");
  73. $post = [
  74. 'body' => $encryptedData,
  75. ];
  76. list($status,$result) = $this->post_helper($url,json_encode($post),['Content-Type:application/json']);
  77. if(! $status) return [false, $result];
  78. if(! empty($result['status']) && $result['status'] == 'error') return [false, $result['message']];
  79. return [true, $result];
  80. }
  81. public function getTeam($data,$param){
  82. if(empty($data['site'])) return [false, '站点不能为空'];
  83. $url = config("j_rfid.get_team");
  84. $post['rules'] = [
  85. [
  86. 'field' => 'site',
  87. 'option' => 'LIKE_ANYWHERE',
  88. 'values' => [$data['site']]
  89. ]
  90. ];
  91. list($status,$result) = $this->post_helper($url,json_encode($post),$param['header']);
  92. if(! $status) return [false, $result];
  93. if(! empty($result['status']) && $result['status'] == 'error') return [false, $result['msg']];
  94. return [true, $result['content'] ?? []];
  95. }
  96. public function post_helper($url, $data, $header = [], $timeout = 20){
  97. file_put_contents('jrfid_record.txt',date('Y-m-d H:i:s') . PHP_EOL. "请求API:" . $url . PHP_EOL . "请求参数:" . $data . PHP_EOL . "请求头部:" . json_encode($header) . PHP_EOL,8);
  98. $ch = curl_init();
  99. curl_setopt($ch, CURLOPT_URL, $url);
  100. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  101. curl_setopt($ch, CURLOPT_ENCODING, '');
  102. curl_setopt($ch, CURLOPT_POST, 1);
  103. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
  104. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  105. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  106. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  107. if(!is_null($data)) curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  108. $r = curl_exec($ch);
  109. if ($r === false) {
  110. // 获取错误号
  111. $errorNumber = curl_errno($ch);
  112. // 获取错误信息
  113. $errorMessage = curl_error($ch);
  114. return [false, "cURL Error #{$errorNumber}: {$errorMessage}"];
  115. }
  116. curl_close($ch);
  117. file_put_contents('jrfid_record.txt',date('Y-m-d H:i:s') . PHP_EOL . "返回结果:" . $r . PHP_EOL,8);
  118. return [true, json_decode($r, true)];
  119. }
  120. public function get_helper($url,$header=[],$timeout = 20){
  121. $ch = curl_init();
  122. curl_setopt_array($ch, array(
  123. CURLOPT_URL => $url,
  124. CURLOPT_RETURNTRANSFER => true,
  125. CURLOPT_ENCODING => '',
  126. CURLOPT_MAXREDIRS => 10,
  127. CURLOPT_TIMEOUT => $timeout,
  128. CURLOPT_FOLLOWLOCATION => true,
  129. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  130. CURLOPT_CUSTOMREQUEST => 'GET',
  131. CURLOPT_SSL_VERIFYPEER => false,
  132. CURLOPT_HTTPHEADER => $header,
  133. ));
  134. $r = curl_exec($ch);
  135. if ($r === false) {
  136. // 获取错误号
  137. $errorNumber = curl_errno($ch);
  138. // 获取错误信息
  139. $errorMessage = curl_error($ch);
  140. return [false, "cURL Error #{$errorNumber}: {$errorMessage}"];
  141. }
  142. curl_close($ch);
  143. file_put_contents('jrfid_record.txt',date('Y-m-d H:i:s') . PHP_EOL . "GET返回结果:" . $r . PHP_EOL,8);
  144. return [true, json_decode($r, true)];
  145. }
  146. }