|
...
|
...
|
@@ -160,4 +160,95 @@ class ProductController extends BaseController |
|
|
|
}
|
|
|
|
return $category_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Request $request
|
|
|
|
* @author zbj
|
|
|
|
* @date 2024/1/22
|
|
|
|
*/
|
|
|
|
protected function searchProduct(Request $request)
|
|
|
|
{
|
|
|
|
$project_id = $request->input('project_id');
|
|
|
|
$limit = $request->input('limit') ?: 5;
|
|
|
|
$text = $request->input('text');
|
|
|
|
$key = $request->input('key') ?: 'title';
|
|
|
|
$key_limit = $request->input('key_limit') ?: 15;
|
|
|
|
|
|
|
|
$project = ProjectServer::useProject($project_id);
|
|
|
|
if (!$project) {
|
|
|
|
$this->response('项目不存在', Code::SYSTEM_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
//匹配产品
|
|
|
|
$products = Product::with('category')
|
|
|
|
->where("title", 'like', $text . '%')
|
|
|
|
->where("status", 1)
|
|
|
|
->orderBy("id", "DESC")
|
|
|
|
->limit($limit)
|
|
|
|
->select('title', 'thumb', 'id', 'route')
|
|
|
|
->get()
|
|
|
|
->toArray();
|
|
|
|
|
|
|
|
//对应分类
|
|
|
|
$categories = [];
|
|
|
|
foreach ($products as &$product) {
|
|
|
|
foreach ($product['category'] as $category) {
|
|
|
|
$categories[$category['route']] = [
|
|
|
|
'title' => $category['title'],
|
|
|
|
'route' => '/' . $category['route'] . '/',
|
|
|
|
];
|
|
|
|
}
|
|
|
|
unset($product['id']);
|
|
|
|
unset($product['category']);
|
|
|
|
|
|
|
|
if(!empty($product['thumb']) && !empty($product['thumb']['url'])){
|
|
|
|
$product['thumb'] = getImageUrl($product['thumb']['url'],$project['storage_type'] ?? 0,$project['project_location']);
|
|
|
|
}
|
|
|
|
|
|
|
|
$product['route'] = '/' . $product['route'] . '/';
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'products' => $products,
|
|
|
|
'categories' => array_values($categories),
|
|
|
|
'suggestions' => $this->searchSuggestion($text, $key, $key_limit)
|
|
|
|
];
|
|
|
|
|
|
|
|
$this->response('success', Code::SUCCESS, $data);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function searchSuggestion($text, $key, $key_limit): array
|
|
|
|
{
|
|
|
|
$model = new Product();
|
|
|
|
$columns = $model->getConnection()->getSchemaBuilder()->getColumnListing($model->getTable());
|
|
|
|
|
|
|
|
//产品字段
|
|
|
|
if (in_array($key, $columns)) {
|
|
|
|
//匹配产品
|
|
|
|
$suggestions = Product::where("status", 1)
|
|
|
|
->where($key, 'like', $text . '%')
|
|
|
|
->orderBy("id", "DESC")
|
|
|
|
->limit($key_limit)
|
|
|
|
->select($key .' as title', 'route')
|
|
|
|
->get()
|
|
|
|
->toArray();
|
|
|
|
} else {
|
|
|
|
//扩展字段
|
|
|
|
$suggestions = Product::leftJoin('gl_product_extend_info as pei', 'gl_product.id', '=', 'pei.product_id')
|
|
|
|
->where('pei.values', 'like', $text . '%')
|
|
|
|
->where("gl_product.status", 1)
|
|
|
|
->orderBy("gl_product.id", "DESC")
|
|
|
|
->limit($key_limit)
|
|
|
|
->select('pei.values','gl_product.route')
|
|
|
|
->get()
|
|
|
|
->toArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($suggestions as &$suggestion){
|
|
|
|
$suggestion['route'] = '/' . $suggestion['route'] . '/';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $suggestions;
|
|
|
|
}
|
|
|
|
} |
...
|
...
|
|