From dbcf3769d7832b3ecfde5a9c86fc8a818cf0d198 Mon Sep 17 00:00:00 2001 From: liu <1873441552@qq.com> Date: Tue, 19 Mar 2024 17:47:29 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=84=E7=90=86=E8=B0=83=E7=94=A8=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E7=AC=AC=E4=B8=89=E6=96=B9=E6=9D=A1=E7=A0=81=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controller/admin/ProductLibrary.php | 51 +++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 4 deletions(-) diff --git a/app/controller/admin/ProductLibrary.php b/app/controller/admin/ProductLibrary.php index caba68ce..e04f1ea8 100644 --- a/app/controller/admin/ProductLibrary.php +++ b/app/controller/admin/ProductLibrary.php @@ -99,11 +99,17 @@ class ProductLibrary extends BaseController }else{ $codes=$code; } - $client = new Client("b1eb52b9-0379-4c56-b795-47d90a73ca6a"); - $result = $client->barcodeQuery() - ->withCode($codes) - ->request(); + $appCode = 'b1eb52b9-0379-4c56-b795-47d90a73ca6a'; + $barcode = $codes; + $url = 'https://api.topthink.com/barcode/query?' . http_build_query(['appCode' => $appCode, 'code' => $barcode]); + + $result = $this->requestApi($url); + +// $client = new Client("b1eb52b9-0379-4c56-b795-47d90a73ca6a"); +// $result = $client->barcodeQuery() +// ->withCode($codes) +// ->request(); try { if ($result['code'] == 0) { $upload = UploadService::create(); @@ -180,4 +186,41 @@ class ProductLibrary extends BaseController $a=$client->get($url); return $a->getBody()->getContents(); } + + + + public function requestApi($url, $method = 'GET', $data = []) { + $curl = curl_init(); + curl_setopt_array($curl, array( + CURLOPT_URL => $url, + CURLOPT_RETURNTRANSFER => true, + CURLOPT_ENCODING => '', + CURLOPT_MAXREDIRS => 10, + CURLOPT_TIMEOUT => 0, + CURLOPT_FOLLOWLOCATION => true, + CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, + CURLOPT_CUSTOMREQUEST => strtoupper($method), + CURLOPT_HTTPHEADER => array( + 'User-Agent: Apifox/1.0.0 (https://apifox.com)' + ), + )); + + // 如果是POST请求,添加POST数据 + if ($method === 'POST') { + curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data)); + } + $response = curl_exec($curl); + curl_close($curl); + + // 将响应结果从JSON格式转换为PHP数组 + $responseData = json_decode($response, true); + // 检查是否成功解码 + if (json_last_error() === JSON_ERROR_NONE) { + return $responseData; + } else { + return app('json')->fail(json_last_error_msg()); + } + } + + }