VideoTempController.php 9.05 KB
<?php

namespace App\Admin\Controllers;

use App\Models\VideoTemp;
use App\Models\Component;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Widgets\Table;
use Illuminate\Support\Facades\DB;

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('','组件信息')
                ->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 form2()
    {
        return Form::make(new VideoTemp(), function (Form $form) {
            $form->display('id');
            $form->block(8, function (Form\BlockForm $form) {
                // 设置标题
                $form->title('基本设置');
                // 显示底部提交按钮
                $form->showFooter();
                // 设置字段宽度
                $form->width(8, 3);

                $form->text('title');
                $form->radio('type')->addElementClass('type')
                    ->options([1=>'图文音频',2=>'视频'])->default(1);
                $form->radio('bg_type')
                    ->options([1=>'视频', 2=>'图片'])
                    ->when(1,function (Form\BlockForm $form){
                        $form->file('bg_url')
                            ->accept('mp4')
                            ->autoUpload()
                            ->uniqueName()
                            ->addElementClass('bg_video_url');
                    })
                    ->when(2,function (Form\BlockForm $form){
                        $form->multipleImage('bg_url')
                            ->limit(5)
                            ->uniqueName()
                            ->addElementClass('bg_img_url');
                    })
                    ->default(1);
                $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->number('top');
                $form->number('left');
                $form->number('font_size');
                $form->color('text_color')->default('#f5f5f5')
                    ->addElementClass('text_color');
                $form->color('text_bg_color')->addElementClass('text_bg_color');
                $form->number('opacity')->min(0)->max(100)
                    ->addElementClass('opacity')->default(90)
                    ->help('范围为0-100,100表示不透明,0表示完全透明');

                $form->number('sn');
                $form->radio('state')->options(['不显示', '显示'])->default(0);

            });

            $form->block(4, function (Form\BlockForm $form) {
                $form->html(view('admin.form.phone'));
            });

            $form->display('created_at');
            $form->display('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new VideoTemp(), function (Form $form) {
            $form->display('id');
            $form->block(8, function (Form\BlockForm $form) {
                // 设置标题
                $form->title('基本设置');
                // 显示底部提交按钮
                $form->showFooter();
                // 设置字段宽度
                $form->width(8, 3);

                $form->text('title');

                $form->checkbox('components','组件')
                    ->when('every_poem', function (Form\BlockForm $form) {
                        $form->select('pos_every_poem', '每日位置')->options([
                            'topLeft'=>'上左','topMiddle'=>'上中','topRight'=>'上右',
                        ]);
                        $form->divider();
                    })
                    ->when('one_poem', function (Form\BlockForm $form) {
                        $form->select('pos_one_poem', '一言位置')->options([
                            'topLeft'=>'上左','topMiddle'=>'上中','topRight'=>'上右',
                        ]);
                        $form->divider();
                    })
                    ->when('weather', function (Form\BlockForm $form) {
                        $form->select('pos_weather', '天气位置')->options([
                            'topLeft'=>'上左','topMiddle'=>'上中','topRight'=>'上右',
                        ]);
                        $form->divider();
                    })
                    ->when('date', function (Form\BlockForm $form) {
                        $form->select('pos_date', '日期位置')->options([
                            'topLeft'=>'上左','topMiddle'=>'上中','topRight'=>'上右',
                        ]);
                        $form->divider();
                    })
                    ->when('feel', function (Form\BlockForm $form) {
                        $form->select('pos_feel', '日期位置')->options([
                            'topLeft'=>'上左','topMiddle'=>'上中','topRight'=>'上右',
                        ]);
                        $form->divider();
                    })
                    ->default(['one_poem','weather','date'])
                    ->options([
                        'every_poem' => '每日一言组件',
                        'one_poem' => '一言组件',
                        'weather' => '天气组件',
                        'date' => '日期组件',
                        'feel' => '临境有感组件',
                    ]);


                $form->hidden('state')
                    ->saving(function ($v) {
                        return $v;
                    });
            });

//            $form->block(4, function (Form\BlockForm $form) {
//                $form->html(view('admin.form.phone'));
//            });

            $form->display('created_at');
            $form->display('updated_at');
        });
    }

    public function store()
    {
        $all = \request()->all();
        dd($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]
                        ]);
                    }
                }
            });

        }catch (\Exception $exception){
            return $this->form()->response()->error($exception->getMessage());
        }

        return $this->form()->response()->refresh()->success(trans('admin.save_succeeded'));
    }
}