Category.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 https://www.sapixx.com All rights reserved.
  4. * @license Licensed (http://www.apache.org/licenses/LICENSE-2.0).
  5. * @author pillar<ltmn@qq.com>
  6. * 商品分类管理
  7. */
  8. namespace app\popupshop\controller;
  9. use app\common\controller\Manage;
  10. use app\popupshop\model\Category as AppCategory;
  11. use app\popupshop\model\Item;
  12. use think\facade\Request;
  13. class Category extends Manage{
  14. /**
  15. * 列表
  16. */
  17. public function index(){
  18. $parent_id = Request::param('parent_id/d',0);
  19. $view['pathMaps'] = AppCategory::selectPath($this->member_miniapp_id,$parent_id);
  20. $view['lists'] = AppCategory::where(['parent_id' => $parent_id,'member_miniapp_id' => $this->member_miniapp_id])->order('sort desc,id desc')->paginate(20);
  21. $view['parent_id'] = $parent_id;
  22. return view('index',$view);
  23. }
  24. /**
  25. * 添加
  26. */
  27. public function add(){
  28. if(request()->isAjax()){
  29. $data = [
  30. 'title' => input('post.title/s'),
  31. 'name' => input('post.name/s'),
  32. 'sort' => input('post.sort/d'),
  33. 'parent_id' => input('post.parent_id/d'),
  34. 'picture' => input('post.picture/s'),
  35. 'types' => input('post.types/d'),
  36. 'member_miniapp_id' => $this->member_miniapp_id,
  37. ];
  38. $validate = $this->validate($data,'Category.add');
  39. if(true !== $validate){
  40. return json(['code'=>0,'msg'=>$validate]);
  41. }
  42. $result = AppCategory::edit($data);
  43. if($result){
  44. return enjson(200,'操作成功',['url'=>url('popupshop/category/index',['parent_id'=>$data['parent_id']])]);
  45. }else{
  46. return enjson(0);
  47. }
  48. }else{
  49. $parent_id = Request::param('parent_id/d',0);
  50. $view['pathMaps'] = AppCategory::selectPath($this->member_miniapp_id,$parent_id);
  51. $view['parent_id'] = $parent_id;
  52. return view('add',$view);
  53. }
  54. }
  55. //编辑
  56. public function edit(){
  57. if(request()->isAjax()){
  58. $data = [
  59. 'id' => input('post.id/s'),
  60. 'title' => input('post.title/s'),
  61. 'name' => input('post.name/s'),
  62. 'sort' => input('post.sort/d'),
  63. 'parent_id' => input('post.parent_id/d'),
  64. 'types' => input('post.types/d'),
  65. 'picture' => input('post.picture/s'),
  66. ];
  67. $validate = $this->validate($data,'Category.edit');
  68. if(true !== $validate){
  69. return json(['code'=>0,'msg'=>$validate]);
  70. }
  71. $result = AppCategory::edit($data);
  72. if($result){
  73. return enjson(200,'操作成功',['url'=>url('popupshop/category/index',['parent_id'=>$data['parent_id']])]);
  74. }else{
  75. return enjson(0);
  76. }
  77. }else{
  78. $info = AppCategory::where(['id' => Request::param('id/d',0),'member_miniapp_id' => $this->member_miniapp_id])->find();
  79. if(!$info){
  80. $this->error("404 NOT FOUND");
  81. }
  82. $view['pathMaps'] = AppCategory::selectPath($this->member_miniapp_id,$info['parent_id']);
  83. $view['info'] = $info;
  84. return view('edit',$view);
  85. }
  86. }
  87. /**
  88. * 排序
  89. */
  90. public function sort(){
  91. if(request()->isAjax()){
  92. $data = [
  93. 'sort' => input('post.sort/d'),
  94. 'id' => input('post.id/d'),
  95. ];
  96. $validate = $this->validate($data,'Category.sort');
  97. if(true !== $validate){
  98. return json(['code'=>0,'msg'=>$validate]);
  99. }
  100. $result = AppCategory::update(['sort'=>$data['sort']],['id' => $data['id']]);
  101. if($result){
  102. return enjson(200);
  103. }else{
  104. return enjson(0);
  105. }
  106. }
  107. }
  108. //删除
  109. public function delete(){
  110. $id = Request::param('id/d',0);
  111. $info = AppCategory::where(['parent_id' => $id,'member_miniapp_id' => $this->member_miniapp_id])->find();
  112. if($info){
  113. return enjson(403,'删除失败,请查看是否包含子栏目');
  114. }
  115. $goods = Item::where(['category_id' => $id,'member_miniapp_id' => $this->member_miniapp_id])->find();
  116. if($goods){
  117. return enjson(403,'删除失败,栏目中还包含商品');
  118. }
  119. $result = AppCategory::destroy($id);
  120. if($result){
  121. return enjson(200);
  122. }else{
  123. return enjson(403,'删除失败,请查看是否包含子栏目');
  124. }
  125. }
  126. //全选删除
  127. public function alldelete(){
  128. $ids = ids(Request::param('ids/s',''),true);
  129. foreach ($ids as $id) {
  130. $goods = Item::where(['category_id' => $id,'member_miniapp_id' => $this->member_miniapp_id])->find();
  131. if(empty($goods)){
  132. $info = AppCategory::where(['parent_id' => $id,'member_miniapp_id' => $this->member_miniapp_id])->find();
  133. if(empty($info)){
  134. AppCategory::destroy(['id' => $id]);
  135. }
  136. }
  137. }
  138. return enjson(200,'操作成功,如有未删除的,可能含有子栏目或商品');
  139. }
  140. }