WorkGroupChatAuthServices.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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\services\work;
  12. use app\dao\work\WorkGroupChatAuthDao;
  13. use app\services\BaseServices;
  14. use app\services\user\label\UserLabelServices;
  15. use crmeb\services\wechat\Work;
  16. use crmeb\traits\service\ContactWayQrCode;
  17. use crmeb\traits\ServicesTrait;
  18. use think\exception\ValidateException;
  19. /**
  20. * 企业微信自动拉群
  21. * Class WorkGroupChatAuthServices
  22. * @package app\services\work
  23. * @mixin WorkGroupChatAuthDao
  24. */
  25. class WorkGroupChatAuthServices extends BaseServices
  26. {
  27. use ServicesTrait, ContactWayQrCode;
  28. /**
  29. * WorkGroupChatAuthServices constructor.
  30. * @param WorkGroupChatAuthDao $dao
  31. */
  32. public function __construct(WorkGroupChatAuthDao $dao)
  33. {
  34. $this->dao = $dao;
  35. }
  36. /**
  37. * @param array $where
  38. * @return array
  39. * @throws \think\db\exception\DataNotFoundException
  40. * @throws \think\db\exception\DbException
  41. * @throws \think\db\exception\ModelNotFoundException
  42. */
  43. public function getList(array $where)
  44. {
  45. [$page, $limit] = $this->getPageValue();
  46. $list = $this->dao->getDataList($where, ['*'], $page, $limit, 'create_time');
  47. $chatIds = [];
  48. $labels = [];
  49. foreach ($list as $item) {
  50. $chatIds = array_merge($chatIds, $item['chat_id']);
  51. $labels = array_merge($labels, $item['label'] ?? []);
  52. }
  53. $chatIds = array_merge(array_unique(array_filter($chatIds)));
  54. $labels = array_merge(array_unique(array_filter($labels)));
  55. /** @var UserLabelServices $userLabelService */
  56. $userLabelService = app()->make(UserLabelServices::class);
  57. $labelList = $userLabelService->getColumn([
  58. ['tag_id', 'in', $labels]
  59. ], 'label_name', 'tag_id');
  60. /** @var WorkGroupChatServices $service */
  61. $service = app()->make(WorkGroupChatServices::class);
  62. $chatList = $service->getColumn([
  63. ['chat_id', 'in', $chatIds]
  64. ], 'name', 'chat_id');
  65. foreach ($list as &$item) {
  66. $chatNewList = $labelNewList = [];
  67. foreach ($chatList as $key => $val) {
  68. if (in_array($key, $item['chat_id'])) {
  69. $chatNewList[] = ['name' => $val, 'chat_id' => $key];
  70. }
  71. }
  72. if ($item['label']) {
  73. foreach ($labelList as $k => $v) {
  74. if (in_array($k, $item['label'])) {
  75. $labelNewList[] = ['name' => $v, 'label_id' => $k];
  76. }
  77. }
  78. }
  79. $item['chat_list'] = $chatNewList;
  80. $item['label_list'] = $labelNewList;
  81. }
  82. $count = $this->dao->count($where);
  83. return compact('list', 'count');
  84. }
  85. /**
  86. * 保存或者修改自动拉群
  87. * @param array $data
  88. * @param int $id
  89. * @return mixed
  90. */
  91. public function saveGroupChatAuth(array $data, int $id = 0)
  92. {
  93. return $this->transaction(function () use ($data, $id) {
  94. if ($id) {
  95. $authInfo = $this->dao->get(['id' => $id], ['config_id']);
  96. $this->dao->update($id, $data);
  97. $this->handleGroupChat($data, $id, $authInfo->config_id);
  98. } else {
  99. $res = $this->dao->save($data);
  100. $id = $res->id;
  101. $this->handleGroupChat($data, $id);
  102. }
  103. return $id;
  104. });
  105. }
  106. /**
  107. * 配置加入群聊并获取二维码
  108. * @param string $configId
  109. * @param array $data
  110. * @param int $id
  111. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  112. * @throws \GuzzleHttp\Exception\GuzzleException
  113. */
  114. protected function handleGroupChat(array $data, int $id, string $configId = '')
  115. {
  116. //设置进群配置
  117. if ($configId) {
  118. $qrCode = Work::groupChat()->updateJoinWay($configId, $data['group_name'], $data['chat_id'], 'groupChat-' . $id, (int)$data['auth_group_chat'], (int)$data['group_num']);
  119. } else {
  120. $qrCode = Work::groupChat()->addJoinWay($data['group_name'], $data['chat_id'], 'groupChat-' . $id, (int)$data['auth_group_chat'], (int)$data['group_num']);
  121. }
  122. if (0 !== $qrCode['errcode']) {
  123. throw new ValidateException($qrCode['errmsg']);
  124. }
  125. if (!$configId) {
  126. $this->dao->update($id, ['config_id' => $qrCode['config_id']]);
  127. $configId = $qrCode['config_id'];
  128. }
  129. //获取群二维码
  130. $qrCodeInfo = Work::groupChat()->getJoinWay($configId);
  131. if (0 !== $qrCodeInfo['errcode']) {
  132. throw new ValidateException($qrCodeInfo['errmsg']);
  133. }
  134. $this->dao->update($id, ['qr_code' => $qrCodeInfo['join_way']['qr_code']]);
  135. }
  136. /**
  137. * 删除客户进群配置
  138. * @param int $id
  139. * @return bool
  140. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  141. * @throws \GuzzleHttp\Exception\GuzzleException
  142. * @throws \think\db\exception\DataNotFoundException
  143. * @throws \think\db\exception\DbException
  144. * @throws \think\db\exception\ModelNotFoundException
  145. */
  146. public function deleteGroupChatAuth(int $id)
  147. {
  148. $groupChatAuthInfo = $this->dao->get($id);
  149. if (!$groupChatAuthInfo) {
  150. throw new ValidateException('删除的群聊配置不存在');
  151. }
  152. //删除入群配置
  153. if ($groupChatAuthInfo->config_id) {
  154. $qrCode = Work::groupChat()->deleteJoinWay($groupChatAuthInfo->config_id);
  155. if (0 !== $qrCode['errcode']) {
  156. throw new ValidateException($qrCode['errmsg']);
  157. }
  158. }
  159. return $this->dao->destroy($id);
  160. }
  161. /**
  162. * 获取群配置
  163. * @param int $id
  164. * @return array
  165. * @throws \think\db\exception\DataNotFoundException
  166. * @throws \think\db\exception\DbException
  167. * @throws \think\db\exception\ModelNotFoundException
  168. */
  169. public function getGrouChatAuthInfo(int $id)
  170. {
  171. $groupChatAuthInfo = $this->dao->get($id);
  172. if (!$groupChatAuthInfo) {
  173. throw new ValidateException('删除的群聊配置不存在');
  174. }
  175. $groupChatAuthInfo['labelList'] = $groupChatAuthInfo['chatList'] = [];
  176. if ($groupChatAuthInfo->label) {
  177. /** @var UserLabelServices $userLabelService */
  178. $userLabelService = app()->make(UserLabelServices::class);
  179. $groupChatAuthInfo['labelList'] = $userLabelService->getColumn([
  180. ['tag_id', 'in', $groupChatAuthInfo->label]
  181. ], 'label_name,tag_id');
  182. }
  183. if ($groupChatAuthInfo->chat_id) {
  184. /** @var WorkGroupChatServices $service */
  185. $service = app()->make(WorkGroupChatServices::class);
  186. $groupChatAuthInfo['chatList'] = $service->getColumn([
  187. ['chat_id', 'in', $groupChatAuthInfo->chat_id]
  188. ], 'name,chat_id');
  189. }
  190. return $groupChatAuthInfo->toArray();
  191. }
  192. /**
  193. *
  194. * @param int $groupAuthId
  195. * @param string $userid
  196. * @param string $externalUserID
  197. */
  198. public function clientAddLabel(int $groupAuthId, string $userid, string $externalUserID)
  199. {
  200. $label = $this->dao->value(['id' => $groupAuthId], 'label');
  201. $resTage = Work::markTags($userid, $externalUserID, $label);
  202. if (0 !== $resTage['errcode']) {
  203. throw new ValidateException($resTage['errmsg']);
  204. }
  205. }
  206. }