UserLabelCate.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. declare (strict_types=1);
  12. namespace app\adminapi\controller\v1\user;
  13. use app\adminapi\controller\AuthController;
  14. use app\services\user\UserLabelCateServices;
  15. use app\adminapi\validate\user\UserLabeCateValidata;
  16. use think\facade\App;
  17. use app\Request;
  18. /**
  19. * Class UserLabelCate
  20. * @package app\adminapi\controller\v1\user
  21. */
  22. class UserLabelCate extends AuthController
  23. {
  24. /**
  25. * UserLabelCate constructor.
  26. * @param App $app
  27. * @param UserLabelCateServices $services
  28. */
  29. public function __construct(App $app, UserLabelCateServices $services)
  30. {
  31. parent::__construct($app);
  32. $this->services = $services;
  33. }
  34. /**
  35. * 显示资源列表
  36. *
  37. * @return \think\Response
  38. */
  39. public function index(Request $request)
  40. {
  41. $where = $request->postMore([
  42. ['name', '']
  43. ]);
  44. $where['type'] = 0;
  45. return app('json')->success($this->services->getLabelList($where));
  46. }
  47. /**
  48. * 显示创建资源表单页.
  49. *
  50. * @return \think\Response
  51. */
  52. public function create()
  53. {
  54. return app('json')->success($this->services->createForm());
  55. }
  56. /**
  57. * 保存新建的资源
  58. *
  59. * @param \app\Request $request
  60. * @return \think\Response
  61. */
  62. public function save(Request $request)
  63. {
  64. $data = $request->postMore([
  65. ['name', ''],
  66. ['sort', 0]
  67. ]);
  68. validate(UserLabeCateValidata::class)->check($data);
  69. if ($this->services->count(['name' => $data['name']])) {
  70. return app('json')->fail('分类已经存在,请勿重复添加');
  71. }
  72. $data['type'] = 0;
  73. if ($this->services->save($data)) {
  74. $this->services->deleteCateCache();
  75. return app('json')->success('保存分类成功');
  76. } else {
  77. return app('json')->fail('保存分类失败');
  78. }
  79. }
  80. /**
  81. * 显示指定的资源
  82. *
  83. * @param int $id
  84. * @return \think\Response
  85. */
  86. public function read($id)
  87. {
  88. if (!$id) {
  89. return app('json')->fail('缺少标签分类id');
  90. }
  91. $info = $this->services->get($id);
  92. if (!$info) {
  93. return app('json')->fail('获取标签分类失败');
  94. }
  95. return app('json')->success($info->toArray());
  96. }
  97. /**
  98. * 显示编辑资源表单页.
  99. *
  100. * @param int $id
  101. * @return \think\Response
  102. */
  103. public function edit($id)
  104. {
  105. return app('json')->success($this->services->updateForm((int)$id));
  106. }
  107. /**
  108. * 保存更新的资源
  109. *
  110. * @param \app\Request $request
  111. * @param int $id
  112. * @return \think\Response
  113. */
  114. public function update(Request $request, $id)
  115. {
  116. $data = $request->postMore([
  117. ['name', ''],
  118. ['sort', 0],
  119. ]);
  120. validate(UserLabeCateValidata::class)->check($data);
  121. if ($this->services->update($id, $data)) {
  122. $this->services->deleteCateCache();
  123. return app('json')->success('修改成功');
  124. } else {
  125. return app('json')->fail('修改失败');
  126. }
  127. }
  128. /**
  129. * 删除指定资源
  130. *
  131. * @param int $id
  132. * @return \think\Response
  133. */
  134. public function delete($id)
  135. {
  136. if (!$id || !($info = $this->services->get($id))) {
  137. return app('json')->fail('删除的数据不存在');
  138. }
  139. if ($info->delete()) {
  140. $this->services->deleteCateCache();
  141. return app('json')->success('删除成功');
  142. } else {
  143. return app('json')->fail('删除失败');
  144. }
  145. }
  146. /**
  147. * 获取用户标签分类全部
  148. * @return mixed
  149. */
  150. public function getAll()
  151. {
  152. return app('json')->success($this->services->getLabelCateAll());
  153. }
  154. }