Commit 4e639277 authored by clone's avatar clone

未读消息个数

parent 406215a5
......@@ -23,6 +23,7 @@ use app\model\HouseImgs;
use app\model\HouseinfoExts;
use app\model\HouseInfos;
use think\Cache;
use think\Exception;
use Think\Log;
class AppChat extends Basic
......@@ -323,13 +324,31 @@ class AppChat extends Basic
$params = $this->params;
/* $params = array(
"target" => "agent_5739",
"is_user"=> 0,//0用户1经纪人
"is_user" => 0,//0用户1经纪人
"relation_list" => '[ { "r_id" : 12, "msg_id" : 111}, { "r_id" : 11, "msg_id" : 111}]',
);*/
if (!isset($params['target']) || !isset($params["is_user"])) {
return $this->response("300", "参数不全");
}
$target = $params["target"];
$relationList = $this->_chat->getRelationList($target,$params["is_user"]);
$is_user = $params["is_user"];
$relation_list = null;
if (!empty($params["relation_list"])) {
try {
$relation_ = json_decode($params["relation_list"], true);
dump($relation_);
foreach ($relation_ as $item){
$relation_list[$item["r_id"]][] = $item;
}
} catch (Exception $exception) {
return $this->response("101", "解析关系异常:" . $exception);
}
}
dump($relation_list);
$relationList = $this->_chat->getRelationList($target, $is_user, $relation_list);
return $this->response("200", "success", $relationList);
......
......@@ -21,6 +21,7 @@ use app\model\ChatRelation;
use app\model\ChatUser;
use app\model\ChatUserExt;
use app\model\Users;
use think\Exception;
class ChatService
{
......@@ -381,12 +382,13 @@ class ChatService
/**获取聊天列表
* @param $target
* @param $is_user
* @param $relation_list
* @return array
* * @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function getRelationList($target, $is_user)
public function getRelationList($target, $is_user, $relation_list)
{
$chatRelationModel = new ChatRelation();
$params["target"] = $target;
......@@ -420,6 +422,21 @@ class ChatService
$select_["from"] = $item["from_id"];
$select_["target"] = $item["to_id"];
$chat_info = $msgModel->getChatHistory($select_, $fields, 1, 1);
if (!empty($relation_list)) {
$where_ = $select_;
try {
$where_["id"] = array( "between", array( $relation_list[$item["id"]][0]["msg_id"], $chat_info[0]["id"] ) );
//计算未读消息个数
$unread_count = $msgModel->getTotalUnread($where_, "id");
$result[$key]["unread"] = $unread_count;
} catch (Exception $exception) {
$result[$key]["unread"] = 0;
}
}
if (count($chat_info) > 0) {
$result[$key]["chat_info"] = $chat_info[0];
} else {
......@@ -430,6 +447,7 @@ class ChatService
return $this->sortByTime(array_values($result));
}
private function sortByTime($result)
{
for ($i = 0; $i < count($result); $i++) {
......
......@@ -78,4 +78,31 @@ class ChatMsg extends Model
return $data;
}
public function getTotalUnread($params, $field){
if (isset($params["from"])) {
$where_["a.from_id"] = $params["from"];
$where_or["a.to_id"] = $params["from"];
}
if (isset($params["target"])) {
$where_["a.to_id"] = $params["target"];
$where_or["a.from_id"] = $params["target"];
}
if (isset($params["id"])) {
$where_["a.id"] = $params["id"];
}
$where_["a.is_del"] = 0;
$data = $this->db->field($field)
->alias("a")
->join('chat_msg_ext b', 'a.id = b.msg_id', 'LEFT')
->where($where_)
->whereOr(function ($query) use ($where_or) {
$query->where($where_or);
})
->order("a.created_at desc")
->count();
return $data;
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment