diff --git a/niucloud/app/api/controller/apiController/Chat.php b/niucloud/app/api/controller/apiController/Chat.php new file mode 100644 index 00000000..e25c2266 --- /dev/null +++ b/niucloud/app/api/controller/apiController/Chat.php @@ -0,0 +1,189 @@ +param('personnel_id', '');//员工人力资源表id(两个参数2选1) + $customer_resources_id = $request->param('customer_resources_id', '');//学生资源表id(两个参数2选1) + if (empty($personnel_id) && empty($customer_resources_id)) { + return fail('缺少参数'); + } + + $where = [ + 'personnel_id' => $personnel_id, + 'customer_resources_id' => $customer_resources_id, + ]; + + $res = (new ChatService())->getChatFriendsPage($where); + + return success($res); + } + + //获取好友关系详情 + public function getChatFriendsInfo(Request $request) + { + $personnel_id = $request->param('personnel_id','');//员工人力资源表id + $customer_resources_id = $request->param('customer_resources_id','');//学生资源表id + + + if (empty($personnel_id) && empty($customer_resources_id)) { + return fail('缺少参数'); + } + + $where = [ + 'personnel_id' => $personnel_id, + 'customer_resources_id' => $customer_resources_id, + ]; + + $res = (new ChatService())->getChatFriendsInfo($where); + + if(!$res['data']){ + //创建好友关系 + $add = (new ChatService())->addChatFriends($where); + if(!$add['code']){ + return fail('创建好友关系失败'); + }else{ + $res = (new ChatService())->getChatFriendsInfo($where); + } + } + + if(!$res['code']){ + return fail($res['msg']); + } + + return success($res['data']); + } + + //创建好友关系 + public function addChatFriends(Request $request) + { + $personnel_id = $request->param('personnel_id', ''); + $customer_resources_id = $request->param('customer_resources_id', ''); + if (empty($personnel_id) || empty($customer_resources_id)) { + return fail('缺少参数'); + } + + $where = [ + 'personnel_id' => $personnel_id, + 'customer_resources_id' => $customer_resources_id, + ]; + + + //检测好友关系是否存在 + $getChatFriendsInfo = (new ChatService())->getChatFriendsInfo($where); + + if ($getChatFriendsInfo['data']) { + return fail('好友关系已存在'); + } + + $addChatFriends = (new ChatService())->addChatFriends($where); + if (!$addChatFriends['code']) { + return fail($addChatFriends['msg']); + } + return success($addChatFriends['data']); + } + + //获取聊天记录 + public function getChatMessagesList(Request $request) + { + + $friend_id = $request->param('friend_id', '');//关联chat_friends表id + + if (empty($friend_id)) { + return fail('发送者类型不正确'); + } + $where = [ + 'friend_id' => $friend_id, + ]; + $res = (new ChatService())->getChatMessagesList($where); + return success($res); + } + + //发送聊天内容 + public function sendChatMessages(Request $request) + { + $from_type = $request->param('from_type', '');//发送者类型|personnel=员工,customer=学生(客户) + + $from_id = $request->param('from_id', '');//发送者ID + + $to_id = $request->param('to_id', '');//接收者ID + + $friend_id = $request->param('friend_id', '');//关联chat_friends表id + + $message_type = $request->param('message_type', '');//消息类型|text=文本,img=图片 + + $content = $request->param('content', '');//文本内容(JSON 格式扩展字段),文本类型=纯文字,图片类型=绝对路径 + + if (!in_array($message_type, ['text', 'img'])) { + return fail('消息类型不正确'); + } + if (!in_array($from_type, ['personnel', 'customer'])) { + return fail('发送者类型不正确'); + } + if (empty($from_id) || empty($to_id) || empty($friend_id) || empty($content)) { + return fail('缺少参数'); + } + + $data = [ + 'from_type' => $from_type, // 发送者类型:personnel=员工,customer=学生(客户) + 'from_id' => $from_id, // 发送者ID(员工/学生),根据from_type判断 + 'to_id' => $to_id, // 接收者ID(员工/学生),根据from_type判断 + 'friend_id' => $friend_id, // 关联chat_friends表id + 'message_type' => $message_type, // 消息类型:text=文本,img=图片 + 'content' => $content // 文本内容(JSON格式扩展字段) + ]; + + $res = (new ChatService())->sendChatMessages($data); + if(!$res['code']){ + return fail($res['msg']); + } + return success($res['data']); + } + + //修改未读消息数量 + public function editUnreadCount(Request $request){ + $from_type = $request->param('from_type', '');//发送者类型|personnel=员工,customer=学生(客户) + + $from_id = $request->param('from_id', '');//发送者ID + + $friend_id = $request->param('friend_id', '');//关联chat_friends表id + + if (!in_array($from_type, ['personnel', 'customer'])) { + return fail('发送者类型不正确'); + } + if (empty($from_id) || empty($friend_id)) { + return fail('缺少参数'); + } + + $res = (new ChatService())->editUnreadCount($from_type,$friend_id); +// if(!$res){ +// return fail('修改失败'); +// } + return success($res); + } +} diff --git a/niucloud/app/api/route/route.php b/niucloud/app/api/route/route.php index 52ae6296..28a6e959 100644 --- a/niucloud/app/api/route/route.php +++ b/niucloud/app/api/route/route.php @@ -240,6 +240,18 @@ Route::group(function () { //员工考勤-编辑(员工打卡/请假/签退) Route::post('attendance/edit', 'apiController.Attendance/edit'); + //员工端-用户聊天-好友关系列表 + Route::get('chat/getChatFriendsList', 'apiController.Chat/getChatFriendsList'); + //员工端-用户聊天-创建好友关系 + Route::post('chat/addChatFriends', 'apiController.Chat/addChatFriends'); + //员工端-用户聊天-好友关系详情 + Route::get('chat/getChatFriendsInfo', 'apiController.Chat/getChatFriendsInfo'); + //员工端-用户聊天-获取聊天记录 + Route::get('chat/getChatMessagesList', 'apiController.Chat/getChatMessagesList'); + //员工端-用户聊天-发送聊天内容 + Route::post('chat/sendChatMessages', 'apiController.Chat/sendChatMessages'); + //员工端-用户聊天-修改未读消息数量 + Route::post('chat/editUnreadCount', 'apiController.Chat/editUnreadCount'); diff --git a/niucloud/app/model/chat_friends/ChatFriends.php b/niucloud/app/model/chat_friends/ChatFriends.php index bb68bf3e..33d14738 100644 --- a/niucloud/app/model/chat_friends/ChatFriends.php +++ b/niucloud/app/model/chat_friends/ChatFriends.php @@ -42,7 +42,7 @@ class ChatFriends extends BaseModel * 定义软删除标记字段. * @var string */ - protected $deleteTime = 'deleted_at'; + protected $deleteTime = 'delete_time'; /** * 定义软删除字段的默认值. * @var int diff --git a/niucloud/app/model/chat_messages/ChatMessages.php b/niucloud/app/model/chat_messages/ChatMessages.php index aaf46334..110ce03a 100644 --- a/niucloud/app/model/chat_messages/ChatMessages.php +++ b/niucloud/app/model/chat_messages/ChatMessages.php @@ -40,7 +40,7 @@ class ChatMessages extends BaseModel * 定义软删除标记字段. * @var string */ - protected $deleteTime = 'deleted_at'; + protected $deleteTime = 'delete_time'; /** * 定义软删除字段的默认值. * @var int diff --git a/niucloud/app/service/api/apiService/ChatService.php b/niucloud/app/service/api/apiService/ChatService.php new file mode 100644 index 00000000..fb568558 --- /dev/null +++ b/niucloud/app/service/api/apiService/ChatService.php @@ -0,0 +1,216 @@ +getPageParam();//获取请求参数中的页码+分页数 + $page = $page_params['page']; + $limit = $page_params['limit']; + + $model = new ChatFriends(); + //判断用没有员工id + if (!empty($where['personnel_id'])) { + $model = $model->where('personnel_id', $where['personnel_id']); + } + + if (!empty($where['customer_resources_id'])) { + $model = $model->where('customer_resources_id', $where['customer_resources_id']); + } + + $data = $model->paginate([ + 'list_rows' => $limit, + 'page' => $page, + ])->toArray(); + + return $data; + } + + //查询好友关系详情 + public function getChatFriendsInfo(array $where) + { + $model = new ChatFriends(); + //判断用没有员工id + if (!empty($where['personnel_id'])) { + $model = $model->where('personnel_id', $where['personnel_id']); + } + if (!empty($where['customer_resources_id'])) { + $model = $model->where('customer_resources_id', $where['customer_resources_id']); + } + $data = $model->find(); + + + if ($data) { + $data = $data->toArray(); + $res = [ + 'code' => 1, + 'msg' => '操作成功', + 'data' => $data + ]; + return $res; + } else { + $res = [ + 'code' => 0, + 'msg' => '暂无数据', + 'data' => [] + ]; + return $res; + } + } + + //创建好友关系 + public function addChatFriends(array $data) + { + $data = ChatFriends::create($data); + if($data){ + $res = [ + 'code' => 1, + 'msg' => '操作成功', + 'data' => $data + ]; + return $res; + }else{ + $res = [ + 'code' => 0, + 'msg' => '操作失败', + 'data' => [] + ]; + return $res; + } + } + + //发送聊天信息 + public function sendChatMessages(array $data){ + //开启事物操作 + Db::startTrans(); + try { + $add = ChatMessages::create($data); + + if(!empty($data['from_type']) && !empty($data['from_id']) && !empty($data['friend_id'])){ + $to_type = 'personnel'; + if($data['from_type'] == 'personnel'){ + $to_type = 'customer'; + } + $this->addUnreadCount($to_type,$data['friend_id']); + + $this->editUnreadCount($data['from_type'],$data['friend_id']); + } + + if($add){ + Db::commit(); + $res = [ + 'code' => 1, + 'msg' => '操作成功', + 'data' => $add->toArray() + ]; + return $res; + }else{ + Db::rollback(); + $res = [ + 'code' => 0, + 'msg' => '操作失败', + 'data' => [] + ]; + return $res; + } + }catch (\Exception $exception){ + Db::rollback(); + $res = [ + 'code' => 0, + 'msg' => '操作失败', + 'data' => [] + ]; + return $res; + } + } + + //获取聊天记录 + public function getChatMessagesList(array $where){ + $page_params = $this->getPageParam();//获取请求参数中的页码+分页数 + $page = $page_params['page']; + $limit = $page_params['limit']; + + $model = new ChatMessages(); + $model = $model->where('friend_id',$where['friend_id']); + + // 按id倒序排列 + $data = $model->order('id', 'desc')->paginate([ + 'list_rows' => $limit, + 'page' => $page, + ])->toArray(); + + return $data; + } + + /** + * 修改未读消息数量 + * @param $from_type 发送者类型|personnel=员工,customer=学生(客户) + * @param $from_id 发送者ID(员工/学生) + * @param $friend_id 关联chat_friends表id + */ + public function editUnreadCount($from_type ,$friend_id){ + $where = []; + if($from_type == 'personnel'){ + //员工发送的消息->把员工的未读消息数量清空 + $data['unread_count_personnel'] = 0; + }else{ + //学生发送的消息->把学生的未读消息数量清空 + $data['unread_count_customer_resources'] = 0; + } + $model = ChatFriends::where('id',$friend_id); + + $data['updated_at'] = date('Y-m-d H:i:s'); + $model = $model->update($data); + return $model; + } + + /** + * 追加接收消息的人未读消息数量+1 + * @param $to_type 接收者类型|personnel=员工,customer=学生(客户) + * @param $to_id 接收者ID(员工/学生) + * @param $friend_id 关联chat_friends表id + */ + public function addUnreadCount($to_type, $friend_id) + { + $model = ChatFriends::where('id', $friend_id); + + if ($to_type == 'personnel') { + // 员工接收的消息 -> 员工的未读消息数量+1 + return $model->inc('unread_count_personnel')->update(); + } else { + // 学生接收的消息 -> 学生的未读消息数量+1 + return $model->inc('unread_count_customer_resources')->update(); + } + } + +}