CategoryController.php 7.2 KB

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