UserLabelCate.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 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 app\services\user\UserLabelServices;
  17. use think\facade\App;
  18. use app\Request;
  19. /**
  20. * Class UserLabelCate
  21. * @package app\adminapi\controller\v1\user
  22. */
  23. class UserLabelCate extends AuthController
  24. {
  25. /**
  26. * UserLabelCate constructor.
  27. * @param App $app
  28. * @param UserLabelCateServices $services
  29. */
  30. public function __construct(App $app, UserLabelCateServices $services)
  31. {
  32. parent::__construct($app);
  33. $this->services = $services;
  34. }
  35. /**
  36. * 显示资源列表
  37. *
  38. * @return \think\Response
  39. */
  40. public function index(Request $request)
  41. {
  42. $where = $request->postMore([
  43. ['name', '']
  44. ]);
  45. $where['type'] = 0;
  46. return app('json')->success($this->services->getLabelList($where));
  47. }
  48. /**
  49. * 显示创建资源表单页.
  50. *
  51. * @return \think\Response
  52. */
  53. public function create()
  54. {
  55. return app('json')->success($this->services->createForm());
  56. }
  57. /**
  58. * 保存新建的资源
  59. *
  60. * @param Request $request
  61. * @return \think\Response
  62. */
  63. public function save(Request $request)
  64. {
  65. $data = $request->postMore([
  66. ['name', ''],
  67. ['sort', 0]
  68. ]);
  69. $this->validate($data, UserLabeCateValidata::class);
  70. if ($this->services->count(['name' => $data['name']])) {
  71. return app('json')->fail(400101);
  72. }
  73. $data['type'] = 0;
  74. if ($this->services->save($data)) {
  75. $this->services->deleteCateCache();
  76. return app('json')->success(100000);
  77. } else {
  78. return app('json')->fail(100006);
  79. }
  80. }
  81. /**
  82. * 显示指定的资源
  83. *
  84. * @param int $id
  85. * @return \think\Response
  86. */
  87. public function read($id)
  88. {
  89. if (!$id) {
  90. return app('json')->fail(100100);
  91. }
  92. $info = $this->services->get($id);
  93. if (!$info) {
  94. return app('json')->fail(100026);
  95. }
  96. return app('json')->success($info->toArray());
  97. }
  98. /**
  99. * 显示编辑资源表单页.
  100. *
  101. * @param int $id
  102. * @return \think\Response
  103. */
  104. public function edit($id)
  105. {
  106. return app('json')->success($this->services->updateForm((int)$id));
  107. }
  108. /**
  109. * 保存更新的资源
  110. *
  111. * @param Request $request
  112. * @param int $id
  113. * @return \think\Response
  114. */
  115. public function update(Request $request, $id)
  116. {
  117. $data = $request->postMore([
  118. ['name', ''],
  119. ['sort', 0],
  120. ]);
  121. $this->validate($data, UserLabeCateValidata::class);
  122. if ($this->services->update($id, $data)) {
  123. $this->services->deleteCateCache();
  124. return app('json')->success(100001);
  125. } else {
  126. return app('json')->fail(100007);
  127. }
  128. }
  129. /**
  130. * 删除指定资源
  131. *
  132. * @param int $id
  133. * @return \think\Response
  134. */
  135. public function delete($id)
  136. {
  137. if (!$id || !($info = $this->services->get($id))) {
  138. return app('json')->fail(100026);
  139. }
  140. /** @var $labelService $labelservice */
  141. $labelService = app()->make(UserLabelServices::class);
  142. $count = $labelService->getCount(['label_cate' => $id]);
  143. if($count) return app('json')->fail(400323);
  144. if ($info->delete()) {
  145. $this->services->deleteCateCache();
  146. return app('json')->success(100002);
  147. } else {
  148. return app('json')->fail(100008);
  149. }
  150. }
  151. /**
  152. * 获取用户标签分类全部
  153. * @return mixed
  154. */
  155. public function getAll()
  156. {
  157. return app('json')->success($this->services->getLabelCateAll());
  158. }
  159. }