KeywordController.php 3.3 KB
<?php

namespace App\Http\Controllers\Bside\Product;

use App\Enums\Common\Code;
use App\Helper\Arr;
use App\Http\Controllers\Bside\BaseController;
use App\Http\Logic\Bside\Product\KeywordLogic;
use App\Http\Requests\Bside\Product\KeywordRequest;
use App\Models\Product\Keyword;
use App\Models\Product\KeywordRelated;
use App\Models\Product\Product;
use App\Models\RouteMap\RouteMap;
use App\Rules\Ids;
use Illuminate\Http\Request;

/**
 * Class KeywordController
 * @package App\Http\Controllers\Bside
 * @author zbj
 * @date 2023/4/15
 */
class KeywordController extends BaseController
{

    /**
     * @remark :关键字列表
     * @name   :index
     * @author :lyh
     * @method :post
     * @time   :2023/8/17 10:57
     */
    public function index(Keyword $keyword)
    {
        if(!empty($this->map['title'])){
            $this->map['title'] = ['like','%'.$this->map['title'].'%'];
        }
        $this->map['project_id'] = $this->user['project_id'];
        $filed =  ['id', 'project_id', 'title', 'seo_title', 'seo_keywords', 'seo_description', 'status', 'created_at'];
        $data = $keyword->lists($this->map,$this->page,$this->row,$this->order,$filed);
        if(!empty($data)){
            foreach ($data['list'] as &$v){
                $v['product_num'] = Product::where('keyword_id','like' ,'%,'.$v['id'].',%')->count();
                $v['tdk'] = boolval($v['seo_title']) * boolval($v['seo_keywords']) * boolval($v['seo_description']);
                $v['url'] = $this->user['domain'] . $v['url'];
            }
        }
        return $this->response('success',Code::SUCCESS,$data);
    }

    /**
     * @remark :获取数据详情
     * @name   :info
     * @author :lyh
     * @method :post
     * @time   :2023/8/23 16:57
     */
    public function info(Request $request, KeywordLogic $logic){
        $request->validate([
            'id'=>'required'
        ],[
            'id.required' => 'ID不能为空'
        ]);
        $data = $logic->getInfo($this->param['id']);
        return $this->success(Arr::twoKeepKeys($data, ['id', 'project_id', 'title', 'seo_title', 'seo_keywords', 'seo_description', 'created_at', 'route', 'url']));
    }

    /**
     * @remark :保存
     * @name   :save
     * @author :lyh
     * @method :post
     * @time   :2023/8/28 14:24
     */
    public function save(KeywordRequest $request, KeywordLogic $logic)
    {
        $request->validated();
        $logic->keywordSave();
        $this->response('success');
    }

    /**
     * @remark :批量添加
     * @name   :batchAdd
     * @author :lyh
     * @method :post
     * @time   :2023/8/28 14:25
     */
    public function batchAdd(KeywordLogic $logic){
        $this->request->validate([
            'title'=>['required','array']
        ],[
            'title.required' => 'title不能为空',
            'title.array' => 'title为数组'
        ]);
        $logic->batchAdd();
        $this->response('success');
    }

    /**
     * @remark :删除数据
     * @name   :delete
     * @author :lyh
     * @method :post
     * @time   :2023/8/23 17:44
     */
    public function delete(KeywordLogic $logic)
    {
        $this->request->validate([
            'ids'=>['required', new Ids()]
        ],[
            'ids.required' => 'ID不能为空'
        ]);
        $logic->keywordDelete();
        $this->response('success');
    }

}