Commit 257f13f3 authored by zw's avatar zw

chat

parent cd4bf09a
......@@ -98,17 +98,17 @@ class AppChat extends Basic
/*if ($params['is_self'] == 1 && (!isset($params['device_id']) || !isset($params['push_id']))) {
return $this->response(ErrorCodeConst::ERROR_CODE_PARAM_NOT_EXIST, "请求参数错误1");
}*/
$user_id = $params['user_id'];
$mobile = $params['mobile'];
$source = $params['source'];
$user_id = $params['user_id'];
$mobile = $params['mobile'];
$source = $params['source'];
$device_id = !isset($params['device_id']) ? null : $params['device_id'];
$push_id = !isset($params['push_id']) ? null : $params['push_id'];
$push_id = !isset($params['push_id']) ? null : $params['push_id'];
$only_arr = $this->_chat->register($user_id, $mobile, $source, $device_id, $push_id);
if ($only_arr["code"] == 200) {
return $this->response("200", "success", [ "only_id" => $only_arr["only_id"] ]);
return $this->response("200", "success", ["only_id" => $only_arr["only_id"]]);
} else {
return $this->response("101", $only_arr["msg"]);
}
......@@ -141,17 +141,17 @@ class AppChat extends Basic
if ($params['is_self'] == 1 && (!isset($params['device_id']) || !isset($params['push_id']))) {
return $this->response(ErrorCodeConst::ERROR_CODE_PARAM_NOT_EXIST, "请求参数错误1");
}
$user_id = $params['user_id'];
$mobile = $params['mobile'];
$source = $params['source'];
$user_id = $params['user_id'];
$mobile = $params['mobile'];
$source = $params['source'];
$device_id = !isset($params['device_id']) ? null : $params['device_id'];
$push_id = !isset($params['push_id']) ? null : $params['push_id'];
$push_id = !isset($params['push_id']) ? null : $params['push_id'];
$only_arr = $this->_chat->register($user_id, $mobile, $source, $device_id, $push_id);
if ($only_arr["code"] == 200) {
return $this->response("200", "success", [ "only_id" => $only_arr["only_id"] ]);
return $this->response("200", "success", ["only_id" => $only_arr["only_id"]]);
} else {
return $this->response("101", $only_arr["msg"]);
}
......@@ -211,23 +211,23 @@ class AppChat extends Basic
return $this->response(ErrorCodeConst::ERROR_CODE_PARAM_NOT_EXIST, $checkResult);
}
$user_name = isset($params['user_name']) ? $params['user_name'] : $params['from'];//用户昵称
$user_name = isset($params['user_name']) ? $params['user_name'] : $params['from'];//用户昵称
$target_type = $params['target_type']; // 消息类型 users 给用户发消息。chatgroups: 给群发消息,chatrooms: 给聊天室发消息
$target = $params['target']; //接受人 if target_type 群 者表示群id
$source = $params['source']; //消息来源 1c端app 2b端app 3其他
$is_user = $params['is_user']; //发送人是否是会员 0是1经济人
$type = $params['type']; //消息类型 1文字 2图片 3楼盘
$target = $params['target']; //接受人 if target_type 群 者表示群id
$source = $params['source']; //消息来源 1c端app 2b端app 3其他
$is_user = $params['is_user']; //发送人是否是会员 0是1经济人
$type = $params['type']; //消息类型 1文字 2图片 3楼盘
$msg_content = $params['msg_content'];
$from = $params['from']; //消息发送人
$from = $params['from']; //消息发送人
if ($target_type != "users" && $target_type != "chatgroups" && $target_type != "chatrooms") {
return $this->response(ErrorCodeConst::ERROR_CODE_TARGET_TYPE_ERROR, "错误的消息类型");
}
$result = $this->_chat->sendMsg($user_name, $target_type, $target, $source, $is_user, $type, $msg_content, $from);
if ($result) {
return $this->response("200", "", [ "msg" => "消息发送成功" ]);
return $this->response("200", "", ["msg" => "消息发送成功"]);
} else {
return $this->response("101", "", [ "msg" => "消息发送失败" ]);
return $this->response("101", "", ["msg" => "消息发送失败"]);
}
}
......@@ -239,24 +239,33 @@ class AppChat extends Basic
public function getChatHistory()
{
$params = $this->params;
/* $params = array(
'target_type' => 'users', // 消息类型 users 用户消息。chatgroups: 群消息,chatrooms: 聊天室消息
'from' => 'agent_5740', //发送人
'last_msg_id' => 123, //最后一条消息id 默认0 表示不处理
'target' => 'agent_5741', //接受人
'page_no' => '1', //第几页
'page_size' => '15' //每页多少条
);*/
/* $params = array(
'target_type' => 'users', // 消息类型 users 用户消息。chatgroups: 群消息,chatrooms: 聊天室消息
'from' => 'agent_5740', //发送人
'last_msg_id' => 123, //最后一条消息id 默认0 表示不处理
'target' => 'agent_5741', //接受人
'page_no' => '1', //第几页
'page_size' => '15' //每页多少条
);*/
if (!isset($params['target_type']) || !isset($params['from']) || !isset($params['target']) || !isset($params['last_msg_id'])) {
return $this->response(ErrorCodeConst::ERROR_CODE_PARAM_NOT_EXIST, "请求参数错误");
}
$msgModel = new ChatMsg();
//进入详情页修改已读消息 只修改自己收到的消息为已读,自己发送的是在对方判断已读未读
if (!empty($params["last_msg_id"]) && $params["last_msg_id"] > 0) {
$where_arr["from"] = $params["from"];
$where_arr["target"] = $params["target"];
$update_arr["is_read"] = 0;
$msgModel->updateIsRead($where_arr, $update_arr);
}
$page_no = empty($params['page_no']) ? 1 : $params['page_no'];
$page_no = empty($params['page_no']) ? 1 : $params['page_no'];
$page_size = empty($params['page_size']) ? 15 : $params['page_size'];
$field = "a.id,a.to_id,a.from_id,a.created_at,b.type,b.body";
$msgModel = new ChatMsg();
$history_result = $msgModel->getChatHistory($params, $field, $page_no, $page_size);
return $this->response("200", "request success", array_reverse($history_result));
......@@ -275,15 +284,15 @@ class AppChat extends Basic
$data = [];
if ($file) {
$path = ROOT_PATH . 'public' . DS . 'static' . DS . 'chat_image';
$info = $file->validate([ 'size' => 10240000, 'ext' => 'jpg,png' ])//限制100m
$info = $file->validate(['size' => 10240000, 'ext' => 'jpg,png'])//限制100m
->move($path);
if ($info) {
$img_path = $info->getSaveName(); //生成的图片路径
$img_path = $info->getSaveName(); //生成的图片路径
$static_path = $path . DS . $img_path; //生成图片的绝对路径
$image = \think\Image::open($static_path);
$image = \think\Image::open($static_path);
$image->thumb(500, 500)->save($static_path); //生成缩略图
$data = [ 'name' => $img_path ];
$data = ['name' => $img_path];
} else {
// 上传失败获取错误信息
return $this->response("101", $file->getError());
......@@ -312,8 +321,8 @@ class AppChat extends Basic
if (!isset($params['id']) || !isset($params['from'])) {
return $this->response("300", "参数不全");
}
$conditions['id'] = array( "eq", $params["id"] );
$gHousesModel = new GHouses();
$conditions['id'] = array("eq", $params["id"]);
$gHousesModel = new GHouses();
//external_title c端
if ($params["from"] == "C") {
$field = "id,external_title as title,shop_area_start,shop_area_end,rent_type,rent_price,shop_type";
......@@ -328,7 +337,7 @@ class AppChat extends Basic
$param["house_id"] = $params['id'];
$param["img_type"] = 2;
$images = $gHousesImgModel->getHouseImages($param, 1);
$images = $gHousesImgModel->getHouseImages($param, 1);
$data['images'] = IMG_PATH . '/img/houseinfobackgroundimg_new.png';
if (count($images) > 0) {
......@@ -381,7 +390,7 @@ class AppChat extends Basic
if (!isset($params['target']) || !isset($params["is_user"])) {
return $this->response("300", "参数不全");
}
$target = $params["target"];
$target = $params["target"];
$is_user = $params["is_user"];
$relation_list = null;
......@@ -423,16 +432,16 @@ class AppChat extends Basic
//3.ID
//4.手机号隐藏中间4位
$user = new Users();
$field = 'a.id,a.user_name,a.user_nick,a.user_phone,a.user_pic,a.other_pic';
$user = new Users();
$field = 'a.id,a.user_name,a.user_nick,a.user_phone,a.user_pic,a.other_pic';
$params_["a.id"] = $params['id'];
$result = $user->getAgentByUserId($field, $params_);
$result = $user->getAgentByUserId($field, $params_);
if (count($result) > 0) {
$result_["id"] = $result[0]["id"];
$result_["agent_id"] = 'agent_' . $result[0]["id"];
$result_["user_name"] = $result[0]["user_name"];
$result_["user_nick"] = $result[0]["user_nick"];
$result_["user_pic"] = !empty($result[0]["user_pic"]) ? HEADERIMGURL . $result[0]["user_pic"] : $result[0]["other_pic"];
$result_["id"] = $result[0]["id"];
$result_["agent_id"] = 'agent_' . $result[0]["id"];
$result_["user_name"] = $result[0]["user_name"];
$result_["user_nick"] = $result[0]["user_nick"];
$result_["user_pic"] = !empty($result[0]["user_pic"]) ? HEADERIMGURL . $result[0]["user_pic"] : $result[0]["other_pic"];
$result_["user_phone"] = $result[0]["user_phone"];
return $this->response("200", "success!", $result_);
......@@ -457,7 +466,7 @@ class AppChat extends Basic
return $this->response("101", "请求参数错误");
}
$page_no = empty($params['page_no']) ? 1 : $params['page_no'];
$page_no = empty($params['page_no']) ? 1 : $params['page_no'];
$page_size = empty($params['page_size']) ? 15 : $params['page_size'];
$field = "id,addressee_id,type,message,house_id,create_time,is_read";
......@@ -465,9 +474,9 @@ class AppChat extends Basic
//获取此经纪人的所有盘方楼盘id
$param["addressee_id"] = $params["agent_id"];
$param["type"] = array( "in", "1,3" );
$param["type"] = array("in", "1,3");
$msgModel = new MPushMessage();
$msgModel = new MPushMessage();
$history_result = $msgModel->getListByWhere($field, $param, $page_no, $page_size);
return $this->response("200", "request success", $history_result);
......@@ -490,7 +499,7 @@ class AppChat extends Basic
return $this->response("101", "请求参数错误");
}
$page_no = empty($params['page_no']) ? 1 : $params['page_no'];
$page_no = empty($params['page_no']) ? 1 : $params['page_no'];
$page_size = empty($params['page_size']) ? 15 : $params['page_size'];
$field = "id,addressee_id,type,message,user_id,create_time,is_read";
......@@ -498,9 +507,9 @@ class AppChat extends Basic
//获取此经纪人的所有盘方楼盘id
$param["addressee_id"] = $params["agent_id"];
$param["type"] = 2;
$param["type"] = 2;
$msgModel = new MPushMessage();
$msgModel = new MPushMessage();
$history_result = $msgModel->getListByWhere($field, $param, $page_no, $page_size);
return $this->response("200", "request success", $history_result);
......@@ -511,50 +520,60 @@ class AppChat extends Basic
* 获取商铺消息和客户消息的未读消息个数,并修改已读消息状态
* @return \think\Response
*/
public function updateSystemMessageIsRead(){
public function updateSystemMessageIsRead()
{
$params = $this->params;
/* $params = array(
"agent_id" => 1,//经纪人id
"read_ids" => "1,2,3" //需要修改的消息id
);*/
/* $params = array(
"agent_id" => 1,//经纪人id
"read_ids" => "1,2,3" //需要修改的消息id
);*/
if (empty($params['agent_id'])) {
return $this->response("101", "请求参数错误");
}
$msgModel = new MPushMessage();
$msgModel = new MPushMessage();
//修改已读消息
if(!empty($params['read_ids'])){
$where_arr["id"] = array("in",$params["read_ids"]);
if (!empty($params['read_ids'])) {
$where_arr["id"] = array("in", $params["read_ids"]);
$update_arr["is_read"] = 1;
$msgModel->updateIsRead($where_arr,$update_arr);
$msgModel->updateIsRead($where_arr, $update_arr);
}
$param["addressee_id"] = $params["agent_id"];
$param["type"] = 2;
$param["type"] = 2;
//TODO 获取未读个数
$data["user_count"] = $msgModel->getListCountByWhere($param);
$param["type"] = 1;
$param["type"] = 1;
$data["shop_count"] = $msgModel->getListCountByWhere($param);
return $this->response("200","success",$data);
return $this->response("200", "success", $data);
}
public function getMessageIsRead(){
public function getMessageIsRead()
{
$params = $this->params;
$params = array(
/* $params = array(
"agent_id" => 1
);
);*/
//todo 先获取是否有店铺或客户修改消息是否为0,大于0返回true,否则查询聊天消息是否有未读
$param["addressee_id"] = $params["agent_id"];
$param["type"] = 2;
$param["type"] = 2;
//TODO 获取未读个数
$msgModel = new MPushMessage();
$msgModel = new MPushMessage();
$user_count = $msgModel->getListCountByWhere($param);
if($user_count > 0){
return $this->response("200","success",["is_show_red"=>true]);
if ($user_count > 0) {
return $this->response("200", "success", ["is_show_red" => true]);
}
$param["type"] = 1;
$param["type"] = 1;
$shop_count = $msgModel->getListCountByWhere($param);
if($shop_count > 0){
return $this->response("200","success",["is_show_red"=>true]);
if ($shop_count > 0) {
return $this->response("200", "success", ["is_show_red" => true]);
}
$where["to_id"] = "agent_" . $params["agent_id"];
$chatModel = new ChatMsg();
$msg_count = $chatModel->getTotalByToId($where);
if ($msg_count > 0) {
return $this->response("200", "success", ["is_show_red" => true]);
}
......
......@@ -82,6 +82,36 @@ class ChatMsg extends Model
return $data;
}
/** 批量更新已看记录
* @param array $where_arr
* @param array $update_arr
* @return bool
*/
public function updateIsRead(array $where_arr,array $update_arr){
$where_ = [];
if (isset($where_arr["from"])) {
$where_["from_id"] = $where_arr["from"];
}
if (isset($where_arr["target"])) {
$where_["to_id"] = $where_arr["target"];
}
if(empty($where_)){
return false;
}
try {
$this->db->where($where_)
->update($update_arr);
return true;
} catch (\Exception $e) {
return false;
}
}
/**
* @param $params
* @param $field
* @return mixed
*/
public function getTotalUnread($params, $field)
{
......@@ -96,7 +126,9 @@ class ChatMsg extends Model
}
$where_["a.is_del"] = 0;
$data = $this->db->field($field)
$data = $this->db
->field($field)
->alias("a")
->join('chat_msg_ext b', 'a.id = b.msg_id', 'LEFT')
->where($where_)
......@@ -105,4 +137,11 @@ class ChatMsg extends Model
return $data;
}
public function getTotalByToId($params){
$data = $this->db
->where($params)
->count();
return $data;
}
}
\ No newline at end of file
......@@ -458,6 +458,7 @@ Route::group('chat', [
'getSystemMessageByShop' => ['chat/AppChat/getSystemMessageByShop', ['method' => 'post|get']],
'getSystemMessageByUser' => ['chat/AppChat/getSystemMessageByUser', ['method' => 'post|get']],
'updateSystemMessageIsRead' => ['chat/AppChat/updateSystemMessageIsRead', ['method' => 'post|get']],
'getMessageIsRead' => ['chat/AppChat/getMessageIsRead', ['method' => 'post|get']],
]);
......@@ -668,8 +669,8 @@ Route::group('broker', [
'getSiteListApp' => ['api_broker/Site/getSiteList', ['method' => 'POST|GET']], //获取站点列表
'getAgentSiteList' => [ 'api_broker/Site/getAgentSiteList', [ 'method' => 'POST|GET' ] ], //获取经纪人站点列表
'getPhoneBook' => [ 'api_broker/Agent/getPhoneBook', [ 'method' => 'POST|GET' ] ], //消息页面--通讯录
'getAgentSiteList' => ['api_broker/Site/getAgentSiteList', ['method' => 'POST|GET']], //获取经纪人站点列表
'getPhoneBook' => ['api_broker/Agent/getPhoneBook', ['method' => 'POST|GET']], //消息页面--通讯录
]);
//Route::miss('api/index/miss');//处理错误的url
\ 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