Commit 58393c5b authored by clone's avatar clone Committed by hujun

chat build

parent e3cbc6f6
...@@ -69,12 +69,12 @@ class AppChat extends Basic ...@@ -69,12 +69,12 @@ class AppChat extends Basic
public function userChat() public function userChat()
{ {
/* $params = array( /* $params = array(
"user_id" => 2, "user_id" => 1,
"mobile" => "18205625020", "mobile" => "15720682761",
"source" => 2 //1经纪人 2用户 "source" => 2 //1经纪人 2用户
);*/ );*/
$params = $this->params; $params = $this->params;
if (!isset($params['user_id']) || !isset($params['mobile']) || !isset($params['source'])) { if (!isset($params['user_id']) || !isset($params['mobile']) || !isset($params['source'])) {
return $this->response(ErrorCodeConst::ERROR_CODE_PARAM_NOT_EXIST, "请求参数错误"); return $this->response(ErrorCodeConst::ERROR_CODE_PARAM_NOT_EXIST, "请求参数错误");
...@@ -93,24 +93,45 @@ class AppChat extends Basic ...@@ -93,24 +93,45 @@ class AppChat extends Basic
} else { } else {
return $this->response("101", $only_arr["msg"]); return $this->response("101", $only_arr["msg"]);
} }
}
/**
* c端根据经纪人的手机号获取唯一id
* @return \think\Response
*/
public function getAgentOnlyId()
{
//todo
return $this->response("200", "success", "");
} }
/**
* 发送消息接口
* @return \think\Response
*/
public function pushMsg() public function pushMsg()
{ {
$params = $this->params; $params = $this->params;
if (!isset($params['target_type']) || !isset($params['target']) || !isset($params['type']) || !isset($params['msg_content'])
|| !isset($params['source']) || !isset($params['is_user']) || !isset($params['from'])) {
return $this->response(ErrorCodeConst::ERROR_CODE_PARAM_NOT_EXIST, "请求参数错误");
}
$target_type = $params['target_type']; // 消息类型 users 给用户发消息。chatgroups: 给群发消息,chatrooms: 给聊天室发消息 $target_type = $params['target_type']; // 消息类型 users 给用户发消息。chatgroups: 给群发消息,chatrooms: 给聊天室发消息
$target = $params['target']; //发送人 if target_type 群 者表示群id $target = $params['target']; //接受人 if target_type 群 者表示群id
$type = $params['type']; //消息类型 1文字 2图片 $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']; $msg_content = $params['msg_content'];
$from = $params['from']; //消息发送人 $from = $params['from']; //消息发送人
if ($target_type != "users" && $target_type != "chatgroups" && $target_type != "chatrooms") {
if (!$target_type || !$target || !$type || !$msg_content || !$from) { return $this->response(ErrorCodeConst::ERROR_CODE_TARGET_TYPE_ERROR, "错误的消息类型");
return $this->response(ErrorCodeConst::ERROR_CODE_PARAM_NOT_EXIST, "请求参数错误");
} }
$result = $this->_chat->sendMsg($target_type, $target, $type, $msg_content, $from, $this->accessToken);
$result = $this->_chat->sendMsg($target_type, $target, $source, $is_user, $type, $msg_content, $from, $this->accessToken);
if (count($result) > 0) { if (count($result) > 0) {
return $this->response("200", [ "msg" => "消息发送成功" ]); return $this->response("200", [ "msg" => "消息发送成功" ]);
} }
......
<?php
namespace app\chat\service;
/**
* Created by PhpStorm.
* User : zw
* Date : 2018/1/8
* Time : 16:54
* Intro:
*/
use app\chat\utils\RegisterUtil;
use app\chat\utils\RPush;
use app\model\Agents;
use app\model\ChatUser;
use app\model\Users;
class ChatService
{
public $connection;
public $logs;
const MSG_CONTENT_TYPE_TEXT = 1; //文字
const MSG_CONTENT_TYPE_VOICE = 2; //语音
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 = 'user_'; // 用户 c端
const SOURCE_TYPE_AGENT = 'agent_';//经纪人 b端
protected $userModel;
protected $agentsModel;
protected $chatUserModel;
public function __construct()
{
$this->userModel = new Users();
$this->agentsModel = new Agents();
$this->chatUserModel = new ChatUser();
}
/**
* @param $user_id
* @param $mobile
* @param $source
* @return array
*/
public function register($user_id, $mobile, $source)
{
//todo
$params["type"] = $source;
$params["user_id"] = $user_id;
$params["phone"] = $mobile;
$fields = "id,type,phone,only_id,password";
$chatUser = $this->chatUserModel->getChatUser($params, $fields);
if (count($chatUser) > 0) {
return [ "code" => "200", "only_id" => $chatUser[0]["only_id"] ];
} else {
$reg = new RegisterUtil();
$only_id = $this->createOnlyId($user_id, $source);
if (!$only_id) {
return [ "code" => "101", "msg" => "user msg not found" ];
}
$response = $reg->registerByCurl($only_id, $mobile);
if ($response) {
//todo insert
$this->insertChatUser($source, $user_id, $mobile, $only_id);
}
return [ "code" => 200, "only_id" => $only_id ];
}
}
/**
* 生成唯一id
* @param $userId integer 用户id
* @param $source integer 来源 '1经纪人 2用户'
* @return string
*/
public function createOnlyId($userId, $source)
{
$onlyId = "";
switch ($source) {
case 1:
$agentsResult = $this->agentsModel->getAgentsById($userId);
if (count($agentsResult) > 1)
$onlyId = self::SOURCE_TYPE_AGENT . $userId;
break;
case 2:
$userResult = $this->userModel->selectUser($userId);
if ($userResult->id)
$onlyId = self::SOURCE_TYPE_APP . $userId;
break;
}
if (!$onlyId) {
//return array( "code" => 101, "msg" => "没找到用户信息" );
return null;
}
return $onlyId;
}
/**
* 删除注册用户
* @param $only_id
* @param $access_token
* @return bool
*/
public function deleteUser($only_id, $access_token)
{
$reg = new RegisterUtil();
$reg->deleteByCurl($only_id, $access_token);
return true;
}
/**
* 发送消息
* @param $target_type
* @param $target
* @param $source
* @param $is_user
* @param $type
* @param $msg_content
* @param $from
* @param $accessToken
* @return bool
*/
public function sendMsg($target_type, $target,$source,$is_user, $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
$this->insertMsg($target_type, $target,$source,$is_user, $type, $msg_content, $from);
$rPush = new RPush();
$rPush->send($target_type, $target, $type, $msg_content, $from, $accessToken, [ $this, 'saveSendStatus' ]);
//返回消息发送成功
return true;
}
/**
* 保存推送到环信的消息状态 往msg_status中插入数据
* @param $response
* @param $target
* @param $sender
* @param $msg_content
*/
public function saveSendStatus($response, $target, $sender, $msg_content)
{
$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']);
}
/**
* 保存用户发送的消息
* @param $target_type
* @param $target
* @param $source
* @param $is_user
* @param $type
* @param $msg_content
* @param $from
*/
private function insertMsg($target_type, $target,$source,$is_user, $type, $msg_content, $from)
{
//todo
$params["msg_type"] = 1; //目前只用到了环信的文本消息
$params["to_id"] = $target;
$params["from_id"] = $from;
$params["is_group"] = $target_type == "users" ? 0 : 1;
$params["account_type"] = date("Y-m-d H:i:s", time());
$params["source"] = $source;
$params["is_user"] = $is_user;
$params["created_at"] = date("Y-m-d H:i:s", time());
$params["updated_at"] = date("Y-m-d H:i:s", time());
$this->chatUserModel->addChatUser($params);
}
/** 获取聊天记录
* @param $sender
* @param $start
* @param $limit
* @param $site_id
* @return array
*/
public function getHistoryChatList($sender, $start, $limit, $site_id)
{
//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;
}
/**
* @param $type
* @param $user_id
* @param $phone
* @param $only_id
* @return bool
*/
public function insertChatUser($type, $user_id, $phone, $only_id)
{
$params["type"] = $type;
$params["user_id"] = $user_id;
$params["phone"] = $phone;
$params["only_id"] = $only_id;
$params["password"] = $phone;
$params["last_login_time"] = date("Y-m-d H:i:s", time());
$params["created_at"] = date("Y-m-d H:i:s", time());
$params["updated_at"] = date("Y-m-d H:i:s", time());
$this->chatUserModel->addChatUser($params);
return true;
}
}
\ No newline at end of file
...@@ -30,7 +30,6 @@ class RegisterUtil ...@@ -30,7 +30,6 @@ class RegisterUtil
'username' => $username, 'username' => $username,
'password' => $password, 'password' => $password,
); );
$data = json_encode($arr); $data = json_encode($arr);
$curl = new CurlUtil(); $curl = new CurlUtil();
......
...@@ -2,4 +2,4 @@ ...@@ -2,4 +2,4 @@
# http://curl.haxx.se/docs/http-cookies.html # http://curl.haxx.se/docs/http-cookies.html
# This file was generated by libcurl! Edit at your own risk. # This file was generated by libcurl! Edit at your own risk.
a1.easemob.com FALSE / FALSE 1516264437 rememberMe deleteMe a1.easemob.com FALSE / FALSE 1516352505 rememberMe deleteMe
...@@ -206,3 +206,42 @@ response body = <?xml version="1.0" encoding="UTF-8" standalone="yes"?> ...@@ -206,3 +206,42 @@ response body = <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<statusMsg>IP鉴权失败</statusMsg> <statusMsg>IP鉴权失败</statusMsg>
</Response> </Response>
request body = <TemplateSMS>
<to>18756258888</to>
<appId>8a216da85f5c89b1015f7718e2b90a63</appId>
<templateId>214759</templateId>
<datas><data>7572</data><data>5分钟</data></datas>
</TemplateSMS>
request url = https://app.cloopen.com:8883/2013-12-26/Accounts/8a48b55153eae51101540e763d3b3888/SMS/TemplateSMS?sig=1566AAB75F25B6DD28A4902FA26AEC0F
response body = <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Response>
<statusCode>160053</statusCode>
<statusMsg>IP鉴权失败</statusMsg>
</Response>
request body = <TemplateSMS>
<to>18756259999</to>
<appId>8a216da85f5c89b1015f7718e2b90a63</appId>
<templateId>214759</templateId>
<datas><data>3479</data><data>5分钟</data></datas>
</TemplateSMS>
request url = https://app.cloopen.com:8883/2013-12-26/Accounts/8a48b55153eae51101540e763d3b3888/SMS/TemplateSMS?sig=DE9865EE495BD9B0D85EDAF249CDA24F
response body = <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Response>
<statusCode>160053</statusCode>
<statusMsg>IP鉴权失败</statusMsg>
</Response>
request body = <TemplateSMS>
<to>18756257777</to>
<appId>8a216da85f5c89b1015f7718e2b90a63</appId>
<templateId>214759</templateId>
<datas><data>7094</data><data>5分钟</data></datas>
</TemplateSMS>
request url = https://app.cloopen.com:8883/2013-12-26/Accounts/8a48b55153eae51101540e763d3b3888/SMS/TemplateSMS?sig=7977CEC21F534DD1E5D675D792595DC9
response body = <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Response>
<statusCode>160053</statusCode>
<statusMsg>IP鉴权失败</statusMsg>
</Response>
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