UserLabelCate.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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\controller\admin\v1\user;
  13. use app\controller\admin\AuthController;
  14. use app\services\user\label\UserLabelCateServices;
  15. use app\services\user\label\UserLabelServices;
  16. use app\validate\admin\user\UserLabelCateValidate;
  17. use think\facade\App;
  18. use app\Request;
  19. /**
  20. * Class UserLabelCate
  21. * @package app\controller\admin\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. $where['group'] = 0;
  47. return $this->success($this->services->getLabelList($where));
  48. }
  49. /**
  50. * 显示创建资源表单页.
  51. *
  52. * @return \think\Response
  53. */
  54. public function create()
  55. {
  56. return $this->success($this->services->createForm());
  57. }
  58. /**
  59. * 保存新建的资源
  60. *
  61. * @param \app\Request $request
  62. * @return \think\Response
  63. */
  64. public function save(Request $request)
  65. {
  66. $data = $request->postMore([
  67. ['name', ''],
  68. ['sort', 0]
  69. ]);
  70. $this->validate($data, UserLabelCateValidate::class);
  71. if ($this->services->count(['name' => $data['name'], 'type' => 0, 'group' => 0])) {
  72. return $this->fail('分类已经存在,请勿重复添加');
  73. }
  74. $data['group'] = 0;
  75. $data['add_time'] = time();
  76. if ($this->services->save($data)) {
  77. $this->services->deleteCateCache();
  78. return $this->success('保存分类成功');
  79. } else {
  80. return $this->fail('保存分类失败');
  81. }
  82. }
  83. /**
  84. * 显示指定的资源
  85. *
  86. * @param int $id
  87. * @return \think\Response
  88. */
  89. public function read($id)
  90. {
  91. if (!$id) {
  92. return $this->fail('缺少标签分类id');
  93. }
  94. $info = $this->services->get($id);
  95. if (!$info) {
  96. return $this->fail('获取标签分类失败');
  97. }
  98. return $this->success($info->toArray());
  99. }
  100. /**
  101. * 显示编辑资源表单页.
  102. *
  103. * @param int $id
  104. * @return \think\Response
  105. */
  106. public function edit($id)
  107. {
  108. return $this->success($this->services->updateForm((int)$id));
  109. }
  110. /**
  111. * 保存更新的资源
  112. *
  113. * @param \app\Request $request
  114. * @param int $id
  115. * @return \think\Response
  116. */
  117. public function update(Request $request, $id)
  118. {
  119. $data = $request->postMore([
  120. ['name', ''],
  121. ['sort', 0],
  122. ]);
  123. $this->validate($data, UserLabelCateValidate::class);
  124. if ($this->services->update($id, $data)) {
  125. $this->services->deleteCateCache();
  126. return $this->success('修改成功');
  127. } else {
  128. return $this->fail('修改失败');
  129. }
  130. }
  131. /**
  132. * 删除指定资源
  133. *
  134. * @param int $id
  135. * @return \think\Response
  136. */
  137. public function delete($id)
  138. {
  139. if (!$id || !($info = $this->services->get($id))) {
  140. return $this->fail('删除的数据不存在');
  141. }
  142. /** @var UserLabelServices $userLable */
  143. $userLable = app()->make(UserLabelServices::class);
  144. if ($userLable->count(['label_cate' => $id])) {
  145. return $this->fail('该分类下有标签数据');
  146. }
  147. if ($info->delete()) {
  148. $this->services->deleteCateCache();
  149. return $this->success('删除成功');
  150. } else {
  151. return $this->fail('删除失败');
  152. }
  153. }
  154. /**
  155. * 获取用户标签分类全部
  156. * @return mixed
  157. */
  158. public function getAll()
  159. {
  160. return $this->success($this->services->getLabelCateAll());
  161. }
  162. }