Skip to content

Commit a595f43

Browse files
committed
增加好友分组
1 parent e116194 commit a595f43

File tree

7 files changed

+199
-38
lines changed

7 files changed

+199
-38
lines changed

app/Constants/Laboratory/WsMessage.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,14 @@ class WsMessage extends AbstractConstants
5959
* @Message("群聊撤回消息")
6060
*/
6161
const MESSAGE_TYPE_GROUP_WITHDRAW_MESSAGE = 'group_withdraw_message';
62+
63+
/**
64+
* @Message("新用户加入")
65+
*/
66+
const MESSAGE_TYPE_NEW_FRIEND_JOIN = 'new_friend_join_message';
67+
68+
/**
69+
* @Message("用户删除")
70+
*/
71+
const MESSAGE_TYPE_FRIEND_DELETE = 'friend_delete_message';
6272
}

app/Model/Auth/User.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,23 @@
44

55
namespace App\Model\Auth;
66

7+
use App\Model\Laboratory\FriendRelation;
78
use App\Model\Model;
9+
use App\Task\Laboratory\FriendWsTask;
810
use Donjan\Permission\Traits\HasRoles;
11+
use Hyperf\Database\Model\Events\Created;
12+
use Hyperf\Database\Model\Events\Deleted;
13+
use Hyperf\Di\Annotation\Inject;
14+
use Psr\Container\ContainerInterface;
915

1016
class User extends Model
1117
{
18+
/**
19+
* @Inject()
20+
* @var ContainerInterface
21+
*/
22+
protected $container;
23+
1224
use HasRoles;
1325
/**
1426
* The table associated with the model.
@@ -64,4 +76,36 @@ static function getOneByUid($id)
6476

6577
return $query->first();
6678
}
79+
80+
/**
81+
* 监听用户添加事件
82+
* @param Created $event
83+
*/
84+
public function created(Created $event)
85+
{
86+
$currentUser = $event->getModel();
87+
$userList = User::query()->where('id', '!=', $currentUser['id'])->get()->pluck('id');
88+
89+
foreach ($userList as $user_id) {
90+
FriendRelation::insert([
91+
'uid' => $currentUser['id'],
92+
'friend_id' => $user_id,
93+
'created_at' => date('Y-m-d H:i:s'),
94+
'updated_at' => date('Y-m-d H:i:s'),
95+
]);
96+
}
97+
//维护其他用户好友关系
98+
$this->container->get(FriendWsTask::class)->maintainFriendRelation($currentUser);
99+
}
100+
101+
/**
102+
* 监听用户删除事件
103+
* @param Deleted $event
104+
*/
105+
public function deleted(Deleted $event)
106+
{
107+
$currentUser = $event->getModel();
108+
//维护其他用户好友关系
109+
$this->container->get(FriendWsTask::class)->deleteContactEvent($currentUser);
110+
}
67111
}

app/Model/Laboratory/FriendChatHistory.php

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,16 @@
55
namespace App\Model\Laboratory;
66

77
use App\Model\Model;
8+
use App\Pool\Redis;
89
use phpDocumentor\Reflection\Types\Boolean;
910

11+
/**
12+
* 好友消息
13+
* Class FriendChatHistory
14+
* @package App\Model\Laboratory
15+
* @Author YiYuan-Lin
16+
* @Date: 2021/7/8
17+
*/
1018
class FriendChatHistory extends Model
1119
{
1220
/**
@@ -59,6 +67,11 @@ class FriendChatHistory extends Model
5967
const FRIEND_CHAT_MESSAGE_STATUS_SUCCEED = 'succeed';
6068
const FRIEND_CHAT_MESSAGE_STATUS_FAILED = 'failed';
6169

70+
/**
71+
* 好友消息容器
72+
*/
73+
const FRIEND_MESSAGE_CONTAINER_REDIS_KEY = 'FRIEND_MESSAGE_CONTAINER_BY_USER_';
74+
6275

6376
/**
6477
* 添加聊天记录
@@ -82,7 +95,29 @@ static function addMessage(array $message, int $receptionState = 0)
8295
$model->to_uid = $message['toContactId'];
8396
$model->from_uid = $message['fromUser']['id'] ?? 0;
8497
$model->reception_state = $receptionState;
98+
$model->save();
99+
100+
//添加消息到好友容器中
101+
//self::addMessageToContainer($message['id']);
85102

86-
return $model->save();
103+
return true;
87104
}
105+
106+
/**
107+
* 将消息添加到个人消息容器中
108+
* @param $messageId
109+
* @return bool
110+
*/
111+
// static function addMessageToContainer(string $messageId)
112+
// {
113+
// if (empty($messageId)) return false;
114+
//
115+
// $messageInfo = static::query()->where('message_id', $messageId)->first();
116+
// if (!empty($messageId)) {
117+
// Redis::getInstance()->hset(self::FRIEND_MESSAGE_CONTAINER_REDIS_KEY . $messageInfo['from_uid'], $messageId, json_encode($messageInfo));
118+
// Redis::getInstance()->hset(self::FRIEND_MESSAGE_CONTAINER_REDIS_KEY . $messageInfo['to_uid'], $messageId, json_encode($messageInfo));
119+
// }
120+
//
121+
// return true;
122+
// }
88123
}

