作者 liyuhang

gx

@@ -2,8 +2,7 @@ @@ -2,8 +2,7 @@
2 2
3 namespace App\Helper; 3 namespace App\Helper;
4 4
5 -use App\Enums\Common\Code;  
6 -use App\Models\AiCommand as AiCommandModel; 5 +use App\Models\Ai\AiCommand as AiCommandModel;
7 use App\Models\User\UserLog as UserLogModel; 6 use App\Models\User\UserLog as UserLogModel;
8 use App\Models\User\UserLogin as UserLoginModel; 7 use App\Models\User\UserLogin as UserLoginModel;
9 use Illuminate\Support\Facades\Cache; 8 use Illuminate\Support\Facades\Cache;
@@ -47,8 +46,7 @@ class Common @@ -47,8 +46,7 @@ class Common
47 * @author :liyuhang 46 * @author :liyuhang
48 * @method 47 * @method
49 */ 48 */
50 - public static function send_openai_msg($url){  
51 - $param = request()->post(); 49 + public static function send_openai_msg($url,$param){
52 $url = HTTP_OPENAI_URL.$url; 50 $url = HTTP_OPENAI_URL.$url;
53 $aiCommandModel = New AiCommandModel(); 51 $aiCommandModel = New AiCommandModel();
54 //指定库获取指令 52 //指定库获取指令
@@ -75,7 +73,7 @@ class Common @@ -75,7 +73,7 @@ class Common
75 */ 73 */
76 public static function get_user_cache($table,$id,$type = 'B'){ 74 public static function get_user_cache($table,$id,$type = 'B'){
77 $key = 'cache_'.$table.'_'.$id.'_type'; 75 $key = 'cache_'.$table.'_'.$id.'_type';
78 - $lists = Cache::get($key); 76 + $lists = Cache::store('file')->get($key);
79 return $lists; 77 return $lists;
80 } 78 }
81 79
@@ -86,18 +84,22 @@ class Common @@ -86,18 +84,22 @@ class Common
86 * @method 84 * @method
87 */ 85 */
88 public static function set_user_cache($data = [],$table,$id,$type = 'B'){ 86 public static function set_user_cache($data = [],$table,$id,$type = 'B'){
89 - $key = 'cache_'.$table.'_'.$id.'_type';  
90 - return Cache::add($key,$data); 87 + $user_cache = config('cache.user_is_cache');
  88 + if(!empty($user_cache) && $user_cache['is_cache'] == true){
  89 + $key = 'cache_'.$table.'_'.$id.'_type';
  90 + Cache::store('file')->set($key,$data);
  91 + }
  92 + return true;
91 } 93 }
92 94
93 /** 95 /**
94 - * @name :清缓存 96 + * @name :清缓存
95 * @return bool 97 * @return bool
96 * @author :liyuhang 98 * @author :liyuhang
97 * @method 99 * @method
98 */ 100 */
99 - public static function del_user_cache($data = [],$rule,$project_id,$type = 'B'){  
100 - $key = 'cache_user_'.$rule.'_'.$project_id.'_'.$type;  
101 - return Cache::add($key,$data); 101 + public static function del_user_cache($table,$id,$type = 'B'){
  102 + $key = 'cache_'.$table.'_'.$id.'_type';
  103 + return Cache::store('file')->pull($key);
102 } 104 }
103 } 105 }
@@ -6,7 +6,7 @@ use App\Enums\Common\Code; @@ -6,7 +6,7 @@ use App\Enums\Common\Code;
6 use App\Http\Controllers\Aside\BaseController; 6 use App\Http\Controllers\Aside\BaseController;
7 use App\Http\Logic\Aside\Ai\AiCommandLogic; 7 use App\Http\Logic\Aside\Ai\AiCommandLogic;
8 use App\Http\Requests\Aside\Ai\AiCommandRequest; 8 use App\Http\Requests\Aside\Ai\AiCommandRequest;
9 -use App\Models\AiCommand as AiCommandModel; 9 +use App\Models\Ai\AiCommand as AiCommandModel;
10 use Illuminate\Http\Request; 10 use Illuminate\Http\Request;
11 use function App\Helper\send_openai_msg; 11 use function App\Helper\send_openai_msg;
12 12
@@ -4,6 +4,7 @@ namespace App\Http\Controllers\Bside; @@ -4,6 +4,7 @@ namespace App\Http\Controllers\Bside;
4 4
5 use App\Enums\Common\Code; 5 use App\Enums\Common\Code;
6 use App\Helper\Common; 6 use App\Helper\Common;
  7 +use App\Models\Ai\AiLog;
