12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163 |
- <?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\ChatTopicClass;
- 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::leftJoin('chat_friends', function ($join) use ($userId) {
- $join->on('user.id', '=', 'chat_friends.friend_id')
- ->where('chat_friends.user_id', '=', $userId);
- })
- ->where(function ($query) use ($data) {
- $query->where('user.user_name', 'like', '%' . $data['keyword'] . '%')
- ->orWhere('user.nickname', 'like', '%' . $data['keyword'] . '%')
- ->orWhere('user.mobile', 'like', '%' . $data['keyword'] . '%');
- })
- ->where('user.id', '<>', $userId)
- ->where('user.type_id', '<>', '20000')
- ->select('user.*', 'chat_friends.remark as remark', 'chat_friends.id as isfriend')
- // ->select('user.*', 'chat_friends.friend_id')
- ->get();
- return Result::success($result);
- 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,
- ]
- );
- // 给对方发送通知
- $friendId = $fr['user_id'];
- $userId = $fr['friend_id'];
- $content = "你们已经成为好友了" . date("Y-m-d H:i:s");
- $chatRecordsData = [[
- 'user_id' => $userId,
- 'receiver_id' => $friendId,
- 'content' => $content,
- 'msg_type' => 1,
- 'is_read' => 1,
- 'talk_type' => 1,
- 'action' => 'said',
- ], [
- 'user_id' => $friendId,
- 'receiver_id' => $userId,
- 'content' => $content,
- 'msg_type' => 1,
- 'is_read' => 1,
- 'talk_type' => 1,
- 'action' => 'recieved',
- ]];
- ChatRecords::insert($chatRecordsData);
- 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::insert($info);
- var_dump($result, '--------------------');
- 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');
- $creatter = $topdata['user_id'];
- 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);
- } elseif ($data['status'] == 3) {
- ChatRecords::where('receiver_id', $topdata['group_id'])->delete();
- ChatGroupsMember::where('group_id', $topdata['group_id'])
- ->where([["user_id", '!=', $creatter]])->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')
- ->orderBy('chat_topics_reply.id', 'desc')
- ->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);
- }
- public function getTopicClassList(array $data): array
- {
- $where = [];
- if (!empty($data['topicname'])) {
- $where[] = ['topicname', 'like', '%' . $data['topicname'] . '%'];
- }
- $result = ChatTopicClass::where($where)->orderBy('updated_at', 'desc')
- ->paginate($data['page_size'], ['*'], 'page', $data['page'] ?? 1);
- return Result::success($result);
- }
- public function deleteTopicClass(array $data): array
- {
- $result = ChatTopicClass::where(['id' => $data['id']])->delete();
- if ($result) {
- return Result::success("删除成功");
- }
- return Result::error("删除失败");
- }
- public function updateTopicClass(array $data): array
- {
- //topicname
- if (!empty($data['topicname'])) {
- $re = ChatTopicClass::where(['topicname' => $data['topicname']])->first();
- if ($re) {
- return Result::error("话题分类已存在");
- }
- }
- $result = ChatTopicClass::where(['id' => $data['id']])->update([
- 'topicname' => $data['topicname'],
- 'updated_at' => date('Y-m-d H:i:s'),
- ]);
- if ($result) {
- return Result::success("修改成功");
- }
- return Result::error("修改失败");
- }
- public function addTopicClass(array $data): array
- {
- //topicname
- if (!empty($data['topicname'])) {
- $re = ChatTopicClass::where(['topicname' => $data['topicname']])->first();
- if ($re) {
- return Result::error("话题分类已存在");
- }
- }
- $result = ChatTopicClass::insert($data);
- if ($result) {
- return Result::success("添加成功");
- }
- return Result::error("添加失败");
- }
- /**
- * 获取话题分类信息
- * @param array $data
- * @return array
- */
- public function getTopicClassInfo(array $data): array
- {
- $result = ChatTopicClass::where(['id' => $data['id']])->first();
- return Result::success($result);
- }
- }
|