BlogController.php 6.8 KB
<?php

namespace App\Http\Controllers\Bside\Blog;

use App\Enums\Common\Code;
use App\Http\Controllers\Bside\BaseController;
use App\Http\Logic\Bside\Blog\BlogLogic;
use App\Http\Requests\Bside\Blog\BlogRequest;
use App\Models\Blog\BlogCategory;
use App\Models\Com\AssociationCate;
use App\Models\User\User;

class BlogController extends BaseController
{

    /**
     * @remark :博客列表
     * @name   :lists
     * @author :lyh
     * @method :post
     * @time   :2023/9/14 10:45
     */
    public function lists(BlogLogic $blogLogic){
        $this->map = $this->searchParam();
        $filed = ['id','operator_id','status','created_at','label_id','image','updated_at','name','sort','url'];
        $lists = $blogLogic->lists($this->map,$this->page,$this->row,$this->order = 'sort', $filed);
        if(!empty($lists) && !empty($lists['list'])){
            //获取当前数据的分类
            $user = new User();
            foreach ($lists['list'] as $k => $v){
                $v['category_name'] = $this->getCateName($v['category_id']);
                $v['url'] = $this->user['domain'] .$v['url'];
                $v['image_link'] = getImageUrl($v['image']);
                $v['operator_name'] = $user->getName($v['operator_id']);
                $lists['list'][$k] = $v;
            }
        }
        $this->response('success',Code::SUCCESS,$lists);
    }

    /**
     * @remark :处理列表返回参数
     * @name   :handleReturnParam
     * @author :lyh
     * @method :post
     * @time   :2023/9/14 10:01
     */
    public function searchParam(){
        $this->map['project_id'] = $this->user['project_id'];
        if(isset($this->map['category_id']) && !empty($this->map['category_id'])){
            //获取当前分类下的所有子集
            $str = $this->getAllSub($this->map['category_id'],$str);
            //根据分类获取所有子集
            $assCateModel = new AssociationCate();
            $type_id = $assCateModel->formatQuery(['category_id'=>['in',$str],'type'=>$assCateModel::BLOG_CATE])->pluck('type_id')->toArray();
            $this->map['id'] = ['in',$type_id];
            unset($this->map['category_id']);
        }
        return $this->map;
    }

    /**
     * @remark :获取当前id下的所有子集
     * @name   :getAllSub
     * @author :lyh
     * @method :post
     * @time   :2023/10/18 15:10
     */
    public function getAllSub($id,&$str = []){
        $cateModel = new BlogCategory();
        $str[] = $id;
        $list = $cateModel->list(['pid'=>$id,'status'=>0],['id','pid']);
        if(!empty($list)){
            foreach ($list as $v){
                $str[] = $v['id'];
                $this->getAllSub($v['id'],$str);
            }
        }
        return $str;
    }

    /**
     * @remark :获取分类名
     * @name   :getCateName
     * @author :lyh
     * @method :post
     * @time   :2023/10/18 16:43
     */
    public function getCateName($id){
        //根据分类获取所有子集
        $str = '';
        if(!empty($id)){
            $assCateModel = new AssociationCate();
            $category_id = $assCateModel->formatQuery(['type_id'=>$id,'type'=>$assCateModel::BLOG_CATE])->pluck('category_id')->toArray();
            if(!empty($category_id)){
                $categoryModel = new BlogCategory();
                $category_name_arr = $categoryModel->formatQuery(['category_id'=>['in',$category_id]])->pluck('name')->toArray();
            }
            $str = explode(',',$category_name_arr);
        }
        return $str;
    }

    /**
     * @remark :根据状态数量
     * @name   :getStatusNumber
     * @author :lyh
     * @method :post
     * @time   :2023/6/19 9:39
     */
    public function getStatusNumber(BlogLogic $blogLogic){
        $data = $blogLogic->getStatusNumber();
        $this->response('success',Code::SUCCESS,$data);
    }

    /**
     * @remark :添加博客时获取分类列表
     * @name   :get_category_list
     * @author :lyh
     * @method :post
     * @time   :2023/10/18 16:51
     */
    public function getCategoryList(BlogLogic $blogLogic){
        $list = $blogLogic->getCategoryList();
        $this->response('success',Code::SUCCESS,$list);
    }

    /**
     * @remark :获取详情
     * @name   :info
     * @author :lyh
     * @method :post
     * @time   :2023/10/18 16:58
     */
    public function info(BlogLogic $blogLogic){
        $this->request->validate([
            'id'=>['required']
        ],[
            'id.required' => 'ID不能为空'
        ]);
        $info = $blogLogic->blogInfo();
        $this->response('success',Code::SUCCESS,$info);
    }

    /**
     * @remark :保存数据
     * @name   :save
     * @author :lyh
     * @method :post
     * @time   :2023/9/7 13:40
     */
    public function save(BlogRequest $request,BlogLogic $blogLogic){
        $request->validated();
        $blogLogic->blogSave();
        $this->response('success');
    }

    /**
     * @name :编辑新闻seo
     * @author :liyuhang
     * @method
     */
    public function edit_seo(BlogLogic $blogLogic){
        $this->request->validate([
            'id'=>['required'],
            'seo_title'=>['required'],
            'seo_description'=>['required'],
            'seo_keywords'=>['required'],
        ],[
            'id.required' => 'ID不能为空',
            'seo_title.required' => 'seo_title不能为空',
            'seo_description.required' => 'seo_description不能为空',
            'seo_keywords.required' => 'seo_description不能为空',
        ]);
        $blogLogic->edit_seo();
        $this->response('success');
    }

    /**
     * @name :编辑博客状态/排序
     * @author :liyuhang
     * @method
     */
    public function status(BlogLogic $blogLogic){
        $this->request->validate([
            'id'=>['required','array'],
        ],[
            'id.required' => 'ID不能为空',
            'id.array' => 'ID为数组',
        ]);
        $blogLogic->blogStatus();
        //TODO::写入日志
        $this->response('success');
    }

    /**
     * @name :删除博客(批量逻辑删除)
     * @author :liyuhang
     * @method
     */
    public function del(BlogLogic $blogLogic){
        $this->request->validate([
            'id'=>['required','array'],
        ],[
            'id.required' => 'ID不能为空',
            'id.array' => 'ID为数组',
        ]);
        $blogLogic->blogDel();
        $this->response('success');
    }

    /**
     * @remark :排序
     * @name   :sort
     * @author :lyh
     * @method :post
     * @time   :2023/8/22 10:24
     */
    public function sort(BlogLogic $logic){
        $this->request->validate([
            'id'=>'required',
            'sort'=>'required'
        ],[
            'id.required' => '产品ID不能为空',
            'sort.required'=>'排序字段不能为空'
        ]);
        $logic->setSort();
        $this->response('success');
    }
}