7 use Illuminate\Http\Request; 8 use Illuminate\Http\Request;
8 9
9 class AiCommandController extends BaseController 10 class AiCommandController extends BaseController
@@ -15,8 +16,8 @@ class AiCommandController extends BaseController @@ -15,8 +16,8 @@ class AiCommandController extends BaseController
15 * @author :liyuhang 16 * @author :liyuhang
16 * @method 17 * @method
17 */ 18 */
18 - public function ai_http_post(Request $request){  
19 - $request->validate([ 19 + public function ai_http_post(){
  20 + $this->request->validate([
20 'keywords'=>['required'], 21 'keywords'=>['required'],
21 'key'=>['required'] 22 'key'=>['required']
22 ],[ 23 ],[
@@ -24,7 +25,25 @@ class AiCommandController extends BaseController @@ -24,7 +25,25 @@ class AiCommandController extends BaseController
24 'key.required' => '场景不能为空', 25 'key.required' => '场景不能为空',
25 ]); 26 ]);
26 #TODO 通过key获取到ai指令对象 27 #TODO 通过key获取到ai指令对象
27 - $data = Common::send_openai_msg($this->chat_url); 28 + $data = Common::send_openai_msg($this->chat_url,$this->param);
  29 + $this->set_ai_log($data);
28 $this->response('success',Code::SUCCESS,$data); 30 $this->response('success',Code::SUCCESS,$data);
29 } 31 }
  32 +
  33 + /**
  34 + * @name :写入日志
  35 + * @return void
  36 + * @author :liyuhang
  37 + * @method
  38 + */
  39 + public function set_ai_log($data){
  40 + //写入日志
  41 + $param = [
  42 + 'key'=> $this->param['key'],
  43 + 'keywords'=>$this->param['key'],
  44 + 'remark' =>$data
  45 + ];
  46 + $aiLog = new AiLog();
  47 + return $aiLog->add($param);
  48 + }
