CategoryDistrictController.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. namespace App\Admin\Controllers\System;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\CategoryDistrict;
  5. use Encore\Admin\Controllers\HasResourceActions;
  6. use Encore\Admin\Form;
  7. use Encore\Admin\Grid;
  8. use Encore\Admin\Layout\Content;
  9. use Encore\Admin\Show;
  10. use Encore\Admin\Tree;
  11. use Illuminate\Http\Request;
  12. class CategoryDistrictController extends Controller
  13. {
  14. use HasResourceActions;
  15. /**
  16. * Index interface.
  17. *
  18. * @param Content $content
  19. * @return Content
  20. */
  21. public function index(Content $content)
  22. {
  23. if (request()->has('_expand_')) {
  24. $CategoryDistrict=CategoryDistrict::find(request()->get('_expand_'));
  25. if (!$CategoryDistrict || $CategoryDistrict->children->isEmpty()) {
  26. $data['has_children'] =0;
  27. return response()->json($data);
  28. }
  29. $data['has_children'] =1;
  30. $data['html']=view(
  31. 'admin.grid.district_expand',
  32. [
  33. 'parent_group'=>implode(" ", explode(",", request()->get('parent_group'))),
  34. 'key'=>request()->get('_expand_'),
  35. 'list'=>$CategoryDistrict->children,
  36. 'level'=>request()->get('level')+1,
  37. ]
  38. )->render();
  39. return response()->json($data);
  40. }
  41. return $content
  42. ->header('地区分类')
  43. ->description('分类信息谨慎修改,如需修改请提前咨询系统客服。')
  44. ->body($this->grid());
  45. }
  46. /**
  47. * Show interface.
  48. *
  49. * @param mixed $id
  50. * @param Content $content
  51. * @return Content
  52. */
  53. public function show($id, Content $content)
  54. {
  55. return $content
  56. ->header('地区分类')
  57. ->description('分类信息谨慎修改,如需修改请提前咨询系统客服。')
  58. ->body($this->detail($id));
  59. }
  60. /**
  61. * Edit interface.
  62. *
  63. * @param mixed $id
  64. * @param Content $content
  65. * @return Content
  66. */
  67. public function edit($id, Content $content)
  68. {
  69. return $content
  70. ->header('地区分类')
  71. ->description('分类信息谨慎修改,如需修改请提前咨询系统客服。')
  72. ->body($this->editForm()->edit($id));
  73. }
  74. /**
  75. * Create interface.
  76. *
  77. * @param Content $content
  78. * @return Content
  79. */
  80. public function create(Content $content)
  81. {
  82. return $content
  83. ->header('地区分类')
  84. ->description('分类信息谨慎修改,如需修改请提前咨询系统客服。')
  85. ->body($this->createForm());
  86. }
  87. /**
  88. * Make a grid builder.
  89. *
  90. * @return Grid
  91. */
  92. protected function grid()
  93. {
  94. $grid = new Grid(new CategoryDistrict);
  95. $grid->model()->where('parent_id',0)->orderBy('order', 'desc');
  96. $grid->id('ID');
  97. $grid->name('名称')->ajaxExpand();
  98. $grid->spell('拼音')->label();
  99. $grid->order('排序');
  100. $grid->created_at('添加时间');
  101. $grid->updated_at('更新时间');
  102. $grid->disableCreateButton(false);
  103. $grid->actions(function ($actions) {
  104. $actions->disableEdit(false);
  105. $actions->disableDelete(false);
  106. });
  107. $grid->filter(function ($filter) {
  108. $filter->like('name', '名称');
  109. });
  110. return $grid;
  111. }
  112. /**
  113. * Make a show builder.
  114. *
  115. * @param mixed $id
  116. * @return Show
  117. */
  118. protected function detail($id)
  119. {
  120. $show = new Show(CategoryDistrict::findOrFail($id));
  121. $show->id('ID');
  122. $show->name('名称');
  123. $show->spell('拼音')->label();
  124. $show->order('排序')->label();
  125. $show->created_at('添加时间');
  126. $show->updated_at('更新时间');
  127. return $show;
  128. }
  129. protected function createForm()
  130. {
  131. $form = new Form(new CategoryDistrict);
  132. $res = CategoryDistrict::all()->toArray();
  133. $array = $this->categoryDistrictArr($res,0);
  134. $array = array_pluck($array,'name','id');
  135. $option = ['0'=>'顶级'] +$array;
  136. $form->select('parent_id', '所属上级')->options($option)->setMustMark();
  137. $form->text('name', '名称');
  138. $form->text('spell', '拼音');
  139. $form->number('order', '排序')->default(0)->min(0);
  140. return $form;
  141. }
  142. protected function editForm()
  143. {
  144. $form = new Form(new CategoryDistrict);
  145. $res = CategoryDistrict::all()->toArray();
  146. $array = $this->categoryDistrictArr($res,0);
  147. $array = array_pluck($array,'name','id');
  148. $option = ['0'=>'顶级'] +$array;
  149. $form->select('parent_id', '所属上级')->options($option)->setMustMark();
  150. $form->text('name', '名称');
  151. $form->text('spell', '拼音');
  152. $form->number('order', '排序')->default(0)->min(0);
  153. return $form;
  154. }
  155. public function store()
  156. {
  157. return $this->createForm()->store();
  158. }
  159. public function update($id)
  160. {
  161. return $this->editForm()->update($id);
  162. }
  163. public function destroy($id)
  164. {
  165. \DB::beginTransaction();
  166. try {
  167. $res = CategoryDistrict::all()->toArray();
  168. $list = $this->categoryDistrictArr($res,$id);
  169. $ids = array_pluck($list,'id');
  170. CategoryDistrict::where('id',$id)->delete();
  171. CategoryDistrict::whereIn('id',$ids)->delete();
  172. $data = [
  173. 'status' => true,
  174. 'message' => '删除成功!',
  175. ];
  176. \DB::commit();
  177. return response()->json($data);
  178. } catch (\Exception $e) {
  179. \DB::rollback();
  180. $data = [
  181. 'status' => false,
  182. 'message' => '删除失败!',
  183. ];
  184. return response()->json($data);
  185. }
  186. }
  187. /**
  188. * Make a form builder.
  189. *
  190. * @return Form
  191. */
  192. protected function form()
  193. {
  194. $form = new Form(new CategoryDistrict);
  195. $form->display('ID');
  196. $form->display('Created at');
  197. $form->display('Updated at');
  198. return $form;
  199. }
  200. public function categoryDis(Request $request)
  201. {
  202. $q = $request->get('q');
  203. return CategoryDistrict::where('parent_id', $q)->get(['id','name as text']);
  204. }
  205. /**
  206. * Make a grid builder.
  207. *
  208. * @return Tree
  209. */
  210. protected function tree()
  211. {
  212. $res = CategoryDistrict::where(['parent_id'=>0])->select(['id','name','spell','order'])->get();
  213. return view('admin.category.district')->with(['content'=>$res]);
  214. }
  215. //递归查询
  216. protected function categoryDistrictArr($data,$pid,$level=''){
  217. static $arr=array();
  218. foreach($data as $k => $v){
  219. //如果该分类pid=0
  220. if($v['parent_id']==$pid){
  221. $v['name']= $level.$v['name'];
  222. $arr[]=$v;
  223. self::categoryDistrictArr($data,$v['id'],$level.'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
  224. }
  225. }
  226. return $arr;
  227. }
  228. public function ajax(Request $request)
  229. {
  230. $id = $request->id;
  231. $res = CategoryDistrict::where(['parent_id'=>$id])->select(['id','name','spell','order'])->get();
  232. return json_encode($res);
  233. }
  234. }