|
@@ -1,747 +1,959 @@
|
|
|
<?php
|
|
|
namespace App\JsonRpc;
|
|
|
-use App\Model\ChatChannel;
|
|
|
-use App\Model\ContactApply;
|
|
|
-use App\Model\Contact;
|
|
|
-use App\Model\TalkSession;
|
|
|
-use App\Model\TalkRecords;
|
|
|
-use App\Model\TalkRecordsFile;
|
|
|
-use App\Model\TalkGroup;
|
|
|
-use App\Model\TalkGroupMember;
|
|
|
-use App\Model\TalkSessionAssociation;
|
|
|
+
|
|
|
+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;
|
|
|
-use App\Tools\Result;
|
|
|
+
|
|
|
#[RpcService(name: "ChatService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
|
|
|
class ChatService implements ChatServiceInterface
|
|
|
{
|
|
|
/**
|
|
|
+ * 获取用户信息
|
|
|
* @param array $data
|
|
|
* @return array
|
|
|
*/
|
|
|
- public function getChatChannelList(array $data): array
|
|
|
+ public function getFriendInfo(array $data): array
|
|
|
{
|
|
|
- $result = ChatChannel::get();
|
|
|
+ $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 delChatChannel(array $data): array
|
|
|
+ public function searchFriend(array $data): array
|
|
|
{
|
|
|
- $result = ChatChannel::where(['fd'=>$data['fd']])->delete();
|
|
|
- if($result){
|
|
|
- return Result::success($data);
|
|
|
- }else{
|
|
|
- return Result::error($data);
|
|
|
+ $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 addChatChannel(array $data): array
|
|
|
+ public function addFriend(array $data): array
|
|
|
{
|
|
|
- $result = ChatChannel::insertGetId($data);
|
|
|
- if($result){
|
|
|
- return Result::success($data);
|
|
|
- }else{
|
|
|
- return Result::error($data);
|
|
|
+ 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 addContactApply(array $data): array
|
|
|
+ public function getFriendsList(array $data): array
|
|
|
{
|
|
|
- $result = ContactApply::insertGetId($data);
|
|
|
- if($result){
|
|
|
- return Result::success($result);
|
|
|
- }else{
|
|
|
- return Result::error('添加申请失败');
|
|
|
- }
|
|
|
+ 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 updateContactApply(array $data): array
|
|
|
+ public function applyFriend(array $data): array
|
|
|
{
|
|
|
- $type = $data['type'];
|
|
|
+ $status = $data['status'];
|
|
|
//判断同意还是不同意
|
|
|
- if($type == 1){
|
|
|
+ if ($status == 2) {
|
|
|
Db::beginTransaction();
|
|
|
- try{
|
|
|
- //同意 双方互为好友 -添加好友关系
|
|
|
- $data1 = [
|
|
|
- 'user_id'=>$data['user_id'],
|
|
|
- 'friend_id'=>$data['friend_id'],
|
|
|
- ];
|
|
|
- var_dump("好友关系表insert",$data1);
|
|
|
- Contact::insertGetId($data1);
|
|
|
- $data2 = [
|
|
|
- 'user_id'=>$data['friend_id'],
|
|
|
- 'friend_id'=>$data['user_id'],
|
|
|
- ];
|
|
|
- var_dump("好友关系表insert22",$data1);
|
|
|
- Contact::insertGetId($data2);
|
|
|
- $where1 = [
|
|
|
- 'friend_id'=>$data['user_id'],
|
|
|
- 'user_id'=>$data['friend_id'],
|
|
|
+ try {
|
|
|
+ $where = [
|
|
|
+ 'id' => $data['id'],
|
|
|
+ 'status' => 1,
|
|
|
];
|
|
|
- var_dump("申请记录删除:",$data1);
|
|
|
- ContactApply::where($where1)->delete();
|
|
|
- var_dump("会话id:",PublicData::uuid());
|
|
|
- //创建会话
|
|
|
- $sessionData = [
|
|
|
- 'id'=>PublicData::uuid(),
|
|
|
- 'talk_type'=>'1',
|
|
|
- ];
|
|
|
- TalkSession::insert($sessionData);
|
|
|
-
|
|
|
- //添加会话关系
|
|
|
- $talkSessionAssociationData = [
|
|
|
- [
|
|
|
- 'id'=>PublicData::uuid(),
|
|
|
- 'user_id'=>$data['user_id'],
|
|
|
- 'to_user_id'=>$data['friend_id'],
|
|
|
- 'session_id'=>$sessionData['id']
|
|
|
- ]
|
|
|
- ,[
|
|
|
- 'id'=>PublicData::uuid(),
|
|
|
- 'user_id'=>$data['friend_id'],
|
|
|
- 'to_user_id'=>$data['user_id'],
|
|
|
- 'session_id'=>$sessionData['id']
|
|
|
- ]
|
|
|
-
|
|
|
- ];
|
|
|
- var_dump("创建会话:",$talkSessionAssociationData);
|
|
|
- TalkSessionAssociation::insert($talkSessionAssociationData);
|
|
|
+ $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){
|
|
|
+
|
|
|
+ } catch (\Throwable $ex) {
|
|
|
Db::rollBack();
|
|
|
var_dump($ex->getMessage());
|
|
|
- return Result::error("同意添加为好友失败",0);
|
|
|
+ return Result::error("同意添加为好友失败", 0);
|
|
|
}
|
|
|
- }else if($type == 2){
|
|
|
+ return Result::success(['添加成功']);
|
|
|
+ } else if ($status == 4) { //拒绝
|
|
|
Db::beginTransaction();
|
|
|
- try{
|
|
|
+ try {
|
|
|
$where1 = [
|
|
|
- 'friend_id'=>$data['user_id'],
|
|
|
- 'user_id'=>$data['friend_id'],
|
|
|
+ 'id' => $data['id'],
|
|
|
];
|
|
|
- ContactApply::where($where1)->delete();
|
|
|
- Db::commit();
|
|
|
- } catch(\Throwable $ex){
|
|
|
+ $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::error("拒绝添加好友失败", 0);
|
|
|
}
|
|
|
+ return Result::success(['已经拒绝']);
|
|
|
}
|
|
|
- return Result::success([]);
|
|
|
-
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 好友申请列表
|
|
|
+ * 删除好友
|
|
|
* @param array $data
|
|
|
* @return array
|
|
|
*/
|
|
|
- public function getContactApplyList(array $data): array
|
|
|
+ public function delFriend(array $data): array
|
|
|
{
|
|
|
$where = [
|
|
|
- 'friend_id'=>$data['user_id']
|
|
|
+ 'user_id' => $data['user_id'],
|
|
|
+ 'friend_id' => $data['friend_id'],
|
|
|
];
|
|
|
- $result = ContactApply::where($where)->get();
|
|
|
- return Result::success($result);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 好有查询
|
|
|
- * @param array $data
|
|
|
- * @return array
|
|
|
- */
|
|
|
- public function getContactList(array $data): array
|
|
|
- {
|
|
|
- $where = [
|
|
|
- ['contact.status','=','1'],
|
|
|
- ['contact.user_id','=',$data['user_id']],
|
|
|
+ $orwhere = [
|
|
|
+ 'friend_id' => $data['user_id'],
|
|
|
+ 'user_id' => $data['friend_id'],
|
|
|
];
|
|
|
- if(isset($data['pageSize'])){
|
|
|
- $result = Contact::where($where)
|
|
|
- ->where(function ($query) use ($data) {
|
|
|
- $query->where('user.nickname', 'like','%'.$data['keyWord'].'%')
|
|
|
- ->orWhere('contact.remark', 'like','%'.$data['keyWord'].'%');
|
|
|
- })
|
|
|
- ->leftJoin('user', 'user.id', '=', 'contact.friend_id')
|
|
|
- ->select('contact.*', 'user.nickname','user.avatar')
|
|
|
- ->limit($data['pageSize'])->offset(($data['page']-1)*$data['pageSize'])->orderBy("contact.created_at","desc")->get();
|
|
|
- }else{
|
|
|
- $result = Contact::where($where)
|
|
|
- ->where(function ($query) use ($data) {
|
|
|
- $query->where('user.nickname', 'like','%'.$data['keyWord'].'%')
|
|
|
- ->orWhere('contact.remark', 'like','%'.$data['keyWord'].'%');
|
|
|
- })
|
|
|
- ->leftJoin('user', 'user.id', '=', 'contact.friend_id')
|
|
|
- ->select('contact.*', 'user.nickname','user.avatar')
|
|
|
- ->get();
|
|
|
+ $result = ChatFriends::where($where)
|
|
|
+ ->orWhere($orwhere)->delete();
|
|
|
+ var_dump($result, '-0------------------');
|
|
|
+ if ($result) {
|
|
|
+ return Result::success("删除成功”");
|
|
|
+ } else {
|
|
|
+ return Result::error('删除失败');
|
|
|
}
|
|
|
-
|
|
|
- $count = Contact::where($where)
|
|
|
- ->where(function ($query) use ($data) {
|
|
|
- $query->where('user.nickname', 'like','%'.$data['keyWord'].'%')
|
|
|
- ->orWhere('contact.remark', 'like','%'.$data['keyWord'].'%');
|
|
|
- })
|
|
|
- ->leftJoin('user', 'user.id', '=', 'contact.friend_id')
|
|
|
- ->count();
|
|
|
- if (empty($result)) {
|
|
|
- return Result::error("没有数据",0);
|
|
|
- }
|
|
|
- $rep = $result->toArray();
|
|
|
- $data = [
|
|
|
- 'rows'=>$rep,
|
|
|
- 'count'=>$count
|
|
|
- ];
|
|
|
- return Result::success($data);
|
|
|
}
|
|
|
/**
|
|
|
- * 添加好友
|
|
|
+ * 是否好友
|
|
|
* @param array $data
|
|
|
* @return array
|
|
|
*/
|
|
|
- public function addContact(array $data): array
|
|
|
+ public function isFriend(array $data): array
|
|
|
{
|
|
|
- $result = Contact::insertGetId($data);
|
|
|
- if($result){
|
|
|
- return Result::success($data);
|
|
|
- }else{
|
|
|
- return Result::error($data);
|
|
|
+ $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 updateContact(array $data): array
|
|
|
+ public function addChatRecords(array $data): array
|
|
|
{
|
|
|
- $where = [
|
|
|
- 'id'=>$data['id']
|
|
|
- ];
|
|
|
- $result = Contact::where($where)->update($data);
|
|
|
- if($result){
|
|
|
- return Result::success($data);
|
|
|
- }else{
|
|
|
- return Result::error($data);
|
|
|
+ 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 delContact(array $data): array
|
|
|
+ public function updateFriend(array $data): array
|
|
|
{
|
|
|
- $where = [
|
|
|
- 'user_id'=>$data['user_id'],
|
|
|
- 'friend_id'=>$data['friend_id'],
|
|
|
- ];
|
|
|
- $result = Contact::where($where)->update(['status'=>2]);
|
|
|
- if($result){
|
|
|
- return Result::success($result);
|
|
|
- }else{
|
|
|
- return Result::error('删除失败');
|
|
|
+ $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 getTalkSessionList(array $data): array
|
|
|
+ public function getConversation(array $data): array
|
|
|
{
|
|
|
- $where = [
|
|
|
- 'talk_session_association.status'=>'1',
|
|
|
- 'talk_session_association.user_id'=>$data['user_id']
|
|
|
- ];
|
|
|
- $result = TalkSessionAssociation::where($where)
|
|
|
- ->leftJoin('talk_session','talk_session.id','talk_session_association.session_id')
|
|
|
- ->leftJoin('user','user.id','talk_session_association.to_user_id')
|
|
|
- ->leftJoin('talk_group','talk_group.id','talk_session_association.to_user_id')
|
|
|
+ $userId = $data['user_id'];
|
|
|
+ $unreadMessages = ChatRecords::where('user_id', $userId)
|
|
|
+ ->where('is_read', 0)
|
|
|
+ ->leftJoin('user', 'chat_records.receiver_id', '=', 'user.id')
|
|
|
+ ->leftJoin('chat_groups', 'chat_records.receiver_id', '=', 'chat_groups.id')
|
|
|
->select(
|
|
|
- 'talk_session.talk_type',
|
|
|
- 'talk_group.group_name',
|
|
|
- 'talk_group.avatar as group_avatar',
|
|
|
- 'user.avatar as user_avatar',
|
|
|
- 'user.nickname',
|
|
|
- 'talk_session.last_content',
|
|
|
- 'user.user_name',
|
|
|
- 'talk_session.id as session_id',
|
|
|
- 'talk_session.talk_type',
|
|
|
- 'user.id as user_id',
|
|
|
- 'talk_group.id as group_id'
|
|
|
+ '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'
|
|
|
)
|
|
|
- ->limit($data['pageSize'])->offset(($data['page']-1)*$data['pageSize'])->orderBy("talk_session.updated_at","desc")->get();
|
|
|
- $count = TalkSessionAssociation::where($where)
|
|
|
- ->leftJoin('talk_session','talk_session.id','talk_session_association.session_id')
|
|
|
- ->leftJoin('user','user.id','talk_session_association.to_user_id')
|
|
|
- ->leftJoin('talk_group','talk_group.id','talk_session_association.to_user_id')
|
|
|
- ->count();
|
|
|
- $repData = [
|
|
|
- 'row'=>$result,
|
|
|
- 'count'=>$count
|
|
|
- ];
|
|
|
- return Result::success($repData);
|
|
|
- }
|
|
|
- /**
|
|
|
- * 添加会话
|
|
|
- * @param array $data
|
|
|
- * @return array
|
|
|
- */
|
|
|
- public function addTalkSession(array $data): array
|
|
|
- {
|
|
|
- $result = TalkSession::insert($data);
|
|
|
- if($result){
|
|
|
- return Result::success(['id'=>$data['id']]);
|
|
|
- }else{
|
|
|
- return Result::error('创建会话失败');
|
|
|
+ ->groupBy('receiver_id')
|
|
|
+ ->orderBy(DB::raw('MAX(chat_records.id)'), 'desc')
|
|
|
+ ->get();
|
|
|
+
|
|
|
+ // 查询已读消息,并将 num 字段设置为 0
|
|
|
+ $readMessages = ChatRecords::where('user_id', $userId)
|
|
|
+ ->where('is_read', 1)
|
|
|
+ ->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());
|
|
|
+ // 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;
|
|
|
}
|
|
|
- }
|
|
|
|
|
|
- /**
|
|
|
- * 更新会话
|
|
|
- * @param array $data
|
|
|
- * @return array
|
|
|
- */
|
|
|
- public function updateTalkSession(array $data): array
|
|
|
- {
|
|
|
- $where = [
|
|
|
- 'id'=>$data['id']
|
|
|
- ];
|
|
|
- $result = TalkSession::where($where)->update($data);
|
|
|
- if($result){
|
|
|
- return Result::success($data);
|
|
|
- }else{
|
|
|
- return Result::error($data);
|
|
|
+ if (!empty($formattedMessages)) {
|
|
|
+ return Result::success($formattedMessages);
|
|
|
+ } else {
|
|
|
+ return Result::error('没有消息');
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
/**
|
|
|
- * 删除会话
|
|
|
+ * 获取聊天记录
|
|
|
* @param array $data
|
|
|
* @return array
|
|
|
*/
|
|
|
- public function delTalkSession(array $data): array
|
|
|
+ public function getChatRecords(array $data): array
|
|
|
{
|
|
|
- $where = [
|
|
|
- 'id'=>$data['id']
|
|
|
- ];
|
|
|
- $result = TalkSession::where($where)->delete();
|
|
|
- if($result){
|
|
|
- return Result::success($data);
|
|
|
- }else{
|
|
|
- return Result::error($data);
|
|
|
+ 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 getTalkRecordsList(array $data): array
|
|
|
+ public function getGroupChatRecords(array $data): array
|
|
|
{
|
|
|
- $talkSessionAssociationInfo = TalkSessionAssociation::where(['session_id'=>$data['session_id'],'to_user_id'=>$data['user_id']])->first();
|
|
|
- $where = [
|
|
|
- 'talk_records.session_id' =>$data['session_id'],
|
|
|
- ];
|
|
|
- if($talkSessionAssociationInfo['status'] == 2){
|
|
|
- $where[] = ['talk_records.created_at','>=',$talkSessionAssociationInfo['del_at']];
|
|
|
- }
|
|
|
- $result = TalkRecords::where($where)
|
|
|
- ->leftJoin('user','user.id','talk_records.user_id')
|
|
|
- ->select("user.avatar","talk_records.*")
|
|
|
- ->limit($data['pageSize'])
|
|
|
- ->offset(($data['page']-1)*$data['pageSize'])
|
|
|
- ->orderBy("talk_session.updated_at","desc")
|
|
|
- ->get();
|
|
|
-
|
|
|
- return Result::success($result);
|
|
|
+ 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 addTalkRecords(array $data): array
|
|
|
+ public function addGroup(array $data): array
|
|
|
{
|
|
|
Db::beginTransaction();
|
|
|
- try{;
|
|
|
- //添加会话内容
|
|
|
- $talkRecordsData = [
|
|
|
- 'id'=>PublicData::uuid(),
|
|
|
- 'msg_type'=>$data['msg_type']??0,
|
|
|
- 'user_id'=>$data['user_id']??0,
|
|
|
- 'session_id'=>$data['session_id']??'',
|
|
|
- 'talk_type'=>$data['talk_type']??0,
|
|
|
- 'content'=>$data['content']??'',
|
|
|
- 'receiver_id'=>$data['receiver_id']??''
|
|
|
+ try {
|
|
|
+ //创建群
|
|
|
+ $groupData = [
|
|
|
+ 'id' => PublicData::uuid(),
|
|
|
+ 'creator_id' => $data['user_id'],
|
|
|
+ 'group_name' => $data['group_name'],
|
|
|
+ 'avatar' => $data['avatar'] ?? '',
|
|
|
+ 'profile' => $data['profile'] ?? '',
|
|
|
];
|
|
|
-
|
|
|
- TalkRecords::insert($talkRecordsData);
|
|
|
- $fileData = [
|
|
|
- 'id'=>PublicData::uuid(),
|
|
|
- 'record_id'=>$talkRecordsData['id']??'',
|
|
|
- 'user_id'=>$data['user_id']??0,
|
|
|
- 'source'=>$data['source']??'',
|
|
|
- 'type'=>$data['type']??0,
|
|
|
- 'drive'=>$data['drive']??'',
|
|
|
- 'original_name'=>$data['original_name']??'',
|
|
|
- 'suffix'=>$data['suffix']??'',
|
|
|
- 'size'=>$data['size']??0,
|
|
|
- 'path'=>$data['path']??'',
|
|
|
+ ChatGroups::insert($groupData);
|
|
|
+ //创建群用户
|
|
|
+ $groupMemberData = [];
|
|
|
+ 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,
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ 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'],
|
|
|
];
|
|
|
- var_dump($fileData);
|
|
|
- TalkRecordsFile::insert($fileData);
|
|
|
+ ChatRecords::insert($chatRecordsData);
|
|
|
Db::commit();
|
|
|
- } catch(\Throwable $ex){
|
|
|
+ } catch (\Throwable $ex) {
|
|
|
Db::rollBack();
|
|
|
var_dump($ex->getMessage());
|
|
|
- return Result::error("存储消息失败",0);
|
|
|
+ return Result::error("创建群失败", 0);
|
|
|
}
|
|
|
-
|
|
|
return Result::success([]);
|
|
|
}
|
|
|
-
|
|
|
- /**
|
|
|
- * 更新聊天内容
|
|
|
- * @param array $data
|
|
|
- * @return array
|
|
|
- */
|
|
|
- public function updateTalkRecords(array $data): array
|
|
|
- {
|
|
|
- $where = [
|
|
|
- 'id'=>$data['id']
|
|
|
- ];
|
|
|
- $result = TalkRecords::where($where)->update($data);
|
|
|
- if($result){
|
|
|
- return Result::success($data);
|
|
|
- }else{
|
|
|
- return Result::error($data);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
- * 删除聊天内容
|
|
|
+ * 群组 - 加入群
|
|
|
* @param array $data
|
|
|
* @return array
|
|
|
*/
|
|
|
- public function delTalkRecords(array $data): array
|
|
|
+ public function addGroupMember(array $data): array
|
|
|
{
|
|
|
- $where = [
|
|
|
- 'id'=>$data['id']
|
|
|
- ];
|
|
|
- $result = TalkRecords::where($where)->delete();
|
|
|
- if($result){
|
|
|
- return Result::success($data);
|
|
|
- }else{
|
|
|
- return Result::error($data);
|
|
|
+ $result = ChatGroupsMember::where(['group_id' => $data['group_id'], 'user_id' => $data['user_id']])->update(['leader' => 2]);
|
|
|
+ if ($result) {
|
|
|
+ return Result::success('修改成功');
|
|
|
+ } else {
|
|
|
+ return Result::error('修改失败');
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
/**
|
|
|
- * 聊天内容-附件列表
|
|
|
+ * 群组 - 群信息
|
|
|
* @param array $data
|
|
|
* @return array
|
|
|
*/
|
|
|
- public function getTalkRecordsFileList(array $data): array
|
|
|
+ public function getGroupInfo(array $data): array
|
|
|
{
|
|
|
- $result = TalkRecordsFile::get();
|
|
|
+ $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 addTalkRecordsFile(array $data): array
|
|
|
+ public function delGroup(array $data): array
|
|
|
{
|
|
|
- $result = TalkRecordsFile::insertGetId($data);
|
|
|
- if($result){
|
|
|
- return Result::success($data);
|
|
|
- }else{
|
|
|
- return Result::error($data);
|
|
|
+ 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);
|
|
|
}
|
|
|
- }
|
|
|
|
|
|
- /**
|
|
|
- * 聊天内容 - 更新附件
|
|
|
- * @param array $data
|
|
|
- * @return array
|
|
|
- */
|
|
|
- public function updateTalkRecordsFile(array $data): array
|
|
|
- {
|
|
|
- $where = [
|
|
|
- 'id'=>$data['id']
|
|
|
- ];
|
|
|
- $result = TalkRecordsFile::where($where)->update($data);
|
|
|
- if($result){
|
|
|
- return Result::success($data);
|
|
|
- }else{
|
|
|
- return Result::error($data);
|
|
|
+ if ($result) {
|
|
|
+ return Result::success('删除成功');
|
|
|
+ } else {
|
|
|
+ return Result::error('删除失败');
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
/**
|
|
|
- * 聊天内容 - 删除附件
|
|
|
+ * 群组 - 退出群
|
|
|
* @param array $data
|
|
|
* @return array
|
|
|
*/
|
|
|
- public function delTalkRecordsFile(array $data): array
|
|
|
+ public function quitGroup(array $data): array
|
|
|
{
|
|
|
- $where = [
|
|
|
- 'id'=>$data['id']
|
|
|
- ];
|
|
|
- $result = TalkRecordsFile::where($where)->delete();
|
|
|
- if($result){
|
|
|
- return Result::success($data);
|
|
|
- }else{
|
|
|
- return Result::error($data);
|
|
|
+ $result = ChatGroupsMember::where(['group_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 getTalkGroupList(array $data): array
|
|
|
+ public function getGroupList(array $data): array
|
|
|
{
|
|
|
- $result = TalkGroup::get();
|
|
|
+ $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 addTalkGroup(array $data): array
|
|
|
+ public function delGroupMembers(array $data): array
|
|
|
{
|
|
|
- Db::beginTransaction();
|
|
|
- try{
|
|
|
- //创建群
|
|
|
- $groupData = [
|
|
|
- 'id' => PublicData::uuid(),
|
|
|
- 'creator_id'=>$data['user_id'],
|
|
|
- 'group_name'=>$data['group_name']
|
|
|
- ];
|
|
|
- TalkGroup::insert($groupData);
|
|
|
-
|
|
|
- //创建群用户
|
|
|
- $groupMemberData = [];
|
|
|
- if($data['user_id_arr']){
|
|
|
- foreach ($data['user_id_arr'] as $key=>$val){
|
|
|
- $groupMemberData[$key] = [
|
|
|
- 'id' => PublicData::uuid(),
|
|
|
- 'group_id' => $groupData['id'],
|
|
|
- 'user_id' =>$val,
|
|
|
- 'leader' =>$data['user_id'] == $val?2:0,
|
|
|
- ];
|
|
|
- }
|
|
|
- }
|
|
|
- TalkGroupMember::insert($groupMemberData);
|
|
|
- //创建会话
|
|
|
- $sessionData = [
|
|
|
- 'id' => PublicData::uuid(),
|
|
|
- 'talk_type' => '2'
|
|
|
- ];
|
|
|
- TalkSession::insertGetId($sessionData);
|
|
|
- //创建会话关系
|
|
|
- $talkSessionAssociationData = [];
|
|
|
- if($data['user_id_arr']){
|
|
|
- foreach ($data['user_id_arr'] as $key=>$val){
|
|
|
- $talkSessionAssociationData[$key] = [
|
|
|
- 'id' => PublicData::uuid(),
|
|
|
- 'user_id' => $val,
|
|
|
- 'to_user_id' => $groupData['id'],
|
|
|
- 'session_id' =>$sessionData['id']
|
|
|
- ];
|
|
|
- }
|
|
|
- }
|
|
|
- TalkSessionAssociation::insert($talkSessionAssociationData);
|
|
|
- Db::commit();
|
|
|
- } catch(\Throwable $ex){
|
|
|
- Db::rollBack();
|
|
|
- var_dump($ex->getMessage());
|
|
|
- return Result::error("创建群失败",0);
|
|
|
+ $result = ChatGroupsMember::where(['group_id' => $data['group_id'], 'user_id' => $data['user_id']])->delete();
|
|
|
+ if ($result) {
|
|
|
+ return Result::success('删除成功');
|
|
|
+ } else {
|
|
|
+ return Result::error('删除失败');
|
|
|
}
|
|
|
- return Result::success([]);
|
|
|
}
|
|
|
-
|
|
|
/**
|
|
|
* 群组 - 更新群
|
|
|
* @param array $data
|
|
|
* @return array
|
|
|
*/
|
|
|
- public function updateTalkGroup(array $data): array
|
|
|
+ public function updateGroup(array $data): array
|
|
|
{
|
|
|
- $where = [
|
|
|
- 'id'=>$data['id']
|
|
|
- ];
|
|
|
- $result = TalkGroup::where($where)->update($data);
|
|
|
- if($result){
|
|
|
- return Result::success($data);
|
|
|
- }else{
|
|
|
- return Result::error($data);
|
|
|
+ // 提取需要的字段
|
|
|
+ $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 delTalkGroup(array $data): array
|
|
|
+ public function deleteGroup(array $data): array
|
|
|
{
|
|
|
- $where = [
|
|
|
- 'id'=>$data['id']
|
|
|
- ];
|
|
|
- $result = TalkGroup::where($where)->delete();
|
|
|
- if($result){
|
|
|
- return Result::success($data);
|
|
|
- }else{
|
|
|
- return Result::error($data);
|
|
|
+ $result = ChatGroups::where(['id' => $data['group_id']])->delete();
|
|
|
+ if ($result) {
|
|
|
+ return Result::success('删除成功');
|
|
|
+ } else {
|
|
|
+ return Result::error('删除失败');
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
/**
|
|
|
* 群组 - 群用户列表
|
|
|
* @param array $data
|
|
|
* @return array
|
|
|
*/
|
|
|
- public function getTalkGroupMember(array $data): array
|
|
|
+ public function getGroupMembers(array $data): array
|
|
|
{
|
|
|
- $where = [
|
|
|
- 'group_id'=>$data['group_id']
|
|
|
- ];
|
|
|
- $result = TalkGroupMember::where($where)->get();
|
|
|
- return Result::success($result);
|
|
|
+ $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 getAllTalkGroupMember(array $data): array
|
|
|
+ public function joinGroup(array $data): array
|
|
|
{
|
|
|
- $where = [
|
|
|
- 'group_id'=>$data['group_id']
|
|
|
+ $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 = TalkGroupMember::where($where)->get();
|
|
|
- return Result::success($result);
|
|
|
+ $result = ChatGroupsMember::insertGetId($info);
|
|
|
+ if ($result) {
|
|
|
+ return Result::success($data);
|
|
|
+ } else {
|
|
|
+ return Result::error($data);
|
|
|
+ };
|
|
|
}
|
|
|
/**
|
|
|
- * 群组 - 创建群用户
|
|
|
+ * 话题 - 列表
|
|
|
* @param array $data
|
|
|
* @return array
|
|
|
*/
|
|
|
- public function addTalkGroupMember(array $data): array
|
|
|
+ public function getTopicsList(array $data): array
|
|
|
{
|
|
|
- $result = TalkGroupMember::insertGetId($data);
|
|
|
- if($result){
|
|
|
- return Result::success($data);
|
|
|
- }else{
|
|
|
- return Result::error($data);
|
|
|
+
|
|
|
+ $where = [];
|
|
|
+ if (!empty($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['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);
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 群组 - 更新群用户
|
|
|
- * @param array $data
|
|
|
- * @return array
|
|
|
- */
|
|
|
- public function updateTalkGroupMember(array $data): array
|
|
|
+ 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);
|
|
|
+
|
|
|
+ //插入一条消息
|
|
|
+ $chatRecordsData = [
|
|
|
+ 'user_id' => $data['user_id'],
|
|
|
+ 'receiver_id' => $group_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);
|
|
|
+ // 查询 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 updateTopic(array $data): array
|
|
|
{
|
|
|
- $where = [
|
|
|
- 'id'=>$data['id']
|
|
|
- ];
|
|
|
- $result = TalkGroupMember::where($where)->update($data);
|
|
|
- if($result){
|
|
|
+ $result = ChatTopic::where(['id' => $data['id']])->update($data);
|
|
|
+ if ($result) {
|
|
|
return Result::success($data);
|
|
|
- }else{
|
|
|
+ } 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);
|
|
|
|
|
|
- /**
|
|
|
- * 群组 - 删除群用户
|
|
|
- * @param array $data
|
|
|
- * @return array
|
|
|
- */
|
|
|
- public function delTalkGroupMember(array $data): array
|
|
|
+ }
|
|
|
+ public function addReply(array $data): array
|
|
|
{
|
|
|
- $where = [
|
|
|
- 'id'=>$data['id']
|
|
|
- ];
|
|
|
- $result = TalkGroupMember::where($where)->delete();
|
|
|
- if($result){
|
|
|
+ $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{
|
|
|
+ } 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 updateTalkSessionAssociation(array $data): array
|
|
|
+ public function updateGroupMembers(array $data): array
|
|
|
{
|
|
|
$where = [
|
|
|
- 'id'=>$data['id']
|
|
|
+ 'group_id' => $data['group_id'],
|
|
|
];
|
|
|
- $result = TalkSessionAssociation::where($where)->update($data);
|
|
|
- if($result){
|
|
|
- return Result::success($data);
|
|
|
- }else{
|
|
|
+ $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);
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- /**
|
|
|
- * 添加会话关系
|
|
|
- * @param array $data
|
|
|
- * @return array
|
|
|
- */
|
|
|
- public function addTalkSessionAssociation(array $data): array
|
|
|
+ public function clearGroupRecords(array $data): array
|
|
|
{
|
|
|
- var_dump($data);
|
|
|
- $result = TalkSessionAssociation::insertGetId($data);
|
|
|
- if($result){
|
|
|
- return Result::success($data);
|
|
|
- }else{
|
|
|
- return Result::error($data);
|
|
|
+ $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);
|
|
|
+ }
|
|
|
+}
|