CategoryController.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace app\admin\controller\sys_manage;
  3. use app\admin\service\sys_manage\CategoryService;
  4. use app\admin\validate\sys_manage\CategoryValidate;
  5. use app\common\Tree;
  6. use app\controller\Curd;
  7. use app\model\SysCategory;
  8. use support\Request;
  9. use support\Response;
  10. class CategoryController extends Curd
  11. {
  12. public function __construct()
  13. {
  14. $this->model = new SysCategory();
  15. }
  16. public function index(Request $request)
  17. {
  18. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  19. $format = 'tree';
  20. $order = $request->get('order', 'desc');
  21. $field = $field ?? 'category_sort';
  22. $query = $this->doSelect($where, $field, $order);
  23. return $this->doFormat($query, $format, 1000);
  24. }
  25. /**
  26. * @Desc 分类列表
  27. * @Author Gorden
  28. * @Date 2024/2/22 11:28
  29. *
  30. * @param Request $request
  31. * @return \support\Response
  32. */
  33. public function categoryList(Request $request)
  34. {
  35. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  36. $format = 'tree';
  37. $order = $request->get('order', 'desc');
  38. $field = $field ?? 'category_sort';
  39. $where['category_status'] = 'ACTIVED';
  40. $query = $this->doSelect($where, $field, $order);
  41. return $this->doFormat($query, $format, 1000);
  42. }
  43. public function afterQuery($items){
  44. $categoryDatas = [];
  45. foreach($items as &$item){
  46. $categorys = SysCategory::where('category_super_path','like',$item->category_super_path.'%')
  47. ->where('category_status','ACTIVED')
  48. ->paginate(1000)
  49. ->items();
  50. $categoryDatas = array_merge($categorys,$categoryDatas);
  51. }
  52. $items = array_merge($items,$categoryDatas);
  53. return $items;
  54. }
  55. protected function formatTree($items): Response
  56. {
  57. $format_items = [];
  58. foreach ($items as $item) {
  59. $format_items[] = [
  60. 'name' => $item->category_name,
  61. 'value' => (string)$item->category_id,
  62. 'id' => $item->id ?? $item->category_id,
  63. 'pid' => $item->pid ?? $item->category_super_id,
  64. 'category_id' => $item->category_id,
  65. 'category_name' => $item->category_name,
  66. 'category_super_id' => $item->category_super_id,
  67. 'category_status' => $item->category_status,
  68. 'category_classify' => $item->category_classify,
  69. 'category_type' => $item->category_type,
  70. 'category_icon' => $item->category_icon,
  71. 'category_images' => $item->category_images,
  72. 'category_url' => $item->category_url,
  73. 'category_page' => $item->category_page,
  74. 'category_sort' => $item->category_sort,
  75. 'category_groupby' => $item->category_groupby,
  76. 'category_remark' => $item->category_remark,
  77. 'category_extend_json' => $item->category_extend_json,
  78. 'category_addtimes' => $item->category_addtimes,
  79. ];
  80. }
  81. $tree = new Tree($format_items);
  82. return json_success('success', $tree->getTree());
  83. }
  84. /**
  85. * @Desc 分类详情
  86. * @Author Gorden
  87. * @Date 2024/2/22 11:37
  88. *
  89. * @param $id
  90. * @return \support\Response
  91. */
  92. public function categoryInfo($id)
  93. {
  94. return CategoryService::categoryInfo($id);
  95. }
  96. /**
  97. * @Desc 添加分类
  98. * @Author Gorden
  99. * @Date 2024/2/22 10:25
  100. *
  101. * @param Request $request
  102. * @return \support\Response
  103. */
  104. public function addCategory(Request $request)
  105. {
  106. $validate = new CategoryValidate();
  107. if (!$validate->scene('add')->check($request->post())) {
  108. return json_fail($validate->getError());
  109. }
  110. return CategoryService::insertCategory($request->post());
  111. }
  112. /**
  113. * @Desc 修改分类
  114. * @Author Gorden
  115. * @Date 2024/2/22 11:05
  116. *
  117. * @param $id
  118. * @param Request $request
  119. * @return \support\Response
  120. */
  121. public function updateCategory($id, Request $request)
  122. {
  123. $validate = new CategoryValidate();
  124. if (!$validate->scene('update')->check($request->post())) {
  125. return json_fail($validate->getError());
  126. }
  127. return CategoryService::updateCategory($id, $request->post());
  128. }
  129. /**
  130. * @Desc 修改分类状态
  131. * @Author Gorden
  132. * @Date 2024/2/22 11:49
  133. *
  134. * @param $id
  135. * @param Request $request
  136. * @return \support\Response
  137. */
  138. public function updateStatus($id, Request $request)
  139. {
  140. $validate = new CategoryValidate();
  141. if (!$validate->scene('update_status')->check($request->post())) {
  142. return json_fail($validate->getError());
  143. }
  144. return CategoryService::updateStatus($id, $request->post());
  145. }
  146. /**
  147. * @Desc 删除分类
  148. * @Author Gorden
  149. * @Date 2024/2/22 11:57
  150. *
  151. * @param $id
  152. * @return \support\Response
  153. */
  154. public function delCategory(Request $request)
  155. {
  156. return CategoryService::delCategory($request);
  157. }
  158. }