UserLabel.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 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. * @return mixed
  37. */
  38. public function index($label_cate = 0)
  39. {
  40. return app('json')->success($this->services->getList(['label_cate' => $label_cate]));
  41. }
  42. /**
  43. * 添加修改标签表单
  44. * @return mixed
  45. * @throws \FormBuilder\Exception\FormBuilderException
  46. */
  47. public function add()
  48. {
  49. [$id, $cateId] = $this->request->getMore([
  50. ['id', 0],
  51. ['cate_id', 0],
  52. ], true);
  53. return app('json')->success($this->services->add((int)$id, (int)$cateId));
  54. }
  55. /**
  56. * 保存标签表单数据
  57. * @param int $id
  58. * @return mixed
  59. */
  60. public function save()
  61. {
  62. $data = $this->request->postMore([
  63. ['id', 0],
  64. ['label_cate', 0],
  65. ['label_name', ''],
  66. ]);
  67. if (!$data['label_name'] = trim($data['label_name'])) return app('json')->fail(400322);
  68. $this->services->save((int)$data['id'], $data);
  69. return app('json')->success(100000);
  70. }
  71. /**
  72. * 删除
  73. * @param $id
  74. * @throws \Exception
  75. */
  76. public function delete()
  77. {
  78. list($id) = $this->request->getMore([
  79. ['id', 0],
  80. ], true);
  81. if (!$id) return app('json')->fail(100100);
  82. $this->services->delLabel((int)$id);
  83. return app('json')->success(100002);
  84. }
  85. /**
  86. * 标签分类
  87. * @param UserLabelCateServices $services
  88. * @return mixed
  89. */
  90. public function getUserLabel(UserLabelCateServices $services, $uid)
  91. {
  92. return app('json')->success($services->getUserLabel((int)$uid));
  93. }
  94. /**
  95. * 设置用户标签
  96. * @param UserLabelRelationServices $services
  97. * @param $uid
  98. * @return mixed
  99. */
  100. public function setUserLabel(UserLabelRelationServices $services, $uid)
  101. {
  102. [$labels, $unLabelIds] = $this->request->postMore([
  103. ['label_ids', []],
  104. ['un_label_ids', []]
  105. ], true);
  106. if (!count($labels) && !count($unLabelIds)) {
  107. return app('json')->fail(100100);
  108. }
  109. if ($services->setUserLable($uid, $labels) && $services->unUserLabel($uid, $unLabelIds)) {
  110. return app('json')->success(100014);
  111. } else {
  112. return app('json')->fail(100015);
  113. }
  114. }
  115. /**
  116. * 获取带分类的用户标签列表
  117. * @param \app\services\user\label\UserLabelCateServices $userLabelCateServices
  118. * @return mixed
  119. * @throws \think\db\exception\DataNotFoundException
  120. * @throws \think\db\exception\DbException
  121. * @throws \think\db\exception\ModelNotFoundException
  122. */
  123. public function tree_list(UserLabelCateServices $userLabelCateServices)
  124. {
  125. $cate = $userLabelCateServices->getLabelCateAll();
  126. $data = [];
  127. $label = [];
  128. if ($cate) {
  129. foreach ($cate as $value) {
  130. $data[] = [
  131. 'id' => $value['id'] ?? 0,
  132. 'value' => $value['id'] ?? 0,
  133. 'label_cate' => 0,
  134. 'label_name' => $value['name'] ?? '',
  135. 'label' => $value['name'] ?? '',
  136. 'store_id' => $value['store_id'] ?? 0,
  137. 'type' => $value['type'] ?? 1,
  138. ];
  139. }
  140. $label = $this->services->getList(['type' => 1]);
  141. $label = $label['list'] ?? [];
  142. if ($label) {
  143. foreach ($label as &$item) {
  144. $item['label'] = $item['label_name'];
  145. $item['value'] = $item['id'];
  146. }
  147. }
  148. }
  149. return app('json')->success($this->services->get_tree_children($data, $label));
  150. }
  151. }