123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- <?php
- namespace App\Service;
- use Illuminate\Support\Facades\Config;
- use Illuminate\Support\Facades\Redis;
- class JRFIDServerService extends Service
- {
- public function loginRule($data){
- if(empty($data['name'])) return [false, '请输入账号!'];
- if(empty($data['password'])) return [false, '请输入密码!'];
- list($status, $msg) = $this->getToken($data);
- if(! $status) return [false, $msg];
- return [true, ['data' => $msg]];
- }
- public function getToken($data){
- $account = $data['name'];
- $password = $data['password'];
- $url = config("j_rfid.login");
- $post = [
- "name" => $account,
- "password" => $password,
- "rememberMe" => true
- ];
- $header = ['Content-Type:application/json'];
- list($status, $result) = $this->post_helper($url,json_encode($post), $header);
- if(! $status) return [false, $result];
- //登录失败
- if(! empty($result['errorMessage'])) return [false, $result['errorMessage']];
- return [true, $result];
- }
- public function getSite($data){
- $url = config("j_rfid.site");
- list($status,$result) = $this->get_helper($url);
- if(! $status) return [false, $result];
- if(! empty($result['errorMessage'])) return [false, $result['errorMessage']];
- return [true, $result];
- }
- public function getFlowByProduce($data,$param){
- if(empty($data['produce_no'])) return [false, '订单号不能为空'];
- if(empty($data['site'])) return [false, '站点不能为空'];
- $url = config("j_rfid.get_flow_by_produce");
- $post = [
- 'produce_no' => $data['produce_no'],
- 'site' => $data['site'],
- ];
- list($status,$result) = $this->post_helper($url,json_encode($post),$param['header']);
- if(! $status) return [false, $result];
- if(! empty($result['status']) && $result['status'] == 'error') return [false, $result['msg']];
- return [true, $result];
- }
- public function getProduceByContract($data,$param){
- if(empty($data['contract_no'])) return [false, '合同不能为空'];
- if(empty($data['site'])) return [false, '站点不能为空'];
- $url = config("j_rfid.get_produce_by_contract");
- $post = [
- 'contract_no' => $data['contract_no'],
- 'site' => $data['site'],
- ];
- list($status,$result) = $this->post_helper($url,json_encode($post),$param['header']);
- if(! $status) return [false, $result];
- if(! empty($result['status']) && $result['status'] == 'error') return [false, $result['msg']];
- return [true, $result];
- }
- public function getPrintData($data){
- if(empty($data['id'])) return [false, '数据ID不能为空'];
- if(empty($data['type'])) return [false, '打印数据类型不能为空'];
- $rsaService = new RsaEncryptionService();
- $dataToEncrypt = [
- 'id' => [$data['id']],
- 'type' => $data['type'],
- ];
- $encryptedData = $rsaService->encrypt(json_encode($dataToEncrypt));
- $url = config("j_rfid.get_print_data");
- $post = [
- 'body' => $encryptedData,
- ];
- list($status,$result) = $this->post_helper($url,json_encode($post),['Content-Type:application/json']);
- if(! $status) return [false, $result];
- if(! empty($result['status']) && $result['status'] == 'error') return [false, $result['message']];
- return [true, $result];
- }
- public function getTeam($data,$param){
- if(empty($data['site'])) return [false, '站点不能为空'];
- $url = config("j_rfid.get_team");
- $post['rules'] = [
- [
- 'field' => 'site',
- 'option' => 'LIKE_ANYWHERE',
- 'values' => [$data['site']]
- ]
- ];
- list($status,$result) = $this->post_helper($url,json_encode($post),$param['header']);
- if(! $status) return [false, $result];
- if(! empty($result['status']) && $result['status'] == 'error') return [false, $result['msg']];
- return [true, $result['content'] ?? []];
- }
- public function post_helper($url, $data, $header = [], $timeout = 20){
- 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);
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_ENCODING, '');
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
- curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
- if(!is_null($data)) curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
- $r = curl_exec($ch);
- if ($r === false) {
- // 获取错误号
- $errorNumber = curl_errno($ch);
- // 获取错误信息
- $errorMessage = curl_error($ch);
- return [false, "cURL Error #{$errorNumber}: {$errorMessage}"];
- }
- curl_close($ch);
- file_put_contents('jrfid_record.txt',date('Y-m-d H:i:s') . PHP_EOL . "返回结果:" . $r . PHP_EOL,8);
- return [true, json_decode($r, true)];
- }
- public function get_helper($url,$header=[],$timeout = 20){
- $ch = curl_init();
- curl_setopt_array($ch, array(
- CURLOPT_URL => $url,
- CURLOPT_RETURNTRANSFER => true,
- CURLOPT_ENCODING => '',
- CURLOPT_MAXREDIRS => 10,
- CURLOPT_TIMEOUT => $timeout,
- CURLOPT_FOLLOWLOCATION => true,
- CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
- CURLOPT_CUSTOMREQUEST => 'GET',
- CURLOPT_SSL_VERIFYPEER => false,
- CURLOPT_HTTPHEADER => $header,
- ));
- $r = curl_exec($ch);
- if ($r === false) {
- // 获取错误号
- $errorNumber = curl_errno($ch);
- // 获取错误信息
- $errorMessage = curl_error($ch);
- return [false, "cURL Error #{$errorNumber}: {$errorMessage}"];
- }
- curl_close($ch);
- file_put_contents('jrfid_record.txt',date('Y-m-d H:i:s') . PHP_EOL . "GET返回结果:" . $r . PHP_EOL,8);
- return [true, json_decode($r, true)];
- }
- }
|