UserLabel.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /**
  3. * @package merchant
  4. *
  5. * @author xaboy
  6. * @day 2020-05-07
  7. *
  8. *
  9. */
  10. namespace app\controller\admin\user;
  11. use crmeb\basic\BaseController;
  12. use app\common\repositories\user\UserLabelRepository;
  13. use app\validate\admin\UserLabelValidate;
  14. use FormBuilder\Exception\FormBuilderException;
  15. use think\App;
  16. use think\db\exception\DataNotFoundException;
  17. use think\db\exception\DbException;
  18. use think\db\exception\ModelNotFoundException;
  19. /**
  20. * Class UserLabel
  21. * @package app\controller\admin\user
  22. * @author xaboy
  23. * @day 2020-05-07
  24. */
  25. class UserLabel extends BaseController
  26. {
  27. /**
  28. * @var UserLabelRepository
  29. */
  30. protected $repository;
  31. /**
  32. * UserGroup constructor.
  33. * @param App $app
  34. * @param UserLabelRepository $repository
  35. */
  36. public function __construct(App $app, UserLabelRepository $repository)
  37. {
  38. parent::__construct($app);
  39. $this->repository = $repository;
  40. }
  41. /**
  42. * @return mixed
  43. * @throws DataNotFoundException
  44. * @throws DbException
  45. * @throws ModelNotFoundException
  46. * @author xaboy
  47. * @day 2020-05-07
  48. */
  49. public function lst()
  50. {
  51. [$page, $limit] = $this->getPage();
  52. return app('json')->success($this->repository->getList([], $page, $limit));
  53. }
  54. /**
  55. * @return mixed
  56. * @throws FormBuilderException
  57. * @author xaboy
  58. * @day 2020-05-07
  59. */
  60. public function createForm()
  61. {
  62. return app('json')->success(formToData($this->repository->form()));
  63. }
  64. /**
  65. * @param UserLabelValidate $validate
  66. * @return mixed
  67. * @author xaboy
  68. * @day 2020-05-07
  69. */
  70. public function create(UserLabelValidate $validate)
  71. {
  72. $data = $this->checkParams($validate);
  73. $this->repository->create($data);
  74. return app('json')->success('添加成功');
  75. }
  76. /**
  77. * @param $id
  78. * @return mixed
  79. * @throws DbException
  80. * @throws FormBuilderException
  81. * @throws DataNotFoundException
  82. * @throws ModelNotFoundException
  83. * @author xaboy
  84. * @day 2020-05-07
  85. */
  86. public function updateForm($id)
  87. {
  88. if (!$this->repository->exists($id))
  89. return app('json')->fail('数据不存在');
  90. return app('json')->success(formToData($this->repository->updateForm($id)));
  91. }
  92. /**
  93. * @param $id
  94. * @param UserLabelValidate $validate
  95. * @return mixed
  96. * @throws DbException
  97. * @author xaboy
  98. * @day 2020-05-07
  99. */
  100. public function update($id, UserLabelValidate $validate)
  101. {
  102. $data = $this->checkParams($validate);
  103. if (!$this->repository->exists($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 xaboy
  113. * @day 2020-05-07
  114. */
  115. public function delete($id)
  116. {
  117. if (!$this->repository->exists($id))
  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 xaboy
  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. }