app/Model/Laboratory/FriendRelation.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,12 @@ class FriendRelation extends Model
4343
*/
4444
const FRIEND_ONLINE_STATUS = 1;
4545
const FRIEND_ONLINE_STATUS_NO = 0;
46+
47+
/**
48+
* 与user表关联
49+
* @return \Hyperf\Database\Model\Relations\BelongsTo
50+
*/
51+
public function getUser() {
52+
return $this->belongsTo('App\Model\Auth\User', 'friend_id', 'id');
53+
}
4654
}

app/Service/Laboratory/InitService.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use App\Foundation\Traits\Singleton;
77
use App\Model\Auth\User;
88
use App\Model\Laboratory\FriendChatHistory;
9+
use App\Model\Laboratory\FriendGroup;
910
use App\Model\Laboratory\FriendRelation;
1011
use App\Model\Laboratory\Group;
1112
use App\Model\Laboratory\GroupChatHistory;
@@ -39,17 +40,30 @@ public function initialization() : array
3940
$returnUserInfo['avatar'] = $userInfo['avatar'];
4041

4142
//获取用户联系人
42-
$userList = User::query()->where('id', '!=', $userInfo['id'])->get()->toArray();
43+
$userListQuery = FriendRelation::query();
44+
$userListQuery->where('uid', $userInfo['id']);
45+
$userListQuery->with('getUser:id,desc,avatar');
46+
$userList = $userListQuery->get()->toArray();
47+
48+
//获取用户的组
49+
$friendGroup[0] = [
50+
'id' => 0,
51+
'uid' => $userInfo['id'],
52+
'friend_group_name' => '我的好友',
53+
];
54+
$friendGroup += FriendGroup::query()->select('id', 'friend_group_name as label')->where('uid', $userInfo['id'])->orderBy('sort', 'asc')->get()->toArray();
55+
$friendGroup = array_column($friendGroup, null, 'id');
4356
$userContactList = [];
4457
foreach ($userList as $key => $val) {
4558
$fd = Redis::getInstance()->hget(ChatRedisKey::ONLINE_USER_FD_KEY, (string) $val['id']);
4659
$unreadMessageInfo = $this->getUnReadMessageByUser($val, $userInfo);
4760
$userContactList[] = [
48-
'id' => $val['id'],
61+
'id' => $val['get_user']['id'],
4962
'is_group' => Group::IS_NOT_GROUP_TYPE,
50-
'displayName' => $val['desc'],
51-
'avatar' => $val['avatar'],
52-
'index' => $val['desc'],
63+
'displayName' => empty($val['friend_remark']) ? $val['get_user']['desc'] : $val['friend_remark'],
64+
'avatar' => $val['get_user']['avatar'],
65+
'index' => $friendGroup[$val['friend_group']]['friend_group_name'],
66+
'friend_group' => $val['friend_group'],
5367
'unread' => $unreadMessageInfo['unread'] ?? 0,
5468
'status' => empty($fd) ? FriendRelation::FRIEND_ONLINE_STATUS_NO : FriendRelation::FRIEND_ONLINE_STATUS,
5569
'lastContent' => $unreadMessageInfo['lastContent'] ?? '',
@@ -99,6 +113,7 @@ public function initialization() : array
99113
'event' => WsMessage::MESSAGE_TYPE_INIT,
100114
'user_info' => $returnUserInfo,
101115
'user_contact' => $userContactList,
116+
'friend_group' =>$friendGroup,
102117
'user_group' => $userGroupList
103118
];
104119
}

app/Task/Laboratory/FriendWsTask.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55

66
use App\Constants\Laboratory\ChatRedisKey;
77
use App\Constants\Laboratory\WsMessage;
8+
use App\Model\Auth\User;
89
use App\Model\Laboratory\FriendChatHistory;
910
use App\Model\Laboratory\FriendRelation;
1011
use App\Model\Laboratory\GroupChatHistory;
1112
use App\Pool\Redis;
1213
use App\Service\Laboratory\FriendService;
1314
use App\Service\Laboratory\MessageService;
15+
use Hyperf\Database\Model\Model;
1416
use Hyperf\Di\Annotation\Inject;
1517

1618
/**
@@ -146,4 +148,83 @@ function forwardMessage(array $userInfo, array $user, array $content)
146148
}
147149
}
148150

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+
}
149230
}

app/Task/TestTask.php

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)