|
@@ -221,21 +221,51 @@ class ChatService implements ChatServiceInterface
|
|
*/
|
|
*/
|
|
public function delFriend(array $data): array
|
|
public function delFriend(array $data): array
|
|
{
|
|
{
|
|
- $where = [
|
|
|
|
- 'user_id' => $data['user_id'],
|
|
|
|
- 'friend_id' => $data['friend_id'],
|
|
|
|
- ];
|
|
|
|
- $orwhere = [
|
|
|
|
- 'friend_id' => $data['user_id'],
|
|
|
|
- 'user_id' => $data['friend_id'],
|
|
|
|
- ];
|
|
|
|
- $result = ChatFriends::where($where)
|
|
|
|
- ->orWhere($orwhere)->delete();
|
|
|
|
- var_dump($result, '-0------------------');
|
|
|
|
- if ($result) {
|
|
|
|
- return Result::success("删除成功”");
|
|
|
|
- } else {
|
|
|
|
- return Result::error('删除失败');
|
|
|
|
|
|
+ Db::beginTransaction();
|
|
|
|
+ try {
|
|
|
|
+ $where = [
|
|
|
|
+ 'user_id' => $data['user_id'],
|
|
|
|
+ 'friend_id' => $data['friend_id'],
|
|
|
|
+ ];
|
|
|
|
+ $orwhere = [
|
|
|
|
+ 'user_id' => $data['friend_id'],
|
|
|
|
+ 'friend_id' => $data['user_id'],
|
|
|
|
+ ];
|
|
|
|
+
|
|
|
|
+ // 使用闭包来确保正确的 OR 关系
|
|
|
|
+ $result = ChatFriends::where(function ($query) use ($where) {
|
|
|
|
+ $query->where($where);
|
|
|
|
+ })
|
|
|
|
+ ->orWhere(function ($query) use ($orwhere) {
|
|
|
|
+ $query->where($orwhere);
|
|
|
|
+ })
|
|
|
|
+ ->delete();
|
|
|
|
+
|
|
|
|
+ var_dump($result, '-0------------------');
|
|
|
|
+
|
|
|
|
+ $wherechat = [
|
|
|
|
+ 'user_id' => $data['user_id'],
|
|
|
|
+ 'receiver_id' => $data['friend_id'],
|
|
|
|
+ ];
|
|
|
|
+ $orwherechat = [
|
|
|
|
+ 'user_id' => $data['friend_id'],
|
|
|
|
+ 'receiver_id' => $data['user_id'],
|
|
|
|
+ ];
|
|
|
|
+
|
|
|
|
+ // 使用闭包来确保正确的 OR 关系
|
|
|
|
+ ChatRecords::where(function ($query) use ($wherechat) {
|
|
|
|
+ $query->where($wherechat);
|
|
|
|
+ })
|
|
|
|
+ ->orWhere(function ($query) use ($orwherechat) {
|
|
|
|
+ $query->where($orwherechat);
|
|
|
|
+ })
|
|
|
|
+ ->delete();
|
|
|
|
+
|
|
|
|
+ Db::commit();
|
|
|
|
+ return Result::success("删除成功");
|
|
|
|
+ } catch (\Exception $e) {
|
|
|
|
+ Db::rollback();
|
|
|
|
+ return Result::error($e->getMessage());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
/**
|
|
/**
|
|
@@ -313,7 +343,7 @@ class ChatService implements ChatServiceInterface
|
|
$userId = $data['user_id'];
|
|
$userId = $data['user_id'];
|
|
$unreadMessages = ChatRecords::where('user_id', $userId)
|
|
$unreadMessages = ChatRecords::where('user_id', $userId)
|
|
->where('is_read', 0)
|
|
->where('is_read', 0)
|
|
- ->where('action', 'recieved')
|
|
|
|
|
|
+ // ->where('action', 'recieved')
|
|
->leftJoin('user', 'chat_records.receiver_id', '=', 'user.id')
|
|
->leftJoin('user', 'chat_records.receiver_id', '=', 'user.id')
|
|
->leftJoin('chat_groups', 'chat_records.receiver_id', '=', 'chat_groups.id')
|
|
->leftJoin('chat_groups', 'chat_records.receiver_id', '=', 'chat_groups.id')
|
|
->select(
|
|
->select(
|
|
@@ -332,7 +362,7 @@ class ChatService implements ChatServiceInterface
|
|
// 查询已读消息,并将 num 字段设置为 0
|
|
// 查询已读消息,并将 num 字段设置为 0
|
|
$readMessages = ChatRecords::where('user_id', $userId)
|
|
$readMessages = ChatRecords::where('user_id', $userId)
|
|
->where('is_read', 1)
|
|
->where('is_read', 1)
|
|
- ->where('action', 'recieved')
|
|
|
|
|
|
+ // ->where('action', 'recieved')
|
|
->leftJoin('user', 'chat_records.receiver_id', '=', 'user.id')
|
|
->leftJoin('user', 'chat_records.receiver_id', '=', 'user.id')
|
|
->leftJoin('chat_groups', 'chat_records.receiver_id', '=', 'chat_groups.id')
|
|
->leftJoin('chat_groups', 'chat_records.receiver_id', '=', 'chat_groups.id')
|
|
->select(
|
|
->select(
|
|
@@ -358,13 +388,13 @@ class ChatService implements ChatServiceInterface
|
|
|
|
|
|
foreach ($readMessages as $message) {
|
|
foreach ($readMessages as $message) {
|
|
if (strlen($message['receiver_id']) === 18) {
|
|
if (strlen($message['receiver_id']) === 18) {
|
|
- if (!isset($allMessages[$message['receiver_id']])) {
|
|
|
|
- $allMessages[$message['receiver_id']] = $message->toArray();
|
|
|
|
- }
|
|
|
|
|
|
+
|
|
} else {
|
|
} else {
|
|
|
|
+ // $allMessages[$message['receiver_id']] = $message->toArray();
|
|
|
|
+ }
|
|
|
|
+ if (!isset($allMessages[$message['receiver_id']])) {
|
|
$allMessages[$message['receiver_id']] = $message->toArray();
|
|
$allMessages[$message['receiver_id']] = $message->toArray();
|
|
}
|
|
}
|
|
-
|
|
|
|
}
|
|
}
|
|
|
|
|
|
// var_dump($allMessages);
|
|
// var_dump($allMessages);
|
|
@@ -493,6 +523,7 @@ class ChatService implements ChatServiceInterface
|
|
ChatGroups::insert($groupData);
|
|
ChatGroups::insert($groupData);
|
|
//创建群用户
|
|
//创建群用户
|
|
$groupMemberData = [];
|
|
$groupMemberData = [];
|
|
|
|
+ $groupChatData = [];
|
|
if ($data['group_member']) {
|
|
if ($data['group_member']) {
|
|
foreach ($data['group_member'] as $key => $val) {
|
|
foreach ($data['group_member'] as $key => $val) {
|
|
$groupMemberData[$key] = [
|
|
$groupMemberData[$key] = [
|
|
@@ -501,21 +532,22 @@ class ChatService implements ChatServiceInterface
|
|
'user_id' => $val,
|
|
'user_id' => $val,
|
|
'leader' => $data['user_id'] == $val ? 2 : 0,
|
|
'leader' => $data['user_id'] == $val ? 2 : 0,
|
|
];
|
|
];
|
|
|
|
+
|
|
|
|
+ $groupChatData[$key] = [
|
|
|
|
+ 'user_id' => $val,
|
|
|
|
+ 'receiver_id' => $groupData['id'],
|
|
|
|
+ 'content' => '创建群' . Date('Y-m-d H:i:s'),
|
|
|
|
+ 'msg_type' => 1,
|
|
|
|
+ 'is_read' => 0,
|
|
|
|
+ 'talk_type' => 2,
|
|
|
|
+ 'action' => 'recieved',
|
|
|
|
+ 'group_receiver_id' => $data['user_id'],
|
|
|
|
+ ];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
ChatGroupsMember::insert($groupMemberData);
|
|
ChatGroupsMember::insert($groupMemberData);
|
|
//插入一条消息
|
|
//插入一条消息
|
|
- $chatRecordsData = [
|
|
|
|
- 'user_id' => $data['user_id'],
|
|
|
|
- 'receiver_id' => $groupData['id'],
|
|
|
|
- 'content' => '我创建了一个群' . Date('Y-m-d H:i:s'),
|
|
|
|
- 'msg_type' => 1,
|
|
|
|
- 'is_read' => 0,
|
|
|
|
- 'talk_type' => 2,
|
|
|
|
- 'action' => 'said',
|
|
|
|
- 'group_receiver_id' => $data['user_id'],
|
|
|
|
- ];
|
|
|
|
- ChatRecords::insert($chatRecordsData);
|
|
|
|
|
|
+ ChatRecords::insert($groupChatData);
|
|
Db::commit();
|
|
Db::commit();
|
|
} catch (\Throwable $ex) {
|
|
} catch (\Throwable $ex) {
|
|
Db::rollBack();
|
|
Db::rollBack();
|
|
@@ -532,6 +564,19 @@ class ChatService implements ChatServiceInterface
|
|
public function addGroupMember(array $data): array
|
|
public function addGroupMember(array $data): array
|
|
{
|
|
{
|
|
$result = ChatGroupsMember::where(['group_id' => $data['group_id'], 'user_id' => $data['user_id']])->update(['leader' => 2]);
|
|
$result = ChatGroupsMember::where(['group_id' => $data['group_id'], 'user_id' => $data['user_id']])->update(['leader' => 2]);
|
|
|
|
+
|
|
|
|
+ $groupChatData = [
|
|
|
|
+ 'user_id' => $data['user_id'],
|
|
|
|
+ 'receiver_id' => $data['group_id'],
|
|
|
|
+ 'content' => '加入群' . Date('Y-m-d H:i:s'),
|
|
|
|
+ 'msg_type' => 1,
|
|
|
|
+ 'is_read' => 0,
|
|
|
|
+ 'talk_type' => 2,
|
|
|
|
+ 'action' => 'recieved',
|
|
|
|
+ 'group_receiver_id' => $data['user_id'],
|
|
|
|
+ ];
|
|
|
|
+ ChatRecords::insert($groupChatData);
|
|
|
|
+
|
|
if ($result) {
|
|
if ($result) {
|
|
return Result::success('修改成功');
|
|
return Result::success('修改成功');
|
|
} else {
|
|
} else {
|
|
@@ -585,6 +630,8 @@ class ChatService implements ChatServiceInterface
|
|
public function quitGroup(array $data): array
|
|
public function quitGroup(array $data): array
|
|
{
|
|
{
|
|
$result = ChatGroupsMember::where(['group_id' => $data['group_id'], 'user_id' => $data['user_id']])->delete();
|
|
$result = ChatGroupsMember::where(['group_id' => $data['group_id'], 'user_id' => $data['user_id']])->delete();
|
|
|
|
+ ChatRecords::where(['receiver_id' => $data['group_id'], 'user_id' => $data['user_id']])->delete();
|
|
|
|
+
|
|
if ($result) {
|
|
if ($result) {
|
|
return Result::success('退出成功');
|
|
return Result::success('退出成功');
|
|
} else {
|
|
} else {
|
|
@@ -613,6 +660,7 @@ class ChatService implements ChatServiceInterface
|
|
public function delGroupMembers(array $data): array
|
|
public function delGroupMembers(array $data): array
|
|
{
|
|
{
|
|
$result = ChatGroupsMember::where(['group_id' => $data['group_id'], 'user_id' => $data['user_id']])->delete();
|
|
$result = ChatGroupsMember::where(['group_id' => $data['group_id'], 'user_id' => $data['user_id']])->delete();
|
|
|
|
+ ChatRecords::where(['receiver_id' => $data['group_id'], 'user_id' => $data['user_id']])->delete();
|
|
if ($result) {
|
|
if ($result) {
|
|
return Result::success('删除成功');
|
|
return Result::success('删除成功');
|
|
} else {
|
|
} else {
|
|
@@ -721,8 +769,9 @@ class ChatService implements ChatServiceInterface
|
|
if (!empty($data['title'])) {
|
|
if (!empty($data['title'])) {
|
|
$where[] = ['chat_topics.title', 'like', '%' . $data['title'] . '%'];
|
|
$where[] = ['chat_topics.title', 'like', '%' . $data['title'] . '%'];
|
|
}
|
|
}
|
|
- if (!empty($data['user_id'])) {
|
|
|
|
- $where[] = ['chat_topics.user_id', '=', $data['user_id']];
|
|
|
|
|
|
+ // 不是看自己的话题
|
|
|
|
+ if (!empty($data['user_id_search'])) {
|
|
|
|
+ $where[] = ['chat_topics.user_id', '=', $data['user_id_search']];
|
|
}
|
|
}
|
|
if (!empty($data['status'])) {
|
|
if (!empty($data['status'])) {
|
|
$where[] = ['chat_topics.status', '=', $data['status']];
|
|
$where[] = ['chat_topics.status', '=', $data['status']];
|
|
@@ -779,33 +828,66 @@ class ChatService implements ChatServiceInterface
|
|
$data['group_id'] = $group_id;
|
|
$data['group_id'] = $group_id;
|
|
ChatTopic::where(['id' => $result])->update($data);
|
|
ChatTopic::where(['id' => $result])->update($data);
|
|
|
|
|
|
|
|
+ // 查询 Chattopic 数据
|
|
|
|
+ $chattopic = Chattopic::find($result);
|
|
|
|
+ } else {
|
|
|
|
+ $chattopic = Chattopic::find($result);
|
|
|
|
+ }
|
|
|
|
+ Db::beginTransaction();
|
|
|
|
+ Db::commit();
|
|
|
|
+ } catch (\Exception $e) {
|
|
|
|
+ Db::rollBack();
|
|
|
|
+ return Result::error($data, $e->getMessage());
|
|
|
|
+ }
|
|
|
|
+ return Result::success($chattopic);
|
|
|
|
+ }
|
|
|
|
+ public function applyTopic(array $data): array
|
|
|
|
+ {
|
|
|
|
+ date_default_timezone_set('Asia/Shanghai');
|
|
|
|
+ db::beginTransaction();
|
|
|
|
+ try {
|
|
|
|
+ $query = ChatTopic::where(['id' => $data['id']]);
|
|
|
|
+ $topdata = $query->first();
|
|
|
|
+
|
|
|
|
+ $result = ChatTopic::where(['id' => $data['id']])->update(['status' => $data['status']]);
|
|
|
|
+ var_dump($result, 'tedst111111111111111');
|
|
|
|
+ var_dump(date('Y-m-d H:i:s'), 'tedst111111111111111');
|
|
|
|
+ if ($data['status'] == 2) {
|
|
//插入一条消息
|
|
//插入一条消息
|
|
$chatRecordsData = [
|
|
$chatRecordsData = [
|
|
- 'user_id' => $data['user_id'],
|
|
|
|
- 'receiver_id' => $group_id,
|
|
|
|
|
|
+ 'user_id' => $topdata['user_id'],
|
|
|
|
+ 'receiver_id' => $topdata['group_id'],
|
|
'content' => '我创建了一个群' . Date('Y-m-d H:i:s'),
|
|
'content' => '我创建了一个群' . Date('Y-m-d H:i:s'),
|
|
'msg_type' => 1,
|
|
'msg_type' => 1,
|
|
'is_read' => 0,
|
|
'is_read' => 0,
|
|
'talk_type' => 2,
|
|
'talk_type' => 2,
|
|
'action' => 'said',
|
|
'action' => 'said',
|
|
- 'group_receiver_id' => $data['user_id'],
|
|
|
|
|
|
+ 'group_receiver_id' => $topdata['user_id'],
|
|
];
|
|
];
|
|
ChatRecords::insert($chatRecordsData);
|
|
ChatRecords::insert($chatRecordsData);
|
|
- // 查询 Chattopic 数据
|
|
|
|
- $chattopic = Chattopic::find($result);
|
|
|
|
} else {
|
|
} else {
|
|
- $chattopic = Chattopic::find($result);
|
|
|
|
|
|
+ ChatRecords::where('user_id', $topdata['user_id'])->where('receiver_id', $topdata['group_id'])->delete();
|
|
}
|
|
}
|
|
- Db::beginTransaction();
|
|
|
|
Db::commit();
|
|
Db::commit();
|
|
|
|
+ if ($result) {
|
|
|
|
+ return Result::success($data);
|
|
|
|
+ } else {
|
|
|
|
+ return Result::error($data);
|
|
|
|
+ }
|
|
} catch (\Exception $e) {
|
|
} catch (\Exception $e) {
|
|
Db::rollBack();
|
|
Db::rollBack();
|
|
return Result::error($data, $e->getMessage());
|
|
return Result::error($data, $e->getMessage());
|
|
}
|
|
}
|
|
- return Result::success($chattopic);
|
|
|
|
|
|
+ if (empty($data['id'])) {
|
|
|
|
+ return Result::error('id不能为空');
|
|
|
|
+ }
|
|
}
|
|
}
|
|
public function updateTopic(array $data): array
|
|
public function updateTopic(array $data): array
|
|
{
|
|
{
|
|
|
|
+ if (empty($data['id'])) {
|
|
|
|
+ return Result::error('id不能为空');
|
|
|
|
+ }
|
|
|
|
+
|
|
$result = ChatTopic::where(['id' => $data['id']])->update($data);
|
|
$result = ChatTopic::where(['id' => $data['id']])->update($data);
|
|
if ($result) {
|
|
if ($result) {
|
|
return Result::success($data);
|
|
return Result::success($data);
|