CategoryMajorController.php 6.3 KB

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