VideoTempController.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
<?php
namespace App\Admin\Controllers;
use App\Models\VideoTemp;
use App\Models\Component;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Widgets\Table;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
class VideoTempController extends AdminController
{
protected $title = '临境模板';
/**
* Make a grid builder.
*
* @return Grid
*/
protected function grid()
{
return Grid::make(new VideoTemp(), function (Grid $grid) {
$grid->model()->with('components');
// 设置自定义视图
$grid->setActionClass(Grid\Displayers\Actions::class);
$grid->column('id',__('ID'))->sortable();
$grid->column('title');
$grid->column('bg_music')->bool();
$grid->column('bgm_url')->display(function ($url){
if ($this->bg_music == 0)
return '暂无';
elseif (Str::of($url)->contains('.mp3'))
return "<a target='_blank' href='". $url ."'>查看</a>";
else
return "<a target='_blank' href='". $url ."'>下载后查看</a>";
});
$grid->column('','组件信息')
->display('展开')
->expand(function (){
$th = ['id','模板id','名称','位置','字号','字体颜色','背景色','背景厚度','透明度','避免剪切','创建时间','修改时间'];
return Table::make($th, $this->components->toArray())->withBorder();
});
// $grid->column('type');
// $grid->column('bg_type');
// $grid->column('bg_url')->image('/storage/');
// $grid->column('bg_music');
$grid->column('state')->switch();
// $grid->column('sn');
// $grid->column('top');
// $grid->column('left');
// $grid->column('font_size');
$grid->column('created_at');
$grid->column('updated_at')->sortable();
//
// $grid->filter(function (Grid\Filter $filter) {
// $filter->equal('id');
//
// });
});
}
/**
* Make a show builder.
*
* @param mixed $id
*
* @return Show
*/
protected function detail($id)
{
return Show::make($id, new VideoTemp(), function (Show $show) {
$show->field('id');
$show->field('title');
$show->field('type');
$show->field('bg_type');
$show->field('bg_url');
$show->field('bg_music');
$show->field('state');
$show->field('sn');
$show->field('top');
$show->field('left');
$show->field('font_size');
$show->field('created_at');
$show->field('updated_at');
});
}
/**
* Make a form builder.
*
* @return Form
*/
protected function form()
{
return Form::make(new VideoTemp(), function (Form $form) {
$form->display('id');
$form->block(7, function (Form\BlockForm $form) {
// 设置标题
$form->title('基本设置');
// 显示底部提交按钮
$form->showFooter();
// 设置字段宽度
$form->width(8, 3);
$form->text('title');
$form->radio('bg_music')
->options(['无', '有'])
->when(1,function (Form\BlockForm $form){
$form->file('bgm_url')
->accept('mp3,aac,wav')
->autoUpload()
->uniqueName()
->addElementClass('bgm_url');
})
->default(0);
$form->checkbox('components','组件')
->when('every_poem', $this->buildCheckBoxOption('every_poem',$form))
->when('one_poem', $this->buildCheckBoxOption('one_poem',$form))
->when('weather', $this->buildCheckBoxOption('weather',$form))
->when('date', $this->buildCheckBoxOption('date',$form))
->when('feel', $this->buildCheckBoxOption('feel',$form))
->default(['one_poem','weather','date'])
->options([
'every_poem' => '每日一言组件',
'one_poem' => '一言组件',
'weather' => '天气组件',
'date' => '日期组件',
'feel' => '临境有感组件',
]);
$form->hidden('state')
->saving(function ($v) {
return $v;
});
});
$form->block(5, function (Form\BlockForm $form) {
$form->html(view('admin.form.phone'));
});
$form->display('created_at');
$form->display('updated_at');
});
}
public function edit($id, Content $content)
{
return $content
->translation($this->translation())
->title($this->title())
->description($this->description()['edit'] ?? trans('admin.edit'))
->body($this->form2()->edit($id));
}
public function form2()
{
return Form::make(VideoTemp::with('components'), function (Form $form) {
// dd($form->model()->toArray());
$form->display('id');
$form->block(7, function (Form\BlockForm $form) {
// 设置标题
$form->title('基本设置');
// 显示底部提交按钮
$form->showFooter();
// 设置字段宽度
$form->width(8, 3);
$form->text('title');
$form->radio('bg_music')
->options(['无', '有'])
->when(1,function (Form\BlockForm $form){
$form->file('bgm_url')
->accept('mp3,aac,wav')
->autoUpload()
->uniqueName()
->addElementClass('bgm_url');
})
->default(0);
$form->checkbox('components','组件')
// ->when('every_poem', $this->buildCheckBoxOption('every_poem',$form))
// ->when('one_poem', $this->buildCheckBoxOption('one_poem',$form))
// ->when('weather', $this->buildCheckBoxOption('weather',$form))
// ->when('date', $this->buildCheckBoxOption('date',$form))
// ->when('feel', $this->buildCheckBoxOption('feel',$form))
->default(['one_poem','weather','date'])
->options([
'every_poem' => '每日一言组件',
'one_poem' => '一言组件',
'weather' => '天气组件',
'date' => '日期组件',
'feel' => '临境有感组件',
])
->customFormat(function ($v) {
if (! $v) {
return [];
}
return array_column($v, 'id');
});;
$form->hidden('state')
->saving(function ($v) {
return $v;
});
});
$form->block(5, function (Form\BlockForm $form) {
$form->html(view('admin.form.phone'));
});
$form->display('created_at');
$form->display('updated_at');
});
}
public function store()
{
$all = \request()->all();
try{
DB::transaction(function ()use ($all){
$vide_temp = VideoTemp::query()->create([
'title' => $all['title'],
'state' => 1,
]);
foreach ($all['components'] as $component) {
if ($component !== null){
Component::query()->create([
'temp_id' => $vide_temp->id,
'name' => $component,
'position' => $all['pos_' . $component],
'font_size' => $all['font_size_' . $component],
'text_color' => $all['text_color_' . $component],
'text_bg_color' => $all['text_bg_color_' . $component],
'text_bg_box' => $all['text_bg_box_' . $component],
'opacity' => $all['opacity_' . $component],
'fix_bounds' => $all['fix_bounds_' . $component],
]);
}
}
});
}catch (\Exception $exception){
return $this->form()->response()->error($exception->getMessage());
}
return $this->form()->response()->refresh()->success(trans('admin.save_succeeded'));
}
public function buildCheckBoxOption($prefix, Form\BlockForm $form)
{
return function ()use ($prefix, $form) {
switch ($prefix) {
case 'every_poem':
$label = '每日一言位置';
break;
case 'one_poem':
$label = '一言位置';
break;
case 'weather':
$label = '天气位置';
break;
case 'date':
$label = '日期位置';
break;
case 'feel':
$label = '有感位置';
break;
default:
$label = '组件位置';
}
$form->divider();
$form->select('pos_' . $prefix, $label)->options(VideoTemp::POSITION_OPTIONS);
$form->number('text_bg_box_' . $prefix, '背景厚度')->default(0)
->addElementClass('text_bg_box_' . $prefix)->help('设置背景块边缘厚度(用于在背景块边缘用背景色填充一圈),默认为0');
$form->color('text_bg_color_' . $prefix, '背景色')->default('#5c6bc6')->addElementClass('text_bg_color_' . $prefix);
$form->number('font_size_' . $prefix, '字号')->min(12);
$form->color('text_color_' . $prefix, '字体颜色')->default('#f5f5f5')->addElementClass('text_color_' . $prefix);
$form->number('opacity_' . $prefix, '透明度')->min(0)->max(100)
->addElementClass('opacity_' . $prefix)->default(100)
->help('范围为0-100,100表示不透明,0表示完全透明');
$form->switch('fix_bounds_' . $prefix, '避免剪切');
};
}
}