Commit b3a62841 authored by clone's avatar clone

chat

parent abbeba2e
...@@ -75,78 +75,43 @@ class AppChat extends Basic ...@@ -75,78 +75,43 @@ class AppChat extends Basic
public function userChat() public function userChat()
{ {
$params = array( $params = array(
"userId" => 1, "user_id" => 1,
"mobile" => "13817616471", "mobile" => "13817616471",
"source" => 1 //1经纪人 2用户 "source" => 1 //1经纪人 2用户
); );
//$params = $this->params; //$params = $this->params;
$userId = $params['sender']; $user_id = $params['sender'];
$mobile = $params['receiver']; $mobile = $params['receiver'];
$source = $params['msg_type']; $source = $params['msg_type'];
if (!$userId || !$mobile || !$source) { if (!$user_id || !$mobile || !$source) {
return $this->response(ErrorCodeConst::ERROR_CODE_PARAM_NOT_EXIST, "请求参数错误"); return $this->response(ErrorCodeConst::ERROR_CODE_PARAM_NOT_EXIST, "请求参数错误");
} }
$onlyId = $this->_chat->createOnlyId($userId, $mobile, $source); $onlyId = $this->_chat->createOnlyId($user_id, $mobile, $source);
return $this->response("200", array( "onlyId" => $onlyId )); return $this->response("200", array( "onlyId" => $onlyId ));
} }
public function pushMsg(){
$params = $this->params;
$sender = $params['sender'];//发送者帐号
$receiver = $params['receiver'];//接收者帐号
$msg_type = $params['msg_type'];//这个用我们自己的类型不用容联的 1文本 2图片 3,系统消息 4推送消息
$msg_content = $params['msg_content'];//文本内容
$msg_filename = $params['msg_filename'];//文件名
$msg_file_url = $params['msg_fileurl'];//文件绝对路径
$site_id = $params['site_id'];
$group_id = $params['group_id'];//用户组id
$lng = $params['lng'];//113;//经度
$lat = $params['lat'];//31;//纬度
$source = $params['source'];
$this->_log->info(sprintf("%s::%s,request[%s]",__CLASS__, __FUNCTION__, json_encode($this->request->getJsonRawBody(true))));
if (!$sender || !$msg_type || !$site_id || !isset($lng) || !isset($lat)) {
throw new HTTPException(ErrorCodeConsts::ERROR_CODE_PARAM_NOT_EXIST);
}
if (!$this->_chat->getCanChat($sender, $receiver, $msg_type)) { public function pushMsg()
throw new CHTTPException(ErrorCodeConsts::ERROR_CODE_NOT_FRIENDS); {
$params = $this->params;
$target_type = $params['target_type']; // 消息类型 users 给用户发消息。chatgroups: 给群发消息,chatrooms: 给聊天室发消息
$target = $params['target']; //发送人 if target_type 群 者表示群id
$type = $params['type']; //消息类型 1文字 2图片
$msg_content = $params['msg_content'];
$from = $params['from']; //消息发送人
if (!$target_type || !$target || !$type || !$msg_content || !$from) {
return $this->response(ErrorCodeConst::ERROR_CODE_PARAM_NOT_EXIST, "请求参数错误");
} }
$result = $this->_chat->sendMsg($target_type, $target, $type, $msg_content, $from,$this->accessToken);
$result = $this->_queue->msgSend($this->request->getHttpHost() . '/chat/receiveMsg', [ if (count($result) > 0) {
'sender' => $sender, return $this->response("200",["msg" => "消息发送成功"]);
'receiver' => $receiver,
'site_id' => $site_id,
'group_id' => $group_id,
'msg_type' => $msg_type,
'msg_content' => json_encode($msg_content),
'msg_filename' => $msg_filename,
'msg_file_url' => $msg_file_url,
'lng' => $lng,
'lat' => $lat,
'source' => $source,
]);
$this->_log->info(sprintf("%s::%s,msgSend---->[%s]",__CLASS__, __FUNCTION__, json_encode($result)));
if ($result) {
return array(
'statusMsg' => "success",
'statusCode' => "000000",
'time' => date("Y-m-d H:m:s", time()
)
);
} else {
return array(
'statusMsg' => "error",
'statusCode' => "170003",
'time' => date("Y-m-d H:m:s", time()
)
);
} }
} }
} }
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
namespace app\chat\service; namespace app\chat\service;
use app\chat\utils\RPush;
use app\model\Agents; use app\model\Agents;
use app\model\Users; use app\model\Users;
...@@ -14,6 +15,10 @@ class ChatService ...@@ -14,6 +15,10 @@ class ChatService
const MSG_CONTENT_TYPE_VOICE = 2; //语音 const MSG_CONTENT_TYPE_VOICE = 2; //语音
const MSG_CONTENT_TYPE_VIDEO = 3; //视频 const MSG_CONTENT_TYPE_VIDEO = 3; //视频
const MSG_TYPE_USERS = "users"; //给用户发消息
const MSG_TYPE_CHAT_GROUPS = "chatgroups"; //给群发消息
const MSG_TYPE_CHAT_CHAT_ROOMS = "chatrooms"; //给聊天室发消息
const SOURCE_TYPE_APP = 'app_'; // 用户 c端 const SOURCE_TYPE_APP = 'app_'; // 用户 c端
const SOURCE_TYPE_AGENT = 'agent_';//经纪人 b端 const SOURCE_TYPE_AGENT = 'agent_';//经纪人 b端
...@@ -49,40 +54,62 @@ class ChatService ...@@ -49,40 +54,62 @@ class ChatService
if (count($userResult) > 1) if (count($userResult) > 1)
$onlyId = self::SOURCE_TYPE_APP . $userId; $onlyId = self::SOURCE_TYPE_APP . $userId;
break; break;
default: }
return array( "code" => 101, "msg" => "没找到用户信息" ); if (!$onlyId) {
return array( "code" => 101, "msg" => "没找到用户信息" );
} }
return $onlyId; return $onlyId;
} }
/** /**
* 发送消息 * 发送消息
* @param $sender_id * @param $target_type
* @param $receiver_ids * @param $target
* @param $group_id * @param $type
* @param $msg_content * @param $msg_content
* @param $msg_type * @param $from
* @param $msg_filename * @param $accessToken
* @param $msg_file_url * @return bool
*/ */
public function sendMsg($sender_id, $receiver_ids, $group_id, $msg_content, $msg_type, $msg_filename, $msg_file_url) public function sendMsg($target_type, $target, $type, $msg_content, $from ,$accessToken)
{ {
switch ($target_type) {
case self::MSG_TYPE_USERS:
//todo 判断用户是否存在
break;
case self::MSG_TYPE_CHAT_GROUPS:
//todo 判断组群是否存在
break;
case self::MSG_TYPE_CHAT_CHAT_ROOMS:
//todo 判断聊天室是否存在
break;
default:
return ["code"=> 101,"msg"=>"消息类型错误"];
}
//todo //todo
$this->insertMsg(); $this->insertMsg();
$rPush = new RPush();
$rPush->send($target_type, $target, $type, $msg_content, $from,$accessToken,[ $this, 'saveSendStatus' ]);
//返回消息发送成功
return true;
} }
/** /**
* 保存推送到环信的消息状态 往msg_status中插入数据 * 保存推送到环信的消息状态 往msg_status中插入数据
* @param $response * @param $response
* @param $tmp * @param $target
* @param $sender * @param $sender
* @param $msg_content * @param $msg_content
*/ */
public function saveSendStatus($response, $tmp, $sender, $msg_content) public function saveSendStatus($response, $target, $sender, $msg_content)
{ {
//todo $response = json_decode($response, true);
$response['time'] = date('Y-m-d H:i:s');
$status = $response['statusCode'] == RPush::PUSH_STATUS_SUCCESS ? MsgStatus::STATUS_SUCCESS : MsgStatus::STATUS_FAILURE;
$this->insertPushLog($sender, $target, $msg_content, $status, $response['statusMsg']);
} }
/** /**
...@@ -112,5 +139,21 @@ class ChatService ...@@ -112,5 +139,21 @@ class ChatService
//todo //todo
} }
/**
* 往chat_msg_status中插入数据,记录推送的状态
* @param $sender
* @param $target
* @param $content
* @param $status
* @param $error_reason
* @return bool
*/
public function insertPushLog($sender, $target, $content, $status, $error_reason)
{
//todo
return true;
}
} }
\ No newline at end of file
<?php <?php
namespace app\chat\utils;
use app\chat\consts\ConfigConst;
/** /**
...@@ -11,12 +14,23 @@ ...@@ -11,12 +14,23 @@
class RPush class RPush
{ {
/**
* 发送消息
* @param $target_type
* @param $target
* @param $type
* @param $msg_content
* @param $from
* @param $access_token
* @param $callback
*/
public function send($target_type, $target, $type, $msg_content, $from, $access_token, $callback) public function send($target_type, $target, $type, $msg_content, $from, $access_token, $callback)
{ {
//todo 这里做算法优化,环信接口有限制,每个时间段最多能推送多少请求
$response = $this->sendRequestByCurl($target_type, $target, $type, $msg_content, $from, $access_token); $response = $this->sendRequestByCurl($target_type, $target, $type, $msg_content, $from, $access_token);
call_user_func_array([ $callback[0], $callback[1] ],
[ $response, $receivers_to_send, $sender, $msg_content ]); call_user_func_array([ $callback[0], $callback[1] ], [ $response, $target, $from, $msg_content ]);
} }
...@@ -28,7 +42,7 @@ class RPush ...@@ -28,7 +42,7 @@ class RPush
* @param $msg_content * @param $msg_content
* @param $from * @param $from
* @param $access_token * @param $access_token
* @return $this * @return \app\chat\utils\CurlResponse|bool
*/ */
public function sendRequestByCurl($target_type, $target, $type, $msg_content, $from, $access_token) public function sendRequestByCurl($target_type, $target, $type, $msg_content, $from, $access_token)
{ {
...@@ -57,11 +71,11 @@ class RPush ...@@ -57,11 +71,11 @@ class RPush
} }
/** /**
* 请求api
* @return string * @return string
*/ */
private function buildSendUrl() private function buildSendUrl()
{ {
return $this->base_url . '/Accounts/' . $this->account_sid . return ConfigConst::API_PATH . ConfigConst::ORG_NAME . "/" . ConfigConst::APP_NAME;
'/IM/PushMsg?sig=' . $this->main_sig;
} }
} }
\ 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