StoreCate.php 4.8 KB

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