FeedBackCategory.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 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. /**
  16. * 用户反馈分类
  17. * app\controller\admin\user
  18. * FeedBackCategory
  19. */
  20. class FeedBackCategory extends BaseController
  21. {
  22. /**
  23. * @var repository
  24. */
  25. protected $repository;
  26. /**
  27. * User constructor.
  28. * @param App $app
  29. * @param $repository
  30. */
  31. public function __construct(App $app, repository $repository)
  32. {
  33. parent::__construct($app);
  34. $this->repository = $repository;
  35. }
  36. /**
  37. * 列表
  38. * @return mixed
  39. * @author Qinii
  40. */
  41. public function lst()
  42. {
  43. return app('json')->success($this->repository->getFormatList(0));
  44. }
  45. /**
  46. * 创建表单
  47. * @return mixed
  48. * @author Qinii
  49. */
  50. public function createForm()
  51. {
  52. return app('json')->success(formToData($this->repository->form(0)));
  53. }
  54. /**
  55. * 编辑表单
  56. * @param $id
  57. * @return mixed
  58. * @author Qinii
  59. */
  60. public function updateForm($id)
  61. {
  62. if (!$this->repository->merExists(0, $id))
  63. return app('json')->fail('数据不存在');
  64. return app('json')->success(formToData($this->repository->updateForm(0, $id)));
  65. }
  66. /**
  67. * 创建
  68. * @param $id
  69. * @return mixed
  70. * @author Qinii
  71. */
  72. public function create()
  73. {
  74. $data = $this->request->params(['pid', 'cate_name', 'sort', 'pic', 'is_show']);
  75. if(is_array($data['pid']) && empty($data['pid']))
  76. return app('json')->fail('请选择上级分类');
  77. if (empty($data['cate_name']))
  78. return app('json')->fail('分类名不可为空');
  79. if (strlen($data['cate_name']) > 60)
  80. return app('json')->fail('分类名不得超过20个汉字');
  81. if ($data['pid'] && !$this->repository->merExists(0, $data['pid']))
  82. return app('json')->fail('上级分类不存在');
  83. if ($data['pid'] && !$this->repository->checkLevel($data['pid']))
  84. return app('json')->fail('不可添加更低阶分类');
  85. $this->repository->create($data);
  86. return app('json')->success('添加成功');
  87. }
  88. /**
  89. * 修改
  90. * @param $id
  91. * @return mixed
  92. * @author Qinii
  93. */
  94. public function update($id)
  95. {
  96. $data = $this->request->params(['pid', 'cate_name', 'sort', 'pic', 'is_show']);
  97. if (!$this->repository->merExists(0, $id))
  98. return app('json')->fail('数据不存在');
  99. if ($data['pid'] && !$this->repository->merExists(0, $data['pid']))
  100. return app('json')->fail('上级分类不存在');
  101. if ($data['pid'] && !$this->repository->checkLevel($data['pid']))
  102. return app('json')->fail('不可添加更低阶分类');
  103. if (!$this->repository->checkChangeToChild($id, $data['pid']))
  104. return app('json')->fail('无法修改到当前分类到子集,请先修改子类');
  105. //todo 待优化
  106. // if (!$this->repository->checkChildLevel($id,$data['pid']))
  107. // return app('json')->fail('子类超过最低限制,请先修改子类');
  108. $this->repository->update($id, $data);
  109. return app('json')->success('编辑成功');
  110. }
  111. /**
  112. * 修改状态
  113. * @param $id
  114. * @return mixed
  115. * @author Qinii
  116. */
  117. public function switchStatus($id)
  118. {
  119. $status = $this->request->param('status', 0) == 1 ? 1 : 0;
  120. if (!$this->repository->merExists(0, $id))
  121. return app('json')->fail('数据不存在');
  122. $this->repository->switchStatus($id, $status);
  123. return app('json')->success('修改成功');
  124. }
  125. /**
  126. * 删除
  127. * @param $id
  128. * @return mixed
  129. * @author Qinii
  130. */
  131. public function delete($id)
  132. {
  133. if (!$this->repository->merExists(0, $id))
  134. return app('json')->fail('数据不存在');
  135. if ($this->repository->hasChild($id))
  136. return app('json')->fail('该分类存在子集,请先处理子集');
  137. $this->repository->delete($id);
  138. return app('json')->success('删除成功');
  139. }
  140. /**
  141. * 详情
  142. * @param $id
  143. * @return mixed
  144. * @author Qinii
  145. */
  146. public function detail($id)
  147. {
  148. if (!$this->repository->merExists(0, $id))
  149. return app('json')->fail('数据不存在');
  150. return app('json')->success($this->repository->get($id));
  151. }
  152. }