NewsCategoryController.php 4.1 KB
<?php

namespace App\Http\Controllers\Bside\News;

use App\Enums\Common\Code;
use App\Http\Controllers\Bside\BaseController;
use App\Http\Logic\Bside\News\NewsCategoryLogic;
use App\Http\Requests\Bside\News\NewsCategoryRequest;
use App\Models\News\News as NewsModel;
use App\Models\News\NewsCategory as NewsCategoryModel;
use App\Models\RouteMap\RouteMap;

class NewsCategoryController extends BaseController
{
    /**
     * @name :新闻分类列表
     * @author :liyuhang
     * @method
     */
    public function lists(NewsCategoryModel $newsCategory){
        //搜索条件
        $this->map['project_id'] = $this->user['project_id'];
        $filed = ['id','pid','name','num','alias','status','sort','remark','created_at','updated_at'];
        $lists = $newsCategory->list($this->map,$this->order,$filed);
        $data = [];
        if(!empty($lists)){
            $newsModel = new NewsModel();
            foreach ($lists as $k => $v){
                $v['num'] = $newsModel->formatQuery(['category_id'=>['like','%,' . $v['id'] . ',%']])->count();
                $v['url'] = $this->user['domain'] . $v['alias'].'/';
                $lists[$k] = $v;
            }
            if(!isset($this->map['name'])){
                $data = $this->getListSon($lists);
            }else{
                $data = $lists;
            }
        }
        $this->response('success',Code::SUCCESS,$data);
    }

    /**
     * @remark :无分页子集处理
     * @name   :getListSon
     * @author :lyh
     * @method :post
     * @time   :2023/8/17 11:12
     */
    public function getListSon($list){
        $data = array();
        foreach ($list as $v){
            $v = (array)$v;
            if ($v['pid'] == 0) {
                $v['sub'] = _get_child($v['id'], $list);
                $data[]   = $v;
            }
        }
        return $data;
    }

    /**
     * @name   :(添加/编辑时获取顶级分类)topList
     * @author :lyh
     * @method :post
     * @time   :2023/6/13 9:03
     */
    public function categoryTopList(NewsCategoryLogic $newsCategoryLogic){
        $list = $newsCategoryLogic->categoryTopList();
        $this->response('success',Code::SUCCESS,$list);
    }

    /**
     * @name :获取当前分类详情
     * @author :liyuhang
     * @method
     */
    public function info(NewsCategoryLogic $newsCategoryLogic){
        $this->request->validate([
            'id'=>['required']
        ],[
            'id.required' => 'ID不能为空'
        ]);
        $info = $newsCategoryLogic->info_news_category();
        $this->response('success',Code::SUCCESS,$info);
    }

    /**
     * @remark :保存数据
     * @name   :save
     * @author :lyh
     * @method :post
     * @time   :2023/9/7 14:51
     */
    public function save(NewsCategoryRequest $request,NewsCategoryLogic $newsCategoryLogic){
        $request->validated();
        $newsCategoryLogic->newsCategorySave();
        $this->response('success');
    }

    /**
     * @name :编辑状态/排序
     * @author :liyuhang
     * @method
     */
    public function status(NewsCategoryLogic $newsCategoryLogic){
        $this->request->validate([
            'id'=>['required'],
        ],[
            'id.required' => 'ID不能为空',
        ]);
        $newsCategoryLogic->status_news_category();
        $this->response('success');
    }

    /**
     * @remark :排序
     * @name   :sort
     * @author :lyh
     * @method :post
     * @time   :2023/9/26 17:35
     */
    public function sort(NewsCategoryLogic $newsCategoryLogic){
        $this->request->validate([
            'id'=>['required'],
        ],[
            'id.required' => 'ID不能为空',
        ]);
        $newsCategoryLogic->categorySort();
        $this->response('success');
    }

    /**
     * @name :删除分类
     * @author :liyuhang
     * @method
     */
    public function del(NewsCategoryLogic $newsCategoryLogic){
        $this->request->validate([
            'id'=>['required','array'],
        ],[
            'id.required' => 'ID不能为空',
            'id.array' => 'ID为数组',
        ]);
        $newsCategoryLogic->del_news_category();
        $this->response('success');
    }

}