GroupInfo.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. /**
  3. * 群组相关
  4. * @author solu
  5. */
  6. class GroupInfo extends Model {
  7. protected $tableName = 'group_info';
  8. protected $dbKey = 'dw_chat';
  9. const OFFICIAL_ID = 10000; // 官方群id
  10. /**
  11. * 加入群组
  12. * @author solu
  13. * @param $userId
  14. * @param $groupId
  15. * @param $group
  16. * @return bool
  17. * @throws Exception
  18. */
  19. public function joinGroup($userId, $groupId, $group = []) {
  20. $where = ['group_id' => $groupId];
  21. !$group && $group = $this->objTable->getRow($where);
  22. if (!$group) {
  23. throw new Exception('group not exists', CODE_PARAM_ERROR);
  24. }
  25. $objUserGroup = new TableHelper('user_group', 'dw_chat');
  26. if ($objUserGroup->getRow(['user_id' => $userId, 'group_id' => $groupId, 'state' => UserGroup::STATE_IN_GROUP])) {
  27. throw new Exception('already joined this group', CODE_PARAM_ERROR);
  28. }
  29. $objSession = new Session();
  30. $objUserGroup->autoCommit(false);
  31. $data = [
  32. 'user_id' => $userId,
  33. 'group_id' => $groupId,
  34. 'join_time' => NOW,
  35. 'state' => UserGroup::STATE_IN_GROUP,
  36. ];
  37. if (!$objUserGroup->replaceObject($data)) {
  38. throw new Exception('join fail', CODE_DB_ERROR);
  39. }
  40. $memberNum = $group['member_num'] + 1;
  41. $where['member_num'] = $group['member_num'];
  42. if (!$this->objTable->updateObject(['member_num' => $memberNum], $where)) {
  43. throw new Exception('update group info fail', CODE_DB_ERROR);
  44. }
  45. $sessData = [
  46. 'user_id' => $userId,
  47. 'session_id' => $groupId,
  48. 'is_group' => 1,
  49. ];
  50. if (!$objSession->objTable->replaceObject($sessData)) {
  51. throw new Exception('create session error', CODE_DB_ERROR);
  52. }
  53. $objUserGroup->commit();
  54. // 给自己发消息订阅新群组
  55. ThirdApi::pushPersonEvent($userId, [
  56. 'type' => 'join_group',
  57. 'group_id' => $groupId,
  58. ]);
  59. $user_info = User::getUserInfoById($userId);
  60. // 给群组发消息有人加入了
  61. ThirdApi::pushGroupEvent($groupId, [
  62. 'type' => 'join',
  63. 'group_id' => $groupId,
  64. 'user_info' => $user_info,
  65. ]);
  66. return true;
  67. }
  68. /**
  69. * 加入群组
  70. * @author solu
  71. * @param $userId
  72. * @param $groupId
  73. * @param $group
  74. * @return bool
  75. * @throws Exception
  76. */
  77. public function appendToGroup($userIds, $groupId) {
  78. $userGroupDatas = [];
  79. $sessDatas = [];
  80. foreach ($userIds as $userId) {
  81. $userGroupDatas[] = [
  82. 'user_id' => $userId,
  83. 'group_id' => $groupId,
  84. 'join_time' => NOW,
  85. 'state' => UserGroup::STATE_IN_GROUP,
  86. ];
  87. $sessDatas[] = [
  88. 'user_id' => $userId,
  89. 'session_id' => $groupId,
  90. 'is_group' => 1,
  91. ];
  92. }
  93. $objUserGroup = new UserGroup();
  94. $objUserGroup->objTable->replaceObjects2($userGroupDatas);
  95. $objSession = new Session();
  96. $objSession->objTable->replaceObjects2($sessDatas);
  97. $where = ['group_id' => $groupId];
  98. $newData = [
  99. 'member_num' => count($userIds),
  100. 'group_name' => $groupId,
  101. ];
  102. $this->objTable->updateObject($newData, $where);
  103. }
  104. /**
  105. * 离开群组
  106. * @author solu
  107. * @param $userId
  108. * @param $groupId
  109. * @param $group
  110. * @return bool
  111. * @throws Exception
  112. */
  113. public function leaveGroup($userId, $groupId, $group = []) {
  114. $groupId = (int) $groupId;
  115. $where = ['group_id' => $groupId];
  116. !$group && $group = $this->objTable->getRow($where);
  117. if (!$group) {
  118. throw new Exception('group not exists', CODE_PARAM_ERROR);
  119. }
  120. $objUserGroup = new TableHelper('user_group', 'dw_chat');
  121. if (!$objUserGroup->getRow(['user_id' => $userId, 'group_id' => $groupId, 'state' => UserGroup::STATE_IN_GROUP])) {
  122. throw new Exception('not in group', CODE_PARAM_ERROR);
  123. }
  124. $objSession = new Session();
  125. $objUserGroup->autoCommit(false);
  126. $ugWhere = [
  127. 'user_id' => $userId,
  128. 'group_id' => $groupId,
  129. ];
  130. if (!$objUserGroup->updateObject(['state' => UserGroup::STATE_LEAVE], $ugWhere)) {
  131. throw new Exception('leave fail', CODE_DB_ERROR);
  132. }
  133. $sql = "UPDATE group_info SET member_num = member_num - 1 WHERE group_id = {$groupId}";
  134. $this->objDb->update($sql);
  135. // $memberNum = $group['member_num'] - 1;
  136. // $where['member_num'] = $group['member_num'];
  137. // if (!$this->objTable->updateObject(['member_num' => $memberNum], $where)) {
  138. // throw new Exception('update group info fail', CODE_DB_ERROR);
  139. // }
  140. $sessWhere = [
  141. 'user_id' => $userId,
  142. 'session_id' => $groupId,
  143. ];
  144. if (!$objSession->objTable->delObject($sessWhere)) {
  145. throw new Exception('delete session error', CODE_DB_ERROR);
  146. }
  147. $objUserGroup->commit();
  148. // 给自己发消息订阅新群组
  149. ThirdApi::pushPersonEvent($userId, [
  150. 'type' => 'leave_group',
  151. 'group_id' => $groupId,
  152. ]);
  153. $user_info = User::getUserInfoById($userId);
  154. // 给群组发消息有人离开了
  155. ThirdApi::pushGroupEvent($groupId, [
  156. 'type' => 'leave',
  157. 'group_id' => $groupId,
  158. 'user_id' => $userId,
  159. 'user_info' => $user_info,
  160. ]);
  161. return true;
  162. }
  163. /**
  164. * 更新信息
  165. * @author solu
  166. * @param $userId
  167. * @param $groupId
  168. * @param array $data
  169. * @return bool|int
  170. * @throws Exception
  171. */
  172. public function setData($userId, $groupId, array $data) {
  173. if (!(new UserGroup())->isAdmin($groupId, $userId)) {
  174. throw new Exception('no permission', CODE_NO_PERMITION);
  175. }
  176. $data['update_time'] = NOW;
  177. return $this->objTable->updateObject($data, ['group_id' => $groupId]);
  178. }
  179. /**
  180. * 分享连接
  181. * @author solu
  182. * @param string $name
  183. * @return string
  184. */
  185. public static function genInviteUrl($name) {
  186. if (!$name) {
  187. return '';
  188. }
  189. return sprintf('%s/s/%s', URL_SELF, $name);
  190. }
  191. }