WorkGroupChatMemberServices.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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\WorkGroupChatMemberDao;
  13. use app\services\BaseServices;
  14. use app\services\user\UserServices;
  15. use crmeb\traits\ServicesTrait;
  16. /**
  17. * 企业微信群成员
  18. * Class WorkGroupChatMemberServices
  19. * @package app\services\work
  20. * @mixin WorkGroupChatMemberDao
  21. */
  22. class WorkGroupChatMemberServices extends BaseServices
  23. {
  24. use ServicesTrait;
  25. /**
  26. * WorkGroupChatMemberServices constructor.
  27. * @param WorkGroupChatMemberDao $dao
  28. */
  29. public function __construct(WorkGroupChatMemberDao $dao)
  30. {
  31. $this->dao = $dao;
  32. }
  33. /**
  34. * 获取群成员
  35. * @param int $chatId
  36. * @param string $name
  37. * @return array
  38. */
  39. public function getChatMemberList(int $chatId, string $name = '')
  40. {
  41. [$page, $limit] = $this->getPageValue();
  42. $list = $this->dao->getDataList(['group_id' => $chatId, 'name_like' => $name, 'status' => 1], ['*'], $page, $limit, 'create_time', [
  43. 'member' => function ($query) {
  44. $query->field(['name', 'userid', 'gender', 'avatar', 'id', 'mobile']);
  45. },
  46. 'client' => function ($query) {
  47. $query->field(['name', 'external_userid', 'gender', 'avatar', 'id']);
  48. }
  49. ]);
  50. $clientId = $mobile = [];
  51. foreach ($list as &$item) {
  52. $item['group_chat_num'] = $this->dao->getChatSum($item['userid']);
  53. if ($item['group_chat_num'] > 0) {
  54. $item['group_chat_num']--;
  55. }
  56. if (isset($item['client']['id'])) {
  57. $clientId[] = $item['client']['id'];
  58. }
  59. if (isset($item['member'])) {
  60. $mobile[] = $item['member']['mobile'];
  61. }
  62. }
  63. /** @var WorkClientFollowServices $followService */
  64. $followService = app()->make(WorkClientFollowServices::class);
  65. $followList = $followService->getDataList(['client_id' => $clientId], ['id', 'client_id',], 0, 0, 'create_time', ['tags']);
  66. if ($followList) {
  67. foreach ($list as &$item) {
  68. $newTag = [];
  69. if (isset($item['client']['id'])) {
  70. foreach ($followList as $value) {
  71. if ($item['client']['id'] == $value['client_id'] && !empty($value['tags'])) {
  72. $newTag = array_column($value['tags'], 'tag_name');
  73. }
  74. }
  75. $item['tags'] = $newTag;
  76. }
  77. }
  78. }
  79. if ($mobile) {
  80. /** @var UserServices $userService */
  81. $userService = app()->make(UserServices::class);
  82. $userList = $userService->getList(['phone' => $mobile], 'phone,uid', 0, 0);
  83. if ($userList) {
  84. foreach ($list as &$item) {
  85. $newTag = [];
  86. if (isset($item['member'])) {
  87. foreach ($userList as $value) {
  88. if ($item['member']['mobile'] == $value['phone'] && !empty($value['label'])) {
  89. $newTag = array_column($value['label'], 'label_name');
  90. }
  91. }
  92. $item['tags'] = $newTag;
  93. }
  94. }
  95. }
  96. }
  97. $count = $this->dao->count(['group_id' => $chatId, 'name_like' => $name, 'status' => 1]);
  98. return compact('list', 'count');
  99. }
  100. }