SearchController.php 6.0 KB
<?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;
    }
}