feat: 产品搜索页

This commit is contained in:
2025-05-13 15:47:38 +08:00
parent e431dcd6d0
commit 2e29b169bf
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');
}
/**
* 产品详情页
*/

View File

@@ -4,6 +4,7 @@ declare (strict_types = 1);
namespace app\index\model;
use app\common\model\ProductBaseModel;
use think\facade\Db;
use think\model\concern\SoftDelete;
/**
@@ -74,4 +75,10 @@ class ProductModel extends ProductBaseModel
{
$query->where('is_new', '=', (int)$stat);
}
// 关键词搜索
public function searchKeywordsAttr($query, string $keywords)
{
$query->whereRaw('BINARY spu LIKE "%' . $keywords . '%" OR BINARY name LIKE "%' . $keywords . '%" OR BINARY short_name LIKE "%' . $keywords . '%"');
}
}

View File

@@ -5,27 +5,33 @@
{block name="main"}
<!--内容 -->
<div class="orico_Page_search">
<div class="searchMain">
<!-- 搜索框 -->
<div class="search-ipt">
<input class="ssipt" placeholder="请搜索" />
<img src="/static/index/images/ssico.png" class="ssico" />
</div>
<!-- 搜索结果列表-->
<ul class="seul">
<a>
<li class="seitme">
<div class="imgb">
<img src="/static/index/images/ORCIO-HSQ-02H-800-220.jpg" class="search-pr-img" />
</div>
<div class="prInfp">
<div class="txt1"><strong class="redpoint">电脑</strong>显示器支架</div>
<div class="txt2"> ORICO-HSQ-02Q </div>
<div class="txt3"> ORICO-HSQ-02Q </div>
</div>
</li>
</a>
</ul>
</div>
<div class="searchMain">
<!-- 搜索框 -->
<form action="{:url('product/search')}" method="get">
<div class="search-ipt">
<input class="ssipt" name="keywords" value="{$Request.param.keywords}" placeholder="请搜索" />
<img src="__IMAGES__/ssico.png" class="ssico" />
</div>
</form>
<!-- 搜索结果列表-->
<ul class="seul">
{volist name="products" id="pro"}
<a href="{:url('product/detail', ['id' => $pro['id']])}">
<li class="seitme">
<div class="imgb">
<img src="{$pro.cover_image}" class="search-pr-img" />
</div>
<div class="prInfp">
<div class="txt1">{$pro.name|raw}</div>
{notempty name="pro.short_name"}
<div class="txt2">{$pro.short_name|raw}</div>
{/notempty}
<div class="txt3">{$pro.spu|raw}</div>
</div>
</li>
</a>
{/volist}
</ul>
</div>
</div>
{/block}