BlogLogic.php 10.4 KB
<?php

namespace App\Http\Logic\Bside\Blog;

use App\Http\Logic\Bside\BaseLogic;
use App\Models\Blog\Blog;
use App\Models\Blog\BlogCategory as BlogCategoryModel;
use App\Models\RouteMap\RouteMap;
use App\Services\CosService;
use Illuminate\Support\Facades\DB;

class BlogLogic extends BaseLogic
{
    const STATUS_TWO = 2;

    public function __construct()
    {
        parent::__construct();

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

    /**
     * @remark :保存数据
     * @name   :blogSave
     * @author :lyh
     * @method :post
     * @time   :2023/9/7 11:49
     */
    public function blogSave(){
        //拼接参数
        DB::beginTransaction();
        try {
            $this->param = $this->paramProcessing($this->param);
            if(isset($this->param['id']) && !empty($this->param['id'])){
                $this->param['url'] = RouteMap::setRoute($this->param['url'], RouteMap::SOURCE_BLOG, $this->param['id'], $this->user['project_id']);
                $route = $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_BLOG, $id, $this->user['project_id']);
                $this->edit(['url'=>$route],['id'=>$id]);
            }
            DB::commit();
        }catch (\Exception $e){
            DB::rollBack();
            $this->fail('error');
        }
        $this->addUpdateNotify(RouteMap::SOURCE_BLOG,$route);
        $this->curlDelRoute(['new_route'=>$route]);
        return $this->success();
    }

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


    /**
     * @remark :获取数据详情
     * @name   :blogInfo
     * @author :lyh
     * @method :post
     * @time   :2023/11/30 15:17
     */
    public function blogInfo(){
        $info = $this->model->read($this->param);
        if($info === false){
            $this->fail('error');
        }
        $info['category_id'] = explode(',',trim($info['category_id'],','));
        //获取标签名称
        $blogLabelLogic = new BlogLabelLogic();
        $info['label_name'] = $blogLabelLogic->getLabelName($info['label_id']);
        $info['image_link'] = getImageUrl($info['image']);
        return $this->success($info);
    }


    /**
     * @remark :修改状态
     * @name   :blogStatus
     * @author :lyh
     * @method :post
     * @time   :2023/11/30 15:16
     */
    public function blogStatus(){
        $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();
    }

    /**
     * @remark :删除
     * @name   :blogDel
     * @author :lyh
     * @method :post
     * @time   :2023/11/30 15:17
     */
    public function blogDel(){
        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();
    }

    /**
     * @remark :删除路由
     * @name   :delRoute
     * @author :lyh
     * @method :post
     * @time   :2023/9/7 10:50
     */
    public function delRoute($id){
        //删除路由映射
        RouteMap::delRoute(RouteMap::SOURCE_BLOG, $id, $this->user['project_id']);
        //生成一条删除路由记录
        $info = $this->model->read(['id'=>$id],['id','url']);
        $this->curlDelRoute(['route'=>$info['url']]);
        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 BlogCategoryModel();
        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();
    }

    /**
     * 博客导入
     * @param $project_id
     * @param $user_id
     * @param $domain
     * @param $data
     * @return bool
     * @throws \Exception
     * @author Akun
     * @date 2023/09/20 17:51
     */
    public function importBlog($project_id, $user_id, $domain, $data)
    {
        $route = $data[1]??'';//自定义路由
        if($route){
            $blog = $this->model->read(['url' => $route]);
        }else{
            $blog = $this->model->read(['name' => $data[0]]);
        }
        if (!$blog) {

            $category_id = '';
            if ($data[2]??'') {
                //处理分类
                $blogCategoryLogic = new BlogCategoryLogic();
                $category_id = $blogCategoryLogic->importBlogCategory($project_id, $user_id, $data[2]);
            }

            $text = '';
            if($data[4]??''){
                //处理内容中的图片
                preg_match_all('/<img\s+[^>]*?src\s*=\s*(\'|\")(.*?)\\1[^>]*?\/?\s*>/i', $data[4], $result);
                if($result[2]??[]){
                    foreach ($result[2] as $img){
                        $new_img = check_remote_url_down($img,$project_id,$domain,1);
                        $new_img && $data[4] = str_replace($img,$new_img,$data[4]);
                    }
                }

                //处理内容中的视频
                preg_match_all('/<source\s+[^>]*?src\s*=\s*(\'|\")(.*?)\\1[^>]*?\/?\s*>/i', $data[4], $result_video);
                if($result_video[2]??[]){
                    foreach ($result_video[2] as $video){
                        $new_video = check_remote_url_down($video,$project_id,$domain,1);
                        $new_video && $data[4] = str_replace($video,$new_video,$data[4]);
                    }
                }

                $text = $data[4];
            }

            $img = '';
            if($data[5]??''){
                $one_img = check_remote_url_down($data[5],$project_id,$domain);
                if($one_img){
                    $img = $one_img;
                }
            }

            $seo_title = '';
            if($data[6]??''){
                $seo_title = substr(strip_tags($data[6]),0,70);
            }

            $seo_keywords = '';
            if($data[7]??''){
                $seo_keywords = substr(strip_tags(str_replace('^v6sp$',',',$data[7])),0,255);
            }

            $seo_description = '';
            if($data[8]??''){
                $seo_description = substr(strip_tags($data[8]),0,200);
            }

            $id = $this->model->addReturnId(
                [
                    'name' => $data[0],
                    'category_id' => $category_id,
                    'text' => $text,
                    'remark' => $data[3] ?? '',
                    'image' => $img,
                    'seo_title' => $seo_title,
                    'seo_keywords' => $seo_keywords,
                    'seo_description' => $seo_description,
                    'project_id' => $project_id,
                    'operator_id' => $user_id,
                    'create_id' => $user_id,
                    'status' => Blog::STATUS_ONE,
                    'url' => ''
                ]
            );
            //更新路由
            if($route){
                $route_map = RouteMap::where('project_id', $project_id)->where('source', RouteMap::SOURCE_BLOG)->where('source_id', $id)->first();
                if (!$route_map) {
                    $route_map = new RouteMap();
                    $route_map->project_id = $project_id;
                    $route_map->path = RouteMap::SOURCE_BLOG.'s';
                    $route_map->source = RouteMap::SOURCE_BLOG;
                    $route_map->source_id = $id;
                    $route_map->route = $route;

                    $route_map->save();
                }
            }else{
                $route = RouteMap::setRoute($data[0], RouteMap::SOURCE_BLOG, $id, $project_id);
            }

            $this->edit(['url' => $route], ['id' => $id]);

            return true;
        }

        return false;
    }
}