GoogleKeywordInsightController.php 3.3 KB
<?php
/**
 * @remark :
 * @name   :GoogleKeywordInsightController.php
 * @author :lyh
 * @method :post
 * @time   :2025/3/25 14:23
 */

namespace App\Http\Controllers\Bside\GoogleKeyword;

use App\Enums\Common\Code;
use App\Http\Controllers\Bside\BaseController;
use App\Http\Logic\Bside\GoogleKeywordInsight\GoogleKeywordInsightLogic;
use App\Models\GoogleKeywordInsight\GoogleKeywordInsightDetail;
use App\Models\Project\ProjectKeyword;

/**
 * @remark :谷歌洞察数据
 * @name   :GoogleKeywordInsightController
 * @author :lyh
 * @method :post
 * @time   :2025/3/25 14:24
 */
class GoogleKeywordInsightController extends BaseController
{
    /**
     * @remark :保存数据
     * @name   :saveKeywordInsight
     * @author :lyh
     * @method :post
     * @time   :2025/3/25 14:30
     */
    public function getKeywordInsight(GoogleKeywordInsightLogic $logic){
        $this->request->validate([
            'keyword' => 'required'
        ],[
            'keyword.required' => '关键词不能为空',
        ]);
        $logic->getGoogleInsight();
        $detailModel = new GoogleKeywordInsightDetail();
        $data = $detailModel->lists(['search'=>$this->param['keyword']],$this->page,$this->row,'id',['*'],'asc');
        $this->response('success',Code::SUCCESS,$data);
    }

    /**
     * @remark :获取优化关键词列表
     * @name   :getOptimizeList
     * @author :lyh
     * @method :post
     * @time   :2025/4/1 9:12
     */
    public function getOptimizeList(){
        $this->request->validate([
            'field' => 'required'
        ],[
            'field.required' => 'field不能为空',
        ]);
        $projectKeywordModel = new ProjectKeyword();
        $info = $projectKeywordModel->read(['project_id'=>$this->user['project_id']],[$this->param['field']]);
        if($info === false || empty($info[$this->param['field']])){
            $this->response('success');
        }
        $array = explode("\r\n", $info[$this->param['field']]);
        $detailModel = new GoogleKeywordInsightDetail();
        $resultData = [];
        if(!empty($array)){
            $resultData = paginateArray($array,$this->page,$this->row);
            $detailList = $detailModel->list(['search'=>['in',$resultData['list']]]);
            foreach ($resultData['list'] as $key => $item){
                $result['keyword'] = $item;
                $searchKeyword = $detailModel->getSearchDetail($item,$detailList);
                if($searchKeyword === false){
                    $result['data'] = [];
                }else{
                    $result['data'] = $searchKeyword;
                }
                $resultData['list'][$key] = $result;
            }

        }
        $this->response('success',Code::SUCCESS,$resultData);
    }

    /**
     * @remark :保存一条关键字数据
     * @name   :getKeywordInsightDetail
     * @author :lyh
     * @method :post
     * @time   :2025/4/1 11:23
     */
    public function getKeywordInsightDetail(GoogleKeywordInsightLogic $logic){
        $this->request->validate([
            'keyword' => 'required'
        ],[
            'keyword.required' => '关键词不能为空',
        ]);
        $result = $logic->getGoogleInsightDetail();
        $detailModel = new GoogleKeywordInsightDetail();
        $data = $detailModel->read(['id'=>$result['id']]);
        $this->response('success',Code::SUCCESS,$data);
    }

}