AskCategory.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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\BestbaoAskCategory;
  10. use app\bestbao\model\BestbaoAsk;
  11. use think\facade\Request;
  12. class AskCategory 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'] = BestbaoAskCategory::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. 'sort' => $this->request->param('sort/d'),
  32. 'picture' => $this->request->param('picture/s'),
  33. 'member_miniapp_id' => $this->member_miniapp_id,
  34. ];
  35. $validate = $this->validate($data,'Category.edit');
  36. if(true !== $validate){
  37. return json(['code'=>0,'msg'=>$validate]);
  38. }
  39. $result = BestbaoAskCategory::edit($data);
  40. if($result){
  41. return enjson(200,'操作成功',['url'=>url('askCategory/index')]);
  42. }else{
  43. return enjson(0);
  44. }
  45. }else{
  46. return view();
  47. }
  48. }
  49. //编辑
  50. public function edit(){
  51. if(request()->isAjax()){
  52. $data = [
  53. 'id' => $this->request->param('id/s'),
  54. 'title' => $this->request->param('title/s'),
  55. 'sort' => $this->request->param('sort/d'),
  56. 'picture' => $this->request->param('picture/s'),
  57. ];
  58. $validate = $this->validate($data,'Category.edit');
  59. if(true !== $validate){
  60. return json(['code'=>0,'msg'=>$validate]);
  61. }
  62. $result = BestbaoAskCategory::edit($data);
  63. if($result){
  64. return enjson(200,'操作成功',['url'=>url('askCategory/index')]);
  65. }else{
  66. return enjson(0);
  67. }
  68. }else{
  69. $view['info'] = BestbaoAskCategory::where(['id' => $this->request->param('id/d'),'member_miniapp_id' => $this->member_miniapp_id])->find();
  70. return view()->assign($view);
  71. }
  72. }
  73. /**
  74. * 排序
  75. */
  76. public function sort(){
  77. if(request()->isAjax()){
  78. $data = [
  79. 'sort' => $this->request->param('sort/d'),
  80. 'id' => $this->request->param('id/d'),
  81. ];
  82. $validate = $this->validate($data,'Category.sort');
  83. if(true !== $validate){
  84. return json(['code'=>0,'msg'=>$validate]);
  85. }
  86. $result = BestbaoAskCategory::update(['sort'=>$data['sort']],['id' => $data['id']]);
  87. if($result){
  88. return enjson(200);
  89. }else{
  90. return enjson(0);
  91. }
  92. }
  93. }
  94. //删除
  95. public function delete(int $id){
  96. $goods = BestbaoAsk::where(['category_id' => $id,'member_miniapp_id' => $this->member_miniapp_id])->find();
  97. if($goods){
  98. return enjson(403,'删除失败,栏目中还包含资源');
  99. }
  100. $result = BestbaoAskCategory::destroy($id);
  101. if($result){
  102. return enjson(200);
  103. }else{
  104. return enjson(403,'删除失败');
  105. }
  106. }
  107. }