|
5 | 5 |
|
6 | 6 | use App\Constants\Laboratory\ChatRedisKey; |
7 | 7 | use App\Constants\Laboratory\WsMessage; |
| 8 | +use App\Model\Auth\User; |
8 | 9 | use App\Model\Laboratory\FriendChatHistory; |
9 | 10 | use App\Model\Laboratory\FriendRelation; |
10 | 11 | use App\Model\Laboratory\GroupChatHistory; |
11 | 12 | use App\Pool\Redis; |
12 | 13 | use App\Service\Laboratory\FriendService; |
13 | 14 | use App\Service\Laboratory\MessageService; |
| 15 | +use Hyperf\Database\Model\Model; |
14 | 16 | use Hyperf\Di\Annotation\Inject; |
15 | 17 |
|
16 | 18 | /** |
@@ -146,4 +148,83 @@ function forwardMessage(array $userInfo, array $user, array $content) |
146 | 148 | } |
147 | 149 | } |
148 | 150 |
|
| 151 | + /** |
| 152 | + * 维护好友关系 |
| 153 | + * @param Model $model |
| 154 | + * @return bool |
| 155 | + */ |
| 156 | + public function maintainFriendRelation(Model $model) |
| 157 | + { |
| 158 | + $userList = User::query()->where('id', '!=', $model['id'])->get()->pluck('id'); |
| 159 | + |
| 160 | + foreach ($userList as $user_id) { |
| 161 | + FriendRelation::insert([ |
| 162 | + 'uid' => $user_id, |
| 163 | + 'friend_id' => $model['id'], |
| 164 | + 'created_at' => date('Y-m-d H:i:s'), |
| 165 | + 'updated_at' => date('Y-m-d H:i:s'), |
| 166 | + ]); |
| 167 | + } |
| 168 | + |
| 169 | + //组装联系人信息 |
| 170 | + $contact['id'] = $model['id']; |
| 171 | + $contact['displayName'] = $model['desc']; |
| 172 | + $contact['avatar'] = $model['avatar']; |
| 173 | + $contact['index'] = $model['desc']; |
| 174 | + $contact['unread'] = 0; |
| 175 | + $contact['lastSendTime'] = 0; |
| 176 | + $contact['lastContent'] = ''; |
| 177 | + $contact['is_group'] = 0; |
| 178 | + $contact['status'] = FriendRelation::FRIEND_ONLINE_STATUS_NO; |
| 179 | + |
| 180 | + //获取在线用户 |
| 181 | + $fdList = FriendService::getInstance()->getOnlineFriendList($model->toArray()); |
| 182 | + //组装消息 |
| 183 | + $message['id'] = generate_rand_id(); |
| 184 | + $message['status'] = FriendChatHistory::FRIEND_CHAT_MESSAGE_STATUS_SUCCEED; |
| 185 | + $message['type'] = FriendChatHistory::FRIEND_CHAT_MESSAGE_TYPE_EVENT; |
| 186 | + $message['sendTime'] = time() * 1000; |
| 187 | + $message['event'] = WsMessage::MESSAGE_TYPE_NEW_FRIEND_JOIN; |
| 188 | + $message['contact'] = $contact; |
| 189 | + |
| 190 | + foreach ($fdList as $key => $value) { |
| 191 | + $sendMessage = [ |
| 192 | + 'message' => $message, |
| 193 | + 'event' => WsMessage::MESSAGE_TYPE_NEW_FRIEND_JOIN |
| 194 | + ]; |
| 195 | + $this->sender->push((int) $value['fd'], json_encode($sendMessage)); |
| 196 | + } |
| 197 | + return true; |
| 198 | + } |
| 199 | + |
| 200 | + /** |
| 201 | + * 删除好友关系 |
| 202 | + * @param Model $model |
| 203 | + * @return bool |
| 204 | + */ |
| 205 | + public function deleteContactEvent(Model $model) |
| 206 | + { |
| 207 | + $userList = User::query()->where('id', '!=', $model['id'])->get()->pluck('id'); |
| 208 | + |
| 209 | + //维护好友关系 |
| 210 | + FriendRelation::query()->where('uid', $model['id'])->orWhere('friend_id', $model['id'])->delete(); |
| 211 | + //获取在线用户 |
| 212 | + $fdList = FriendService::getInstance()->getOnlineFriendList($model->toArray()); |
| 213 | + //组装消息 |
| 214 | + $message['id'] = generate_rand_id(); |
| 215 | + $message['status'] = FriendChatHistory::FRIEND_CHAT_MESSAGE_STATUS_SUCCEED; |
| 216 | + $message['type'] = FriendChatHistory::FRIEND_CHAT_MESSAGE_TYPE_EVENT; |
| 217 | + $message['sendTime'] = time() * 1000; |
| 218 | + $message['event'] = WsMessage::MESSAGE_TYPE_FRIEND_DELETE; |
| 219 | + $message['contact_id'] = $model['id']; |
| 220 | + |
| 221 | + foreach ($fdList as $key => $value) { |
| 222 | + $sendMessage = [ |
| 223 | + 'message' => $message, |
| 224 | + 'event' => WsMessage::MESSAGE_TYPE_FRIEND_DELETE |
| 225 | + ]; |
| 226 | + $this->sender->push((int) $value['fd'], json_encode($sendMessage)); |
| 227 | + } |
| 228 | + return true; |
| 229 | + } |
149 | 230 | } |
0 commit comments