| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 | <?phpnamespace App\Admin\Controllers\Company;use App\Http\Controllers\Controller;use App\Models\Company;use App\Models\Tpl;use Encore\Admin\Controllers\HasResourceActions;use Encore\Admin\Facades\Admin;use Encore\Admin\Form;use Encore\Admin\Grid;use Encore\Admin\Layout\Content;use Encore\Admin\Show;use Illuminate\Support\Facades\Storage;class CompanyTemplateController extends Controller{    use HasResourceActions;    /**     * Index interface.     *     * @param Content $content     * @return Content     */    public function index(Content  $content)    {        return $content            ->header('企业模板')            ->description('列表')            ->body($this->grid());    }    /**     * Show interface.     *     * @param mixed $id     * @param Content $content     * @return Content     */    public function show($id, Content $content)    {        return $content            ->header('企业模板')            ->description('详情')            ->body($this->detail($id));    }    /**     * Edit interface.     *     * @param mixed $id     * @param Content $content     * @return Content     */    public function edit($id, Content $content)    {        return $content            ->header('企业模板')            ->description('编辑')            ->body($this->editForm($id)->edit($id));    }    /**     * Create interface.     *     * @param Content $content     * @return Content     */    public function create(Content $content)    {        return $content            ->header('企业模板')            ->description('创建')            ->body($this->createForm());    }    /**     * Make a grid builder.     *     * @return Grid     */    protected function grid()    {        $grid = new Grid(new Tpl);        $grid->model()->where('tpl_type', 1)->orderBy('id', 'desc');        $grid->id('ID');        $grid->images('模板缩略图')->image('', '150', '150');        $grid->name('模板名称')->width(200);                $states = [            'on'  => ['value' => 1, 'text' => '是', 'color' => 'primary'],            'off' => ['value' => 0, 'text' => '否', 'color' => 'default'],        ];        $grid->display('是否显示')->switch($states);        $grid->price('价格')->display(function ($display) {            return $display.'积分';        });        $grid->blade_name('压缩包名称');        $grid->created_at('创建时间')->sortable();        $grid->actions(function ($actions) use ($grid) {            if (Admin::user()->can('template_company_edit')) {                $actions->disableEdit(false);            }            if (Admin::user()->can('template_company_delete')) {                $actions->disableDelete(false);            }            $actions->disableView(true);        });        if (Admin::user()->can('template_company_delete')) {            $grid->tools(function ($tools) {                $tools->batch(function ($batch) {                    $batch->disableDelete(false);                });            });            $grid->disableRowSelector(false);        }        if (Admin::user()->can('template_company_create')) {            $grid->disableCreateButton(false);        }        $grid->disableFilter();        return $grid;    }    /**     * Make a show builder.     *     * @param mixed $id     * @return Show     */    protected function detail($id)    {    }    /**     * Make a form builder.     *     * @return Form     */    protected function editForm($id)    {        $form = new Form(new Tpl);                $form->text('name', '模板名称')->rules([            'required',        ])->setWidth(3)->setMustMark();                $form->radio('display', '是否显示')->options([0=>'否',1=>'是'])->default(1);        $form->number('price','价格')->default(0)->min(0)->help('企业会员使用此模板需要支付的积分,0为免费');        $form->image('images','模板缩略图')->setWidth(8)->setMustMark()->options(['layoutTemplates' => ['actionDelete'=>'']]);        $form->file('blade_name','模板压缩包')->help('上传相同的ZIP压缩包会覆盖之前的文件!')->uniqueName()->setWidth(8)->setMustMark()->options(['layoutTemplates' => ['actionDelete'=>'']]);                $form->saving(function (Form $form) use($id) {            $form->old_blade_name = Tpl::find($id)->blade_name;            if ($form->display=='on') {                $form->display=1;            } elseif ($form->display=='off') {                $form->display=0;            }        });        $form->saved(function (Form $form) {            if($form->blade_name!=null && $form->blade_name!='_file_del_'){                Storage::disk('public')->deleteDirectory('template/company/'.$form->old_blade_name);                $model = $form->model();                $new_blade = $model->blade_name;                $zip = new \Chumper\Zipper\Zipper();                $zip->zip(Storage::disk('public')->path($new_blade))->extractTo(Storage::disk('public')->path('template/company/'.$form->old_blade_name));                $zip->close();                $model->blade_name = $form->old_blade_name;                $model->save();                Storage::disk('public')->delete($new_blade);            }        });        return $form;    }    protected function createForm()    {        $form = new Form(new Tpl);        $form->text('name', '模板名称')->rules([            'required',        ])->setWidth(3)->setMustMark();               $form->radio('display', '是否显示')->options([0=>'否',1=>'是'])->default(1);        $form->number('price','价格')->default(0)->min(0)->help('企业会员使用此模板需要支付的积分,0为免费');        $form->image('images','模板缩略图')->setWidth(8)->setMustMark()->rules([            'required',        ]);        $form->file('blade_name','模板压缩包')->help('上传相同的ZIP压缩包会覆盖之前的文件!')->rules('mimes:zip')->uniqueName()            ->setWidth(8)->setMustMark()->rules([                'required',            ]);                $form->saved(function (Form $form) {            $model = $form->model();            $new_blade = $model->blade_name;            $template_dir=uniqid();            $zip = new \Chumper\Zipper\Zipper();            $zip->zip(Storage::disk('public')->path($new_blade))->extractTo(Storage::disk('public')->path('template/company/'.$template_dir));            $zip->close();            $model->blade_name = $template_dir;            $model->save();            Storage::disk('public')->delete($new_blade);        });        return $form;    }    public function store()    {        return $this->createForm()->store();    }    public function update($id)    {        return $this->editForm($id)->update($id);    }    /**     * Make a form builder.     *     * @return Form     */    protected function form()    {        $form = new Form(new Tpl);        $form->display('ID');        $form->display('Created at');        $form->display('Updated at');        return $form;    }        public function destroy($id)    {        if (!$id) {            $data = [                'status'  => false,                'message' => '模板不存在!!',            ];            return response()->json($data);        }        $id_arr =  explode(',', $id);        $blade_name = Tpl::whereIn('id', $id_arr)->pluck('blade_name')->toArray();        \DB::beginTransaction();        try {           Tpl::whereIn('id', $id_arr)->delete();                      if ($blade_name){               Company::whereIn('tpl', $blade_name)->update(['tpl'=>'default']);           }            $data = [                'status'  => true,                'message' => '删除成功!',            ];            \DB::commit();            return response()->json($data);        } catch (\Exception $e) {            \DB::rollback();            $data = [                'status'  => false,                'message' => '删除失败!',            ];            return response()->json($data);        }    }    }
 |