CollectLogic.php 4.5 KB
<?php

namespace App\Http\Logic\Aside\Collect;

use App\Http\Logic\Logic;
use App\Models\Blog\Blog;
use App\Models\Domain\DomainInfo;
use App\Models\News\News;
use App\Models\Product\Keyword;
use App\Models\Product\Product;
use App\Models\Project\Project;
use App\Services\ProjectServer;


/**
 * Class CollectLogic
 * @package App\Http\Logic\Aside
 * @author zbj
 * @date 2023/11/10
 */
class CollectLogic extends Logic
{
    protected $project;
    protected $domain;
    protected $type;
    protected $page_size = 100;

    public function __construct()
    {
        $this->checkAuth();
    }

    /**
     * 校验权限
     * @throws \App\Exceptions\AsideGlobalException
     * @throws \App\Exceptions\BsideGlobalException
     * @author zbj
     * @date 2023/11/10
     */
    public function checkAuth()
    {
        $request = request();
        $site_token = $request->header('site-token');
        $domain = $request->input('domain');
        if (!$site_token) {
            $this->fail('参数异常');
        }
        $this->project = Project::where('site_token', $site_token)->first();
        if (!$this->project) {
            $this->fail('授权码无效');
        }
        $domain_info = DomainInfo::where('project_id', $this->project->id)->where('domain', $domain)->first();
        if (!$domain_info) {
            $this->fail('域名不匹配');
        }
        $this->domain = 'https://' . $domain_info['domain'] . '/';
        $this->type = $request->input('type', '');
    }

    public function collect_data()
    {
        ProjectServer::useProject($this->project->id);
        $action = $this->type;
        return $this->$action();
    }

    public function __call($name, $param)
    {
        return [];
    }


    public function product()
    {
        $this->model = new Product();
        $where[] = ['status' => Product::STATUS_ON];
        $sort = ['sort' => 'desc'];
        $columns = ['title', 'content', 'gallery', 'seo_mate', 'intro', 'route', 'keyword_id'];
        $list = self::getList($where,$sort, $columns, $this->page_size);
        $data =[];
        foreach ($list['list'] as $item){
            //关键词标签  没有就取seo 键词
            $keyword = '';
            if($item['keyword_id']){
                $keyword = Keyword::whereIn('id', $item['keyword_id'])->pluck('title')->toArray();
                if($keyword){
                    $keyword = implode(',', $keyword);
                }
            }
            $keyword = $keyword ?: ($item['seo_mate']['keyword'] ?? '');
            $data[] = [
                'title' => $item['title'],
                'url' => $this->domain . $item['route'],
                'keywords' => $keyword,
                'desc' => strip_tags($item['intro']?:''),
                'content' => strip_tags($item['content'] ?: ''),
                'images' => getImageUrl(array_column($item['gallery'] ?: [], 'url'))
            ];
        }
        $list['list'] = $data;
        return $list;
    }

    public function news()
    {
        $this->model = new News();
        $where[] = ['status' => News::STATUS_ONE];
        $sort = ['sort' => 'desc'];
        $columns = ['name', 'text', 'image', 'seo_keywords', 'remark', 'url'];
        $list = self::getList($where,$sort, $columns, $this->page_size);
        $data =[];
        foreach ($list['list'] as $item){
            $data[] = [
                'title' => $item['name'],
                'url' => $this->domain . $item['url'],
                'keywords' => $item['seo_keywords'],
                'desc' => strip_tags($item['remark']?:''),
                'content' => strip_tags($item['text'] ?: ''),
                'images' => getImageUrl([$item['image']] ?:[])
            ];
        }
        $list['list'] = $data;
        return $list;
    }

    public function blog()
    {
        $this->model = new Blog();
        $where[] = ['status' => Blog::STATUS_ONE];
        $sort = ['sort' => 'desc'];
        $columns = ['name', 'text', 'image', 'seo_keywords', 'remark', 'url'];
        $list = self::getList($where,$sort, $columns, $this->page_size);
        $data =[];
        foreach ($list['list'] as $item){
            $data[] = [
                'title' => $item['name'],
                'url' => $this->domain . $item['url'],
                'keywords' => $item['seo_keywords'],
                'desc' => strip_tags($item['remark']?:''),
                'content' => strip_tags($item['text'] ?: ''),
                'images' => getImageUrl([$item['image']] ?:[])
            ];
        }
        $list['list'] = $data;
        return $list;
    }
}