Commit ccab486e authored by clone's avatar clone

sendMsg

parent 8cf2aff0
......@@ -13,6 +13,8 @@ namespace app\chat\service;
use app\chat\utils\RegisterUtil;
use app\chat\utils\RPush;
use app\model\Agents;
use app\model\ChatMsg;
use app\model\ChatMsgExt;
use app\model\ChatUser;
use app\model\Users;
......@@ -133,12 +135,15 @@ class ChatService
* @param $accessToken
* @return bool
*/
public function sendMsg($target_type, $target,$source,$is_user, $type, $msg_content, $from, $accessToken)
public function sendMsg($target_type, $target, $source, $is_user, $type, $msg_content, $from, $accessToken)
{
//todo
$this->insertMsg($target_type, $target,$source,$is_user, $type, $msg_content, $from);
$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' ]);
//todo 消息接收人必须是数据
$rPush->send($target_type, [$target], $type, $msg_content, $from, $accessToken, [ $this, 'saveSendStatus' ]);
//返回消息发送成功
return true;
}
......@@ -168,19 +173,51 @@ class ChatService
* @param $msg_content
* @param $from
*/
private function insertMsg($target_type, $target,$source,$is_user, $type, $msg_content, $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["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["account_type"] = 1;
$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);
$msgModel = new ChatMsg();
$id = $msgModel->addChatMsg($params);
if ($id > 0) {
//todo 插入msg_ext
$group_id = 0;
if ($target_type != "users") {
$group_id = $from;
}
$this->insetMsgExt($id, $type, $msg_content, $params["is_group"], $group_id);
}
}
/**
* 记录消息body
* @param $id
* @param $type
* @param $msg_content
* @param $is_group
* @param $group_id
*/
private function insetMsgExt($id, $type, $msg_content, $is_group, $group_id)
{
$params["msg_id"] = $id;
$params["type"] = $type;
$params["body"] = $msg_content;
$params["send_type"] = $is_group;
$params["group_id"] = $group_id;
$params["create_at"] = date("Y-m-d H:i:s", time());
$params["updated_at"] = date("Y-m-d H:i:s", time());
$msgExtModel = new ChatMsgExt();
$msgExtModel->addChatMsgExt($params);
}
/** 获取聊天记录
......
<?php
/**
* Created by PhpStorm.
* User : zw
* Date : 2018/1/18
* Time : 15:33
* Intro:
*/
namespace app\model;
use think\Model;
use think\Db;
class ChatMsg extends Model
{
// 设置当前模型对应的完整数据表名称
protected $table = 'chat_msg';
protected $db;
public function __construct()
{
$this->db = Db($this->table);
}
public function addChatMsg($params)
{
Db::startTrans();
try {
$this->save($params);
$id = $this->id;
return $id;
Db::commit();
} catch (\Exception $e) {
Db::rollback();
}
return 0;
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User : zw
* Date : 2018/1/18
* Time : 15:33
* Intro:
*/
namespace app\model;
use think\Model;
use think\Db;
class ChatMsgExt extends Model
{
// 设置当前模型对应的完整数据表名称
protected $table = 'chat_msg_ext';
protected $db;
public function __construct()
{
$this->db = Db($this->table);
}
public function addChatMsgExt($params)
{
Db::startTrans();
try {
$this->save($params);
$id = $this->id;
return $id;
Db::commit();
} catch (\Exception $e) {
Db::rollback();
}
return 0;
}
}
\ 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