Common.php 1.7 KB
<?php

namespace App\Helper;

use App\Enums\Common\Code;
use App\Models\AiCommand as AiCommandModel;
use App\Models\User\UserLog as UserLogModel;
use App\Models\User\UserLogin as UserLoginModel;

class Common
{
    /**
     * @name :生成用户操作日志
     * @return void
     * @author :liyuhang
     * @method
     */
    public static function set_user_log($param = []){
        $data = [
            'operator_id'=>$param['operator_id'],
            'model'=>$param['model'],
            'remark'=>$param['remark']
        ];
        $model = new UserLogModel();
        return $model->add($data);
    }

    /**
     * @name :写入登录日志
     * @return void
     * @author :liyuhang
     * @method
     */
    public static function set_user_login($param = []){
        $data = [
            'user_id'=>$param['id'],
            'ip'=>$param['ip']
        ];
        $model = new UserLoginModel();
        return $model->add($data);
    }

    /**
     * @name :ai自动生成
     * @return mixed
     * @author :liyuhang
     * @method
     */
    public function send_openai_msg($url){
        $url = HTTP_OPENAI_URL.$url;
        $aiCommandModel = New AiCommandModel();
        //指定库获取指令
        $info = $aiCommandModel->read(['key'=>$this->param['key']]);
        if($info === false){
            response('指令不存在',400);
        }
        //替换关键字
        $content = str_replace('$keyword$', $this->param['keywords'], $info['ai']);
        $data = [
            'messages'=>[
                ['role'=>'system','content'=>$info['scene']],
                ['role'=>'assistant','content'=>$content],
            ]
        ];
        return http_post($url,json_encode($data));
    }
}