CategoryController.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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'] = ['in', ['ACTIVED', 'UNSHOW']];
  40. // dump($where);
  41. $query = $this->doSelect($where, $field, $order);
  42. return $this->doFormat($query, $format, 1000);
  43. }
  44. public function afterQuery($items)
  45. {
  46. $categoryType = \request()->get('category_type', '');
  47. $categoryDatas = [];
  48. foreach ($items as &$item) {
  49. if (empty($item->category_super_path)) {
  50. $item->category_super_path = '#' . $item['category_id'] . '#';
  51. }
  52. $categorys = SysCategory::where('category_super_path', 'like', $item->category_super_path . '%')
  53. ->when(!empty($categoryType), function ($query) use ($categoryType) {
  54. $query->where('category_type', $categoryType);
  55. })->whereIn('category_status', ['ACTIVED', 'UNSHOW'])
  56. ->paginate(1000)
  57. ->items();
  58. $categoryDatas = array_merge($categorys, $categoryDatas);
  59. }
  60. $items = array_merge($items, $categoryDatas);
  61. return $items;
  62. }
  63. protected function formatTree($items): Response
  64. {
  65. $format_items = [];
  66. foreach ($items as $item) {
  67. $format_items[] = [
  68. 'name' => $item->category_name,
  69. 'value' => (string)$item->category_id,
  70. 'id' => $item->id ?? $item->category_id,
  71. 'pid' => $item->pid ?? $item->category_super_id,
  72. 'category_id' => $item->category_id,
  73. 'category_name' => $item->category_name,
  74. 'category_super_id' => $item->category_super_id,
  75. 'category_status' => $item->category_status,
  76. 'category_classify' => $item->category_classify,
  77. 'category_type' => $item->category_type,
  78. 'category_icon' => $item->category_icon,
  79. 'category_images' => $item->category_images,
  80. 'category_url' => $item->category_url,
  81. 'category_page' => $item->category_page,
  82. 'category_sort' => $item->category_sort,
  83. 'category_groupby' => $item->category_groupby,
  84. 'category_remark' => $item->category_remark,
  85. 'category_extend_json' => $item->category_extend_json,
  86. 'category_addtimes' => $item->category_addtimes,
  87. ];
  88. }
  89. $tree = new Tree($format_items);
  90. return json_success('success', $tree->getTree());
  91. }
  92. public function goodsCategory(Request $request)
  93. {
  94. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  95. $format = 'tree';
  96. $order = $request->get('order', 'desc');
  97. $field = $field ?? 'category_sort';
  98. $where['category_status'] = 'ACTIVED';
  99. $where['category_super_id'] = ['in', '31,154,5,181,30,70,42'];
  100. $query = $this->doSelect($where, $field, $order);
  101. return $this->doFormatForGoods($query, $format, 1000);
  102. // $model->orWhereIn('category_id', ['31','154','5','181','30','70','42']);
  103. }
  104. /**
  105. * 执行真正查询,并返回格式化数据
  106. * @param $query
  107. * @param $format
  108. * @param $limit
  109. * @return Response
  110. */
  111. protected function doFormatForGoods($query, $format, $limit): Response
  112. {
  113. $methods = [
  114. 'select' => 'formatSelect',
  115. 'tree' => 'formatTree',
  116. 'table_tree' => 'formatTableTree',
  117. 'normal' => 'formatNormal',
  118. ];
  119. $paginator = $query->paginate($limit);
  120. $total = $paginator->total();
  121. $items = $paginator->items();
  122. if (method_exists($this, "afterQuery")) {
  123. $items = call_user_func([$this, "afterQuery"], $items);
  124. }
  125. $super = SysCategory::whereIn('category_id', ['31', '154', '5', '181', '30', '70', '42']);
  126. $superPaginator = $super->paginate($limit);
  127. $superTotal = $superPaginator->total();
  128. $superItems = $superPaginator->items();
  129. $items = array_merge($items, $superItems);
  130. $total = $total + $superTotal;
  131. $format_function = $methods[$format] ?? 'formatNormal';
  132. return call_user_func([$this, $format_function], $items, $total);
  133. }
  134. /**
  135. * @Desc 分类详情
  136. * @Author Gorden
  137. * @Date 2024/2/22 11:37
  138. *
  139. * @param $id
  140. * @return \support\Response
  141. */
  142. public function categoryInfo($id)
  143. {
  144. return CategoryService::categoryInfo($id);
  145. }
  146. /**
  147. * @Desc 添加分类
  148. * @Author Gorden
  149. * @Date 2024/2/22 10:25
  150. *
  151. * @param Request $request
  152. * @return \support\Response
  153. */
  154. public function addCategory(Request $request)
  155. {
  156. $validate = new CategoryValidate();
  157. if (!$validate->scene('add')->check($request->post())) {
  158. return json_fail($validate->getError());
  159. }
  160. return CategoryService::insertCategory($request->post());
  161. }
  162. /**
  163. * @Desc 修改分类
  164. * @Author Gorden
  165. * @Date 2024/2/22 11:05
  166. *
  167. * @param $id
  168. * @param Request $request
  169. * @return \support\Response
  170. */
  171. public function updateCategory($id, Request $request)
  172. {
  173. $validate = new CategoryValidate();
  174. if (!$validate->scene('update')->check($request->post())) {
  175. return json_fail($validate->getError());
  176. }
  177. return CategoryService::updateCategory($id, $request->post());
  178. }
  179. /**
  180. * @Desc 修改分类状态
  181. * @Author Gorden
  182. * @Date 2024/2/22 11:49
  183. *
  184. * @param $id
  185. * @param Request $request
  186. * @return \support\Response
  187. */
  188. public function updateStatus($id, Request $request)
  189. {
  190. $validate = new CategoryValidate();
  191. if (!$validate->scene('update_status')->check($request->post())) {
  192. return json_fail($validate->getError());
  193. }
  194. return CategoryService::updateStatus($id, $request->post());
  195. }
  196. /**
  197. * @Desc 删除分类
  198. * @Author Gorden
  199. * @Date 2024/2/22 11:57
  200. *
  201. * @param $id
  202. * @return \support\Response
  203. */
  204. public function delCategory(Request $request)
  205. {
  206. return CategoryService::delCategory($request);
  207. }
  208. public function getCategoryTree(Request $request)
  209. {
  210. $category_id = $request->get('category_id');
  211. return CategoryService::getCategoryTree($category_id);
  212. }
  213. }