SearchController.php
6.0 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
namespace App\Http\Controllers\Api;
use App\Helper\Str;
use App\Http\Controllers\Controller;
use App\Models\Product\Product;
use App\Models\Product\ProductExtendInfo;
use App\Services\PageService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;
/***
* 快捷搜索
* Class SearchController
* @package App\Http\Controllers\Api
* @author zbj
* @date 2024/1/22
*/
class SearchController extends Controller
{
/**
* C端搜索框快捷搜索
* @param Request $request
* @return mixed
*/
public function index(Request $request)
{
$limit = $request->input('limit') ?: 5;
$text = $request->input('text');
$key = $request->input('key') ?: 'title';
$key_limit = $request->input('key_limit') ?: 15;
$project = $request->get('project');
if (!$text) {
$this->responseA([]);
}
//匹配产品标题及产品对应的分类
$data = $this->searchProduct($text, $limit, $project->id);
//匹配建议字段
$data['suggestions'] = $this->searchSuggestion($text, $key, $key_limit, $project->id);
$this->responseA($data);
}
/**
* @param $text
* @param $limit
* @return array
* @author zbj
* @date 2024/1/22
*/
protected function searchProduct($text, $limit, $project_id = 0): array
{
//匹配产品
$all_products = Cache::get('product_list_project_' . $project_id);
if($all_products){
$products = [];
foreach ($all_products as $product){
if(Str::contains($product['title'], $text)){
$products[] = [
'title' => $product['title'],
'thumb' => $product['thumb'],
'id' => $product['id'],
'route' => $product['route'],
'category' => $product['category'],
];
if(count($products) >= $limit){
break;
}
}
}
}else{
$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']);
$product['thumb'] = app(PageService::class)->getImageUrl(json_decode($product['thumb'], true)['url'] ?? '');
$product['route'] = '/' . $product['route'] . '/';
}
return [
'products' => $products,
'categories' => array_values($categories),
];
}
/**
* @param $text
* @param $key
* @param $key_limit
* @return array
* @author zbj
* @date 2024/1/22
*/
protected function searchSuggestion($text, $key, $key_limit, $project_id = 0): array
{
$columns = Cache::get('product_filed_project_' . $project_id);
if(!$columns){
$model = new Product();
$columns = $model->getConnection()->getSchemaBuilder()->getColumnListing($model->getTable());
}
//产品字段
if (in_array($key, $columns)) {
$all_products = Cache::get('product_list_project_' . $project_id);
if($all_products){
$suggestions = [];
foreach ($all_products as $product){
if(Str::contains($product[$key], $text)){
$suggestions[] = [
'title' => $product[$key],
'route' => $product['route'],
];
}
if(count($suggestions) >= $key_limit){
break;
}
}
}else{
//匹配产品
$suggestions = Product::where("status", 1)
->where($key, 'like', $text . '%')
->orderBy("id", "DESC")
->limit($key_limit)
->select($key .' as title', 'route')
->get()
->toArray();
}
} else {
$all_suggestions = Cache::get('product_extend_project_' . $project_id);
if($all_suggestions){
$suggestions = [];
foreach ($all_suggestions as $suggestion){
if(Str::contains($suggestion['values'], $text)){
$suggestions[] = [
'values' => $suggestion['values'],
'route' => $suggestion['route'],
];
}
if(count($suggestions) >= $key_limit){
break;
}
}
}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;
}
}