|
@@ -0,0 +1,153 @@
|
|
|
|
+<?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']];
|
|
|
|
+
|
|
|
|
+ if(! isset($result['content'])) {
|
|
|
|
+ $error = $result[0]['message'] ?? "操作失败,请刷新页面";
|
|
|
|
+ return [false, $error];
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return [true, $result];
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ 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];
|
|
|
|
+ }
|
|
|
|
+}
|