CategoryController.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. public function goodsCategory(Request $request)
  85. {
  86. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  87. $format = 'tree';
  88. $order = $request->get('order', 'desc');
  89. $field = $field ?? 'category_sort';
  90. $where['category_status'] = 'ACTIVED';
  91. $where['category_super_id'] = ['in','31,154,5,181,30,70,42'];
  92. $query = $this->doSelect($where, $field, $order);
  93. return $this->doFormatForGoods($query, $format, 1000);
  94. // $model->orWhereIn('category_id', ['31','154','5','181','30','70','42']);
  95. }
  96. /**
  97. * 执行真正查询,并返回格式化数据
  98. * @param $query
  99. * @param $format
  100. * @param $limit
  101. * @return Response
  102. */
  103. protected function doFormatForGoods($query, $format, $limit): Response
  104. {
  105. $methods = [
  106. 'select' => 'formatSelect',
  107. 'tree' => 'formatTree',
  108. 'table_tree' => 'formatTableTree',
  109. 'normal' => 'formatNormal',
  110. ];
  111. $paginator = $query->paginate($limit);
  112. $total = $paginator->total();
  113. $items = $paginator->items();
  114. if (method_exists($this, "afterQuery")) {
  115. $items = call_user_func([$this, "afterQuery"], $items);
  116. }
  117. $super = SysCategory::whereIn('category_id', ['31','154','5','181','30','70','42']);
  118. $superPaginator = $super->paginate($limit);
  119. $superTotal = $superPaginator->total();
  120. $superItems = $superPaginator->items();
  121. $items = array_merge($items,$superItems);
  122. $total = $total + $superTotal;
  123. $format_function = $methods[$format] ?? 'formatNormal';
  124. return call_user_func([$this, $format_function], $items, $total);
  125. }
  126. /**
  127. * @Desc 分类详情
  128. * @Author Gorden
  129. * @Date 2024/2/22 11:37
  130. *
  131. * @param $id
  132. * @return \support\Response
  133. */
  134. public function categoryInfo($id)
  135. {
  136. return CategoryService::categoryInfo($id);
  137. }
  138. /**
  139. * @Desc 添加分类
  140. * @Author Gorden
  141. * @Date 2024/2/22 10:25
  142. *
  143. * @param Request $request
  144. * @return \support\Response
  145. */
  146. public function addCategory(Request $request)
  147. {
  148. $validate = new CategoryValidate();
  149. if (!$validate->scene('add')->check($request->post())) {
  150. return json_fail($validate->getError());
  151. }
  152. return CategoryService::insertCategory($request->post());
  153. }
  154. /**
  155. * @Desc 修改分类
  156. * @Author Gorden
  157. * @Date 2024/2/22 11:05
  158. *
  159. * @param $id
  160. * @param Request $request
  161. * @return \support\Response
  162. */
  163. public function updateCategory($id, Request $request)
  164. {
  165. $validate = new CategoryValidate();
  166. if (!$validate->scene('update')->check($request->post())) {
  167. return json_fail($validate->getError());
  168. }
  169. return CategoryService::updateCategory($id, $request->post());
  170. }
  171. /**
  172. * @Desc 修改分类状态
  173. * @Author Gorden
  174. * @Date 2024/2/22 11:49
  175. *
  176. * @param $id
  177. * @param Request $request
  178. * @return \support\Response
  179. */
  180. public function updateStatus($id, Request $request)
  181. {
  182. $validate = new CategoryValidate();
  183. if (!$validate->scene('update_status')->check($request->post())) {
  184. return json_fail($validate->getError());
  185. }
  186. return CategoryService::updateStatus($id, $request->post());
  187. }
  188. /**
  189. * @Desc 删除分类
  190. * @Author Gorden
  191. * @Date 2024/2/22 11:57
  192. *
  193. * @param $id
  194. * @return \support\Response
  195. */
  196. public function delCategory(Request $request)
  197. {
  198. return CategoryService::delCategory($request);
  199. }
  200. public function getCategoryTree(Request $request)
  201. {
  202. $category_id = $request->get('category_id');
  203. return CategoryService::getCategoryTree($category_id);
  204. }
  205. }