1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060 |
- <?php
- namespace App\JsonRpc;
- use App\Model\ChatFriends;
- use App\Model\ChatGroups;
- use App\Model\ChatGroupsMember;
- use App\Model\ChatRecords;
- use App\Model\ChatTopic;
- use App\Model\ChatTopicsReply;
- use App\Model\User;
- use App\Tools\PublicData;
- use App\Tools\Result;
- use Hyperf\DbConnection\Db;
- use Hyperf\RpcServer\Annotation\RpcService;
- #[RpcService(name: "ChatService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
- class ChatService implements ChatServiceInterface
- {
- /**
- * 获取用户信息
- * @param array $data
- * @return array
- */
- public function getFriendInfo(array $data): array
- {
- $result = ChatFriends::where('friend_id', $data['friend_id'])
- ->where('user_id', $data['user_id'])
- ->select(
- 'chat_friends.*',
- 'user.user_name',
- 'user.mobile',
- 'user.nickname',
- 'user.avatar'
- )
- ->leftJoin('user', 'user.id', '=', 'chat_friends.friend_id')
- ->first();
- return Result::success($result);
- }
- /**
- * 搜索好友
- * @param array $data
- * @return array
- */
- public function searchFriend(array $data): array
- {
- $keyword = $data['keyword'];
- $userId = $data['user_id'];
- $result = User::where('user_name', 'like', '%' . $data['keyword'] . '%')
- ->orWhere('nickname', 'like', '%' . $data['keyword'] . '%')
- ->orWhere('mobile', 'like', '%' . $data['keyword'] . '%')
- ->leftJoin('chat_friends', function ($join) use ($userId) {
- $join->on('user.id', '=', 'chat_friends.friend_id')
- ->where('chat_friends.user_id', $userId);
- })
- ->select('user.*', 'chat_friends.remark as remark', 'chat_friends.id as isfriend')
- ->get();
- if ($result) {
- return Result::success($result);
- } else {
- return Result::error('没有找到相关好友');
- }
- }
- /**
- * 添加申请
- * @param array $data
- * @return array
- */
- public function addFriend(array $data): array
- {
- Db::beginTransaction();
- try
- {
- // 检查是否存在相同的记录
- $existingRecord = ChatFriends::where([
- 'user_id' => $data['user_id'],
- 'friend_id' => $data['friend_id'],
- ])->first();
- if ($existingRecord) {
- Db::rollBack();
- if ($existingRecord->status == 1) {
- return Result::error("好友申请已存在", 0);
- } elseif ($existingRecord->status == 2) {
- return Result::error("已经是好友关系", 0);
- }
- }
- $result = ChatFriends::insertGetId($data);
- Db::commit();
- } catch (\Throwable $ex) {
- Db::rollBack();
- var_dump($ex->getMessage());
- return Result::error("添加好友申请失败", 0);
- }
- return Result::success("添加成功");
- }
- /**
- * 好友列表
- * @param array $data
- * @return array
- */
- public function getFriendsList(array $data): array
- {
- var_dump($data);
- $result = ChatFriends::leftJoin('user', 'user.id', '=', 'chat_friends.friend_id')
- ->select(
- 'chat_friends.*',
- 'user.user_name',
- 'user.mobile',
- 'user.nickname',
- 'user.avatar'
- )
- ->where([
- ['user_id', $data['user_id']],
- ['chat_friends.status', $data['status']], // 明确指定 chat_friends 表中的 status 列
- ])
- ->get();
- return Result::success($result);
- }
- /**
- * 好友列表
- * @param array $data
- * @return array
- */
- public function getFriendsApplyList(array $data): array
- {
- var_dump($data);
- $result = ChatFriends::leftJoin('user', 'user.id', '=', 'chat_friends.user_id')
- ->select(
- 'chat_friends.*',
- 'user.user_name',
- 'user.mobile',
- 'user.nickname',
- 'user.avatar'
- )
- ->where([
- ['friend_id', $data['friend_id']],
- ['chat_friends.status', $data['status']], // 明确指定 chat_friends 表中的 status 列
- ])
- ->get();
- return Result::success($result);
- }
- /**
- * 更新申请
- * @param array $data
- * @return array
- */
- public function applyFriend(array $data): array
- {
- $status = $data['status'];
- //判断同意还是不同意
- if ($status == 2) {
- Db::beginTransaction();
- try {
- $where = [
- 'id' => $data['id'],
- 'status' => 1,
- ];
- $fr = ChatFriends::where($where)->first();
- if (empty($fr)) {
- Db::rollBack();
- return Result::error("好友申请记录不存在,已经是好友了", 0);
- }
- $data['status'] = $status;
- $data['applied_at'] = date("Y-m-d H:i:s"); //好友审核时间操作人friend_id
- unset($data['user_id']);
- ChatFriends::where($where)->update($data);
- Db::table('chat_friends')
- ->updateOrInsert(
- [
- 'user_id' => $fr['friend_id'],
- 'friend_id' => $fr['user_id'],
- ],
- [
- 'status' => $status,
- ]
- );
- Db::commit();
- } catch (\Throwable $ex) {
- Db::rollBack();
- var_dump($ex->getMessage());
- return Result::error("同意添加为好友失败", 0);
- }
- return Result::success(['添加成功']);
- } else if ($status == 4) { //拒绝
- Db::beginTransaction();
- try {
- $where1 = [
- 'id' => $data['id'],
- ];
- $deletedRows = ChatFriends::where($where1)->delete();
- if ($deletedRows > 0) {
- Db::commit();
- } else {
- // 处理记录不存在的情况
- Db::rollBack();
- throw new \Exception('记录不存在');
- }
- } catch (\Throwable $ex) {
- Db::rollBack();
- var_dump($ex->getMessage());
- return Result::error("拒绝添加好友失败", 0);
- }
- return Result::success(['已经拒绝']);
- }
- }
- /**
- * 删除好友
- * @param array $data
- * @return array
- */
- public function delFriend(array $data): array
- {
- 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());
- }
- }
- /**
- * 是否好友
- * @param array $data
- * @return array
- */
- public function isFriend(array $data): array
- {
- $where = [
- 'user_id' => $data['user_id'],
- 'friend_id' => $data['friend_id'],
- ];
- $result = ChatFriends::where($where)->first();
- if ($result) {
- return Result::success(true);
- } else {
- return Result::error('不是好友');
- }
- }
- /**
- * 添加聊天内容
- * @param array $data
- * @return array
- */
- public function addChatRecords(array $data): array
- {
- Db::beginTransaction();
- try {;
- //添加会话内容
- $ChatRecordsData = [[
- 'msg_type' => $data['msg_type'] ?? 0,
- 'user_id' => $data['user_id'] ?? 0,
- 'is_read' => $data['is_read'] ?? 0,
- 'talk_type' => $data['talk_type'] ?? 0,
- 'action' => $data['action'] ?? 0,
- 'group_receiver_id' => $data['group_receiver_id'] ?? 0,
- 'content' => $data['content'] ?? '',
- 'receiver_id' => $data['receiver_id'] ?? '',
- ]];
- ChatRecords::insert($ChatRecordsData);
- Db::commit();} catch (\Throwable $ex) {
- Db::rollBack();
- var_dump($ex->getMessage());
- return Result::error("存储消息失败", 0);
- }
- return Result::success([]);
- }
- /**
- * 修改好友备注
- * @param array $data
- * @return array
- */
- public function updateFriend(array $data): array
- {
- $result = ChatFriends::where(['user_id' => $data['user_id'],
- 'friend_id' => $data['friend_id'],
- 'status' => 2,
- ])->update(['remark' => $data['remark']]);
- if ($result) {
- return Result::success('修改成功');
- } else {
- return Result::error('修改失败');
- }
- }
- /**
- * 会话列表
- * @param array $data
- * @return array
- */
- public function getConversation(array $data): array
- {
- $userId = $data['user_id'];
- $unreadMessages = ChatRecords::where('user_id', $userId)
- ->where('is_read', 0)
- // ->where('action', 'recieved')
- ->leftJoin('user', 'chat_records.receiver_id', '=', 'user.id')
- ->leftJoin('chat_groups', 'chat_records.receiver_id', '=', 'chat_groups.id')
- ->select(
- 'receiver_id',
- DB::raw('COUNT(receiver_id) AS num'),
- DB::raw('MAX(chat_records.id) AS max_id'),
- 'user.user_name as user_name',
- 'user.avatar as avatar',
- 'user.mobile as mobile',
- 'chat_groups.group_name as group_name'
- )
- ->groupBy('receiver_id')
- ->orderBy(DB::raw('MAX(chat_records.id)'), 'desc')
- ->get();
- // 查询已读消息,并将 num 字段设置为 0
- $readMessages = ChatRecords::where('user_id', $userId)
- ->where('is_read', 1)
- // ->where('action', 'recieved')
- ->leftJoin('user', 'chat_records.receiver_id', '=', 'user.id')
- ->leftJoin('chat_groups', 'chat_records.receiver_id', '=', 'chat_groups.id')
- ->select(
- 'receiver_id',
- DB::raw('0 AS num'),
- DB::raw('MAX(chat_records.id) AS max_id'),
- 'user.user_name as user_name',
- 'user.avatar as avatar',
- 'user.mobile as mobile',
- 'chat_groups.group_name as group_name'
- )
- ->groupBy('receiver_id')
- ->orderBy(DB::raw('MAX(chat_records.id)'), 'desc')
- ->get();
- // 合并未读消息和已读消息
- // $allMessages = array_merge($unreadMessages->toArray(), $readMessages->toArray());
- // 使用关联数组去重,并优先保留未读消息
- $allMessages = [];
- foreach ($unreadMessages as $message) {
- $allMessages[$message['receiver_id']] = $message->toArray();
- }
- foreach ($readMessages as $message) {
- if (strlen($message['receiver_id']) === 18) {
- } else {
- // $allMessages[$message['receiver_id']] = $message->toArray();
- }
- if (!isset($allMessages[$message['receiver_id']])) {
- $allMessages[$message['receiver_id']] = $message->toArray();
- }
- }
- // var_dump($allMessages);
- // 处理结果,判断是否是群聊
- $formattedMessages = [];
- foreach ($allMessages as $message) {
- $formattedMessage = [
- 'receiver_id' => $message['receiver_id'],
- 'num' => $message['num'],
- 'max_id' => $message['max_id'],
- 'user_name' => $message['user_name'],
- 'avatar' => $message['avatar'],
- 'mobile' => $message['mobile'],
- 'group_name' => $message['group_name'],
- ];
- if (strlen($message['receiver_id']) === 18) { // 判断是否是 UUID
- $formattedMessage['type'] = 'group';
- $formattedMessage['name'] = $message['group_name'];
- $formattedMessage['is_group'] = 1;
- } else {
- $formattedMessage['type'] = 'user';
- $formattedMessage['name'] = $message['user_name'];
- $formattedMessage['is_group'] = 0;
- }
- $formattedMessages[] = $formattedMessage;
- }
- if (!empty($formattedMessages)) {
- return Result::success($formattedMessages);
- } else {
- return Result::error('没有消息');
- }
- }
- /**
- * 获取聊天记录
- * @param array $data
- * @return array
- */
- public function getChatRecords(array $data): array
- {
- var_dump('222222');
- Db::beginTransaction();
- try {
- $userId = $data['user_id'];
- $friendId = $data['friend_id'];
- $result = ChatRecords::where(function ($query) use ($userId, $friendId) {
- $query->where('user_id', $userId)->where('receiver_id', $friendId);
- })
- // ->orWhere(function ($query) use ($userId, $friendId) {
- // $query->where('user_id', $friendId)->where('receiver_id', $userId);
- // })
- ->leftJoin('user as u1', 'chat_records.user_id', '=', 'u1.id')
- ->leftJoin('user as u2', 'chat_records.receiver_id', '=', 'u2.id')
- ->select('chat_records.*', 'u1.user_name as user_id_name', 'u1.avatar as user_avatar', 'u2.user_name as receiver_id_name', 'u2.avatar as receiver_avatar')
- ->orderBy('id', 'asc')->paginate(100, ['*'], 'page', $data['page'] ?? 1);
- //更新聊天记录已读
- ChatRecords::where('user_id', $userId)
- ->where('receiver_id', $friendId)
- ->where('is_read', 0)
- ->where('talk_type', 1)
- ->update(['is_read' => 1]);
- Db::commit();
- } catch (\Throwable $ex) {
- Db::rollBack();
- var_dump($ex->getMessage());
- return Result::error("获取聊天记录失败", 0);
- }
- if ($result) {
- return Result::success($result);
- } else {
- return Result::error('没有聊天记录');
- }
- }
- /**
- * 获取群聊天记录
- * @param array $data
- * @return array
- */
- public function getGroupChatRecords(array $data): array
- {
- Db::beginTransaction();
- try {
- $userId = $data['user_id'];
- $group_id = $data['group_id'];
- $result = ChatRecords::where('receiver_id', $group_id)
- ->where('user_id', $userId)
- ->leftJoin('user as u1', 'chat_records.user_id', '=', 'u1.id')
- ->leftJoin('user as u2', 'chat_records.group_receiver_id', '=', 'u2.id')
- ->select('chat_records.*', 'u1.user_name as user_id_name', 'u1.avatar as user_avatar', 'u2.user_name as receiver_id_name', 'u2.avatar as receiver_avatar')
- ->orderBy('id', 'asc')->paginate(100, ['*'], 'page', $data['page'] ?? 1);
- //更新群聊天记录
- ChatRecords::where('receiver_id', $group_id)
- ->where('user_id', $userId)
- ->update(['is_read' => 1]);
- Db::commit();
- } catch (\Throwable $ex) {
- Db::rollBack();
- var_dump($ex->getMessage());
- return Result::error("获取群聊天记录失败", 0);
- }
- if ($result) {
- return Result::success($result);
- } else {
- return Result::error('没有群消息');
- }
- }
- /**
- * 群组 - 创建群
- * @param array $data
- * @return array
- */
- public function addGroup(array $data): array
- {
- Db::beginTransaction();
- try {
- //创建群
- $groupData = [
- 'id' => PublicData::uuid(),
- 'creator_id' => $data['user_id'],
- 'group_name' => $data['group_name'],
- 'avatar' => $data['avatar'] ?? '',
- 'profile' => $data['profile'] ?? '',
- ];
- ChatGroups::insert($groupData);
- //创建群用户
- $groupMemberData = [];
- $groupChatData = [];
- if ($data['group_member']) {
- foreach ($data['group_member'] as $key => $val) {
- $groupMemberData[$key] = [
- 'id' => PublicData::uuid(),
- 'group_id' => $groupData['id'],
- 'user_id' => $val,
- '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);
- //插入一条消息
- ChatRecords::insert($groupChatData);
- Db::commit();
- } catch (\Throwable $ex) {
- Db::rollBack();
- var_dump($ex->getMessage());
- return Result::error("创建群失败", 0);
- }
- return Result::success([]);
- }
- /**
- * 群组 - 加入群
- * @param array $data
- * @return array
- */
- public function addGroupMember(array $data): array
- {
- $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) {
- return Result::success('修改成功');
- } else {
- return Result::error('修改失败');
- }
- }
- /**
- * 群组 - 群信息
- * @param array $data
- * @return array
- */
- public function getGroupInfo(array $data): array
- {
- $result = ChatGroups::where(['chat_groups.id' => $data['group_id']])
- ->join('user', 'chat_groups.creator_id', '=', 'user.id')
- ->select('chat_groups.*', 'user.user_name as user_name', 'user.avatar as avatar')
- ->first();
- return Result::success($result);
- }
- /**
- * 群组 - 删除群
- * @param array $data
- * @return array
- */
- public function delGroup(array $data): array
- {
- Db::beginTransaction();
- try {
- $groupMember = ChatGroupsMember::where(['group_id' => $data['group_id']])->delete();
- $result = ChatGroups::where(['id' => $data['group_id']])->delete();
- $result = ChatRecords::where(['receiver_id' => $data['group_id']])->delete();
- //群聊记录
- Db::commit();
- } catch (\Throwable $ex) {
- Db::rollBack();
- var_dump($ex->getMessage());
- return Result::error("删除群失败", 0);
- }
- if ($result) {
- return Result::success('删除成功');
- } else {
- return Result::error('删除失败');
- }
- }
- /**
- * 群组 - 退出群
- * @param array $data
- * @return array
- */
- public function quitGroup(array $data): array
- {
- $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) {
- return Result::success('退出成功');
- } else {
- return Result::error('退出失败');
- }
- }
- /**
- * 群组 - 我的群
- * @param array $data
- * @return array
- */
- public function getGroupList(array $data): array
- {
- $result = ChatGroupsMember::where(['user_id' => $data['user_id']])
- ->leftJoin('chat_groups', 'chat_groups_members.group_id', '=', 'chat_groups.id')
- ->select('chat_groups.*', 'chat_groups_members.group_id as group_id')
- ->orderBy('chat_groups.id', 'desc')
- ->paginate(100, ['*'], 'page', $data['page'] ?? 1);
- return Result::success($result);
- }
- /**
- * 群组 - 删除群成员
- * @param array $data
- * @return array
- */
- public function delGroupMembers(array $data): array
- {
- $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) {
- return Result::success('删除成功');
- } else {
- return Result::error('删除失败');
- }
- }
- /**
- * 群组 - 更新群
- * @param array $data
- * @return array
- */
- public function updateGroup(array $data): array
- {
- // 提取需要的字段
- $groupId = $data['group_id'];
- $groupName = $data['group_name'] ?? null;
- $profile = $data['profile'] ?? null;
- $avatar = $data['avatar'] ?? null;
- // 查询群组
- $group = ChatGroups::find($groupId);
- if (!$group) {
- return Result::error('群组不存在');
- }
- // 更新群组信息
- if ($groupName !== null) {
- $group->group_name = $groupName;
- }
- if ($profile !== null) {
- $group->profile = $profile;
- }
- if ($avatar !== null) {
- $group->avatar = $avatar;
- }
- // 保存更改
- if ($group->save()) {
- return Result::success($group->toArray());
- } else {
- return Result::error('更新群组信息失败');
- }
- }
- /**
- * 群组 - 删除群
- * @param array $data
- * @return array
- */
- public function deleteGroup(array $data): array
- {
- $result = ChatGroups::where(['id' => $data['group_id']])->delete();
- if ($result) {
- return Result::success('删除成功');
- } else {
- return Result::error('删除失败');
- }
- }
- /**
- * 群组 - 群用户列表
- * @param array $data
- * @return array
- */
- public function getGroupMembers(array $data): array
- {
- $groupMember = ChatGroupsMember::where(['group_id' => $data['group_id']])
- ->leftJoin('user as u1', 'chat_groups_members.user_id', '=', 'u1.id')
- ->select('chat_groups_members.*', 'u1.user_name', 'u1.avatar')
- ->orderBy('id', 'desc')
- ->get();
- return Result::success($groupMember);
- }
- /**
- * 群组 - 添加群
- * @param array $data
- * @return array
- */
- public function joinGroup(array $data): array
- {
- $group = ChatGroups::where(['id' => $data['group_id']])->first();
- if (empty($group)) {
- return Result::error("群不存在", 0);
- }
- $groupMember = ChatGroupsMember::where(['user_id' => $data['user_id'], 'group_id' => $data['group_id']])->first();
- if ($groupMember) {
- return Result::error("已加入群", 0);
- }
- $info = [
- 'id' => PublicData::uuid(),
- 'user_id' => $data['user_id'],
- 'group_id' => $data['group_id'],
- ];
- $result = ChatGroupsMember::insertGetId($info);
- if ($result) {
- return Result::success($data);
- } else {
- return Result::error($data);
- };
- }
- /**
- * 话题 - 列表
- * @param array $data
- * @return array
- */
- public function getTopicsList(array $data): array
- {
- $where = [];
- if (!empty($data['title'])) {
- $where[] = ['chat_topics.title', 'like', '%' . $data['title'] . '%'];
- }
- // 不是看自己的话题
- if (!empty($data['user_id_search'])) {
- $where[] = ['chat_topics.user_id', '=', $data['user_id_search']];
- }
- if (!empty($data['status'])) {
- $where[] = ['chat_topics.status', '=', $data['status']];
- }
- if (!empty($data['type'])) {
- $where[] = ['chat_topics.type', '=', $data['type']];
- }
- if (!empty($data['nickname'])) {
- $where[] = ['user.nickname', '=', $data['nickname']];
- }
- var_dump($where);
- $result = ChatTopic::where($where)
- ->leftJoin('user', 'user.id', '=', 'chat_topics.user_id')
- ->leftJoin('chat_topics_reply', 'chat_topics.id', '=', 'chat_topics_reply.topic_id')
- ->select('chat_topics.*', 'user.nickname', 'user.avatar', 'user.user_name'
- ,
- DB::raw('count(chat_topics_reply.id) as num'))
- ->groupBy('chat_topics.id')
- ->orderBy('chat_topics.id', 'desc')
- ->paginate($data['page_size'], ['*'], 'page', $data['page'] ?? 1);
- return Result::success($result);
- }
- public function getTopic(array $data): array
- {
- $result = ChatTopic::where(['id' => $data['id']])->first();
- return Result::success($result);
- }
- public function addTopic(array $data): array
- {
- $chattopic = [];
- try {
- $data['created_at'] = date('Y-m-d H:i:s');
- $data['updated_at'] = date('Y-m-d H:i:s');
- $result = ChatTopic::insertGetId($data);
- if ($result && $data['is_group'] == 1) {
- //chat_group
- $group_id = PublicData::uuid();
- $groupData = [
- 'id' => $group_id,
- 'creator_id' => $data['user_id'],
- 'group_name' => $data['group_name'] ?? '',
- 'profile' => $data['profile'] ?? 0,
- ];
- $groupResult = ChatGroups::insertGetId($groupData);
- $groupMemberData = [
- 'id' => PublicData::uuid(),
- 'user_id' => $data['user_id'],
- 'group_id' => $group_id,
- 'leader' => 2,
- ];
- $groupMemberResult = ChatGroupsMember::insertGetId($groupMemberData);
- //更新result的 group_id
- $data['group_id'] = $group_id;
- 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 = [
- 'user_id' => $topdata['user_id'],
- 'receiver_id' => $topdata['group_id'],
- 'content' => '我创建了一个群' . Date('Y-m-d H:i:s'),
- 'msg_type' => 1,
- 'is_read' => 0,
- 'talk_type' => 2,
- 'action' => 'said',
- 'group_receiver_id' => $topdata['user_id'],
- ];
- ChatRecords::insert($chatRecordsData);
- } else {
- ChatRecords::where('user_id', $topdata['user_id'])->where('receiver_id', $topdata['group_id'])->delete();
- }
- Db::commit();
- if ($result) {
- return Result::success($data);
- } else {
- return Result::error($data);
- }
- } catch (\Exception $e) {
- Db::rollBack();
- return Result::error($data, $e->getMessage());
- }
- if (empty($data['id'])) {
- return Result::error('id不能为空');
- }
- }
- public function updateTopic(array $data): array
- {
- if (empty($data['id'])) {
- return Result::error('id不能为空');
- }
- $result = ChatTopic::where(['id' => $data['id']])->update($data);
- if ($result) {
- return Result::success($data);
- } else {
- return Result::error($data);
- };
- }
- public function delTopic(array $data): array
- {
- $result = ChatTopic::where(['id' => $data['id']])->delete();
- //删除群和成员和聊天
- //删除话题回复
- if ($result) {
- return Result::success($data);
- } else {
- return Result::error('删除失败');
- };
- }
- public function getTopicInfo(array $data): array
- {
- $result = ChatTopic::where(['chat_topics.id' => $data['id']])
- ->leftJoin('user', 'user.id', '=', 'chat_topics.user_id')
- ->select('chat_topics.*', 'user.nickname', 'user.avatar', 'user.user_name')
- ->first();
- return Result::success($result);
- }
- public function addReply(array $data): array
- {
- $result = ChatTopic::where(['id' => $data['id']])->get();
- if ($result) {
- $replydata['created_at'] = date('Y-m-d H:i:s');
- $replydata['updated_at'] = date('Y-m-d H:i:s');
- $replydata['content'] = $data['content'];
- $replydata['user_id'] = $data['user_id'];
- $replydata['topic_id'] = $data['id'];
- $re = ChatTopicsReply::insertGetId($replydata);
- }
- if ($re) {
- return Result::success($data);
- } else {
- return Result::error($data);
- }
- }
- public function getTopicReply(array $data): array
- {
- var_dump($data);
- $result = ChatTopicsReply::where(['topic_id' => $data['id']])
- ->leftJoin('user', 'user.id', '=', 'chat_topics_reply.user_id')
- ->select('chat_topics_reply.*', 'user.nickname', 'user.avatar', 'user.user_name')
- ->paginate($data['page_size'], ['*'], 'page', $data['page'] ?? 1);
- return Result::success($result);
- }
- /**
- * 修改群成员
- * @param array $data
- * @return array
- */
- public function updateGroupMembers(array $data): array
- {
- $where = [
- 'group_id' => $data['group_id'],
- ];
- $group_id = $data['group_id'];
- //先删除群成员
- $result = ChatGroupsMember::where($where)
- ->where([["user_id", '!=', $data['user_id']]])->delete();
- $groupMemberData = [];
- foreach ($data['group_member'] as $value) {
- $groupMemberData[] = [
- 'id' => PublicData::uuid(),
- 'user_id' => $value,
- 'group_id' => $group_id,
- 'leader' => 0,
- ];
- }
- $result = ChatGroupsMember::where($where)->insert($groupMemberData);
- // 获取群信息
- $groupInfo = ChatGroups::where(['id' => $group_id])->first();
- if ($result) {
- return Result::success($groupInfo);
- } else {
- return Result::error($data);
- }
- }
- public function clearGroupRecords(array $data): array
- {
- $result = ChatRecords::where(['user_id' => $data['user_id'], 'receiver_id' => $data['id']])->delete();
- if ($result) {
- return Result::success("删除成功");
- } else {
- return Result::error("删除失败");
- }
- }
- public function recallRecord(array $data): array
- {
- //获取所有id,并删除掉
- $ids = array_column($data, 'id');
- $result = ChatRecords::whereIn('id', $ids)->delete();
- if ($result) {
- return Result::success("删除成功");
- } else {
- return Result::error("删除失败");
- }
- }
- public function clearRecords(array $data): array
- {
- $result = ChatRecords::where(['user_id' => $data['user_id'], 'receiver_id' => $data['friend_id']])->delete();
- if ($result) {
- return Result::success("删除成功");
- } else {
- return Result::error("删除失败");
- }
- }
- public function getRecordByContent(array $data): array
- {
- $result = ChatRecords::where(['chat_records.user_id' => $data['user_id'], 'chat_records.receiver_id' => $data['receiver_id'], 'chat_records.content' => $data['content']])
- ->orWhere(['chat_records.receiver_id' => $data['user_id'], 'chat_records.user_id' => $data['receiver_id'], 'chat_records.content' => $data['content']])
- ->all();
- if ($result) {
- return Result::success($result['id']);
- } else {
- return Result::error("没有数据");
- }
- }
- public function getRecord(array $data): array
- {
- $result = ChatRecords::where(['chat_records.id' => $data['id']])
- ->leftJoin('user', 'user.id', '=', 'chat_records.user_id')
- ->leftJoin('user as user2', 'user2.id', '=', 'chat_records.receiver_id')
- ->select('chat_records.*', 'user.nickname', 'user.avatar', 'user.user_name', 'user2.nickname as receiver_nickname', 'user2.avatar as receiver_avatar')
- ->get();
- return Result::success($result);
- }
- public function delReply(array $data): array
- {
- $result = ChatTopicsReply::where(['id' => $data['id']])->delete();
- if ($result) {
- return Result::success("删除成功");
- } else {
- return Result::error("删除失败");
- }
- }
- public function delAllReply(array $data): array
- {
- $result = ChatTopicsReply::where(['topic_id' => $data['topicid']])->delete();
- if ($result) {
- return Result::success("删除成功");
- } else {
- return Result::error("删除失败");
- }
- }
- public function getTopicsListAdmin(array $data): array
- {
- $where = [];
- if (!empty($data['type'])) {
- $where['type'] = $data['type'];
- }
- if (!empty($data['title'])) {
- $where['title'] = $data['title'];
- }
- $result = ChatTopic::where($where)
- ->leftJoin('user', 'user.id', '=', 'chat_topics.user_id')
- ->select('chat_topics.*', 'user.nickname', 'user.avatar', 'user.user_name')
- ->paginate($data['page_size'], ['*'], 'page', $data['page'] ?? 1);
- return Result::success($result);
- }
- }
|