UserLabel.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. namespace app\controller\admin\v1\user;
  12. use app\controller\admin\AuthController;
  13. use app\services\user\label\UserLabelCateServices;
  14. use app\services\user\label\UserLabelRelationServices;
  15. use app\services\user\label\UserLabelServices;
  16. use crmeb\services\wechat\config\WorkConfig;
  17. use think\facade\App;
  18. /**
  19. * 用户标签控制器
  20. * Class UserLabel
  21. * @package app\controller\admin\v1\user
  22. */
  23. class UserLabel extends AuthController
  24. {
  25. /**
  26. * UserLabel constructor.
  27. * @param App $app
  28. * @param UserLabelServices $service
  29. */
  30. public function __construct(App $app, UserLabelServices $service)
  31. {
  32. parent::__construct($app);
  33. $this->services = $service;
  34. }
  35. /**
  36. * 标签列表
  37. * @return mixed
  38. */
  39. public function index($label_cate = 0)
  40. {
  41. return $this->success($this->services->getList(['label_cate' => $label_cate, 'type' => 0]));
  42. }
  43. /**
  44. * 获取带分类的用户标签列表
  45. * @param UserLabelCateServices $userLabelCateServices
  46. * @return mixed
  47. * @throws \think\db\exception\DataNotFoundException
  48. * @throws \think\db\exception\DbException
  49. * @throws \think\db\exception\ModelNotFoundException
  50. */
  51. public function tree_list(UserLabelCateServices $userLabelCateServices)
  52. {
  53. $cate = $userLabelCateServices->getLabelCateAll();
  54. $data = [];
  55. $label = [];
  56. if ($cate) {
  57. foreach ($cate as $value) {
  58. $data[] = [
  59. 'id' => $value['id'] ?? 0,
  60. 'value' => $value['id'] ?? 0,
  61. 'label_cate' => 0,
  62. 'label_name' => $value['name'] ?? '',
  63. 'label' => $value['name'] ?? '',
  64. 'store_id' => $value['store_id'] ?? 0,
  65. 'type' => $value['type'] ?? 1,
  66. ];
  67. }
  68. $label = $this->services->getList(['type' => 0]);
  69. $label = $label['list'] ?? [];
  70. if ($label) {
  71. foreach ($label as &$item) {
  72. $item['label'] = $item['label_name'];
  73. $item['value'] = $item['id'];
  74. }
  75. }
  76. }
  77. return $this->success($this->services->get_tree_children($data, $label));
  78. }
  79. /**
  80. * 添加修改标签表单
  81. * @return mixed
  82. * @throws \FormBuilder\Exception\FormBuilderException
  83. */
  84. public function add()
  85. {
  86. [$id, $label_cate] = $this->request->getMore([
  87. ['id', 0],
  88. ['label_cate', 0]
  89. ], true);
  90. return $this->success($this->services->add((int)$id, 0, 0, $label_cate));
  91. }
  92. /**
  93. * 保存标签表单数据
  94. * @param int $id
  95. * @return mixed
  96. */
  97. public function save()
  98. {
  99. $data = $this->request->postMore([
  100. ['id', 0],
  101. ['label_cate', 0],
  102. ['label_name', ''],
  103. ]);
  104. if (!$data['label_name'] = trim($data['label_name'])) return $this->fail('会员标签不能为空!');
  105. $this->services->save((int)$data['id'], $data);
  106. return $this->success('保存成功');
  107. }
  108. /**
  109. * 删除
  110. * @param $id
  111. * @throws \Exception
  112. */
  113. public function delete()
  114. {
  115. [$id] = $this->request->getMore([
  116. ['id', 0],
  117. ], true);
  118. if (!$id) return $this->fail('数据不存在');
  119. $this->services->delLabel((int)$id);
  120. return $this->success('刪除成功!');
  121. }
  122. /**
  123. * 标签分类
  124. * @param UserLabelCateServices $services
  125. * @return mixed
  126. */
  127. public function getUserLabel(UserLabelCateServices $services, $uid)
  128. {
  129. [$uids, $all, $where] = $this->request->postMore([
  130. ['uids', []],
  131. ['all', 0],
  132. ['where', ""],
  133. ], true);
  134. if (count($uids) == 1) {
  135. $uid = $uids[0] ?? 0;
  136. }
  137. return $this->success($services->getUserLabel((int)$uid));
  138. }
  139. /**
  140. * 设置用户标签
  141. * @param UserLabelRelationServices $services
  142. * @param $uid
  143. * @return mixed
  144. */
  145. public function setUserLabel(UserLabelRelationServices $services, $uid)
  146. {
  147. [$labels, $unLabelIds] = $this->request->postMore([
  148. ['label_ids', []],
  149. ['un_label_ids', []]
  150. ], true);
  151. if (!count($labels) && !count($unLabelIds)) {
  152. return $this->fail('请先添加用户标签');
  153. }
  154. if ($services->setUserLable($uid, $labels) && $services->unUserLabel($uid, $unLabelIds)) {
  155. return $this->success('设置成功');
  156. } else {
  157. return $this->fail('设置失败');
  158. }
  159. }
  160. /**
  161. * 同步客户标签
  162. * @param WorkConfig $config
  163. * @return mixed
  164. * @throws \think\db\exception\DataNotFoundException
  165. * @throws \think\db\exception\DbException
  166. * @throws \think\db\exception\ModelNotFoundException
  167. */
  168. public function synchroWorkLabel(WorkConfig $config)
  169. {
  170. if (!$config->get('corpId')) {
  171. return $this->fail('请先配置企业微信ID');
  172. }
  173. $config = $config->getAppConfig(WorkConfig::TYPE_USER);
  174. if (empty($config['secret'])) {
  175. return $this->fail('请先配置企业微信客户secret');
  176. }
  177. if ($this->services->authWorkClientLabel()) {
  178. return $this->success('已加入消息队列,进行同步。请稍等片刻');
  179. } else {
  180. return $this->fail('加入消息队列失败');
  181. }
  182. }
  183. }