BlogController.php 10.0 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\Blog as BlogModel;
use App\Models\Blog\BlogCategory;
use App\Models\Blog\BlogCategory as BlogCategoryModel;
use App\Models\RouteMap\RouteMap;
use App\Models\Template\BTemplate;
use App\Models\Template\Setting;
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(BlogModel $blogModel){
        $filed = ['id','category_id','operator_id','status','created_at','label_id','image','updated_at','name','sort','url','release_at','is_upgrade','six_read'];
        $this->order = 'sort';
        $query = $blogModel->orderBy($this->order ,'desc')->orderBy('id','desc');
        $query = $this->searchParam($query);
        $lists = $query->select($filed)->paginate($this->row, ['*'], 'page', $this->page);
        if(!empty($lists)){
            $lists = $lists->toArray();
            $data = $this->getCategoryList();
            $template_id = $this->getTemplateId(BTemplate::SOURCE_BLOG,BTemplate::IS_DETAIL);
            $user = new User();
            foreach ($lists['list'] as $k => $v){
                $v['category_name'] = $this->categoryName($v['category_id'],$data);
                $v['route'] = $v['url'];
                $v['url'] = $this->user['domain'] . getRouteMap(RouteMap::SOURCE_BLOG,$v['id']);;
                $v['image_link'] = getImageUrl($v['image'],$this->user['storage_type'],$this->user['project_location']);
                $v['operator_name'] = $user->getName($v['operator_id']);
                $v['is_renovation']  =  $this->getIsRenovation(BTemplate::SOURCE_BLOG,BTemplate::IS_DETAIL,$template_id,$v['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(&$query){
        $query = $query->where('project_id',$this->user['project_id']);
        if (isset($this->map['category_id']) && !empty($this->map['category_id'])) {
            $str = [];
            $str[] = $this->map['category_id'];
            $this->getAllSub($this->map['category_id'],$str);
            $query->where(function ($subQuery) use ($str) {
                foreach ($str as $v) {
                    $subQuery->orWhereRaw("FIND_IN_SET(?, category_id) > 0", [$v]);
                }
            });
        }
        if(isset($this->map['status'])){
            $query = $query->where('status',$this->map['status']);
        }
        if(isset($this->map['name']) && !empty($this->map['name'])){
            $query = $query->where('name',$this->map['name'][0],'%'.$this->map['name'][1].'%');
        }
        if(!empty($this->param['start_at']) && !empty($this->param['end_at'])){
            $query->whereBetween('created_at', [$this->param['start_at'],$this->param['end_at']]);
        }
        return $query;
    }

    /**
     * @remark :获取当前id下所有子集
     * @name   :getAllSub
     * @author :lyh
     * @method :post
     * @time   :2023/10/18 15:10
     */
    public function getAllSub($id,&$str = []){
        $cateModel = new BlogCategory();
        $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   :getCategoryList
     * @author :lyh
     * @method :post
     * @time   :2023/9/14 13:56
     */
    public function getCategoryList(){
        $categoryModel = new BlogCategory();
        $data = [];
        $cateList = $categoryModel->list(['project_id'=>$this->user['project_id']],['id','name']);
        if(!empty($cateList)){
            foreach ($cateList as $value){
                $data[$value['id']] = $value['name'];
            }
        }
        return $data;
    }

    /**
     * @remark :获取分类名称
     * @name   :categoryName
     * @author :lyh
     * @method :post
     * @time   :2023/9/14 13:58
     */
    public function categoryName($category_id,$data){
        $category_name = '';
        if(!empty($category_id) && !empty($data)){
            $arr = explode(',',trim($category_id,','));
            foreach ($arr as $v){
                if(isset($data[$v])){
                    $category_name .= $data[$v].',';
                }
            }
            $category_name = trim($category_name,',');
        }
        return $category_name;
    }

    /**
     * @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/19 15:08
     */
    public function get_category_list(){
        $this->map['status'] = 0;
        $this->map['project_id'] = $this->user['project_id'];
        $blogCategoryModel = new BlogCategory();
        $cate_list = $blogCategoryModel->list($this->map,'sort');
        if($cate_list === false){
            $this->fail('error',Code::USER_ERROR);
        }
        $list = [];
        foreach ($cate_list as $v){
            $v = (array)$v;
            if ($v['pid'] == 0) {
                $v['sub'] = _get_child($v['id'], $cate_list);
                $list[]   = $v;
            }
        }
        $this->response('success',Code::SUCCESS,$list);
    }

    /**
     * 获取当前博客详情
     * @param BlogLogic $blogLogic
     */
    public function info(BlogLogic $blogLogic){
        $this->request->validate([
            'id'=>['required']
        ],[
            'id.required' => 'ID不能为空'
        ]);
        $info = $blogLogic->blogInfo();
        $template_id = $this->getTemplateId(BTemplate::SOURCE_BLOG,BTemplate::IS_DETAIL);
        $info['is_renovation']  =  $this->getIsRenovation(BTemplate::SOURCE_BLOG,BTemplate::IS_DETAIL,$template_id, $info['id']);
        $this->response('success',Code::SUCCESS,$info);
    }

    /**
     * 保存数据
     * @param BlogRequest $request
     * @param BlogLogic $blogLogic
     */
    public function save(BlogRequest $request,BlogLogic $blogLogic){
        $request->validated();
        $data = $blogLogic->blogSave();
        $this->response('success',Code::SUCCESS,$data);
    }

    /**
     * @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');
    }

    /**
     * @remark :批量排序
     * @name   :allSort
     * @author :lyh
     * @method :post
     * @time   :2024/1/11 9:46
     */
    public function allSort(BlogLogic $logic){
        $logic->setAllSort();
        $this->response('success');
    }

    /**
     * @remark :批量设置产品分类及状态
     * @name   :batchSetCategory
     * @author :lyh
     * @method :post
     * @time   :2023/8/15 17:51
     */
    public function batchSetCategory(BlogLogic $logic){
        $this->request->validate([
            'id'=>'required',
            'category_id'=>'required',
        ],[
            'id.required' => '产品ID不能为空',
            'category_id.required' => '分类ID不能为空',
        ]);
        $logic->batchSetCategory();
        $this->response('success');
    }

    /**
     * @remark :复制新闻
     * @name   :copyNews
     * @author :lyh
     * @method :post
     * @time   :2024/4/28 11:53
     */
    public function copyBlog(BlogLogic $blog){
        $this->request->validate([
            'id'=>'required',
        ],[
            'id.required' => 'id不能为空',
        ]);
        $data = $blog->copyBlogInfo();
        $this->response('success',Code::SUCCESS,$data);
    }
}