BaseController.php
6.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php
namespace App\Http\Controllers\Bside;
use App\Enums\Common\Code;
use App\Http\Controllers\Controller;
use App\Models\AiCommand as AiCommandModel;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Support\Facades\Cache;
class BaseController extends Controller
{
protected $param = [];//所有请求参数
protected $token = ''; //token
protected $request = [];//助手函数
protected $page = 1;//当前页
protected $row = 20;//每页条数
protected $header = [];//设置请求头参数
protected $order = 'id';
protected $map = [];//处理后的参数
protected $uid = 0;
protected $user = [];//当前登录用户详情
/**
* 获取所有参数
*/
public function __construct(Request $request)
{
$this->request = $request;
$this->param = $this->request->all();
$this->token = $this->request->header('token');
if(!empty($this->token) && !empty(Cache::get($this->token))){
$info = Cache::get($this->token);
$this->user = $info;
$this->uid = $info['id'];
}else{
return response(['code'=>Code::USER_ERROR,'msg'=>'当前用户未登录']);
}
$this->get_param();
}
/**
* 成功返回
* @param array $data
* @param string $code
* @param bool $objectData
* @return JsonResponse
* @throws \Psr\Container\ContainerExceptionInterface
* @throws \Psr\Container\NotFoundExceptionInterface
*/
function success(array $data = [], string $code = Code::SUCCESS, bool $objectData = false): JsonResponse
{
if ($objectData) {
$data = (object)$data;
}
$code = Code::fromValue($code);
$response = [
'code' => $code->value,
'data' => $data,
'msg' => $code->description,
];
$this->header['token'] = $this->token;
return response()->json($response,200,$this->header);
}
/**
* @name 参数过滤
* @return void
* @author :liyuhang
* @method
*/
public function get_param(){
$param = $this->param;
foreach ($param as $k => $v){
if(is_array($v)){
continue;
}
switch ($k){
case "order":
$this->order = $v;
break;
case 'page':
$this->page = $v;
break;
case 'row':
$this->row = $v;
break;
case "status":
$this->map['status'] = $v;
break;
case "category_id":
$this->map['category_id'] = $v;
break;
case "name":
$this->map['name'] = ['like','%'.$v.'%'];
break;
case "start_at":
if(!empty($v)){
$this->_btw[0] = $v;
$this->_btw[1] = date('Y-m-d H:i:s',time());
$this->map['created_at'] = ['between', $this->_btw];
}
break;
case "end_at":
if(!empty($v)){
$this->_btw[1] = $v;
$this->map['updated_at'] = ['between', $this->_btw];
}
break;
default:
if(!empty($v)){
$this->map[$k] = $v;
}
break;
}
}
}
/**
* @name 统一返回参数
* @return JsonResponse
* @author :liyuhang
* @method
*/
public function response($msg = null,string $code = Code::SUCCESS,$data = [],$result_code = 200,$type = 'application/json'): JsonResponse
{
$code = Code::fromValue($code);
$result = [
'msg' => $msg == ' ' ? $code->description : $msg,
'code' => $code->value,
'data' => $this->_extents($data),
];
$this->header['Content-Type'] = $type;
$this->header['token'] = $this->token;
$response = response($result,$result_code,$this->header);;
throw new HttpResponseException($response);
}
/**
* 菜单权限->得到子级数组
* @param int
* @return array
*/
public function _get_child($my_id, $arr)
{
$new_arr = array();
foreach ($arr as $k => $v) {
$v = (array)$v;
if ($v['pid'] == $my_id) {
$v['sub'] = $this->_get_child($v['id'],$arr);
$new_arr[] = $v;
}
}
return $new_arr ? $new_arr : false;
}
protected function _extents($data) {
if (empty($data) || !is_array($data)) {
return empty($data) ? is_array($data) ? [] : '' : $data;
}
foreach ($data as $k => $v) {
if (is_array($v)) {
$data[$k] = $this->_extents($v);
} else {
if (is_null($v)) {
$data[$k] = '';
continue;
}
switch ((string) $k) {
case 'image':
$data['image_link'] = url('/b/image/' . $v);
break;
}
}
}
return $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){
$this->response('指令不存在',Code::USER_ERROR);
}
//替换关键字
$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));
}
}