UserLabel.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. namespace app\adminapi\controller\v1\user;
  12. use app\adminapi\controller\AuthController;
  13. use app\services\user\UserLabelCateServices;
  14. use app\services\user\UserLabelRelationServices;
  15. use app\services\user\UserLabelServices;
  16. use think\facade\App;
  17. /**
  18. * 用户标签控制器
  19. * Class UserLabel
  20. * @package app\adminapi\controller\v1\user
  21. */
  22. class UserLabel extends AuthController
  23. {
  24. /**
  25. * UserLabel constructor.
  26. * @param App $app
  27. * @param UserLabelServices $service
  28. */
  29. public function __construct(App $app, UserLabelServices $services)
  30. {
  31. parent::__construct($app);
  32. $this->services = $services;
  33. }
  34. /**
  35. * 标签列表
  36. * @param int $label_cate
  37. * @return mixed
  38. * @throws \think\db\exception\DataNotFoundException
  39. * @throws \think\db\exception\DbException
  40. * @throws \think\db\exception\ModelNotFoundException
  41. */
  42. public function index($label_cate = '')
  43. {
  44. return app('json')->success($this->services->getList(['label_cate' => $label_cate]));
  45. }
  46. /**
  47. * 添加修改标签表单
  48. * @return mixed
  49. * @throws \FormBuilder\Exception\FormBuilderException
  50. */
  51. public function add()
  52. {
  53. [$id, $cateId] = $this->request->getMore([
  54. ['id', 0],
  55. ['cate_id', 0],
  56. ], true);
  57. return app('json')->success($this->services->add((int)$id, (int)$cateId));
  58. }
  59. /**
  60. * 保存标签表单数据
  61. * @return mixed
  62. * @throws \think\db\exception\DataNotFoundException
  63. * @throws \think\db\exception\DbException
  64. * @throws \think\db\exception\ModelNotFoundException
  65. */
  66. public function save()
  67. {
  68. $data = $this->request->postMore([
  69. ['id', 0],
  70. ['label_cate', 0],
  71. ['label_name', ''],
  72. ]);
  73. if (!$data['label_name'] = trim($data['label_name'])) return app('json')->fail(400322);
  74. $this->services->save((int)$data['id'], $data);
  75. return app('json')->success(100000);
  76. }
  77. /**
  78. * 删除
  79. * @param $id
  80. * @throws \Exception
  81. */
  82. public function delete()
  83. {
  84. list($id) = $this->request->getMore([
  85. ['id', 0],
  86. ], true);
  87. if (!$id) return app('json')->fail(100100);
  88. $this->services->delLabel((int)$id);
  89. return app('json')->success(100002);
  90. }
  91. /**
  92. * 标签分类
  93. * @param UserLabelCateServices $services
  94. * @return mixed
  95. */
  96. public function getUserLabel(UserLabelCateServices $services, $uid)
  97. {
  98. return app('json')->success($services->getUserLabel((int)$uid));
  99. }
  100. /**
  101. * 设置用户标签
  102. * @param UserLabelRelationServices $services
  103. * @param $uid
  104. * @return mixed
  105. */
  106. public function setUserLabel(UserLabelRelationServices $services, $uid)
  107. {
  108. [$labels, $unLabelIds] = $this->request->postMore([
  109. ['label_ids', []],
  110. ['un_label_ids', []]
  111. ], true);
  112. if (!count($labels) && !count($unLabelIds)) {
  113. return app('json')->fail(100100);
  114. }
  115. if ($services->setUserLable($uid, $labels) && $services->unUserLabel($uid, $unLabelIds)) {
  116. return app('json')->success(100014);
  117. } else {
  118. return app('json')->fail(100015);
  119. }
  120. }
  121. /**
  122. * 获取带分类的用户标签列表
  123. * @param \app\services\user\label\UserLabelCateServices $userLabelCateServices
  124. * @return mixed
  125. * @throws \think\db\exception\DataNotFoundException
  126. * @throws \think\db\exception\DbException
  127. * @throws \think\db\exception\ModelNotFoundException
  128. */
  129. public function tree_list(UserLabelCateServices $userLabelCateServices)
  130. {
  131. $cate = $userLabelCateServices->getLabelCateAll();
  132. $data = [];
  133. $label = [];
  134. if ($cate) {
  135. foreach ($cate as $value) {
  136. $data[] = [
  137. 'id' => $value['id'] ?? 0,
  138. 'value' => $value['id'] ?? 0,
  139. 'label_cate' => 0,
  140. 'label_name' => $value['name'] ?? '',
  141. 'label' => $value['name'] ?? '',
  142. 'store_id' => $value['store_id'] ?? 0,
  143. 'type' => $value['type'] ?? 1,
  144. ];
  145. }
  146. $label = $this->services->getList(['type' => 1]);
  147. $label = $label['list'] ?? [];
  148. if ($label) {
  149. foreach ($label as &$item) {
  150. $item['label'] = $item['label_name'];
  151. $item['value'] = $item['id'];
  152. }
  153. }
  154. }
  155. return app('json')->success($this->services->get_tree_children($data, $label));
  156. }
  157. }