mirror of
https://gitee.com/koogua/course-tencent-cloud.git
synced 2025-06-23 20:00:27 +08:00
完成更多聊天记录
This commit is contained in:
parent
ceb3e1dcbb
commit
fceffa655f
41
app/Builders/ImMessageList.php
Normal file
41
app/Builders/ImMessageList.php
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Builders;
|
||||||
|
|
||||||
|
use App\Repos\User as UserRepo;
|
||||||
|
|
||||||
|
class ImMessageList extends Builder
|
||||||
|
{
|
||||||
|
|
||||||
|
public function handleUsers(array $messages)
|
||||||
|
{
|
||||||
|
$users = $this->getUsers($messages);
|
||||||
|
|
||||||
|
foreach ($messages as $key => $message) {
|
||||||
|
$messages[$key]['user'] = $users[$message['user_id']] ?? new \stdClass();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $messages;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getUsers(array $messages)
|
||||||
|
{
|
||||||
|
$ids = kg_array_column($messages, 'user_id');
|
||||||
|
|
||||||
|
$userRepo = new UserRepo();
|
||||||
|
|
||||||
|
$users = $userRepo->findByIds($ids, ['id', 'name', 'avatar']);
|
||||||
|
|
||||||
|
$baseUrl = kg_ci_base_url();
|
||||||
|
|
||||||
|
$result = [];
|
||||||
|
|
||||||
|
foreach ($users->toArray() as $user) {
|
||||||
|
$user['avatar'] = $baseUrl . $user['avatar'];
|
||||||
|
$result[$user['id']] = $user;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
31
app/Caches/ImChatGroup.php
Normal file
31
app/Caches/ImChatGroup.php
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Caches;
|
||||||
|
|
||||||
|
use App\Repos\ImChatGroup as ImChatGroupRepo;
|
||||||
|
|
||||||
|
class ImChatGroup extends Cache
|
||||||
|
{
|
||||||
|
|
||||||
|
protected $lifetime = 365 * 86400;
|
||||||
|
|
||||||
|
public function getLifetime()
|
||||||
|
{
|
||||||
|
return $this->lifetime;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getKey($id = null)
|
||||||
|
{
|
||||||
|
return "im_chat_group:{$id}";
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getContent($id = null)
|
||||||
|
{
|
||||||
|
$groupRepo = new ImChatGroupRepo();
|
||||||
|
|
||||||
|
$group = $groupRepo->findById($id);
|
||||||
|
|
||||||
|
return $group ?: null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
29
app/Caches/MaxImChatGroupId.php
Normal file
29
app/Caches/MaxImChatGroupId.php
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Caches;
|
||||||
|
|
||||||
|
use App\Models\ImChatGroup as ImChatGroupModel;
|
||||||
|
|
||||||
|
class MaxImChatGroupId extends Cache
|
||||||
|
{
|
||||||
|
|
||||||
|
protected $lifetime = 365 * 86400;
|
||||||
|
|
||||||
|
public function getLifetime()
|
||||||
|
{
|
||||||
|
return $this->lifetime;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getKey($id = null)
|
||||||
|
{
|
||||||
|
return 'max_im_chat_group_id';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getContent($id = null)
|
||||||
|
{
|
||||||
|
$group = ImChatGroupModel::findFirst(['order' => 'id DESC']);
|
||||||
|
|
||||||
|
return $group->id ?? 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
50
app/Http/Web/Controllers/LayerController.php
Normal file
50
app/Http/Web/Controllers/LayerController.php
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Web\Controllers;
|
||||||
|
|
||||||
|
use App\Models\User as UserModel;
|
||||||
|
use App\Services\Auth\Web as WebAuth;
|
||||||
|
use App\Traits\Response as ResponseTrait;
|
||||||
|
use App\Traits\Security as SecurityTrait;
|
||||||
|
use Phalcon\Mvc\Dispatcher;
|
||||||
|
|
||||||
|
class LayerController extends \Phalcon\Mvc\Controller
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var UserModel
|
||||||
|
*/
|
||||||
|
protected $authUser;
|
||||||
|
|
||||||
|
use ResponseTrait, SecurityTrait;
|
||||||
|
|
||||||
|
public function beforeExecuteRoute(Dispatcher $dispatcher)
|
||||||
|
{
|
||||||
|
if ($this->isNotSafeRequest()) {
|
||||||
|
$this->checkHttpReferer();
|
||||||
|
$this->checkCsrfToken();
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->checkRateLimit();
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function initialize()
|
||||||
|
{
|
||||||
|
$this->authUser = $this->getAuthUser();
|
||||||
|
|
||||||
|
$this->view->setVar('auth_user', $this->authUser);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getAuthUser()
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var WebAuth $auth
|
||||||
|
*/
|
||||||
|
$auth = $this->getDI()->get('auth');
|
||||||
|
|
||||||
|
return $auth->getCurrentUser();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -4,11 +4,12 @@ namespace App\Http\Web\Controllers;
|
|||||||
|
|
||||||
use App\Http\Web\Services\Messenger as MessengerService;
|
use App\Http\Web\Services\Messenger as MessengerService;
|
||||||
use App\Traits\Response as ResponseTrait;
|
use App\Traits\Response as ResponseTrait;
|
||||||
|
use Phalcon\Mvc\View;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @RoutePrefix("/im")
|
* @RoutePrefix("/im")
|
||||||
*/
|
*/
|
||||||
class MessengerController extends \Phalcon\Mvc\Controller
|
class MessengerController extends LayerController
|
||||||
{
|
{
|
||||||
|
|
||||||
use ResponseTrait;
|
use ResponseTrait;
|
||||||
@ -30,24 +31,11 @@ class MessengerController extends \Phalcon\Mvc\Controller
|
|||||||
*/
|
*/
|
||||||
public function groupMembersAction()
|
public function groupMembersAction()
|
||||||
{
|
{
|
||||||
$data = [
|
$service = new MessengerService();
|
||||||
'list' => [
|
|
||||||
[
|
|
||||||
'id' => '1000',
|
|
||||||
'username' => '闲心',
|
|
||||||
'sign' => '我是如此的不寒而栗',
|
|
||||||
'status' => 'online',
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'id' => '1001',
|
|
||||||
'username' => '妹儿美',
|
|
||||||
'sign' => '我是如此的不寒而栗',
|
|
||||||
'status' => 'online',
|
|
||||||
]
|
|
||||||
]
|
|
||||||
];
|
|
||||||
|
|
||||||
return $this->jsonSuccess(['data' => $data]);
|
$list = $service->getGroupUsers();
|
||||||
|
|
||||||
|
return $this->jsonSuccess(['data' => ['list' => $list]]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -63,7 +51,25 @@ class MessengerController extends \Phalcon\Mvc\Controller
|
|||||||
*/
|
*/
|
||||||
public function chatLogAction()
|
public function chatLogAction()
|
||||||
{
|
{
|
||||||
|
$service = new MessengerService();
|
||||||
|
|
||||||
|
$pager = $service->getChatLog();
|
||||||
|
|
||||||
|
$this->view->setRenderLevel(View::LEVEL_ACTION_VIEW);
|
||||||
|
$this->view->pick('messenger/chat_log');
|
||||||
|
$this->view->setVar('pager', $pager);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Get("/chat/history", name="im.chat_history")
|
||||||
|
*/
|
||||||
|
public function chatHistoryAction()
|
||||||
|
{
|
||||||
|
$service = new MessengerService();
|
||||||
|
|
||||||
|
$pager = $service->getChatLog();
|
||||||
|
|
||||||
|
return $this->jsonPaginate($pager);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -2,15 +2,20 @@
|
|||||||
|
|
||||||
namespace App\Http\Web\Services;
|
namespace App\Http\Web\Services;
|
||||||
|
|
||||||
|
use App\Builders\ImMessageList as ImMessageListBuilder;
|
||||||
|
use App\Library\Paginator\Query as PagerQuery;
|
||||||
|
use App\Models\ImFriendMessage as ImFriendMessageModel;
|
||||||
|
use App\Repos\ImChatGroup as ImChatGroupRepo;
|
||||||
|
use App\Repos\ImFriendMessage as ImFriendMessageRepo;
|
||||||
|
use App\Repos\ImGroupMessage as ImGroupMessageRepo;
|
||||||
use App\Repos\User as UserRepo;
|
use App\Repos\User as UserRepo;
|
||||||
use App\Services\Frontend\UserTrait;
|
use App\Validators\ImChatGroup as ImChatGroupValidator;
|
||||||
|
use App\Validators\ImMessage as ImMessageValidator;
|
||||||
use GatewayClient\Gateway;
|
use GatewayClient\Gateway;
|
||||||
|
|
||||||
class Messenger extends Service
|
class Messenger extends Service
|
||||||
{
|
{
|
||||||
|
|
||||||
use UserTrait;
|
|
||||||
|
|
||||||
public function init()
|
public function init()
|
||||||
{
|
{
|
||||||
$user = $this->getLoginUser();
|
$user = $this->getLoginUser();
|
||||||
@ -34,6 +39,77 @@ class Messenger extends Service
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getGroupUsers()
|
||||||
|
{
|
||||||
|
$id = $this->request->getQuery('id');
|
||||||
|
|
||||||
|
$validator = new ImChatGroupValidator();
|
||||||
|
|
||||||
|
$group = $validator->checkGroupCache($id);
|
||||||
|
|
||||||
|
$groupRepo = new ImChatGroupRepo();
|
||||||
|
|
||||||
|
$users = $groupRepo->findGroupUsers($group->id);
|
||||||
|
|
||||||
|
if ($users->count() == 0) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
$baseUrl = kg_ci_base_url();
|
||||||
|
|
||||||
|
$result = [];
|
||||||
|
|
||||||
|
foreach ($users->toArray() as $user) {
|
||||||
|
$user['avatar'] = $baseUrl . $user['avatar'];
|
||||||
|
$result[] = [
|
||||||
|
'id' => $user['id'],
|
||||||
|
'username' => $user['name'],
|
||||||
|
'avatar' => $user['avatar'],
|
||||||
|
'sign' => $user['sign'],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getChatLog()
|
||||||
|
{
|
||||||
|
$user = $this->getLoginUser();
|
||||||
|
|
||||||
|
$pagerQuery = new PagerQuery();
|
||||||
|
|
||||||
|
$params = $pagerQuery->getParams();
|
||||||
|
|
||||||
|
$validator = new ImMessageValidator();
|
||||||
|
|
||||||
|
$validator->checkType($params['type']);
|
||||||
|
|
||||||
|
$sort = $pagerQuery->getSort();
|
||||||
|
$page = $pagerQuery->getPage();
|
||||||
|
$limit = $pagerQuery->getLimit();
|
||||||
|
|
||||||
|
if ($params['type'] == 'friend') {
|
||||||
|
|
||||||
|
$params['chat_id'] = ImFriendMessageModel::getChatId($user->id, $params['id']);
|
||||||
|
|
||||||
|
$messageRepo = new ImFriendMessageRepo();
|
||||||
|
|
||||||
|
$pager = $messageRepo->paginate($params, $sort, $page, $limit);
|
||||||
|
|
||||||
|
return $this->handleChatLog($pager);
|
||||||
|
|
||||||
|
} elseif ($params['type'] == 'group') {
|
||||||
|
|
||||||
|
$params['group_id'] = $params['id'];
|
||||||
|
|
||||||
|
$messageRepo = new ImGroupMessageRepo();
|
||||||
|
|
||||||
|
$pager = $messageRepo->paginate($params, $sort, $page, $limit);
|
||||||
|
|
||||||
|
return $this->handleChatLog($pager);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public function bindUser()
|
public function bindUser()
|
||||||
{
|
{
|
||||||
$user = $this->getLoginUser();
|
$user = $this->getLoginUser();
|
||||||
@ -54,6 +130,13 @@ class Messenger extends Service
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @todo 发送未读消息
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @todo 发送盒子消息
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
public function sendMessage()
|
public function sendMessage()
|
||||||
@ -74,6 +157,10 @@ class Messenger extends Service
|
|||||||
'mine' => false,
|
'mine' => false,
|
||||||
];
|
];
|
||||||
|
|
||||||
|
if ($to['type'] == 'group') {
|
||||||
|
$content['id'] = $to['id'];
|
||||||
|
}
|
||||||
|
|
||||||
$message = json_encode([
|
$message = json_encode([
|
||||||
'type' => 'show_message',
|
'type' => 'show_message',
|
||||||
'content' => $content,
|
'content' => $content,
|
||||||
@ -83,12 +170,20 @@ class Messenger extends Service
|
|||||||
|
|
||||||
if ($to['type'] == 'friend') {
|
if ($to['type'] == 'friend') {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 不推送自己给自己发送的消息
|
||||||
|
*/
|
||||||
|
if ($user->id != $to['id']) {
|
||||||
Gateway::sendToUid($to['id'], $message);
|
Gateway::sendToUid($to['id'], $message);
|
||||||
|
}
|
||||||
|
|
||||||
} elseif ($to['type'] == 'group') {
|
} elseif ($to['type'] == 'group') {
|
||||||
|
|
||||||
$excludeClientId = null;
|
$excludeClientId = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 不推送自己在群组中发的消息
|
||||||
|
*/
|
||||||
if ($user->id == $from['id']) {
|
if ($user->id == $from['id']) {
|
||||||
$excludeClientId = Gateway::getClientIdByUid($user->id);
|
$excludeClientId = Gateway::getClientIdByUid($user->id);
|
||||||
}
|
}
|
||||||
@ -176,6 +271,38 @@ class Messenger extends Service
|
|||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function handleChatLog($pager)
|
||||||
|
{
|
||||||
|
if ($pager->total_items == 0) {
|
||||||
|
return $pager;
|
||||||
|
}
|
||||||
|
|
||||||
|
$messages = $pager->items->toArray();
|
||||||
|
|
||||||
|
$builder = new ImMessageListBuilder();
|
||||||
|
|
||||||
|
$users = $builder->getUsers($messages);
|
||||||
|
|
||||||
|
$items = [];
|
||||||
|
|
||||||
|
foreach ($messages as $message) {
|
||||||
|
|
||||||
|
$user = $user = $users[$message['user_id']] ?? new \stdClass();
|
||||||
|
|
||||||
|
$items[] = [
|
||||||
|
'id' => $message['id'],
|
||||||
|
'content' => $message['content'],
|
||||||
|
'create_time' => $message['create_time'],
|
||||||
|
'timestamp' => $message['create_time'] * 1000,
|
||||||
|
'user' => $user,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
$pager->items = $items;
|
||||||
|
|
||||||
|
return $pager;
|
||||||
|
}
|
||||||
|
|
||||||
protected function getGroupName($groupId)
|
protected function getGroupName($groupId)
|
||||||
{
|
{
|
||||||
return "group_{$groupId}";
|
return "group_{$groupId}";
|
||||||
|
91
app/Http/Web/Views/messenger/chat_log.volt
Normal file
91
app/Http/Web/Views/messenger/chat_log.volt
Normal file
@ -0,0 +1,91 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
|
||||||
|
<title>聊天记录</title>
|
||||||
|
<link rel="stylesheet" href="/static/lib/layui/css/layui.css">
|
||||||
|
<style>
|
||||||
|
body .layim-chat-main {
|
||||||
|
height: auto;
|
||||||
|
}
|
||||||
|
|
||||||
|
#LAY_page {
|
||||||
|
margin-bottom: 20px;
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
|
||||||
|
<div class="layim-chat-main">
|
||||||
|
<ul id="LAY_view"></ul>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div id="LAY_page"></div>
|
||||||
|
|
||||||
|
<textarea title="消息模版" id="LAY_tpl" style="display:none;">
|
||||||
|
<%# layui.each(d.data, function(index, item) {
|
||||||
|
if (item.user.id == parent.layui.layim.cache().mine.id) { %>
|
||||||
|
<li class="layim-chat-mine"><div class="layim-chat-user"><img src="<% item.user.avatar %>"><cite><i><% layui.data.date(item.timestamp) %></i><% item.user.name %></cite></div><div class="layim-chat-text"><% layui.layim.content(item.content) %></div></li>
|
||||||
|
<%# } else { %>
|
||||||
|
<li><div class="layim-chat-user"><img src="<% item.user.avatar %>"><cite><% item.user.name %><i><% layui.data.date(item.timestamp) %></i></cite></div><div class="layim-chat-text"><% layui.layim.content(item.content) %></div></li>
|
||||||
|
<%# }
|
||||||
|
}); %>
|
||||||
|
</textarea>
|
||||||
|
|
||||||
|
<script src="/static/lib/layui/layui.js"></script>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
layui.use(['layim', 'laypage'], function () {
|
||||||
|
|
||||||
|
var $ = layui.jquery;
|
||||||
|
var layim = layui.layim;
|
||||||
|
var laytpl = layui.laytpl;
|
||||||
|
var laypage = layui.laypage;
|
||||||
|
|
||||||
|
laytpl.config({
|
||||||
|
open: '<%',
|
||||||
|
close: '%>'
|
||||||
|
});
|
||||||
|
|
||||||
|
var chatHistoryUrl = '/im/chat/history';
|
||||||
|
|
||||||
|
var currentUrl = layui.url();
|
||||||
|
|
||||||
|
var params = {
|
||||||
|
id: currentUrl.search.id,
|
||||||
|
type: currentUrl.search.type,
|
||||||
|
page: 1,
|
||||||
|
limit: 15,
|
||||||
|
sort: 'oldest'
|
||||||
|
};
|
||||||
|
|
||||||
|
loadChatHistoryHtml(params);
|
||||||
|
|
||||||
|
laypage.render({
|
||||||
|
elem: 'LAY_page',
|
||||||
|
limit: 30,
|
||||||
|
count: {{ pager.total_items }},
|
||||||
|
jump: function (obj, first) {
|
||||||
|
if (!first) {
|
||||||
|
params.page = obj.curr;
|
||||||
|
loadChatHistoryHtml(params);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
function loadChatHistoryHtml(params) {
|
||||||
|
$.get(chatHistoryUrl, params, function (res) {
|
||||||
|
var html = laytpl(LAY_tpl.value).render({
|
||||||
|
data: res.items
|
||||||
|
});
|
||||||
|
$('#LAY_view').html(html);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
|
||||||
|
</body>
|
||||||
|
</html>
|
@ -3,19 +3,16 @@
|
|||||||
<head>
|
<head>
|
||||||
<meta charset="utf-8">
|
<meta charset="utf-8">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
|
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
|
||||||
<meta name="keywords" content="{{ site_seo.getKeywords() }}">
|
|
||||||
<meta name="description" content="{{ site_seo.getDescription() }}">
|
|
||||||
<meta name="csrf-token" content="{{ csrfToken.getToken() }}">
|
<meta name="csrf-token" content="{{ csrfToken.getToken() }}">
|
||||||
<title>{{ site_seo.getTitle() }}</title>
|
|
||||||
{{ icon_link('favicon.ico') }}
|
{{ icon_link('favicon.ico') }}
|
||||||
{{ css_link('lib/layui/css/layui.css') }}
|
{{ css_link('lib/layui/css/layui.css') }}
|
||||||
{{ css_link('web/css/common.css') }}
|
{{ css_link('web/css/common.css') }}
|
||||||
{% block link_css %}{% endblock %}
|
{% block link_css %}{% endblock %}
|
||||||
{% block inline_css %}{% endblock %}
|
{% block inline_css %}{% endblock %}
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body class="layer">
|
||||||
{% block content %}{% endblock %}
|
{% block content %}{% endblock %}
|
||||||
{{ js_include('lib/layui/layui.all.js') }}
|
{{ js_include('lib/layui/layui.js') }}
|
||||||
{{ js_include('web/js/common.js') }}
|
{{ js_include('web/js/common.js') }}
|
||||||
{% block include_js %}{% endblock %}
|
{% block include_js %}{% endblock %}
|
||||||
{% block inline_js %}{% endblock %}
|
{% block inline_js %}{% endblock %}
|
||||||
|
@ -3,21 +3,21 @@
|
|||||||
namespace App\Repos;
|
namespace App\Repos;
|
||||||
|
|
||||||
use App\Library\Paginator\Adapter\QueryBuilder as PagerQueryBuilder;
|
use App\Library\Paginator\Adapter\QueryBuilder as PagerQueryBuilder;
|
||||||
use App\Models\ImChatGroup as ImGroupModel;
|
use App\Models\ImChatGroup as ImChatGroupModel;
|
||||||
use App\Models\ImChatGroupUser as ImGroupUserModel;
|
use App\Models\ImChatGroupUser as ImGroupUserModel;
|
||||||
use App\Models\User as UserModel;
|
use App\Models\User as UserModel;
|
||||||
use Phalcon\Mvc\Model;
|
use Phalcon\Mvc\Model;
|
||||||
use Phalcon\Mvc\Model\Resultset;
|
use Phalcon\Mvc\Model\Resultset;
|
||||||
use Phalcon\Mvc\Model\ResultsetInterface;
|
use Phalcon\Mvc\Model\ResultsetInterface;
|
||||||
|
|
||||||
class ImGroup extends Repository
|
class ImChatGroup extends Repository
|
||||||
{
|
{
|
||||||
|
|
||||||
public function paginate($where = [], $sort = 'latest', $page = 1, $limit = 15)
|
public function paginate($where = [], $sort = 'latest', $page = 1, $limit = 15)
|
||||||
{
|
{
|
||||||
$builder = $this->modelsManager->createBuilder();
|
$builder = $this->modelsManager->createBuilder();
|
||||||
|
|
||||||
$builder->from(ImGroupModel::class);
|
$builder->from(ImChatGroupModel::class);
|
||||||
|
|
||||||
$builder->where('1 = 1');
|
$builder->where('1 = 1');
|
||||||
|
|
||||||
@ -52,21 +52,21 @@ class ImGroup extends Repository
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param int $id
|
* @param int $id
|
||||||
* @return ImGroupModel|Model|bool
|
* @return ImChatGroupModel|Model|bool
|
||||||
*/
|
*/
|
||||||
public function findById($id)
|
public function findById($id)
|
||||||
{
|
{
|
||||||
return ImGroupModel::findFirst($id);
|
return ImChatGroupModel::findFirst($id);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $ids
|
* @param array $ids
|
||||||
* @param string|array $columns
|
* @param string|array $columns
|
||||||
* @return ResultsetInterface|Resultset|ImGroupModel[]
|
* @return ResultsetInterface|Resultset|ImChatGroupModel[]
|
||||||
*/
|
*/
|
||||||
public function findByIds($ids, $columns = '*')
|
public function findByIds($ids, $columns = '*')
|
||||||
{
|
{
|
||||||
return ImGroupModel::query()
|
return ImChatGroupModel::query()
|
||||||
->columns($columns)
|
->columns($columns)
|
||||||
->inWhere('id', $ids)
|
->inWhere('id', $ids)
|
||||||
->execute();
|
->execute();
|
90
app/Repos/ImFriendGroup.php
Normal file
90
app/Repos/ImFriendGroup.php
Normal file
@ -0,0 +1,90 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Repos;
|
||||||
|
|
||||||
|
use App\Library\Paginator\Adapter\QueryBuilder as PagerQueryBuilder;
|
||||||
|
use App\Models\ImFriend as ImFriendModel;
|
||||||
|
use App\Models\ImFriendGroup as ImFriendGroupModel;
|
||||||
|
use App\Models\User as UserModel;
|
||||||
|
use Phalcon\Mvc\Model;
|
||||||
|
use Phalcon\Mvc\Model\Resultset;
|
||||||
|
use Phalcon\Mvc\Model\ResultsetInterface;
|
||||||
|
|
||||||
|
class ImFriendGroup extends Repository
|
||||||
|
{
|
||||||
|
|
||||||
|
public function paginate($where = [], $sort = 'latest', $page = 1, $limit = 15)
|
||||||
|
{
|
||||||
|
$builder = $this->modelsManager->createBuilder();
|
||||||
|
|
||||||
|
$builder->from(ImFriendGroupModel::class);
|
||||||
|
|
||||||
|
$builder->where('1 = 1');
|
||||||
|
|
||||||
|
if (!empty($where['user_id'])) {
|
||||||
|
$builder->andWhere('user_id = :user_id:', ['user_id' => $where['user_id']]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!empty($where['name'])) {
|
||||||
|
$builder->andWhere('name LIKE :name:', ['name' => "%{$where['name']}%"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isset($where['deleted'])) {
|
||||||
|
$builder->andWhere('deleted = :deleted:', ['deleted' => $where['deleted']]);
|
||||||
|
}
|
||||||
|
|
||||||
|
switch ($sort) {
|
||||||
|
default:
|
||||||
|
$orderBy = 'id DESC';
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
$builder->orderBy($orderBy);
|
||||||
|
|
||||||
|
$pager = new PagerQueryBuilder([
|
||||||
|
'builder' => $builder,
|
||||||
|
'page' => $page,
|
||||||
|
'limit' => $limit,
|
||||||
|
]);
|
||||||
|
|
||||||
|
return $pager->paginate();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $id
|
||||||
|
* @return ImFriendGroupModel|Model|bool
|
||||||
|
*/
|
||||||
|
public function findById($id)
|
||||||
|
{
|
||||||
|
return ImFriendGroupModel::findFirst($id);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $ids
|
||||||
|
* @param string|array $columns
|
||||||
|
* @return ResultsetInterface|Resultset|ImFriendGroupModel[]
|
||||||
|
*/
|
||||||
|
public function findByIds($ids, $columns = '*')
|
||||||
|
{
|
||||||
|
return ImFriendGroupModel::query()
|
||||||
|
->columns($columns)
|
||||||
|
->inWhere('id', $ids)
|
||||||
|
->execute();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $groupId
|
||||||
|
* @return ResultsetInterface|Resultset|UserModel[]
|
||||||
|
*/
|
||||||
|
public function findGroupUsers($groupId)
|
||||||
|
{
|
||||||
|
return $this->modelsManager->createBuilder()
|
||||||
|
->columns('u.*')
|
||||||
|
->addFrom(UserModel::class, 'u')
|
||||||
|
->join(ImFriendModel::class, 'u.id = f.user_id', 'f')
|
||||||
|
->where('f.group_id = :group_id:', ['group_id' => $groupId])
|
||||||
|
->andWhere('u.deleted = 0')
|
||||||
|
->getQuery()->execute();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
76
app/Repos/ImFriendMessage.php
Normal file
76
app/Repos/ImFriendMessage.php
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Repos;
|
||||||
|
|
||||||
|
use App\Library\Paginator\Adapter\QueryBuilder as PagerQueryBuilder;
|
||||||
|
use App\Models\ImFriendMessage as ImFriendMessageModel;
|
||||||
|
use Phalcon\Mvc\Model;
|
||||||
|
use Phalcon\Mvc\Model\Resultset;
|
||||||
|
use Phalcon\Mvc\Model\ResultsetInterface;
|
||||||
|
|
||||||
|
class ImFriendMessage extends Repository
|
||||||
|
{
|
||||||
|
|
||||||
|
public function paginate($where = [], $sort = 'latest', $page = 1, $limit = 15)
|
||||||
|
{
|
||||||
|
$builder = $this->modelsManager->createBuilder();
|
||||||
|
|
||||||
|
$builder->from(ImFriendMessageModel::class);
|
||||||
|
|
||||||
|
$builder->where('1 = 1');
|
||||||
|
|
||||||
|
if (!empty($where['chat_id'])) {
|
||||||
|
$builder->andWhere('chat_id = :chat_id:', ['chat_id' => $where['chat_id']]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!empty($where['user_id'])) {
|
||||||
|
$builder->andWhere('user_id = :user_id:', ['user_id' => $where['user_id']]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isset($where['deleted'])) {
|
||||||
|
$builder->andWhere('deleted = :deleted:', ['deleted' => $where['deleted']]);
|
||||||
|
}
|
||||||
|
|
||||||
|
switch ($sort) {
|
||||||
|
case 'oldest':
|
||||||
|
$orderBy = 'id ASC';
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
$orderBy = 'id DESC';
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
$builder->orderBy($orderBy);
|
||||||
|
|
||||||
|
$pager = new PagerQueryBuilder([
|
||||||
|
'builder' => $builder,
|
||||||
|
'page' => $page,
|
||||||
|
'limit' => $limit,
|
||||||
|
]);
|
||||||
|
|
||||||
|
return $pager->paginate();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $id
|
||||||
|
* @return ImFriendMessageModel|Model|bool
|
||||||
|
*/
|
||||||
|
public function findById($id)
|
||||||
|
{
|
||||||
|
return ImFriendMessageModel::findFirst($id);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $ids
|
||||||
|
* @param string|array $columns
|
||||||
|
* @return ResultsetInterface|Resultset|ImFriendMessageModel[]
|
||||||
|
*/
|
||||||
|
public function findByIds($ids, $columns = '*')
|
||||||
|
{
|
||||||
|
return ImFriendMessageModel::query()
|
||||||
|
->columns($columns)
|
||||||
|
->inWhere('id', $ids)
|
||||||
|
->execute();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
76
app/Repos/ImGroupMessage.php
Normal file
76
app/Repos/ImGroupMessage.php
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Repos;
|
||||||
|
|
||||||
|
use App\Library\Paginator\Adapter\QueryBuilder as PagerQueryBuilder;
|
||||||
|
use App\Models\ImGroupMessage as ImGroupMessageModel;
|
||||||
|
use Phalcon\Mvc\Model;
|
||||||
|
use Phalcon\Mvc\Model\Resultset;
|
||||||
|
use Phalcon\Mvc\Model\ResultsetInterface;
|
||||||
|
|
||||||
|
class ImGroupMessage extends Repository
|
||||||
|
{
|
||||||
|
|
||||||
|
public function paginate($where = [], $sort = 'latest', $page = 1, $limit = 15)
|
||||||
|
{
|
||||||
|
$builder = $this->modelsManager->createBuilder();
|
||||||
|
|
||||||
|
$builder->from(ImGroupMessageModel::class);
|
||||||
|
|
||||||
|
$builder->where('1 = 1');
|
||||||
|
|
||||||
|
if (!empty($where['group_id'])) {
|
||||||
|
$builder->andWhere('group_id = :group_id:', ['group_id' => $where['group_id']]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!empty($where['user_id'])) {
|
||||||
|
$builder->andWhere('user_id = :user_id:', ['user_id' => $where['user_id']]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isset($where['deleted'])) {
|
||||||
|
$builder->andWhere('deleted = :deleted:', ['deleted' => $where['deleted']]);
|
||||||
|
}
|
||||||
|
|
||||||
|
switch ($sort) {
|
||||||
|
case 'oldest':
|
||||||
|
$orderBy = 'id ASC';
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
$orderBy = 'id DESC';
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
$builder->orderBy($orderBy);
|
||||||
|
|
||||||
|
$pager = new PagerQueryBuilder([
|
||||||
|
'builder' => $builder,
|
||||||
|
'page' => $page,
|
||||||
|
'limit' => $limit,
|
||||||
|
]);
|
||||||
|
|
||||||
|
return $pager->paginate();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $id
|
||||||
|
* @return ImGroupMessageModel|Model|bool
|
||||||
|
*/
|
||||||
|
public function findById($id)
|
||||||
|
{
|
||||||
|
return ImGroupMessageModel::findFirst($id);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $ids
|
||||||
|
* @param string|array $columns
|
||||||
|
* @return ResultsetInterface|Resultset|ImGroupMessageModel[]
|
||||||
|
*/
|
||||||
|
public function findByIds($ids, $columns = '*')
|
||||||
|
{
|
||||||
|
return ImGroupMessageModel::query()
|
||||||
|
->columns($columns)
|
||||||
|
->inWhere('id', $ids)
|
||||||
|
->execute();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
88
app/Validators/ImChatGroup.php
Normal file
88
app/Validators/ImChatGroup.php
Normal file
@ -0,0 +1,88 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Validators;
|
||||||
|
|
||||||
|
use App\Caches\ImChatGroup as ImChatGroupCache;
|
||||||
|
use App\Caches\MaxImChatGroupId as MaxImChatGroupIdCache;
|
||||||
|
use App\Exceptions\BadRequest as BadRequestException;
|
||||||
|
use App\Models\ImChatGroup as ImChatGroupModel;
|
||||||
|
use App\Repos\ImChatGroup as ImChatGroupRepo;
|
||||||
|
|
||||||
|
class ImChatGroup extends Validator
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $id
|
||||||
|
* @return ImChatGroupModel
|
||||||
|
* @throws BadRequestException
|
||||||
|
*/
|
||||||
|
public function checkGroupCache($id)
|
||||||
|
{
|
||||||
|
$id = intval($id);
|
||||||
|
|
||||||
|
$maxGroupIdCache = new MaxImChatGroupIdCache();
|
||||||
|
|
||||||
|
$maxGroupId = $maxGroupIdCache->get();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 防止缓存穿透
|
||||||
|
*/
|
||||||
|
if ($id < 1 || $id > $maxGroupId) {
|
||||||
|
throw new BadRequestException('im_chat_group.not_found');
|
||||||
|
}
|
||||||
|
|
||||||
|
$groupCache = new ImChatGroupCache();
|
||||||
|
|
||||||
|
$group = $groupCache->get($id);
|
||||||
|
|
||||||
|
if (!$group) {
|
||||||
|
throw new BadRequestException('im_chat_group.not_found');
|
||||||
|
}
|
||||||
|
|
||||||
|
return $group;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function checkGroup($id)
|
||||||
|
{
|
||||||
|
$groupRepo = new ImChatGroupRepo();
|
||||||
|
|
||||||
|
$group = $groupRepo->findById($id);
|
||||||
|
|
||||||
|
if (!$group) {
|
||||||
|
throw new BadRequestException('im_chat_group.not_found');
|
||||||
|
}
|
||||||
|
|
||||||
|
return $group;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function checkName($name)
|
||||||
|
{
|
||||||
|
$value = $this->filter->sanitize($name, ['trim', 'string']);
|
||||||
|
|
||||||
|
$length = kg_strlen($value);
|
||||||
|
|
||||||
|
if ($length < 2) {
|
||||||
|
throw new BadRequestException('im_chat_group.name_too_short');
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($length > 50) {
|
||||||
|
throw new BadRequestException('im_chat_group.name_too_long');
|
||||||
|
}
|
||||||
|
|
||||||
|
return $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function checkAbout($name)
|
||||||
|
{
|
||||||
|
$value = $this->filter->sanitize($name, ['trim', 'string']);
|
||||||
|
|
||||||
|
$length = kg_strlen($value);
|
||||||
|
|
||||||
|
if ($length > 255) {
|
||||||
|
throw new BadRequestException('im_chat_group.about_too_long');
|
||||||
|
}
|
||||||
|
|
||||||
|
return $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
41
app/Validators/ImFriendGroup.php
Normal file
41
app/Validators/ImFriendGroup.php
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Validators;
|
||||||
|
|
||||||
|
use App\Exceptions\BadRequest as BadRequestException;
|
||||||
|
use App\Repos\ImChatGroup as ImChatGroupRepo;
|
||||||
|
|
||||||
|
class ImFriendGroup extends Validator
|
||||||
|
{
|
||||||
|
|
||||||
|
public function checkGroup($id)
|
||||||
|
{
|
||||||
|
$groupRepo = new ImChatGroupRepo();
|
||||||
|
|
||||||
|
$group = $groupRepo->findById($id);
|
||||||
|
|
||||||
|
if (!$group) {
|
||||||
|
throw new BadRequestException('im_friend_group.not_found');
|
||||||
|
}
|
||||||
|
|
||||||
|
return $group;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function checkName($name)
|
||||||
|
{
|
||||||
|
$value = $this->filter->sanitize($name, ['trim', 'string']);
|
||||||
|
|
||||||
|
$length = kg_strlen($value);
|
||||||
|
|
||||||
|
if ($length < 2) {
|
||||||
|
throw new BadRequestException('im_friend_group.name_too_short');
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($length > 15) {
|
||||||
|
throw new BadRequestException('im_friend_group.name_too_long');
|
||||||
|
}
|
||||||
|
|
||||||
|
return $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
64
app/Validators/ImMessage.php
Normal file
64
app/Validators/ImMessage.php
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Validators;
|
||||||
|
|
||||||
|
use App\Exceptions\BadRequest as BadRequestException;
|
||||||
|
use App\Repos\ImFriendMessage as ImFriendMessageRepo;
|
||||||
|
use App\Repos\ImGroupMessage as ImGroupMessageRepo;
|
||||||
|
|
||||||
|
class ImMessage extends Validator
|
||||||
|
{
|
||||||
|
|
||||||
|
public function checkFriendMessage($id)
|
||||||
|
{
|
||||||
|
$messageRepo = new ImFriendMessageRepo();
|
||||||
|
|
||||||
|
$message = $messageRepo->findById($id);
|
||||||
|
|
||||||
|
if (!$message) {
|
||||||
|
throw new BadRequestException('im_message.not_found');
|
||||||
|
}
|
||||||
|
|
||||||
|
return $message;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function checkGroupMessage($id)
|
||||||
|
{
|
||||||
|
$messageRepo = new ImGroupMessageRepo();
|
||||||
|
|
||||||
|
$message = $messageRepo->findById($id);
|
||||||
|
|
||||||
|
if (!$message) {
|
||||||
|
throw new BadRequestException('im_message.not_found');
|
||||||
|
}
|
||||||
|
|
||||||
|
return $message;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function checkType($type)
|
||||||
|
{
|
||||||
|
if (!in_array($type, ['friend', 'group'])) {
|
||||||
|
throw new BadRequestException('im_message.invalid_type');
|
||||||
|
}
|
||||||
|
|
||||||
|
return $type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function checkContent($content)
|
||||||
|
{
|
||||||
|
$value = $this->filter->sanitize($content, ['trim', 'string']);
|
||||||
|
|
||||||
|
$length = kg_strlen($value);
|
||||||
|
|
||||||
|
if ($length < 1) {
|
||||||
|
throw new BadRequestException('im_message.content_too_short');
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($length > 1000) {
|
||||||
|
throw new BadRequestException('im_message.content_too_long');
|
||||||
|
}
|
||||||
|
|
||||||
|
return $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1282,3 +1282,13 @@
|
|||||||
.order-item span {
|
.order-item span {
|
||||||
margin-right: 8px;
|
margin-right: 8px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.layer .layim-chat-main {
|
||||||
|
height: auto;
|
||||||
|
}
|
||||||
|
|
||||||
|
.layim-chat-user img {
|
||||||
|
width: 40px;
|
||||||
|
height: 40px;
|
||||||
|
border-radius: 100%;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user