feat: 产品搜索页

This commit is contained in:
2025-05-13 15:47:38 +08:00
parent f7551cad0d
commit 54a0175efa
3 changed files with 68 additions and 22 deletions

View File

@@ -154,6 +154,39 @@ class Product extends Common
return View::fetch('category');
}
/**
* 产品搜索
*/
public function search()
{
$keywords = request()->param('keywords', '');
// 关键词搜索
$products = ProductModel::field([
'id',
'name',
'short_name',
'cover_image',
'spu'
])
->where(fn ($query) => $query->withSearch(['keywords'], ['keywords' => $keywords]))
->language($this->lang_id)
->enabled(true)
->onSale(true)
->onShelves(true)
->order(['sort' => 'asc', 'id' => 'desc'])
->select()
->each(function ($item) use($keywords) {
$item['spu'] = str_replace($keywords, '<strong class="redpoint">'.$keywords.'</strong>', $item['spu']);
$item['name'] = str_replace($keywords, '<strong class="redpoint">'.$keywords.'</strong>', $item['name']);
$item['short_name'] = str_replace($keywords, '<strong class="redpoint">'.$keywords.'</strong>', $item['short_name']);
return $item;
});
View::assign('products', $products);
return View::fetch('search');
}
/**
* 产品详情页
*/