123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- <?php
- namespace App\Service;
- use Illuminate\Support\Facades\Log;
- class AssetServerService extends Service
- {
- public function loginRule($data){
- if(empty($data['name'])) return [false, '请输入账号!'];
- if(empty($data['password'])) return [false, '请输入密码!'];
- return [true, ''];
- 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("asset.login");
- $post = [
- "name" => $account,
- "password" => $password,
- "rememberMe" => true
- ];
- $header = ['Content-Type:application/json'];
- list($status, $result) = $this->post_helper($url,$post, $header);
- if(! $status) return [$status, $result];
- //登录失败
- if(! empty($result['errorMessage'])) return [false, $result['errorMessage']];
- return [true, $result];
- }
- public function pdList($data,$param){
- $url = config("asset.pdList");
- $post['size'] = $data['size'] ?? 10;
- $post['number'] = ($data['number'] ?? 1) - 1;
- list($status,$result) = $this->post_helper($url,$post,$param['header']);
- if(! $status) return [$status, $result];
- if(! empty($result['status']) && $result['status'] == 'error') return [false, $result['msg']];
- if(! empty($result['type']) && $result['type'] == 'errorVm') return [false, $result['message']];
- if(! isset($result['content'])) {
- $error = $result[0]['message'] ?? "操作失败,请刷新页面";
- return [false, $error];
- }
- return [true, $result];
- }
- public function pdDetail($data,$param){
- if(empty($data['id'])) return [false, '请选择盘点单!'];
- $url = config("asset.pdDetail");
- $url .= $data['id'];
- list($status,$result) = $this->get_helper($url,$param['header']);
- if(! $status) return [$status, $result];
- if(! empty($result['status']) && $result['status'] == 'error') return [false, $result['msg']];
- if(! empty($result['type']) && $result['type'] == 'errorVm') return [false, $result['message']];
- return [true, $result];
- }
- public function pdUpdate($data,$param){
- if(! isset($data['tourism_inventory_a'])) return [false, '盘点单抬头数据不能为空'];
- if(! isset($data['tourism_inventory_a']['id'])) return [false, '盘点单抬头数据ID不能为空'];
- if(! isset($data['tourism_inventory_dtl_b'])) return [false, '盘点单明细数据不能为空'];
- $url = config("asset.updatePd");
- $post = [
- 'tourism_inventory_a' => $data['tourism_inventory_a'],
- 'tourism_inventory_dtl_b' => $data['tourism_inventory_dtl_b'],
- ];
- list($status,$result) = $this->post_helper($url,$post,$param['header']);
- if(! $status) return [$status, $result];
- if(! empty($result['status']) && $result['status'] == 'error') return [false, $result['msg']];
- if(! empty($result['type']) && $result['type'] == 'errorVm') return [false, $result['message']];
- return [true, ''];
- }
- public function post_helper($url, $data, $header = [], $timeout = 20){
- Log::channel('apiLog')->info('资产盘点POST', ["api" => $url , "param" => $data ,"header" => $header]);
- $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, json_encode($data));
- $r = curl_exec($ch);
- if ($r === false) {
- // 获取错误号
- $errorNumber = curl_errno($ch);
- // 获取错误信息
- $errorMessage = curl_error($ch);
- $message = "cURL Error #{$errorNumber}: {$errorMessage}";
- Log::channel('apiLog')->info('资产盘点POST结果', ["message" => $message]);
- return [false, $message];
- }
- curl_close($ch);
- $return = json_decode($r, true);
- Log::channel('apiLog')->info('资产盘点POST结果', ["message" => $return]);
- if(! empty($return['message']) && $return['message'] == 'error.unAuthorized') return [0, '登录凭证已失效或者不正确'];
- return [true, $return];
- }
- public function get_helper($url,$header=[],$timeout = 20){
- Log::channel('apiLog')->info('资产盘点GET', ["api" => $url ,"header" => $header]);
- $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);
- $message = "cURL Error #{$errorNumber}: {$errorMessage}";
- Log::channel('apiLog')->info('资产盘点GET', ["message" => $message]);
- return [false, $message];
- }
- curl_close($ch);
- $return = json_decode($r, true);
- Log::channel('apiLog')->info('资产盘点GET', ["message" => $return]);
- if(! empty($return['message']) && $return['message'] == 'error.unAuthorized') return [0, '登录凭证已失效或者不正常'];
- return [true, $return];
- }
- }
|