作者 赵彬吉

searchProduct

@@ -168,9 +168,12 @@ class ProductController extends BaseController @@ -168,9 +168,12 @@ class ProductController extends BaseController
168 */ 168 */
169 protected function searchProduct(Request $request) 169 protected function searchProduct(Request $request)
170 { 170 {
171 - $text = $request->input('text');  
172 - $limit = $request->input('limit');  
173 $project_id = $request->input('project_id'); 171 $project_id = $request->input('project_id');
  172 + $limit = $request->input('limit') ?: 5;
  173 + $text = $request->input('text');
  174 + $key = $request->input('key') ?: 'title';
  175 + $key_limit = $request->input('key_limit') ?: 15;
  176 +
174 $project = ProjectServer::useProject($project_id); 177 $project = ProjectServer::useProject($project_id);
175 if (!$project) { 178 if (!$project) {
176 $this->response('项目不存在', Code::SYSTEM_ERROR); 179 $this->response('项目不存在', Code::SYSTEM_ERROR);
@@ -208,9 +211,44 @@ class ProductController extends BaseController @@ -208,9 +211,44 @@ class ProductController extends BaseController
208 $data = [ 211 $data = [
209 'products' => $products, 212 'products' => $products,
210 'categories' => array_values($categories), 213 'categories' => array_values($categories),
  214 + 'suggestions' => $this->searchSuggestion($text, $key, $key_limit)
211 ]; 215 ];
212 216
213 $this->response('success', Code::SUCCESS, $data); 217 $this->response('success', Code::SUCCESS, $data);
214 218
215 } 219 }
  220 +
  221 + protected function searchSuggestion($text, $key, $key_limit): array
  222 + {
  223 + $model = new Product();
  224 + $columns = $model->getConnection()->getSchemaBuilder()->getColumnListing($model->getTable());
  225 +
  226 + //产品字段
  227 + if (in_array($key, $columns)) {
  228 + //匹配产品
  229 + $suggestions = Product::where("status", 1)
  230 + ->where($key, 'like', $text . '%')
  231 + ->orderBy("id", "DESC")
  232 + ->limit($key_limit)
  233 + ->select($key .' as title', 'route')
  234 + ->get()
  235 + ->toArray();
  236 + } else {
  237 + //扩展字段
  238 + $suggestions = Product::leftJoin('gl_product_extend_info as pei', 'gl_product.id', '=', 'pei.product_id')
  239 + ->where('pei.values', 'like', $text . '%')
  240 + ->where("gl_product.status", 1)
  241 + ->orderBy("gl_product.id", "DESC")
  242 + ->limit($key_limit)
  243 + ->select('pei.values','gl_product.route')
  244 + ->get()
  245 + ->toArray();
  246 + }
  247 +
  248 + foreach ($suggestions as &$suggestion){
  249 + $suggestion['route'] = '/' . $suggestion['route'] . '/';
  250 + }
  251 +
  252 + return $suggestions;
  253 + }
216 } 254 }