Category.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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\fastshop\controller;
  9. use app\common\controller\Manage;
  10. class Category extends Manage{
  11. public function initialize() {
  12. parent::initialize();
  13. if(!model('auth')->getAuth($this->user->id,1)){
  14. $this->error('无权限,你非【内容管理员】');
  15. }
  16. }
  17. /**
  18. * 列表
  19. */
  20. public function index(){
  21. $parent_id = input('?get.parent_id') ? input('get.parent_id/d') :0;
  22. $view['pathMaps'] = model('Category')->selectPath($this->member_miniapp_id,$parent_id);
  23. $view['lists'] = model('Category')->where(['parent_id' => $parent_id,'member_miniapp_id' => $this->member_miniapp_id])->order('sort desc,id desc')->paginate(20);
  24. $view['parent_id'] = $parent_id;
  25. return view('index',$view);
  26. }
  27. /**
  28. * 添加
  29. */
  30. public function add(){
  31. if(request()->isAjax()){
  32. $data = [
  33. 'title' => input('post.title/s'),
  34. 'name' => input('post.name/s'),
  35. 'sort' => input('post.sort/d'),
  36. 'parent_id' => input('post.parent_id/d'),
  37. 'picture' => input('post.picture/s'),
  38. 'types' => input('post.types/d'),
  39. 'member_miniapp_id' => $this->member_miniapp_id,
  40. ];
  41. $validate = $this->validate($data,'Category.add');
  42. if(true !== $validate){
  43. return json(['code'=>0,'msg'=>$validate]);
  44. }
  45. $result = model('Category')->edit($data);
  46. if($result){
  47. return json(['code'=>200,'url'=>url('fastshop/category/index',['parent_id'=>$data['parent_id']]),'msg'=>'操作成功']);
  48. }else{
  49. return json(['code'=>0,'msg'=>'操作失败']);
  50. }
  51. }else{
  52. $parent_id = input('?get.parent_id') ? input('get.parent_id/d') :0;
  53. $view['pathMaps'] = model('Category')->selectPath($this->member_miniapp_id,$parent_id);
  54. $view['parent_id'] = $parent_id;
  55. return view('add',$view);
  56. }
  57. }
  58. //编辑
  59. public function edit(){
  60. if(request()->isAjax()){
  61. $data = [
  62. 'id' => input('post.id/s'),
  63. 'title' => input('post.title/s'),
  64. 'name' => input('post.name/s'),
  65. 'sort' => input('post.sort/d'),
  66. 'parent_id' => input('post.parent_id/d'),
  67. 'types' => input('post.types/d'),
  68. 'picture' => input('post.picture/s'),
  69. ];
  70. $validate = $this->validate($data,'Category.edit');
  71. if(true !== $validate){
  72. return json(['code'=>0,'msg'=>$validate]);
  73. }
  74. $result = model('Category')->edit($data);
  75. if($result){
  76. return json(['code'=>200,'url'=>url('fastshop/category/index',['parent_id'=>$data['parent_id']]),'msg'=>'操作成功']);
  77. }else{
  78. return json(['code'=>0,'msg'=>'操作失败']);
  79. }
  80. }else{
  81. $id = input('get.id/d');
  82. $info = model('Category')->get($id);
  83. if(!$info){
  84. $this->error("404 NOT FOUND");
  85. }
  86. $view['pathMaps'] = model('Category')->selectPath($this->member_miniapp_id,$info['parent_id']);
  87. $view['info'] = $info;
  88. return view('edit',$view);
  89. }
  90. }
  91. /**
  92. * 排序
  93. */
  94. public function sort(){
  95. if(request()->isAjax()){
  96. $data = [
  97. 'sort' => input('post.sort/d'),
  98. 'id' => input('post.id/d'),
  99. ];
  100. $validate = $this->validate($data,'Category.sort');
  101. if(true !== $validate){
  102. return json(['code'=>0,'msg'=>$validate]);
  103. }
  104. $result = model('Category')->save(['sort'=>$data['sort']],['id' => $data['id']]);
  105. if($result){
  106. return json(['code'=>200,'msg'=>'操作成功']);
  107. }else{
  108. return json(['code'=>0,'msg'=>'操作失败']);
  109. }
  110. }
  111. }
  112. //删除
  113. public function delete(){
  114. $id = input('get.id/d');
  115. $info = model('Category')->get(['parent_id' => $id]);
  116. if($info){
  117. return json(['code'=>403,'msg'=>'删除失败,请查看是否包含子栏目']);
  118. }
  119. $goods = model('Item')->get(['category_id' => $id]);
  120. if($goods){
  121. return json(['code'=>403,'msg'=>'删除失败,栏目中还包含商品']);
  122. }
  123. $result = model('Category')->destroy(['id' => $id,'member_miniapp_id' => $this->member_miniapp_id]);
  124. if($result){
  125. return json(['code'=>200,'msg'=>'操作成功']);
  126. }else{
  127. return json(['code'=>403,'msg'=>'删除失败,请查看是否包含子栏目']);
  128. }
  129. }
  130. //全选删除
  131. public function alldelete(){
  132. $ids = input('?post.ids')?input('post.ids/s'):null;
  133. $ids = ids($ids,true);
  134. foreach ($ids as $id) {
  135. $goods = model('Item')->get(['category_id' => $id]);
  136. if(empty($goods)){
  137. $info = model('Category')->get(['parent_id' => $id]);
  138. if(empty($info)){
  139. model('Category')->destroy(['id' => $id,'member_miniapp_id' => $this->member_miniapp_id]);
  140. }
  141. }
  142. }
  143. return json(['code'=>200,'msg'=>'操作成功,如有未删除的,可能含有子栏目或商品','data' => []]);
  144. }
  145. }