NewsLogic.php 5.6 KB
<?php

namespace App\Http\Logic\Bside\News;

use App\Enums\Common\Code;
use App\Helper\Common;
use App\Http\Logic\Bside\BaseLogic;
use App\Models\Image;
use App\Models\News\News;
use App\Models\News\NewsCategory as NewsCategoryModel;
use App\Models\RouteMap;
use Illuminate\Support\Facades\DB;

class NewsLogic extends BaseLogic
{
    public function __construct()
    {
        parent::__construct();

        $this->model = new News();
        $this->param = $this->requestAll;
    }

    /**
     * @name :获取分类列表
     * @return array
     * @throws \App\Exceptions\BsideGlobalException
     * @author :liyuhang
     * @method
     */
    public function news_get_category_list(){
        $this->map['status'] = 0;
        $this->map['project_id'] = $this->user['project_id'];
        $newsCategoryModel = new NewsCategoryModel();
        $cate_list = $newsCategoryModel->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;
            }
        }
        return $this->success($list);
    }

    /**
     * @name :添加博客
     * @return void
     * @author :liyuhang
     * @method
     */
    public function news_add(){
        $this->param['create_id'] = $this->user['id'];
        $this->param['operator_id'] = $this->user['id'];
        $this->param['project_id'] = $this->user['project_id'];
        $this->param['created_at'] = date('Y-m-d H:i:s',time());
        $this->param['updated_at'] = date('Y-m-d H:i:s',time());
        $this->param['category_id'] = ','.$this->param['category_id'].',';
        DB::beginTransaction();
        try {
            if(isset($this->param['image'])){
                $data = $this->upload();
                $this->param['image'] = $data;
            }
            $rs = $this->model->insertGetId($this->param);
            RouteMap::setRoute($this->param['url'], RouteMap::SOURCE_NEWS, $rs, $this->user['project_id']);
            DB::commit();
        }catch (\Exception $e){
            DB::rollBack();
            $this->fail('添加失败');
        }
        $this->success();
    }

    /**
     * @name :编辑
     * @return void
     * @author :liyuhang
     * @method
     */
    public function news_edit(){
        $condition = [
            'id'=>['!=',$this->param['id']],
            'name'=>$this->param['name']
        ];
        $info = $this->model->read($condition);
        if($info !== false){
            $this->fail('当前名称已存在');
        }
        $this->param['operator_id'] = $this->user['id'];
        //多个分类按逗号隔开
        $this->param['category_id'] = ','.$this->param['category_id'].',';
        DB::beginTransaction();
        try {
            //上传图片
            if(isset($this->param['image']) && is_file($this->param['image'])){
                //查看当前用户是否已有头像
                $info = $this->model->read(['id'=>$this->param['id']],['id','image']);
                if($info !== false && !empty($info['image'])){
                    //TODO::删除资源
                    $imageModel = new Image();
                    $image_info = $imageModel->read(['hash'=>$info['image']],['id','path']);
                    shell_exec('rm -rf '.$image_info['path'] . ' ./../uploads/images/cache_'. $info['image'] . '*');
                    //清空数据库
                    $imageModel->del(['hash'=>$info['image']]);
                }
                $this->param['image'] = $this->upload();
            }
            RouteMap::setRoute($this->param['url'], RouteMap::SOURCE_NEWS, $this->param['id'], $this->user['project_id']);
            $this->model->edit($this->param,['id'=>$this->param['id']]);
            DB::commit();
        }catch (\exception $e){
            DB::rollBack();
            $this->fail('参数错误或其他服务器原因,编辑失败');
        }
        //TODO::写入日志
        $this->success();
    }

    /**
     * @name :修改状态
     * @return void
     * @throws \App\Exceptions\BsideGlobalException
     * @author :liyuhang
     * @method
     */
    public function news_status(){
        $this->param['operator_id'] = $this->user['id'];
        $rs = $this->model->edit($this->param,['id'=>$this->param['id']]);
        if($rs ===  false){
            $this->fail('error',Code::USER_ERROR);
        }
        $this->success();
    }
    /**
     * @name :获取详情
     * @return array
     * @throws \App\Exceptions\BsideGlobalException
     * @author :liyuhang
     * @method
     */
    public function news_info(){
        $info = $this->model->read($this->param);
        //获取分类名称
        $info['category_id'] = explode(',',trim($info['category_id'],','));
        $newsCategoryModel = new NewsCategoryModel();
        $category_list = $newsCategoryModel->list(['id'=>['in',$info['category_id']]],'id',['name']);
        $str = '';
        foreach ($category_list as $v){
            $str .= $v['name'].',';
        }
        $info['category_id'] = trim($str,',');
        if($info === false){
            $this->fail('error');
        }
        return $this->success($info);
    }

    /**
     * @name :逻辑删除
     * @return void
     * @author :liyuhang
     * @method
     */
    public function news_del(){
        $this->param['id'] = ['in',$this->param['id']];
        $rs = $this->model->edit(['status'=>2,'operator_id'=>$this->user['id']],$this->param);
        if($rs === false){
            $this->fail('error');
        }
        return $this->success();
    }
}