JRFIDServerService.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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, ['token' => $msg]];
  13. }
  14. public function getToken($data){
  15. $account = $data['name'];
  16. $password = $data['password'];
  17. $token_key = config("j_rfid.login_redis_topic");
  18. $token_key = $token_key . $account . $password;
  19. $token = Redis::get($token_key);
  20. if(! $token){
  21. $url = config("j_rfid.login");
  22. $expire_time = config("j_rfid.login_expire_time");
  23. $post = [
  24. "name" => $account,
  25. "password" => $password,
  26. "rememberMe" => true
  27. ];
  28. $header = ['Content-Type:application/json'];
  29. list($status, $result) = $this->post_helper($url,json_encode($post), $header);
  30. if(! $status) return [false, $result];
  31. //登录失败
  32. if(! empty($result['errorMessage'])) return [false, $result['errorMessage']];
  33. //登录成功
  34. $token = $result['token'];
  35. Redis::set($token_key,$token);
  36. Redis::expire($token_key, $expire_time);
  37. return [true, $token];
  38. }
  39. return [true, $token];
  40. }
  41. public function getSite($data, $param){
  42. $url = config("j_rfid.site");
  43. $token = $param['token'];
  44. $header = ["Authorization: {$token}"];
  45. list($status,$result) = $this->get_helper($url,$header);
  46. if(! $status) return [false, $result];
  47. if(! empty($result['errorMessage'])) return [false, $result['errorMessage']];
  48. return [true, $result];
  49. }
  50. public function getFlowByProduce($data,$param){
  51. if(empty($data['produce_no'])) return [false, '订单号不能为空'];
  52. if(empty($data['site'])) return [false, '站点不能为空'];
  53. $url = config("j_rfid.get_flow_by_produce");
  54. $post = [
  55. 'produce_no' => $data['produce_no'],
  56. 'site' => $data['site'],
  57. ];
  58. $header = $param['header'];
  59. list($status,$result) = $this->post_helper($url,json_encode($post),$header);
  60. if(! $status) return [false, $result];
  61. if(! empty($result['status']) && $result['status'] == 'error') return [false, $result['msg']];
  62. return [true, $result];
  63. }
  64. public function getProduceByContract($data,$param){
  65. if(empty($data['contract_no'])) return [false, '合同不能为空'];
  66. if(empty($data['site'])) return [false, '站点不能为空'];
  67. $url = config("j_rfid.get_produce_by_contract");
  68. $post = [
  69. 'contract_no' => $data['contract_no'],
  70. 'site' => $data['site'],
  71. ];
  72. $header = $param['header'];
  73. list($status,$result) = $this->post_helper($url,json_encode($post),$header);
  74. if(! $status) return [false, $result];
  75. if(! empty($result['status']) && $result['status'] == 'error') return [false, $result['msg']];
  76. return [true, $result];
  77. }
  78. public function post_helper($url, $data, $header = [], $timeout = 20){
  79. file_put_contents('jrfid_record.txt',date('Y-m-d H:i:s') . "请求参数:" . $data . PHP_EOL . "请求头部:" . json_encode($header) . PHP_EOL,8);
  80. $ch = curl_init();
  81. curl_setopt($ch, CURLOPT_URL, $url);
  82. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  83. curl_setopt($ch, CURLOPT_ENCODING, '');
  84. curl_setopt($ch, CURLOPT_POST, 1);
  85. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
  86. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  87. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  88. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  89. if(!is_null($data)) curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  90. $r = curl_exec($ch);
  91. if ($r === false) {
  92. // 获取错误号
  93. $errorNumber = curl_errno($ch);
  94. // 获取错误信息
  95. $errorMessage = curl_error($ch);
  96. return [false, "cURL Error #{$errorNumber}: {$errorMessage}"];
  97. }
  98. curl_close($ch);
  99. file_put_contents('jrfid_record.txt',date('Y-m-d H:i:s') . "返回结果:" . $r . PHP_EOL,8);
  100. return [true, json_decode($r, true)];
  101. }
  102. public function get_helper($url,$header=[],$timeout = 20){
  103. $ch = curl_init();
  104. curl_setopt_array($ch, array(
  105. CURLOPT_URL => $url,
  106. CURLOPT_RETURNTRANSFER => true,
  107. CURLOPT_ENCODING => '',
  108. CURLOPT_MAXREDIRS => 10,
  109. CURLOPT_TIMEOUT => $timeout,
  110. CURLOPT_FOLLOWLOCATION => true,
  111. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  112. CURLOPT_CUSTOMREQUEST => 'GET',
  113. CURLOPT_SSL_VERIFYPEER => false,
  114. CURLOPT_HTTPHEADER => $header,
  115. ));
  116. $r = curl_exec($ch);
  117. if ($r === false) {
  118. // 获取错误号
  119. $errorNumber = curl_errno($ch);
  120. // 获取错误信息
  121. $errorMessage = curl_error($ch);
  122. return [false, "cURL Error #{$errorNumber}: {$errorMessage}"];
  123. }
  124. curl_close($ch);
  125. file_put_contents('jrfid_record.txt',date('Y-m-d H:i:s') . "GET返回结果:" . $r . PHP_EOL,8);
  126. return [true, json_decode($r, true)];
  127. }
  128. }