You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
216 lines
6.5 KiB
216 lines
6.5 KiB
<?php
|
|
// +----------------------------------------------------------------------
|
|
// | Niucloud-admin 企业快速开发的多应用管理平台
|
|
// +----------------------------------------------------------------------
|
|
// | 官方网址:https://www.niucloud.com
|
|
// +----------------------------------------------------------------------
|
|
// | niucloud团队 版权所有 开源版本可自由商用
|
|
// +----------------------------------------------------------------------
|
|
// | Author: Niucloud Team
|
|
// +----------------------------------------------------------------------
|
|
|
|
namespace app\service\api\apiService;
|
|
|
|
use app\model\campus\Campus;
|
|
use app\model\campus_person_role\CampusPersonRole;
|
|
use app\model\chat_friends\ChatFriends;
|
|
use app\model\chat_messages\ChatMessages;
|
|
use app\model\dict\Dict;
|
|
use core\base\BaseApiService;
|
|
use think\facade\Db;
|
|
|
|
/**
|
|
* 校区服务层
|
|
* Class MemberService
|
|
* @package app\service\api\member
|
|
*/
|
|
class ChatService extends BaseApiService
|
|
{
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
}
|
|
|
|
//查询好友关系列表
|
|
public function getChatFriendsPage(array $where)
|
|
{
|
|
$page_params = $this->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();
|
|
}
|
|
}
|
|
|
|
}
|
|
|