NewsLogic.php 9.9 KB
<?php

namespace App\Http\Logic\Bside\News;

use App\Enums\Common\Code;
use App\Http\Logic\Bside\BaseLogic;
use App\Models\News\News;
use App\Models\News\NewsCategory;
use App\Models\News\NewsCategory as NewsCategoryModel;
use App\Models\RouteMap\RouteMap;
use App\Services\CosService;
use Illuminate\Support\Facades\DB;
use mysql_xdevapi\Exception;

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);
    }

    /**
     * @remark :保存数据
     * @name   :newsSave
     * @author :lyh
     * @method :post
     * @time   :2023/9/7 11:02
     */
    public function newsSave()
    {
        //拼接参数
        DB::beginTransaction();
        try {
            $this->param = $this->paramProcessing($this->param);
            if (isset($this->param['id']) && !empty($this->param['id'])) {
                //是否更新路由
                $id = $this->editNewsRoute($this->param['id'], $this->param['url']);
                $this->edit($this->param, ['id' => $this->param['id']]);
            } else {
                $id = $this->model->addReturnId($this->param);
            }
            //更新路由
            $route = RouteMap::setRoute($this->param['url'], RouteMap::SOURCE_NEWS, $id, $this->user['project_id']);
            $this->edit(['url' => $route], ['id' => $id]);
            DB::commit();
        } catch (\Exception $e) {
            DB::rollBack();
            $this->fail('系统错误,请联系管理员');
        }
        //通知更新
        $this->updateNotify(['project_id' => $this->user['project_id'], 'type' => RouteMap::SOURCE_NEWS, 'route' => $route]);
        return $this->success();
    }

    /**
     * @remark :查看是否编辑路由
     * @name   :editCategoryRoute
     * @author :lyh
     * @method :post
     * @time   :2023/9/7 11:05
     */
    public function editNewsRoute($id, $route)
    {
        //生成一条删除路由记录
        $info = $this->model->read(['id' => $id], ['id', 'url']);
        if ($info['url'] != $route) {
            $data = [
                'source' => RouteMap::SOURCE_NEWS,
                'route' => $info['url'],
            ];
            $this->setRouteDeleteSave($data);
        }
        return $id;
    }


    /**
     * @name :编辑seo
     * @return void
     * @author :liyuhang
     * @method
     */
    public function edit_seo()
    {
        $this->param['operator_id'] = $this->user['id'];
        $rs = $this->model->edit($this->param, ['id' => $this->param['id']]);
        if ($rs === false) {
            $this->fail('error');
        }
        return $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' => ['in', $this->param['id']]]);
        if ($rs === false) {
            $this->fail('error');
        }
        return $this->success();
    }

    /**
     * @name :获取详情
     * @return array
     * @throws \App\Exceptions\BsideGlobalException
     * @author :liyuhang
     * @method
     */
    public function newsInfo()
    {
        $info = $this->model->read($this->param);
        if($info === false){
            $this->fail('error');
        }
        $info['category_id'] = explode(',',trim($info['category_id'],','));
        $info['image_link'] = getImageUrl($info['image']);
        return $this->success($info);
    }

    /**
     * @name :逻辑删除
     * @return void
     * @author :liyuhang
     * @method
     */
    public function newsDel()
    {
        DB::beginTransaction();
        try {
            foreach ($this->param['id'] as $id) {
                $this->delRoute($id);
                $this->model->del(['id' => $id]);
            }
            DB::commit();
        } catch (Exception $e) {
            DB::rollBack();
            $this->fail('error');
        }
        return $this->success();
    }

    /**
     * @name   :(参数处理)paramProcessing
     * @author :lyh
     * @method :post
     * @time   :2023/6/13 11:30
     */
    public function paramProcessing($param)
    {
        if(isset($this->param['id'])){
            $param['operator_id'] = $this->user['id'];
            if(isset($param['category_id']) && !empty($param['category_id'])){
                $param['category_id'] = $this->getLastCategory($param['category_id']);
            }
        }else{
            $param['create_id'] = $this->user['id'];
            $param['operator_id'] = $this->user['id'];
            $param['project_id'] = $this->user['project_id'];
            if(isset($param['category_id']) && !empty($param['category_id'])){
                $param['category_id'] = $this->getLastCategory($param['category_id']);
            }
        }
        return $this->success($param);
    }

    /**
     * @remark :获取最后一级分类id
     * @name   :getLastCategory
     * @author :lyh
     * @method :post
     * @time   :2023/10/20 9:02
     */
    public function getLastCategory($category){
        $str = '';
        $cateModel = new NewsCategoryModel();
        foreach ($category as $v){
            $info = $cateModel->read(['pid'=>$v]);
            if($info === false){
                $str .= $v.',';
            }
        }
        return ','.$str;
    }

    /**
     * @remark :根据状态获取数量
     * @name   :getStatusNumber
     * @author :lyh
     * @method :post
     * @time   :2023/6/19 9:42
     */
    public function getStatusNumber()
    {
        //三种状态 0:草稿 1:发布 2:回收站
        $data = ['dra' => 0, 'pub' => 1, 'del' => 2, 'tal' => 3];
        foreach ($data as $k => $v) {
            if ($v == 3) {
                $data[$k] = $this->model->where(['project_id' => $this->user['project_id']])->count();
            } else {
                $data[$k] = $this->model->where(['status' => $v, 'project_id' => $this->user['project_id']])->count();
            }
        }
        return $this->success($data);
    }

    /**
     * @remark :排序
     * @name   :setSort
     * @author :lyh
     * @method :post
     * @time   :2023/8/19 11:16
     */
    public function setSort()
    {
        $rs = $this->model->edit(['sort' => $this->param['sort']], ['id' => $this->param['id']]);
        if ($rs === false) {
            $this->fail('error');
        }
        return $this->success();
    }

    /**
     * @remark :删除路由
     * @name   :delRoute
     * @author :lyh
     * @method :post
     * @time   :2023/9/7 10:50
     */
    public function delRoute($id)
    {
        //删除路由映射
        RouteMap::delRoute(RouteMap::SOURCE_NEWS, $id, $this->user['project_id']);
        //生成一条删除路由记录
        $info = $this->model->read(['id' => $id], ['id', 'url']);
        $data = [
            'source' => RouteMap::SOURCE_NEWS,
            'route' => $info['url'],
        ];
        $this->setRouteDeleteSave($data);
        return $this->success();
    }

    /**
     * 新闻导入
     * @param $project_id
     * @param $user_id
     * @param $data
     * @return bool
     * @throws \Exception
     * @author Akun
     * @date 2023/09/20 17:51
     */
    public function importNews($project_id, $user_id, $data)
    {
        $news = $this->model->read(['name' => $data[0]]);
        if (!$news) {

            $category_id = '';
            if ($data[2]) {
                //处理分类
                $newsCategoryLogic = new NewsCategoryLogic();
                $category_id = $newsCategoryLogic->importNewsCategory($project_id, $user_id, $data[2]);
            }

            $text = '';
            if($data[4]){
                //处理内容中的图片
                $pattern = '<img src="(.*?)">';
                preg_match_all($pattern, $data[4], $result);
                if($result[1]){
                    foreach ($result[1] as $img){
                        $data[4] = str_replace($img,getImageUrl(CosService::uploadRemote($project_id,'image_news',$img)),$data[4]);
                    }
                }
                $text = $data[4];
            }

            $id = $this->model->addReturnId(
                [
                    'name' => $data[0],
                    'category_id' => $category_id,
                    'text' => $text,
                    'remark' => $data[3] ?? '',
                    'image' => $data['5'] ? CosService::uploadRemote($project_id, 'image_news', $data[5]) : '',
                    'seo_title' => $data[6] ?? '',
                    'seo_keywords' => $data[7] ?? '',
                    'seo_description' => $data[8] ?? '',
                    'project_id' => $project_id,
                    'operator_id' => $user_id,
                    'create_id' => $user_id,
                    'status' => News::STATUS_ONE,
                    'url' => ''
                ]
            );
            //更新路由
            $route = RouteMap::setRoute($data[0], RouteMap::SOURCE_NEWS, $id, $project_id);
            $this->edit(['url' => $route], ['id' => $id]);

            return true;
        }

        return false;
    }
}