Fixed LCSC provider search

Fixes issue #1440
This commit is contained in:
Jan Böhmer 2026-07-19 23:52:33 +02:00
parent a47e56bd93
commit c6983c6516

View file

@ -136,7 +136,9 @@ class LCSCProvider implements BatchInfoProviderInterface, URLHandlerInfoProvider
} }
} }
$response = $this->lcscClient->request('POST', self::ENDPOINT_URL . "/search/v2/global", [ //First we try the search v3 endpoint, which seems to give better results including pictures, but it only works
//on quite exact mpn matches
$response = $this->lcscClient->request('POST', self::ENDPOINT_URL . "/search/v3/global", [
'headers' => [ 'headers' => [
'Cookie' => new Cookie('currencyCode', $this->settings->currency) 'Cookie' => new Cookie('currencyCode', $this->settings->currency)
], ],
@ -147,18 +149,25 @@ class LCSCProvider implements BatchInfoProviderInterface, URLHandlerInfoProvider
$arr = $response->toArray(); $arr = $response->toArray();
// Get products list //If we get exact matches, use them
$products = $arr['result']['productSearchResultVO']['productList'] ?? []; if (!empty($arr['result']['exactMatchResult'])) {
// Get product tip $products = $arr['result']['a'];
$tipProductCode = $arr['result']['tipProductDetailUrlVO']['productCode'] ?? null; } else { //Otherwise fallback onto the third search endpoint, which has a worse data quality but is more likely to return results for vague search terms
$response = $this->lcscClient->request('POST', self::ENDPOINT_URL."/search/third", [
'headers' => [
'Cookie' => new Cookie('currencyCode', $this->settings->currency)
],
'json' => [
'keyword' => $term,
'currentPage' => 1,
'pageSize' => 10,
],
]);
$result = []; $arr = $response->toArray();
// LCSC does not display LCSC codes in the search, instead taking you directly to the // Get products list
// detailed product listing. It does so utilizing a product tip field. $products = $arr['result']['productList'] ?? [];
// If product tip exists and there are no products in the product list try a detail query
if (count($products) === 0 && $tipProductCode !== null) {
$result[] = $this->queryDetail($tipProductCode, $lightweight);
} }
foreach ($products as $product) { foreach ($products as $product) {
@ -219,7 +228,7 @@ class LCSCProvider implements BatchInfoProviderInterface, URLHandlerInfoProvider
provider_key: $this->getProviderKey(), provider_key: $this->getProviderKey(),
provider_id: $product['productCode'], provider_id: $product['productCode'],
name: $product['productModel'], name: $product['productModel'],
description: $this->sanitizeField($product['productIntroEn']), description: $this->sanitizeField($product['productIntroEn']) ?? '',
category: $this->sanitizeField($category ?? null), category: $this->sanitizeField($category ?? null),
manufacturer: $this->sanitizeField($product['brandNameEn'] ?? null), manufacturer: $this->sanitizeField($product['brandNameEn'] ?? null),
mpn: $this->sanitizeField($product['productModel'] ?? null), mpn: $this->sanitizeField($product['productModel'] ?? null),
@ -383,7 +392,7 @@ class LCSCProvider implements BatchInfoProviderInterface, URLHandlerInfoProvider
]); ]);
} else { } else {
// Search API call for other terms // Search API call for other terms
$responses[$keyword] = $this->lcscClient->request('POST', self::ENDPOINT_URL . "/search/v2/global", [ $responses[$keyword] = $this->lcscClient->request('POST', self::ENDPOINT_URL . "/search/v3/global", [
'headers' => [ 'headers' => [
'Cookie' => new Cookie('currencyCode', $this->settings->currency) 'Cookie' => new Cookie('currencyCode', $this->settings->currency)
], ],