作者 liyuhang

gx

... ... @@ -2,8 +2,7 @@
namespace App\Helper;
use App\Enums\Common\Code;
use App\Models\AiCommand as AiCommandModel;
use App\Models\Ai\AiCommand as AiCommandModel;
use App\Models\User\UserLog as UserLogModel;
use App\Models\User\UserLogin as UserLoginModel;
use Illuminate\Support\Facades\Cache;
... ... @@ -47,8 +46,7 @@ class Common
* @author :liyuhang
* @method
*/
public static function send_openai_msg($url){
$param = request()->post();
public static function send_openai_msg($url,$param){
$url = HTTP_OPENAI_URL.$url;
$aiCommandModel = New AiCommandModel();
//指定库获取指令
... ... @@ -75,7 +73,7 @@ class Common
*/
public static function get_user_cache($table,$id,$type = 'B'){
$key = 'cache_'.$table.'_'.$id.'_type';
$lists = Cache::get($key);
$lists = Cache::store('file')->get($key);
return $lists;
}
... ... @@ -86,18 +84,22 @@ class Common
* @method
*/
public static function set_user_cache($data = [],$table,$id,$type = 'B'){
$user_cache = config('cache.user_is_cache');
if(!empty($user_cache) && $user_cache['is_cache'] == true){
$key = 'cache_'.$table.'_'.$id.'_type';
return Cache::add($key,$data);
Cache::store('file')->set($key,$data);
}
return true;
}
/**
* @name :清缓存
* @name :清缓存
* @return bool
* @author :liyuhang
* @method
*/
public static function del_user_cache($data = [],$rule,$project_id,$type = 'B'){
$key = 'cache_user_'.$rule.'_'.$project_id.'_'.$type;
return Cache::add($key,$data);
public static function del_user_cache($table,$id,$type = 'B'){
$key = 'cache_'.$table.'_'.$id.'_type';
return Cache::store('file')->pull($key);
}
}
... ...
... ... @@ -6,7 +6,7 @@ use App\Enums\Common\Code;
use App\Http\Controllers\Aside\BaseController;
use App\Http\Logic\Aside\Ai\AiCommandLogic;
use App\Http\Requests\Aside\Ai\AiCommandRequest;
use App\Models\AiCommand as AiCommandModel;
use App\Models\Ai\AiCommand as AiCommandModel;
use Illuminate\Http\Request;
use function App\Helper\send_openai_msg;
... ...
... ... @@ -4,6 +4,7 @@ namespace App\Http\Controllers\Bside;
use App\Enums\Common\Code;
use App\Helper\Common;
use App\Models\Ai\AiLog;
use Illuminate\Http\Request;
class AiCommandController extends BaseController
... ... @@ -15,8 +16,8 @@ class AiCommandController extends BaseController
* @author :liyuhang
* @method
*/
public function ai_http_post(Request $request){
$request->validate([
public function ai_http_post(){
$this->request->validate([
'keywords'=>['required'],
'key'=>['required']
],[
... ... @@ -24,7 +25,25 @@ class AiCommandController extends BaseController
'key.required' => '场景不能为空',
]);
#TODO 通过key获取到ai指令对象
$data = Common::send_openai_msg($this->chat_url);
$data = Common::send_openai_msg($this->chat_url,$this->param);
$this->set_ai_log($data);
$this->response('success',Code::SUCCESS,$data);
}
/**
* @name :写入日志
* @return void
* @author :liyuhang
* @method
*/
public function set_ai_log($data){
//写入日志
$param = [
'key'=> $this->param['key'],
'keywords'=>$this->param['key'],
'remark' =>$data
];
$aiLog = new AiLog();
return $aiLog->add($param);
}
}
... ...
... ... @@ -39,26 +39,11 @@ class BaseController extends Controller
$this->get_param();
//日志记录
$this->set_user_log();
//读取缓存
$this->get_cache();
}
}
/**
* @name :读取缓存
* @return void
* @author :liyuhang
* @method
*/
public function get_cache(){
//TODO::读取缓存
$data = Cache::get('cache_'.$this->request->route()->getName());
if(isset($data) && !empty($data)){
$this->response('success',Code::SUCCESS,$data);
}
}
/**
* @name 参数过滤
* @return void
* @author :liyuhang
... ... @@ -212,5 +197,6 @@ class BaseController extends Controller
Common::set_user_log(['model'=>$this->request->route()->getName(),'remark'=>'请求的参数:param = '.json_encode($this->param),'operator_id'=>$this->uid]);
}
}
return true;
}
}
... ...
... ... @@ -10,7 +10,6 @@ use App\Http\Logic\Bside\Blog\BlogLogic;
use App\Http\Requests\Bside\Blog\BlogRequest;
use App\Models\Blog\Blog as BlogModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
class BlogController extends BaseController
{
... ... @@ -159,7 +158,7 @@ class BlogController extends BaseController
]);
#TODO 通过key获取到ai指令对象
$url = 'v2/openai_chat';
$data = Common::send_openai_msg($url);
$data = Common::send_openai_msg($url,$request->post());
$this->response('success',Code::SUCCESS,$data);
}
... ...
... ... @@ -159,7 +159,7 @@ class NewsController extends BaseController
]);
#TODO 通过key获取到ai指令对象
$url = 'v2/openai_chat';
$data = Common::send_openai_msg($url);
$data = Common::send_openai_msg($url,$request->post());
$this->response('success',Code::SUCCESS,$data);
}
... ...
... ... @@ -3,7 +3,7 @@
namespace App\Http\Logic\Aside\Ai;
use App\Http\Logic\Aside\BaseLogic;
use App\Models\AiCommand as AiCommandModel;
use App\Models\Ai\AiCommand as AiCommandModel;
class AiCommandLogic extends BaseLogic
{
... ...
... ... @@ -101,7 +101,7 @@ class BlogLogic extends BaseLogic
if($info !== false){
$this->fail('当前名称已存在');
}
$this->param['operator_id'] = $this->uid;
$this->param['operator_id'] = $this->user['id'];
DB::beginTransaction();
try {
if(isset($this->param['image']) && is_file($this->param['image'])){
... ...
<?php
namespace App\Models;
namespace App\Models\Ai;
use App\Models\Base;
class AiCommand extends Base
{
... ...
<?php
namespace App\Models\Ai;
use App\Models\Base;
class AiLog extends Base
{
//设置关联表名
protected $table = 'gl_ai_log';
//自动维护create_at创建时间 updated_at修改时间
public $timestamps = true;
}
... ...
... ... @@ -77,8 +77,6 @@ class Base extends Model
return [];
}
$lists = $lists->toArray();
//TODO::写入缓存
return $lists;
}
/**
... ... @@ -109,11 +107,15 @@ class Base extends Model
*/
public function read($condition,$files = ['*'])
{
$info = Common::get_user_cache($this->getTable(),$condition['id']);
if(empty($info)){
$query = $this->formatQuery($condition);
$info = $query->select($files)->first();
if (empty($info)) {
return false;
}
Common::set_user_cache($info,$this->getTable(),$condition['id']);
}
$info = $info->toArray();
return $info;
}
... ... @@ -137,7 +139,12 @@ class Base extends Model
public function edit($data,$condition){
$query = $this->formatQuery($condition);
$data['updated_at'] = date('Y-m-d H:i:s');
return $query->update($data);
$rs = $query->update($data);
if($rs !== false){
//删除缓存
Common::del_user_cache($this->getTable(),$condition['id']);
}
return $rs;
}
/**
* @name : 删除数据
... ... @@ -147,7 +154,12 @@ class Base extends Model
*/
public function del($condition){
$query = $this->formatQuery($condition);
return $query->delete();
$rs = $query->delete();
if($rs !== false){
//删除缓存
Common::del_user_cache($this->getTable(),$condition['id']);
}
return $rs;
}
/**
* @name :参数处理查询
... ...
... ... @@ -106,5 +106,7 @@ return [
*/
'prefix' => env('CACHE_PREFIX', Str::slug(env('APP_NAME', 'laravel'), '_').'_cache'),
'user_is_cache' =>[
'is_cache' => false,
],
];
... ...