30 } 49 }
@@ -39,26 +39,11 @@ class BaseController extends Controller @@ -39,26 +39,11 @@ class BaseController extends Controller
39 $this->get_param(); 39 $this->get_param();
40 //日志记录 40 //日志记录
41 $this->set_user_log(); 41 $this->set_user_log();
42 - //读取缓存  
43 - $this->get_cache();  
44 } 42 }
45 43
46 } 44 }
47 45
48 /** 46 /**
49 - * @name :读取缓存  
50 - * @return void  
51 - * @author :liyuhang  
52 - * @method  
53 - */  
54 - public function get_cache(){  
55 - //TODO::读取缓存  
56 - $data = Cache::get('cache_'.$this->request->route()->getName());  
57 - if(isset($data) && !empty($data)){  
58 - $this->response('success',Code::SUCCESS,$data);  
59 - }  
60 - }  
61 - /**  
62 * @name 参数过滤 47 * @name 参数过滤
63 * @return void 48 * @return void
64 * @author :liyuhang 49 * @author :liyuhang
@@ -212,5 +197,6 @@ class BaseController extends Controller @@ -212,5 +197,6 @@ class BaseController extends Controller
212 Common::set_user_log(['model'=>$this->request->route()->getName(),'remark'=>'请求的参数:param = '.json_encode($this->param),'operator_id'=>$this->uid]); 197 Common::set_user_log(['model'=>$this->request->route()->getName(),'remark'=>'请求的参数:param = '.json_encode($this->param),'operator_id'=>$this->uid]);
213 } 198 }
214 } 199 }
  200 + return true;
215 } 201 }
216 } 202 }
@@ -10,7 +10,6 @@ use App\Http\Logic\Bside\Blog\BlogLogic; @@ -10,7 +10,6 @@ use App\Http\Logic\Bside\Blog\BlogLogic;
10 use App\Http\Requests\Bside\Blog\BlogRequest; 10 use App\Http\Requests\Bside\Blog\BlogRequest;
11 use App\Models\Blog\Blog as BlogModel; 11 use App\Models\Blog\Blog as BlogModel;
12 use Illuminate\Http\Request; 12 use Illuminate\Http\Request;
13 -use Illuminate\Support\Facades\Cache;  
14 13
15 class BlogController extends BaseController 14 class BlogController extends BaseController
16 { 15 {
@@ -159,7 +158,7 @@ class BlogController extends BaseController @@ -159,7 +158,7 @@ class BlogController extends BaseController
159 ]); 158 ]);
160 #TODO 通过key获取到ai指令对象 159 #TODO 通过key获取到ai指令对象
161 $url = 'v2/openai_chat'; 160 $url = 'v2/openai_chat';
162 - $data = Common::send_openai_msg($url); 161 + $data = Common::send_openai_msg($url,$request->post());
163 $this->response('success',Code::SUCCESS,$data); 162 $this->response('success',Code::SUCCESS,$data);
164 } 163 }
165 164
@@ -159,7 +159,7 @@ class NewsController extends BaseController @@ -159,7 +159,7 @@ class NewsController extends BaseController
159 ]); 159 ]);
160 #TODO 通过key获取到ai指令对象 160 #TODO 通过key获取到ai指令对象
161 $url = 'v2/openai_chat'; 161 $url = 'v2/openai_chat';
162 - $data = Common::send_openai_msg($url); 162 + $data = Common::send_openai_msg($url,$request->post());
163 $this->response('success',Code::SUCCESS,$data); 163 $this->response('success',Code::SUCCESS,$data);
164 } 164 }
165 165
@@ -3,7 +3,7 @@ @@ -3,7 +3,7 @@
3 namespace App\Http\Logic\Aside\Ai; 3 namespace App\Http\Logic\Aside\Ai;
4 4
5 use App\Http\Logic\Aside\BaseLogic; 5 use App\Http\Logic\Aside\BaseLogic;
6 -use App\Models\AiCommand as AiCommandModel; 6 +use App\Models\Ai\AiCommand as AiCommandModel;
7 7
8 class AiCommandLogic extends BaseLogic 8 class AiCommandLogic extends BaseLogic
9 { 9 {
@@ -101,7 +101,7 @@ class BlogLogic extends BaseLogic @@ -101,7 +101,7 @@ class BlogLogic extends BaseLogic
101 if($info !== false){ 101 if($info !== false){
102 $this->fail('当前名称已存在'); 102 $this->fail('当前名称已存在');
103 } 103 }
104 - $this->param['operator_id'] = $this->uid; 104 + $this->param['operator_id'] = $this->user['id'];
105 DB::beginTransaction(); 105 DB::beginTransaction();
106 try { 106 try {
107 if(isset($this->param['image']) && is_file($this->param['image'])){ 107 if(isset($this->param['image']) && is_file($this->param['image'])){
1 <?php 1 <?php
2 2
3 -namespace App\Models; 3 +namespace App\Models\Ai;
  4 +
  5 +use App\Models\Base;
4 6
5 class AiCommand extends Base 7 class AiCommand extends Base
6 { 8 {
  1 +<?php
  2 +
  3 +namespace App\Models\Ai;
  4 +
  5 +use App\Models\Base;
  6 +
  7 +class AiLog extends Base
  8 +{
  9 + //设置关联表名
  10 + protected $table = 'gl_ai_log';
  11 + //自动维护create_at创建时间 updated_at修改时间
  12 + public $timestamps = true;
  13 +}
@@ -77,8 +77,6 @@ class Base extends Model @@ -77,8 +77,6 @@ class Base extends Model
77 return []; 77 return [];
78 } 78 }
79 $lists = $lists->toArray(); 79 $lists = $lists->toArray();
80 - //TODO::写入缓存  
81 -  
82 return $lists; 80 return $lists;
83 } 81 }
84 /** 82 /**
@@ -109,10 +107,14 @@ class Base extends Model @@ -109,10 +107,14 @@ class Base extends Model
109 */ 107 */
110 public function read($condition,$files = ['*']) 108 public function read($condition,$files = ['*'])
111 { 109 {
112 - $query = $this->formatQuery($condition);  
113 - $info = $query->select($files)->first();  
114 - if (empty($info)) {  
115 - return false; 110 + $info = Common::get_user_cache($this->getTable(),$condition['id']);
  111 + if(empty($info)){
  112 + $query = $this->formatQuery($condition);
  113 + $info = $query->select($files)->first();
  114 + if (empty($info)) {
  115 + return false;
  116 + }
  117 + Common::set_user_cache($info,$this->getTable(),$condition['id']);
116 } 118 }
117 $info = $info->toArray(); 119 $info = $info->toArray();
118 return $info; 120 return $info;
@@ -137,7 +139,12 @@ class Base extends Model @@ -137,7 +139,12 @@ class Base extends Model
137 public function edit($data,$condition){ 139 public function edit($data,$condition){
138 $query = $this->formatQuery($condition); 140 $query = $this->formatQuery($condition);
139 $data['updated_at'] = date('Y-m-d H:i:s'); 141 $data['updated_at'] = date('Y-m-d H:i:s');
140 - return $query->update($data); 142 + $rs = $query->update($data);
  143 + if($rs !== false){
  144 + //删除缓存
  145 + Common::del_user_cache($this->getTable(),$condition['id']);
  146 + }
  147 + return $rs;
141 } 148 }
142 /** 149 /**
143 * @name : 删除数据 150 * @name : 删除数据
@@ -147,7 +154,12 @@ class Base extends Model @@ -147,7 +154,12 @@ class Base extends Model
147 */ 154 */
148 public function del($condition){ 155 public function del($condition){
149 $query = $this->formatQuery($condition); 156 $query = $this->formatQuery($condition);
150 - return $query->delete(); 157 + $rs = $query->delete();
  158 + if($rs !== false){
  159 + //删除缓存
  160 + Common::del_user_cache($this->getTable(),$condition['id']);
  161 + }
  162 + return $rs;
151 } 163 }
152 /** 164 /**
153 * @name :参数处理查询 165 * @name :参数处理查询
@@ -106,5 +106,7 @@ return [ @@ -106,5 +106,7 @@ return [
106 */ 106 */
107 107
108 'prefix' => env('CACHE_PREFIX', Str::slug(env('APP_NAME', 'laravel'), '_').'_cache'), 108 'prefix' => env('CACHE_PREFIX', Str::slug(env('APP_NAME', 'laravel'), '_').'_cache'),
109 - 109 + 'user_is_cache' =>[
  110 + 'is_cache' => false,
  111 + ],
110 ]; 112 ];