Category.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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\bestbao\controller;
  9. use app\bestbao\model\BestbaoCategory;
  10. use app\bestbao\model\BestbaoProduct;
  11. use think\facade\Request;
  12. class Category extends Common{
  13. public function initialize(){
  14. parent::initialize();
  15. $this->assign('pathMaps',[['name'=>'产品分类','url'=>'javascript:;']]);
  16. }
  17. /**
  18. * 列表
  19. */
  20. public function index(){
  21. $view['lists'] = BestbaoCategory::where(['member_miniapp_id' => $this->member_miniapp_id])->order('sort desc,id desc')->paginate(20);
  22. return view()->assign($view);
  23. }
  24. /**
  25. * 添加
  26. */
  27. public function add(){
  28. if(request()->isAjax()){
  29. $data = [
  30. 'title' => $this->request->param('title/s'),
  31. 'note' => $this->request->param('note/s'),
  32. 'sort' => $this->request->param('sort/d'),
  33. 'picture' => $this->request->param('picture/s'),
  34. 'member_miniapp_id' => $this->member_miniapp_id,
  35. ];
  36. $validate = $this->validate($data,'Category.edit');
  37. if(true !== $validate){
  38. return json(['code'=>0,'msg'=>$validate]);
  39. }
  40. $result = BestbaoCategory::edit($data);
  41. if($result){
  42. return enjson(200,'操作成功',['url'=>url('category/index')]);
  43. }else{
  44. return enjson(0);
  45. }
  46. }else{
  47. return view();
  48. }
  49. }
  50. //编辑
  51. public function edit(){
  52. if(request()->isAjax()){
  53. $data = [
  54. 'id' => $this->request->param('id/s'),
  55. 'title' => $this->request->param('title/s'),
  56. 'note' => $this->request->param('note/s'),
  57. 'sort' => $this->request->param('sort/d'),
  58. 'picture' => $this->request->param('picture/s'),
  59. ];
  60. $validate = $this->validate($data,'Category.edit');
  61. if(true !== $validate){
  62. return json(['code'=>0,'msg'=>$validate]);
  63. }
  64. $result = BestbaoCategory::edit($data);
  65. if($result){
  66. return enjson(200,'操作成功',['url'=>url('category/index')]);
  67. }else{
  68. return enjson(0);
  69. }
  70. }else{
  71. $view['info'] = BestbaoCategory::where(['id' => Request::param('id/d'),'member_miniapp_id' => $this->member_miniapp_id])->find();
  72. return view()->assign($view);
  73. }
  74. }
  75. /**
  76. * 排序
  77. */
  78. public function sort(){
  79. if(request()->isAjax()){
  80. $data = [
  81. 'sort' => $this->request->param('sort/d'),
  82. 'id' => $this->request->param('id/d'),
  83. ];
  84. $validate = $this->validate($data,'Category.sort');
  85. if(true !== $validate){
  86. return json(['code'=>0,'msg'=>$validate]);
  87. }
  88. $result = BestbaoCategory::update(['sort'=>$data['sort']],['id' => $data['id']]);
  89. if($result){
  90. return enjson(200);
  91. }else{
  92. return enjson(0);
  93. }
  94. }
  95. }
  96. //删除
  97. public function delete(int $id){
  98. $goods = BestbaoProduct::where(['category_id' => $id,'member_miniapp_id' => $this->member_miniapp_id])->find();
  99. if($goods){
  100. return enjson(403,'删除失败,栏目中还包含产品');
  101. }
  102. $result = BestbaoCategory::destroy($id);
  103. if($result){
  104. return enjson(200);
  105. }else{
  106. return enjson(403,'删除失败');
  107. }
  108. }
  109. }