JRFIDServerService.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. 'encrypted_data' => $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['msg']];
  79. return [true, $result];
  80. }
  81. public function post_helper($url, $data, $header = [], $timeout = 20){
  82. file_put_contents('jrfid_record.txt',date('Y-m-d H:i:s') . "请求参数:" . $data . PHP_EOL . "请求头部:" . json_encode($header) . PHP_EOL,8);
  83. $ch = curl_init();
  84. curl_setopt($ch, CURLOPT_URL, $url);
  85. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  86. curl_setopt($ch, CURLOPT_ENCODING, '');
  87. curl_setopt($ch, CURLOPT_POST, 1);
  88. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
  89. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  90. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  91. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  92. if(!is_null($data)) curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  93. $r = curl_exec($ch);
  94. if ($r === false) {
  95. // 获取错误号
  96. $errorNumber = curl_errno($ch);
  97. // 获取错误信息
  98. $errorMessage = curl_error($ch);
  99. return [false, "cURL Error #{$errorNumber}: {$errorMessage}"];
  100. }
  101. curl_close($ch);
  102. file_put_contents('jrfid_record.txt',date('Y-m-d H:i:s') . "返回结果:" . $r . PHP_EOL,8);
  103. return [true, json_decode($r, true)];
  104. }
  105. public function get_helper($url,$header=[],$timeout = 20){
  106. $ch = curl_init();
  107. curl_setopt_array($ch, array(
  108. CURLOPT_URL => $url,
  109. CURLOPT_RETURNTRANSFER => true,
  110. CURLOPT_ENCODING => '',
  111. CURLOPT_MAXREDIRS => 10,
  112. CURLOPT_TIMEOUT => $timeout,
  113. CURLOPT_FOLLOWLOCATION => true,
  114. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  115. CURLOPT_CUSTOMREQUEST => 'GET',
  116. CURLOPT_SSL_VERIFYPEER => false,
  117. CURLOPT_HTTPHEADER => $header,
  118. ));
  119. $r = curl_exec($ch);
  120. if ($r === false) {
  121. // 获取错误号
  122. $errorNumber = curl_errno($ch);
  123. // 获取错误信息
  124. $errorMessage = curl_error($ch);
  125. return [false, "cURL Error #{$errorNumber}: {$errorMessage}"];
  126. }
  127. curl_close($ch);
  128. file_put_contents('jrfid_record.txt',date('Y-m-d H:i:s') . "GET返回结果:" . $r . PHP_EOL,8);
  129. return [true, json_decode($r, true)];
  130. }
  131. }