UserLabel.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. namespace app\controller\admin\user;
  3. use ln\basic\BaseController;
  4. use app\common\repositories\user\UserLabelRepository;
  5. use app\validate\admin\UserLabelValidate;
  6. use FormBuilder\Exception\FormBuilderException;
  7. use think\App;
  8. use think\db\exception\DataNotFoundException;
  9. use think\db\exception\DbException;
  10. use think\db\exception\ModelNotFoundException;
  11. /**
  12. * Class UserLabel
  13. * @package app\controller\admin\user
  14. * @author zfy
  15. * @day 2020-05-07
  16. */
  17. class UserLabel extends BaseController
  18. {
  19. /**
  20. * @var UserLabelRepository
  21. */
  22. protected $repository;
  23. /**
  24. * UserGroup constructor.
  25. * @param App $app
  26. * @param UserLabelRepository $repository
  27. */
  28. public function __construct(App $app, UserLabelRepository $repository)
  29. {
  30. parent::__construct($app);
  31. $this->repository = $repository;
  32. }
  33. /**
  34. * @return mixed
  35. * @throws DataNotFoundException
  36. * @throws DbException
  37. * @throws ModelNotFoundException
  38. * @author zfy
  39. * @day 2020-05-07
  40. */
  41. public function lst()
  42. {
  43. [$page, $limit] = $this->getPage();
  44. $where = $this->request->params(['type', 'all']);
  45. $where['mer_id'] = $this->request->merId();
  46. return app('json')->success($this->repository->getList($where, $page, $limit));
  47. }
  48. /**
  49. * @return mixed
  50. * @throws FormBuilderException
  51. * @author zfy
  52. * @day 2020-05-07
  53. */
  54. public function createForm()
  55. {
  56. return app('json')->success(formToData($this->repository->form()));
  57. }
  58. /**
  59. * @param UserLabelValidate $validate
  60. * @return mixed
  61. * @author zfy
  62. * @day 2020-05-07
  63. */
  64. public function create(UserLabelValidate $validate)
  65. {
  66. $data = $this->checkParams($validate);
  67. $data['mer_id'] = $this->request->merId();
  68. if ($this->repository->existsName($data['label_name'], $data['mer_id'], 0))
  69. return app('json')->fail('标签名已存在');
  70. $this->repository->create($data);
  71. return app('json')->success('添加成功');
  72. }
  73. /**
  74. * @param $id
  75. * @return mixed
  76. * @throws DbException
  77. * @throws FormBuilderException
  78. * @throws DataNotFoundException
  79. * @throws ModelNotFoundException
  80. * @author zfy
  81. * @day 2020-05-07
  82. */
  83. public function updateForm($id)
  84. {
  85. if (!$this->repository->exists($id, $this->request->merId()))
  86. return app('json')->fail('数据不存在');
  87. return app('json')->success(formToData($this->repository->updateForm($id)));
  88. }
  89. /**
  90. * @param $id
  91. * @param UserLabelValidate $validate
  92. * @return mixed
  93. * @throws DbException
  94. * @author zfy
  95. * @day 2020-05-07
  96. */
  97. public function update($id, UserLabelValidate $validate)
  98. {
  99. $data = $this->checkParams($validate);
  100. $merId = $this->request->merId();
  101. if (!$this->repository->exists($id, $merId))
  102. return app('json')->fail('数据不存在');
  103. if ($this->repository->existsName($data['label_name'], $merId, 0, $id))
  104. return app('json')->fail('标签名已存在');
  105. $this->repository->update($id, $data);
  106. return app('json')->success('编辑成功');
  107. }
  108. /**
  109. * @param $id
  110. * @return mixed
  111. * @throws DbException
  112. * @author zfy
  113. * @day 2020-05-07
  114. */
  115. public function delete($id)
  116. {
  117. if (!$this->repository->exists($id, $this->request->merId()))
  118. return app('json')->fail('数据不存在');
  119. $this->repository->delete($id);
  120. return app('json')->success('删除成功');
  121. }
  122. /**
  123. * @param UserLabelValidate $validate
  124. * @return array
  125. * @author zfy
  126. * @day 2020-05-07
  127. */
  128. protected function checkParams(UserLabelValidate $validate)
  129. {
  130. $data = $this->request->params(['label_name']);
  131. $validate->check($data);
  132. return $data;
  133. }
  134. }