FeedBackCategory.php 4.2 KB

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