UserGroup.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /**
  3. * 用户群组相关
  4. * @author solu
  5. */
  6. class UserGroup extends Model {
  7. protected $tableName = 'user_group';
  8. protected $dbKey = 'dw_chat';
  9. const STATE_APPLY = 0; // 申请中
  10. const STATE_IN_GROUP = 1; // 在群里
  11. const STATE_LEAVE = 2; // 离开群
  12. /**
  13. * 检查是否是管理员
  14. * @author benzhan
  15. * @param $group_id
  16. * @param $user_id
  17. * @return array
  18. */
  19. public function isAdmin($group_id, $user_id) {
  20. $state = self::STATE_IN_GROUP;
  21. $row = $this->objTable->getRow(compact('user_id', 'group_id', 'state'));
  22. return $row['is_admin'];
  23. }
  24. /**
  25. * 是否被禁言
  26. * @author solu
  27. * @param $group_id
  28. * @param $user_id
  29. * @return bool
  30. */
  31. public function isBlock($group_id, $user_id) {
  32. $row = $this->objTable->getRow(compact('user_id', 'group_id'));
  33. return !!$row['is_block'];
  34. }
  35. /**
  36. * 设置封禁用户
  37. * @param $group_id
  38. * @param $user_id
  39. * @param int $is_block
  40. */
  41. public function setBlock($group_id, $user_id, $is_block = 1) {
  42. // $this->objTable->updateObject(compact('is_block'), compact('user_id', 'group_id'));
  43. $this->setData($group_id, $user_id, compact('is_block'));
  44. }
  45. /**
  46. * 设置数据
  47. * @param $group_id
  48. * @param $user_id
  49. * @param array $data
  50. */
  51. public function setData($group_id, $user_id, $data = []) {
  52. $data['update_time'] = NOW;
  53. return $this->objTable->updateObject($data, compact('user_id', 'group_id'));
  54. }
  55. /**
  56. * 在群
  57. * @author solu
  58. * @param $groupId
  59. * @param $userId
  60. * @return bool
  61. */
  62. public function inGroup($groupId, $userId) {
  63. return $this->objTable->getCount(['user_id' => $userId, 'group_id' => $groupId, 'state' => self::STATE_IN_GROUP]) > 0;
  64. }
  65. public function getUserIdListSortByAdminAndJoinTime($groupId, $limit = 10) {
  66. return $this->objTable->getCol(['group_id' => $groupId, 'state' => self::STATE_IN_GROUP], [
  67. '_field' => 'user_id',
  68. '_sortKey' => 'is_admin DESC, join_time ASC',
  69. '_limit' => $limit,
  70. ]);
  71. }
  72. }