UserLabelCate.php 5.5 KB

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