123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- <?php
- namespace App\Admin\Controllers\System;
- use App\Http\Controllers\Controller;
- use App\Models\CategoryDistrict;
- use Encore\Admin\Controllers\HasResourceActions;
- use Encore\Admin\Form;
- use Encore\Admin\Grid;
- use Encore\Admin\Layout\Content;
- use Encore\Admin\Show;
- use Encore\Admin\Tree;
- use Illuminate\Http\Request;
- class CategoryDistrictController extends Controller
- {
- use HasResourceActions;
- /**
- * Index interface.
- *
- * @param Content $content
- * @return Content
- */
- public function index(Content $content)
- {
- if (request()->has('_expand_')) {
- $CategoryDistrict=CategoryDistrict::find(request()->get('_expand_'));
- if (!$CategoryDistrict || $CategoryDistrict->children->isEmpty()) {
- $data['has_children'] =0;
- return response()->json($data);
- }
- $data['has_children'] =1;
- $data['html']=view(
- 'admin.grid.district_expand',
- [
- 'parent_group'=>implode(" ", explode(",", request()->get('parent_group'))),
- 'key'=>request()->get('_expand_'),
- 'list'=>$CategoryDistrict->children,
- 'level'=>request()->get('level')+1,
- ]
- )->render();
- return response()->json($data);
- }
- 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()->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 CategoryDistrict);
- $grid->model()->where('parent_id',0)->orderBy('order', 'desc');
- $grid->id('ID');
- $grid->name('名称')->ajaxExpand();
- $grid->spell('拼音')->label();
- $grid->order('排序');
- $grid->created_at('添加时间');
- $grid->updated_at('更新时间');
- $grid->disableCreateButton(false);
- $grid->actions(function ($actions) {
- $actions->disableEdit(false);
- $actions->disableDelete(false);
- });
- $grid->filter(function ($filter) {
- $filter->like('name', '名称');
- });
- return $grid;
- }
- /**
- * Make a show builder.
- *
- * @param mixed $id
- * @return Show
- */
- protected function detail($id)
- {
- $show = new Show(CategoryDistrict::findOrFail($id));
- $show->id('ID');
- $show->name('名称');
- $show->spell('拼音')->label();
- $show->order('排序')->label();
- $show->created_at('添加时间');
- $show->updated_at('更新时间');
- return $show;
- }
- protected function createForm()
- {
- $form = new Form(new CategoryDistrict);
- $res = CategoryDistrict::all()->toArray();
- $array = $this->categoryDistrictArr($res,0);
- $array = array_pluck($array,'name','id');
- $option = ['0'=>'顶级'] +$array;
- $form->select('parent_id', '所属上级')->options($option)->setMustMark();
- $form->text('name', '名称');
- $form->text('spell', '拼音');
- $form->number('order', '排序')->default(0)->min(0);
- return $form;
- }
- protected function editForm()
- {
- $form = new Form(new CategoryDistrict);
- $res = CategoryDistrict::all()->toArray();
- $array = $this->categoryDistrictArr($res,0);
- $array = array_pluck($array,'name','id');
- $option = ['0'=>'顶级'] +$array;
- $form->select('parent_id', '所属上级')->options($option)->setMustMark();
- $form->text('name', '名称');
- $form->text('spell', '拼音');
- $form->number('order', '排序')->default(0)->min(0);
- return $form;
- }
- public function store()
- {
- return $this->createForm()->store();
- }
- public function update($id)
- {
- return $this->editForm()->update($id);
- }
- public function destroy($id)
- {
- \DB::beginTransaction();
- try {
- $res = CategoryDistrict::all()->toArray();
- $list = $this->categoryDistrictArr($res,$id);
- $ids = array_pluck($list,'id');
- CategoryDistrict::where('id',$id)->delete();
- CategoryDistrict::whereIn('id',$ids)->delete();
- $data = [
- 'status' => true,
- 'message' => '删除成功!',
- ];
- \DB::commit();
- return response()->json($data);
- } catch (\Exception $e) {
- \DB::rollback();
- $data = [
- 'status' => false,
- 'message' => '删除失败!',
- ];
- return response()->json($data);
- }
- }
- /**
- * Make a form builder.
- *
- * @return Form
- */
- protected function form()
- {
- $form = new Form(new CategoryDistrict);
- $form->display('ID');
- $form->display('Created at');
- $form->display('Updated at');
- return $form;
- }
- public function categoryDis(Request $request)
- {
- $q = $request->get('q');
- return CategoryDistrict::where('parent_id', $q)->get(['id','name as text']);
- }
- /**
- * Make a grid builder.
- *
- * @return Tree
- */
- protected function tree()
- {
- $res = CategoryDistrict::where(['parent_id'=>0])->select(['id','name','spell','order'])->get();
- return view('admin.category.district')->with(['content'=>$res]);
- }
- //递归查询
- protected function categoryDistrictArr($data,$pid,$level=''){
- static $arr=array();
- foreach($data as $k => $v){
- //如果该分类pid=0
- if($v['parent_id']==$pid){
- $v['name']= $level.$v['name'];
- $arr[]=$v;
- self::categoryDistrictArr($data,$v['id'],$level.' ');
- }
- }
- return $arr;
- }
- public function ajax(Request $request)
- {
- $id = $request->id;
- $res = CategoryDistrict::where(['parent_id'=>$id])->select(['id','name','spell','order'])->get();
- return json_encode($res);
- }
- }
|