JRFIDServerService.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. list($status,$result) = $this->get_helper($url,$param['header']);
  44. if(! $status) return [false, $result];
  45. if(! empty($result['errorMessage'])) return [false, $result['errorMessage']];
  46. return [true, $result];
  47. }
  48. public function getFlowByProduce($data,$param){
  49. if(empty($data['produce_no'])) return [false, '订单号不能为空'];
  50. if(empty($data['site'])) return [false, '站点不能为空'];
  51. $url = config("j_rfid.get_flow_by_produce");
  52. $post = [
  53. 'produce_no' => $data['produce_no'],
  54. 'site' => $data['site'],
  55. ];
  56. list($status,$result) = $this->post_helper($url,json_encode($post),$param['header']);
  57. if(! $status) return [false, $result];
  58. if(! empty($result['status']) && $result['status'] == 'error') return [false, $result['msg']];
  59. return [true, $result];
  60. }
  61. public function getProduceByContract($data,$param){
  62. if(empty($data['contract_no'])) return [false, '合同不能为空'];
  63. if(empty($data['site'])) return [false, '站点不能为空'];
  64. $url = config("j_rfid.get_produce_by_contract");
  65. $post = [
  66. 'contract_no' => $data['contract_no'],
  67. 'site' => $data['site'],
  68. ];
  69. list($status,$result) = $this->post_helper($url,json_encode($post),$param['header']);
  70. if(! $status) return [false, $result];
  71. if(! empty($result['status']) && $result['status'] == 'error') return [false, $result['msg']];
  72. return [true, $result];
  73. }
  74. public function post_helper($url, $data, $header = [], $timeout = 20){
  75. file_put_contents('jrfid_record.txt',date('Y-m-d H:i:s') . "请求参数:" . $data . PHP_EOL . "请求头部:" . json_encode($header) . PHP_EOL,8);
  76. $ch = curl_init();
  77. curl_setopt($ch, CURLOPT_URL, $url);
  78. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  79. curl_setopt($ch, CURLOPT_ENCODING, '');
  80. curl_setopt($ch, CURLOPT_POST, 1);
  81. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
  82. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  83. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  84. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  85. if(!is_null($data)) curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  86. $r = curl_exec($ch);
  87. if ($r === false) {
  88. // 获取错误号
  89. $errorNumber = curl_errno($ch);
  90. // 获取错误信息
  91. $errorMessage = curl_error($ch);
  92. return [false, "cURL Error #{$errorNumber}: {$errorMessage}"];
  93. }
  94. curl_close($ch);
  95. file_put_contents('jrfid_record.txt',date('Y-m-d H:i:s') . "返回结果:" . $r . PHP_EOL,8);
  96. return [true, json_decode($r, true)];
  97. }
  98. public function get_helper($url,$header=[],$timeout = 20){
  99. $ch = curl_init();
  100. curl_setopt_array($ch, array(
  101. CURLOPT_URL => $url,
  102. CURLOPT_RETURNTRANSFER => true,
  103. CURLOPT_ENCODING => '',
  104. CURLOPT_MAXREDIRS => 10,
  105. CURLOPT_TIMEOUT => $timeout,
  106. CURLOPT_FOLLOWLOCATION => true,
  107. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  108. CURLOPT_CUSTOMREQUEST => 'GET',
  109. CURLOPT_SSL_VERIFYPEER => false,
  110. CURLOPT_HTTPHEADER => $header,
  111. ));
  112. $r = curl_exec($ch);
  113. if ($r === false) {
  114. // 获取错误号
  115. $errorNumber = curl_errno($ch);
  116. // 获取错误信息
  117. $errorMessage = curl_error($ch);
  118. return [false, "cURL Error #{$errorNumber}: {$errorMessage}"];
  119. }
  120. curl_close($ch);
  121. file_put_contents('jrfid_record.txt',date('Y-m-d H:i:s') . "GET返回结果:" . $r . PHP_EOL,8);
  122. return [true, json_decode($r, true)];
  123. }
  124. }