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()); + } + } + + }