NewsController.php 5.4 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\Logic\Bside\News\NewsLogic;
use App\Http\Requests\Bside\News\NewsRequest;
use App\Models\News\News as NewsModel;
use App\Models\RouteMap\RouteMap;
use App\Models\User\User;


/**
 * @name:新闻管理
 */
class NewsController extends BaseController
{
    public $updateModelView = 'news';
    /**
     * @name :获取新闻列表
     * @author :liyuhang
     * @method
     */
    public function lists(NewsModel $news,NewsCategoryLogic $newsCategoryLogic){
        $this->map['project_id'] = $this->user['project_id'];
        $lists = $news->lists($this->map,$this->page,$this->row,$this->order = 'sort',
            ['id','category_id','operator_id','status','created_at','updated_at','image','name','sort','url']);
        if(!empty($lists['list'])){
            foreach ($lists['list'] as $k => $v){
                if(!empty($v['category_id'])){
                    $v = $newsCategoryLogic->get_category_name($v);
                }
//                $v['url'] = $this->user['domain'] . RouteMap::getRoute(RouteMap::SOURCE_NEWS, $v['id'], $this->user['project_id']);
                if(!empty($v['image'])){
                    $v['image_link'] = getImageUrl($v['image']);
                }
                if(!empty($v['operator_id'])){
                    $v['operator_name'] = (new User())->getName($v['operator_id']);
                }
                $lists['list'][$k] = $v;
            }
        }
        $this->response('success',Code::SUCCESS,$lists);
    }

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


    /**
     * @name :添加新闻时获取分类列表
     * @author :liyuhang
     * @method
     */
    public function get_category_list(NewsLogic $newsLogic){
        $list = $newsLogic->news_get_category_list();
        $this->response('success',Code::SUCCESS,$list);
    }
    /**
     * @name :获取详情
     * @author :liyuhang
     * @method
     */
    public function info(NewsLogic $newsLogic){
        $this->request->validate([
            'id'=>['required'],
        ],[
            'id.required' => 'ID不能为空',
        ]);
        $info = $newsLogic->news_info();
        $info['route'] = RouteMap::getRoute(RouteMap::SOURCE_NEWS, $info['id'], $this->user['project_id']);
        $info['url'] = $this->user['domain'] . $info['route'];
        $info['image_link'] = getImageUrl($info['image']);
        $this->response('success',Code::SUCCESS,$info);
    }
    /**
     * @name :添加新闻
     * @return json
     * @author :liyuhang
     * @method
     */
    public function add(NewsRequest $newsRequest,NewsLogic $newsLogic){
        $newsRequest->validated();
        $newsLogic->news_add();
        $this->response('success');
    }

    /**
     * @name :编辑
     * @author :liyuhang
     * @method
     */
    public function edit(NewsRequest $newsRequest,NewsLogic $newsLogic){
        $newsRequest->validate([
            'id'=>['required'],
        ],[
            'id.required' => 'ID不能为空',
        ]);
        $newsLogic->news_edit();
        $this->response('success');
    }

    /**
     * @name :编辑新闻seo
     * @author :liyuhang
     * @method
     */
    public function edit_seo(NewsLogic $newsLogic){
        $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不能为空',
        ]);
        $newsLogic->edit_seo();
        $this->response('success');
    }

    /**
     * @name :编辑状态
     * @author :liyuhang
     * @method
     */
    public function status(NewsLogic $newsLogic){
        $this->request->validate([
            'id'=>['required','array'],
        ],[
            'id.required' => 'ID不能为空',
            'id.array' => 'ID为数组',
        ]);
        $newsLogic->news_status();
        //TODO::写入日志
        $this->response('success');
    }
    /**
     * @name :删除新闻(逻辑删除)
     * @author :liyuhang
     * @method
     */
    public function del(NewsLogic $newsLogic){
        $this->request->validate([
            'id'=>['required','array'],
        ],[
            'id.required' => 'ID不能为空',
            'id.array' => 'ID为数组',
        ]);
        $newsLogic->news_del();
        //TODO::清空相关资源/写入日志
        $this->response('success');
    }

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