ChatController.php 2.8 KB
<?php
/**
 * @remark :
 * @name   :ChatController.php
 * @author :lyh
 * @method :post
 * @time   :2025/4/1 14:33
 */

namespace App\Http\Controllers\Bside\Gpt;

use App\Enums\Common\Code;
use App\Http\Controllers\Bside\BaseController;
use App\Http\Logic\Bside\Gpt\ChatLogic;
use App\Models\Gpt\Chat;
use App\Models\Gpt\ChatItem;

class ChatController extends BaseController
{
    /**
     * @remark :获取消息列表
     * @name   :list
     * @author :lyh
     * @method :post
     * @time   :2025/4/2 15:51
     */
    public function list(Chat $chat){
        $list = $chat->lists(['user_id'=>$this->user['id'],'status'=>1],$this->page,$this->row);
        $chatItem = new ChatItem();
        foreach ($list as $k => $v){
            $chatItem->orderBy('id', 'desc')->first();
        }
        $this->response('success',Code::SUCCESS,$list);
    }

    /**
     * @remark :获取所有子消息
     * @name   :itemList
     * @author :lyh
     * @method :post
     * @time   :2025/4/2 15:55
     */
    public function itemList(ChatItem $chatItem){
        $this->request->validate([
            'chat_id'=>['required'],
        ],[
            'chat_id.required' => 'chat_id不能为空',
        ]);
        $list = $chatItem->list(['user_id'=>$this->user['id'],'chat_id'=>$this->map['chat_id']],'id',['*'],'asc');
        $this->response('success',Code::SUCCESS,$list);
    }

    /**
     * @remark :发送消息(流返回)
     * @name   :sendMessage
     * @author :lyh
     * @method :post
     * @time   :2025/4/1 14:35
     */
    public function sendMessage(ChatLogic $logic){
        $this->request->validate([
            'message'=>['required'],
        ],[
            'message.required' => '消息内容不能为空',
        ]);
        return $logic->sendMessage();
    }

    /**
     * @remark :删除消息
     * @name   :del
     * @author :lyh
     * @method :post
     * @time   :2025/4/2 15:54
     */
    public function del(Chat $chat){
        $this->request->validate([
            'id'=>['required'],
        ],[
            'id.required' => 'id不能为空',
        ]);
        $data = $chat->edit(['status'=>0],['id'=>$this->param['id']]);
        $this->response('success',Code::SUCCESS,$data);
    }

    /**
     * @remark :修改标题
     * @name   :saveChat
     * @author :lyh
     * @method :post
     * @time   :2025/4/7 11:15
     */
    public function saveChat(Chat $chat){
        $this->request->validate([
            'id'=>['required'],
            'input_content'=>['required'],
        ],[
            'id.required' => 'id不能为空',
            'input_content.required' => '标题不能为空',
        ]);
        $data = $chat->edit(['input_content'=>$this->param['input_content']],['id'=>$this->param['id']]);
        $this->response('success',Code::SUCCESS,$data);
    }
}