作者 赵彬吉

searchProduct

... ... @@ -168,9 +168,12 @@ class ProductController extends BaseController
*/
protected function searchProduct(Request $request)
{
$text = $request->input('text');
$limit = $request->input('limit');
$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);
... ... @@ -208,9 +211,44 @@ class ProductController extends BaseController
$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;
}
}
... ...