Cate.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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\ais\controller\store;
  9. use app\ais\controller\Common;
  10. use app\ais\model\AisStoreCate;
  11. use app\ais\model\AisStore;
  12. class Cate extends Common{
  13. public function initialize() {
  14. parent::initialize();
  15. $this->cate = new AisStoreCate();
  16. $this->store = new AisStore();
  17. }
  18. /**
  19. * 列表
  20. */
  21. public function index(){
  22. $view['parent_id'] = $this->request->param('parent_id/d',0);
  23. $view['pathMaps'] = $this->cate->selectPath($view['parent_id']);
  24. $view['lists'] = $this->cate->where(['member_miniapp_id' => $this->member_miniapp_id,'parent_id' => $view['parent_id']])->order('sort desc,id desc')->paginate(20);
  25. return view()->assign($view);
  26. }
  27. /**
  28. * 添加
  29. */
  30. public function add(){
  31. if(request()->isAjax()){
  32. $data = [
  33. 'member_miniapp_id' => $this->member_miniapp_id,
  34. 'title' => $this->request->param('title/s'),
  35. 'name' => $this->request->param('name/s'),
  36. 'sort' => $this->request->param('sort/d'),
  37. 'parent_id' => $this->request->param('parent_id/d'),
  38. 'picture' => $this->request->param('picture/s')
  39. ];
  40. $validate = $this->validate($data,'Category.add');
  41. if(true !== $validate){
  42. return json(['code'=>0,'msg'=>$validate]);
  43. }
  44. $result = $this->cate->edit($data);
  45. if($result){
  46. return json(['code'=>200,'url'=> url('store.cate/index',['parent_id'=>$data['parent_id']]),'msg'=>'操作成功']);
  47. }else{
  48. return json(['code'=>0,'msg'=>'操作失败']);
  49. }
  50. }else{
  51. $view['parent_id'] = $this->request->param('parent_id/d',0);
  52. $view['pathMaps'] = $this->cate->selectPath($view['parent_id']);
  53. return view()->assign($view);
  54. }
  55. }
  56. //编辑
  57. public function edit(){
  58. if(request()->isAjax()){
  59. $data = [
  60. 'id' => $this->request->param('id/s'),
  61. 'title' => $this->request->param('title/s'),
  62. 'name' => $this->request->param('name/s'),
  63. 'sort' => $this->request->param('sort/d'),
  64. 'parent_id' => $this->request->param('parent_id/d'),
  65. 'picture' => $this->request->param('picture/s'),
  66. ];
  67. $validate = $this->validate($data,'Category.edit');
  68. if(true !== $validate){
  69. return json(['code'=>0,'msg'=>$validate]);
  70. }
  71. $result = $this->cate->edit($data);
  72. if($result){
  73. return json(['code'=>200,'url'=>url('store.cate/index',['parent_id'=>$data['parent_id']]),'msg'=>'操作成功']);
  74. }else{
  75. return json(['code'=>0,'msg'=>'操作失败']);
  76. }
  77. }else{
  78. $view['info'] = $this->cate->where(['id' => $this->request->param('id/d'),'member_miniapp_id' => $this->member_miniapp_id])->find();
  79. if(empty($view['info'])){
  80. $this->error("404 NOT FOUND");
  81. }
  82. $view['pathMaps'] = $this->cate->selectPath($view['info']->parent_id);
  83. return $this->fetch()->assign($view);
  84. }
  85. }
  86. /**
  87. * 排序
  88. */
  89. public function sort(){
  90. if(request()->isAjax()){
  91. $data = [
  92. 'sort' => $this->request->param('sort/d'),
  93. 'id' => $this->request->param('id/d'),
  94. ];
  95. $validate = $this->validate($data,'Category.sort');
  96. if(true !== $validate){
  97. return json(['code'=>0,'msg'=>$validate]);
  98. }
  99. $result = $this->cate->save(['sort'=>$data['sort']],['id' => $data['id']]);
  100. if($result){
  101. return json(['code'=>200,'msg'=>'操作成功']);
  102. }else{
  103. return json(['code'=>0,'msg'=>'操作失败']);
  104. }
  105. }
  106. }
  107. //删除
  108. public function delete(int $id){
  109. $info = $this->cate->where(['parent_id' => $id])->find();
  110. if($info){
  111. return json(['code'=>403,'msg'=>'删除失败,请查看是否包含子栏目']);
  112. }
  113. $store = $this->store->where(['cate_id' => $id])->find();
  114. if($store){
  115. return json(['code'=>403,'msg'=>'删除失败,栏目中还包含好店']);
  116. }
  117. $result = $this->cate->where(['id'=>$id,'member_miniapp_id' => $this->member_miniapp_id])->delete();
  118. if($result){
  119. return json(['code'=>200,'msg'=>'操作成功']);
  120. }else{
  121. return json(['code'=>403,'msg'=>'删除失败,请查看是否包含子栏目']);
  122. }
  123. }
  124. }