GoogleKeywordInsightLogic.php 2.6 KB
<?php
/**
 * @remark :
 * @name   :GoogleKeywordInsightLogic.php
 * @author :lyh
 * @method :post
 * @time   :2025/3/25 14:31
 */

namespace App\Http\Logic\Bside\GoogleKeywordInsight;

use App\Helper\Translate;
use App\Http\Logic\Bside\BaseLogic;
use App\Models\GoogleKeywordInsight\GoogleKeywordInsight;
use App\Models\GoogleKeywordInsight\GoogleKeywordInsightDetail;
use App\Services\GoogleSearchService;
use Illuminate\Support\Facades\DB;

class GoogleKeywordInsightLogic extends BaseLogic
{
    public $service;

    public function __construct()
    {
        parent::__construct();
        $this->model = new GoogleKeywordInsight();
        $this->param = $this->requestAll;
    }

    /**
     * @remark :获取google洞察数据
     * @name   :saveGoogleKeywordInsight
     * @author :lyh
     * @method :post
     * @time   :2025/3/25 14:36
     */
    public function getGoogleInsight(){
        $data = $this->model->read(['search'=>$this->param['keyword']],['id']);
        if($data === false){
            $this->service = new GoogleSearchService();
            $data = $this->service->requestUrl($this->param['keyword']);
            if(!empty($data)){
                DB::beginTransaction();
                //保存数据库
                try {
                    $detailModel = new GoogleKeywordInsightDetail();
                    $this->model->saveInsight($this->user['project_id'],$this->param['keyword'],$data);
                    $detailModel->saveInsightDetail($this->user['project_id'],$this->param['keyword'],$data);
                    DB::commit();
                }catch (\Exception $e){
                    DB::rollBack();
                    $this->fail('保存失败,请联系管理员');
                }
            }
        }
        return $this->success();
    }

    /**
     * @remark :保存一条数据
     * @name   :saveGoogleKeywordInsight
     * @author :lyh
     * @method :post
     * @time   :2025/3/25 14:36
     */
    public function getGoogleInsightDetail(){
        $this->service = new GoogleSearchService();
        $data = $this->service->requestUrl($this->param['keyword']);
        if(!empty($data)){
            DB::beginTransaction();
            //保存数据库
            try {
                $detailModel = new GoogleKeywordInsightDetail();
                $id = $detailModel->saveInsightDetailOne($this->user['project_id'],$this->param['keyword'],$data[0]);
                DB::commit();
            }catch (\Exception $e){
                DB::rollBack();
                $this->fail('保存失败,请联系管理员');
            }
        }
        return $this->success(['id'=>$id ?? 0]);
    }
}