VideoTempController.php 10.9 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\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, '避免剪切');
        };
    }
}