SystemConfigsController.php
10.9 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
<?php
/**
+-----------------------------------------------------------------------------------------------------------------------
* 管理端控制层: 社区 控制类
+-----------------------------------------------------------------------------------------------------------------------
*
* PHP version 7
*
* @category App\Admin\Controllers\System
* @package App\Admin\Controllers\System
* @author Richer <yangzi1028@163.com>
* @date 2021年1月27日10:57:55
* @copyright 2020-2022 Richer (http://www.Richer.com/)
* @license http://www.Richer.com/ License
* @link http://www.Richer.com/
*/
namespace App\Admin\Controllers\System;
use App\Admin\Controllers\BaseController;
use App\Admin\Rewrite\Admin;
use App\Admin\Rewrite\Form;
use App\Models\System\SystemConfig;
use Encore\Admin\Form\Tools;
use Encore\Admin\Grid;
//use Encore\Admin\Form;
use Encore\Admin\Layout\Content;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Cache;
/**
* Class SystemConfigsController.
*
* @category App\Admin\Controllers\System
* @package App\Admin\Controllers\System
* @author Richer <yangzi1028@163.com>
* @date 2021年1月27日10:57:55
* @copyright 2020-2022 Richer (http://www.Richer.com/)
* @license http://www.Richer.com/ License
* @link http://www.Richer.com/
*/
class SystemConfigsController extends BaseController
{
/**
* SystemConfigsController constructor.
*
* @param SystemConfig $model 注入model
*/
public function __construct(SystemConfig $model)
{
// 资源显示的中名称
$this ->title = $model::OBJ_NAME;
// 是否可查看
$this->can_view = false;
// 是否可新增
$this->can_create = true;
// 是否可编辑
$this->can_edit = true;
// 是否可删除
$this->can_delete = true;
// 是否开启下拉菜单
$this->dropdownActions = false;
// 执行父类构造方法
parent::__construct($model);
}
/**
* User setting page.
*
* @param Content $content
*
* @return Content
*/
public function getSetting(Content $content)
{
// 获取 boss
$boss = $this->model->first();
$form = $this->settingForm();
$form->tools(
function (Tools $tools) {
$tools->disableList();
$tools->disableDelete();
$tools->disableView();
}
);
if ($boss) {
return $content
->title(trans('admin.user_setting'))
->body($form->edit(optional($boss)->id));
} else {
return $content
->title(trans('admin.user_setting'))
->body($form);
}
}
/**
* Update setting.
*
* @return bool|\Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function postSetting()
{
return $this->settingForm()->store();
}
/**
* Update user setting.
*
* @return array|bool|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse|\Illuminate\Http\Response|\Illuminate\Routing\Redirector|mixed|\Symfony\Component\HttpFoundation\Response|null
*/
public function putSetting()
{
return $this->settingForm()->update(1);
}
/**
* Model-form for user setting.
*
* @return Form
*/
protected function settingForm()
{
$this->form = new Form($this->model);
$this->formNumberField('consumption_times', '每次提问消耗次数', 1);
$this->formNumberField('context_timeout', '上下文超时时间')->help('单位,分钟,提问后超过该时间,上下文自动清空,下次提问不会带有上下文广西', 1);
$this->formTextField('start_statement', '提问起始语', 255, 1);
// $form->number('body', '详情')->required()->help('请上传小于1G的视频...')->setWidth(10, 1)->setGroupClass('col-md-12');
//// $this->formRichTextField('body', '详情', 1);
//
//// $form->textarea('name', trans('admin.name'))->rules('required')->setWidth(10, 1)->setGroupClass('col-md-12');
// $form->table('wechat', '微信交流', function ($table) {
// $table->text('name', '名称')->required();
// $table->text('number', '号码')->required();
// })->setGroupClass('col-sm-12')->setWidth(10, 1);
//
// $form->table('phone', '电话交流', function ($table) {
// $table->text('name', '名称')->required();
// $table->text('number', '号码')->required();
// })->setGroupClass('col-sm-12')->setWidth(10, 1);
$this->form->setAction(admin_url('system-config'));
$this->form->saved(function () {
admin_toastr(trans('admin.update_succeeded'));
return redirect(admin_url('system-config'));
});
return $this->form;
}
/**
* 为grid增加筛选条件
*
* @return \Encore\Admin\Grid
*/
public function renderGridFilter()
{
$this->grid->filter(function ($filter) {
// 筛选条件默认展开
$filter->expand();
$filter->equal('key')->select(SystemConfig::getKeyOptions())->placeholder(__('parameter_key'));
//4.去掉默认的搜索
$filter->disableIdFilter();
});
return $this->grid;
}
/**
* 设置默认查询条件
* 增加数据权限的判断。步骤:
* 1、判断用户是否是超级管理员,并判断用户的pid,如果pid为0 则为平台总用户,不进行数据权限的判断
* 2、如果pid大于0 则为 平台子管理员,则需要进行数据权限的判断
*/
public function setGridQuery()
{
}
/**
* 渲染grid字段
*
* @return void
*/
public function renderGridFields()
{
$this->gridNumberField($this->grid, 'id', 'ID', 1);
$this->gridSingleSelectField('key', __('parameter_key'), SystemConfig::getKeyOptions(), 1);
$this->gridTextField($this->grid, 'value', __('parameter_value'))->display(function ($value) {
// TODO
$language = request()->cookie(config('constants.COOKIE_KEY'), 'zh-CN');
$str = '';
$type = $this->type;
if ($value && is_array($value)) {
foreach ($value as $vo) {
$s = $vo['value_zh'];
if ($language === 'en') {
$s = Arr::get($vo, 'value_en');
}
$str .= '<span class="label label-success" style="margin-right: 0.5rem">'.$s.'</span>';
}
}
return $str;
if ($type === 'single_select') {
if ($value && is_array($value)) {
foreach ($value as $vo) {
$s = $vo['value_zh'];
if ($language === 'en') {
$s = $vo['value_en'];
}
$str .= '<span class="label label-success" style="margin-right: 0.5rem">'.$s.'</span>';
}
}
} else {
if ($value && is_array($value)) {
foreach ($value as $vo) {
$s = $vo['value'];
if ($language === 'zh-CN') {
$s = $vo['value_zh'];
}
$str .= '<span class="label label-success" style="margin-right: 0.5rem">'.$s.'</span>';
$items = Arr::get($vo, 'items');
if ($items) {
foreach ($items as $item) {
$s1 = $item['value'];
if ($language === 'zh-CN') {
$s1 = $item['value_zh'];
}
$str .= '<span class="label label-success" style="margin-right: 0.5rem">'.$s1.'</span>';
}
}
}
}
}
return $str;
});
// $this->gridNumberField($this->grid, 'sort', '排序', 1);
// $this->generateGridCreatedByField($this->grid, 'user', '创建用户');
$this->generateGridDateField($this->grid)->sortable();
}
/**
* Make a show builder.
*
* @param mixed $id
* @return Show
*/
protected function detail($id)
{
//
}
/**
* Make a form builder.
*
* @param null $id
* @return Form
*/
public function renderFormFields($id = null)
{
$this->generateFormSingleSelectField($this->form, 'key', __('parameter_key'), SystemConfig::getKeyOptions(), 1);
// $this->form->list('value', __('parameter_value'))->setGroupClass('col-sm-12 parameter_value')->setWidth(10, 1);
$this->form->table('value', __('parameter_value'), function ($table) {
$table->text('value_key', __('key'));
$table->text('value_en', __('value_en'));
$table->text('value_zh', __('value_zh'));
$table->text('description', __('description'));
})->setGroupClass('col-sm-12 parameter_value')->setWidth(10, 1);
// $this->form->keyValue('value', __('parameter_value'))->setGroupClass('col-sm-12 parameter_value')->setWidth(10, 1);
// $this->form->html(view('admin::system.config', compact('id', 'user_type', 'rooms')), '房间信息')->setGroupClass('col-md-12')->setWidth(12, 0);
// $this->generateFormRadioField($this->form, 'key', __('parameter_key'), SystemConfig::getKeyOptions(), 1);
$this->generateFormTextAreaField($this->form, 'description', __('description'))->attribute('class');
$this->form->hidden('type');
// $this->form->hasMany('items', '自定义字段', function (Form\NestedForm $form) {
// $form->text('value', '字段标题')->setWidth(8, 2)->setGroupClass('col-md-6');
// $form->text('value', '字段内容')->setWidth(8, 2)->setGroupClass('col-md-6');
// })->setWidth(10, 1)->setGroupClass('col-md-12');
// 渲染生成地区选择
Admin::script("$(document.body).append(`<script src='/assets/admin/js/module/system-config.js?t=1'>`);");
}
/**
* 保存前回调
*
* @return void
*/
public function saving()
{
// dd(request()->all());
//保存前回调
}
/**
* @param $key
* @return string[]|null
*/
public function getKey($key) {
$options = SystemConfig::KEY_OPTIONS;
$config = null;
foreach ($options as $option) {
if ($option['key'] === $key) {
$config = $option;
break;
}
}
return $config;
}
}