AiCommandController.php 4.4 KB
<?php

namespace App\Http\Controllers\Bside\Ai;

use App\Enums\Common\Code;
use App\Helper\Common;
use App\Helper\Translate;
use App\Http\Controllers\Bside\BaseController;
use App\Http\Controllers\Bside\:写入日志;
use App\Http\Logic\Bside\Ai\AiCommandLogic;
use App\Models\Ai\AiCommand;
use App\Models\Ai\AiLog;
use App\Models\Project\DeployOptimize;
use App\Models\Project\Project;
use App\Models\Project\ProjectKeyword;
use App\Services\AiCommandService;
use Illuminate\Support\Facades\Cache;

class AiCommandController extends BaseController
{
    /**
     * @author zbj
     * @date 2023/11/22
     */
    public function ai_http_post(){
        $this->request->validate([
            'keywords'=>['required'],
            'key'=>['required']
        ],[
            'keywords.required' => '关键字不能为空',
            'key.required' => '场景不能为空',
        ]);
        if($this->param['key'] == 'keyword_seo_title'){
            $prefix = getPrefixKeyword($this->user['project_id'], 'prefix', 1);
            $suffix = getPrefixKeyword($this->user['project_id'], 'suffix', 2);
            $text = $prefix . ' ' . $this->param['keywords']. ' ' . $suffix;
            $data = [
                'code' => 200,
                'text' => $text
            ];
        }else{
            $text = AiCommandLogic::instance()->ai_send();
            $data = [
                'code' => $text ? 200 : 500,
                'text' => $text
            ];
        }
        $param = [
            'key' => $this->param['key'],
            'keywords' => $this->param['keywords'],
            'remark' => $text
        ];
        $this->set_ai_log($param);
        $this->response('success', Code::SUCCESS, $data);
    }

    /**
     * @name :写入日志
     * @author :liyuhang
     * @method
     */
    public function set_ai_log($data){
        //写入日志
        $param = [
            'key'=> $this->param['key'],
            'keywords'=>$this->param['keywords'],
            'remark' =>$data['remark'],
            'operator_id'=>$this->uid
        ];
        $aiLog = new AiLog();
        return $aiLog->add($param);
    }

    /**
     * @remark :获取排版指令
     * @name   :getAiTypesetting
     * @author :lyh
     * @method :post
     * @time   :2025/5/26 17:11
     */
    public function getLayoutDesignInfo(){
        $aiCommonModel = new AiCommand();
        $data = $aiCommonModel->read(['key'=>'ai_layout_design','project_id'=>$this->user['project_id']]);
        if($data === false){
            $data = $aiCommonModel->read(['key'=>'ai_layout_design','project_id'=>0]);
        }
        $this->response('success', Code::SUCCESS, $data);
    }

    /**
     * @remark :保存指令
     * @name   :saveAiCommand
     * @author :lyh
     * @method :post
     * @time   :2025/5/26 17:15
     */
    public function saveLayoutDesign(){
        $this->request->validate([
            'key'=>['required']
        ],[
            'key.required' => '场景不能为空',
        ]);
        $aiCommonModel = new AiCommand();
        if($this->param['key'] != 'ai_layout_design'){
            $this->response('success');
        }
        $data = $aiCommonModel->read(['key'=>'ai_layout_design','project_id'=>$this->user['project_id']]);
        if($data === false) {
            $this->param['project_id'] = $this->user['project_id'];
            $id = $aiCommonModel->addReturnId($this->param);
        }else{
            $id = $data['id'];
            $aiCommonModel->edit($this->param,['id'=>$data['id']]);
        }
        $this->response('success', Code::SUCCESS, ['id'=>$id]);
    }

    /**
     * @remark :根据指令获取内容
     * @name   :sendLayoutDesign
     * @author :lyh
     * @method :post
     * @time   :2025/5/26 17:39
     */
    public function sendLayoutDesign(){
        $this->request->validate([
            'ai'=>['required']
        ],[
            'ai.required' => '场景不能为空',
        ]);
        $aiCommandService = new AiCommandService();
        $this->param['ai'] = '请根据这个 {​海外展会与独立站数字营销的结合:线下资源如何助力线上推广}  文章标题帮我生成一个更有吸引力、引发读者兴趣的新标题,只需要给我标题内容,不要回复我别的内容(比如序号、你的提示、寒暄、代码解释、总结之类的)';
        $result = $aiCommandService->send_layout_design($this->param['ai']);
        $this->response('success', Code::SUCCESS, $result);
    }
}