FeedBackCategory.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace app\controller\admin\user;
  3. use ln\basic\BaseController;
  4. use think\App;
  5. use app\common\repositories\user\FeedBackCategoryRepository as repository;
  6. class FeedBackCategory extends BaseController
  7. {
  8. /**
  9. * @var repository
  10. */
  11. protected $repository;
  12. /**
  13. * User constructor.
  14. * @param App $app
  15. * @param $repository
  16. */
  17. public function __construct(App $app, repository $repository)
  18. {
  19. parent::__construct($app);
  20. $this->repository = $repository;
  21. }
  22. /**
  23. * @return mixed
  24. * @author Qinii
  25. */
  26. public function lst()
  27. {
  28. return app('json')->success($this->repository->getFormatList(0));
  29. }
  30. public function createForm()
  31. {
  32. return app('json')->success(formToData($this->repository->form(0)));
  33. }
  34. public function updateForm($id)
  35. {
  36. if (!$this->repository->merExists(0, $id))
  37. return app('json')->fail('数据不存在');
  38. return app('json')->success(formToData($this->repository->updateForm(0,$id)));
  39. }
  40. public function create()
  41. {
  42. $data = $this->request->params(['pid','cate_name','sort','pic','is_show']);
  43. if(empty($data['cate_name']))
  44. return app('json')->fail('分类名不可为空');
  45. if(strlen($data['cate_name']) > 60)
  46. return app('json')->fail('分类名不得超过20个汉字');
  47. if ($data['pid'] && !$this->repository->merExists(0, $data['pid']))
  48. return app('json')->fail('上级分类不存在');
  49. if ($data['pid'] && !$this->repository->checkLevel($data['pid']))
  50. return app('json')->fail('不可添加更低阶分类');
  51. $this->repository->create($data);
  52. return app('json')->success('添加成功');
  53. }
  54. public function update($id)
  55. {
  56. $data = $this->request->params(['pid','cate_name','sort','pic','is_show']);
  57. if (!$this->repository->merExists(0, $id))
  58. return app('json')->fail('数据不存在');
  59. if ($data['pid'] && !$this->repository->merExists(0, $data['pid']))
  60. return app('json')->fail('上级分类不存在');
  61. if ($data['pid'] && !$this->repository->checkLevel($data['pid']))
  62. return app('json')->fail('不可添加更低阶分类');
  63. if (!$this->repository->checkChangeToChild($id,$data['pid']))
  64. return app('json')->fail('无法修改到当前分类到子集,请先修改子类');
  65. //todo 待优化
  66. // if (!$this->repository->checkChildLevel($id,$data['pid']))
  67. // return app('json')->fail('子类超过最低限制,请先修改子类');
  68. $this->repository->update($id,$data);
  69. return app('json')->success('编辑成功');
  70. }
  71. public function switchStatus($id)
  72. {
  73. $status = $this->request->param('status', 0) == 1 ? 1 : 0;
  74. if (!$this->repository->merExists(0, $id))
  75. return app('json')->fail('数据不存在');
  76. $this->repository->switchStatus($id, $status);
  77. return app('json')->success('修改成功');
  78. }
  79. public function delete($id)
  80. {
  81. if (!$this->repository->merExists(0, $id))
  82. return app('json')->fail('数据不存在');
  83. if ($this->repository->hasChild($id))
  84. return app('json')->fail('该分类存在子集,请先处理子集');
  85. $this->repository->delete($id);
  86. return app('json')->success('删除成功');
  87. }
  88. public function detail($id)
  89. {
  90. if (!$this->repository->merExists(0, $id))
  91. return app('json')->fail('数据不存在');
  92. return app('json')->success($this->repository->get($id));
  93. }
  94. }