数据表情况
说明
数据表分为两个:一个是inbox_dialog用于存放会话,另一个是inbox用于存放两人的聊天信息
inbox_dialog 主要用来确定一个会话。
首先,通过sender_uid与recipient_uid用于确定发件人与收件人;
然后,还要记录一下sender_count和recipient_count,以及sender_unread和recipient_unread数目;
最后,add_time和update_time记录时间。inbox表主要用来记录具体的会话信息。
首先,通过dialog_id以及uid(记录发送方的uid)来确定会话的信息;
然后,通过add_time对会话信息message进行排序,然后传给前台;
最后,通过sender_remove和recipient_remove记录用户是否删除信息。
另外,通过receipt存放查看时间
截图
代码
- 发送私信接口
- 分析
- 主要分为两种情况:一种是会话不存在,建立会话;另一种是会话存在,直接找到该会话。
- 然后根据inbox_dialog的id,向inbox表中插入聊天信息。
- 最后,更新一下未读信息数目。
- 在发送之前需要判断:message不能为空 接受私信的用户要存在 接受用户不能为自己 不能给禁止接受的人发送
- 代码
- 分析
public function send_message($sender_uid, $recipient_uid, $message)
{
if (!$sender_uid OR !$recipient_uid OR !$message)
{
return false;
}
if (! $inbox_dialog = $this->get_dialog_by_user($sender_uid, $recipient_uid))
{
$inbox_dialog_id = $this->insert('inbox_dialog', array(
'sender_uid' => $sender_uid,
'sender_unread' => 0,
'recipient_uid' => $recipient_uid,
'recipient_unread' => 0,
'add_time' => time(),
'update_time' => time(),
'sender_count' => 0,
'recipient_count' => 0
));
}
else
{
$inbox_dialog_id = $inbox_dialog['id'];
}
$message_id = $this->insert('inbox', array(
'dialog_id' => $inbox_dialog_id,
'message' => htmlspecialchars($message),
'add_time' => time(),
'uid' => $sender_uid
));
$this->update_dialog_count($inbox_dialog_id, $sender_uid);
$this->model('account')->update_inbox_unread($recipient_uid);
//$this->model('account')->update_inbox_unread($sender_uid);
if ($user_info = $this->model('account')->get_user_info_by_uid($sender_uid))
{
$this->model('email')->action_email('NEW_MESSAGE', $recipient_uid, get_js_url('/inbox/'), array(
'user_name' => $user_info['user_name'],
));
}
return $message_id;
}
public function send_action()
{
if (trim($_POST['message']) == '')
{
H::ajax_json_output(AWS_APP::RSM(null, '-1', AWS_APP::lang()->_t('请输入私信内容')));
}
if (!$recipient_user = $this->model('account')->get_user_info_by_username($_POST['recipient']))
{
H::ajax_json_output(AWS_APP::RSM(null, '-1', AWS_APP::lang()->_t('接收私信的用户不存在')));
}
if ($recipient_user['uid'] == $this->user_id)
{
H::ajax_json_output(AWS_APP::RSM(null, '-1', AWS_APP::lang()->_t('不能给自己发私信')));
}
if ($recipient_user['inbox_recv'])
{
if (! $this->model('message')->check_permission($recipient_user['uid'], $this->user_id))
{
H::ajax_json_output(AWS_APP::RSM(null, '-1', AWS_APP::lang()->_t('对方设置了只有 Ta 关注的人才能给 Ta 发送私信')));
}
}
// !注: 来路检测后面不能再放报错提示
if (!valid_post_hash($_POST['post_hash']))
{
H::ajax_json_output(AWS_APP::RSM(null, '-1', AWS_APP::lang()->_t('页面停留时间过长,或内容已提交,请刷新页面')));
}
$this->model('message')->send_message($this->user_id, $recipient_user['uid'], $_POST['message']);
if ($_POST['return_url'])
{
$rsm = array(
'url' => get_js_url(strip_tags($_POST['return_url']))
);
}
else
{
$rsm = array(
'url' => get_js_url('/inbox/')
);
}
H::ajax_json_output(AWS_APP::RSM($rsm, 1, null));
}
私信列表接口
- 分析
-
1.
- 代码
- 分析