| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 | <?phpnamespace 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, ['token' => $msg]];    }    public function getToken($data){        $account = $data['name'];        $password = $data['password'];        $token_key = config("j_rfid.login_redis_topic");        $token_key = $token_key . $account . $password;        $token = Redis::get($token_key);        if(! $token){            $url = config("j_rfid.login");            $expire_time = config("j_rfid.login_expire_time");            $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']];            //登录成功            $token = $result['token'];            Redis::set($token_key,$token);            Redis::expire($token_key, $expire_time);            return [true, $token];        }        return [true, $token];    }    public function getSite($data, $param){        $url = config("j_rfid.site");        $token = $param['token'];        $header = ["Authorization: {$token}"];        list($status,$result) = $this->get_helper($url,$header);        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'],        ];        $header = $param['header'];        list($status,$result) = $this->post_helper($url,json_encode($post),$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 = [            'produce_no' => $data['contract_no'],            'site' => $data['site'],        ];        $header = $param['header'];        list($status,$result) = $this->post_helper($url,json_encode($post),$header);        if(! $status) return [false, $result];        if(! empty($result['status']) && $result['status'] == 'error') return [false, $result['msg']];        return [true, $result];    }    public function post_helper($url, $data, $header = [], $timeout = 20){        file_put_contents('jrfid_record.txt',date('Y-m-d H:i:s') . "请求参数:" . $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') . "返回结果:" . $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') . "GET返回结果:" . $r . PHP_EOL,8);        return [true, json_decode($r, true)];    }}
 |