调整消息推送
This commit is contained in:
@@ -51,16 +51,6 @@ class MessageUserStatusController extends BaseController
|
|||||||
|
|
||||||
protected function unReadFormat($data)
|
protected function unReadFormat($data)
|
||||||
{
|
{
|
||||||
// if (isset($data['read_at']) && $data['read_at'] !== null) {
|
|
||||||
// $data['read_at'] = date('Y-m-d H:i:s', $data['read_at']);
|
|
||||||
// } else {
|
|
||||||
// $data['read_at'] = ' - ';
|
|
||||||
// }
|
|
||||||
|
|
||||||
// if (isset($data['message'])) {
|
|
||||||
// $data['message']['created_at'] = date('Y-m-d H:i:s', $data['message']['created_at']);
|
|
||||||
// }
|
|
||||||
|
|
||||||
foreach ($data as &$item) {
|
foreach ($data as &$item) {
|
||||||
if (isset($item['read_at']) && $item['read_at'] !== null) {
|
if (isset($item['read_at']) && $item['read_at'] !== null) {
|
||||||
$item['read_at'] = date('Y-m-d H:i:s', $item['read_at']);
|
$item['read_at'] = date('Y-m-d H:i:s', $item['read_at']);
|
||||||
@@ -72,7 +62,6 @@ class MessageUserStatusController extends BaseController
|
|||||||
$item['message']['created_at'] = date('Y-m-d H:i:s', $item['message']['created_at']);
|
$item['message']['created_at'] = date('Y-m-d H:i:s', $item['message']['created_at']);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
var_dump($data);
|
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -94,6 +83,10 @@ class MessageUserStatusController extends BaseController
|
|||||||
$data = $this->transformData($raw, method_exists($this, 'unReadFormat')
|
$data = $this->transformData($raw, method_exists($this, 'unReadFormat')
|
||||||
? [$this, 'unReadFormat']
|
? [$this, 'unReadFormat']
|
||||||
: null);
|
: null);
|
||||||
|
$data = [
|
||||||
|
'list' => $data,
|
||||||
|
'counts' => count($data)
|
||||||
|
];
|
||||||
return $this->success($data);
|
return $this->success($data);
|
||||||
} catch (\Throwable $e) {
|
} catch (\Throwable $e) {
|
||||||
return $this->error($e->getMessage());
|
return $this->error($e->getMessage());
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ use app\common\logic\MessageNotificationsLogic;
|
|||||||
use app\common\validate\MessageNotificationsValidate;
|
use app\common\validate\MessageNotificationsValidate;
|
||||||
use app\common\traits\crud\CrudServiceTrait;
|
use app\common\traits\crud\CrudServiceTrait;
|
||||||
use app\common\model\MessageNotifications;
|
use app\common\model\MessageNotifications;
|
||||||
|
use app\common\model\MessageUserStatus;
|
||||||
use app\common\model\User;
|
use app\common\model\User;
|
||||||
use support\Db;
|
use support\Db;
|
||||||
use support\Request;
|
use support\Request;
|
||||||
@@ -106,6 +107,111 @@ class MessageNotificationsService
|
|||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// ====== ====== ====== 通知操作的处理 ====== ====== ======
|
||||||
|
/**
|
||||||
|
* 推送消息
|
||||||
|
*/
|
||||||
|
public function pushNotice(array $data = [], string $scene = "Default"): int
|
||||||
|
{
|
||||||
|
// 拼装数据(可能需要处理 消息标题、内容等等数据,可自行处理)
|
||||||
|
$pushData = $this->assemblyProcess($scene, $data);
|
||||||
|
// 写入消息
|
||||||
|
$msgId = $this->insertMessage($pushData);
|
||||||
|
// 推送
|
||||||
|
$this->pushWs($msgId, $pushData);
|
||||||
|
return $msgId;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行推送
|
||||||
|
*/
|
||||||
|
public function pushWs(int $msgId, array $pushData)
|
||||||
|
{
|
||||||
|
foreach ($pushData['recipient'] as $userId) {
|
||||||
|
// 查询消息数据
|
||||||
|
$config['model'] = MessageUserStatus::class;
|
||||||
|
$config['order']['id'] = 'desc';
|
||||||
|
$config['with']['message'] = ['sender_id', 'sender_type', 'msg_no', 'title', 'content', 'scene', 'created_at'];
|
||||||
|
$config['where']['uid'] = $userId;
|
||||||
|
$config['where']['msg_id'] = $msgId;
|
||||||
|
$config['where']['status'] = 0;
|
||||||
|
$wsData = (new MessageUserStatusService())->logicShow($config)->toArray();
|
||||||
|
|
||||||
|
// 组装数据
|
||||||
|
if (isset($wsData['message']) && $wsData['message']) {
|
||||||
|
$wsData['message']['sender_name'] = User::where('id', $wsData['message']['sender_id'])->value('name') ?: '系统';
|
||||||
|
$wsData['message']['scene_name'] = MessageNotifications::SCENE[$wsData['message']['scene']];
|
||||||
|
$wsData['message']['created_at'] = date('Y-m-d H:i:s', $wsData['message']['created_at']);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取未读消息数
|
||||||
|
$counts = MessageUserStatus::where('uid', $userId)->where('status', 0)->count('id');
|
||||||
|
$data = [
|
||||||
|
'notice_data' => $wsData,
|
||||||
|
'unread_counts' => $counts
|
||||||
|
];
|
||||||
|
|
||||||
|
// 执行推送
|
||||||
|
(new \app\common\utils\WsEvents())->pushWsNotice($data, $userId);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 插入消息
|
||||||
|
* @param array $data
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function insertMessage(array $data): int
|
||||||
|
{
|
||||||
|
// 写消息
|
||||||
|
$msgId = $this->createMessage([
|
||||||
|
'sender_id' => $data['sender_id'],
|
||||||
|
'sender_type' => $data['sender_type'],
|
||||||
|
'title' => $data['title'],
|
||||||
|
'content' => $data['content'],
|
||||||
|
'scene' => $data['scene'],
|
||||||
|
'recipient' => implode(',', $data['recipient']),
|
||||||
|
'push_method' => $data['push_method'],
|
||||||
|
]);
|
||||||
|
// 写用户状态
|
||||||
|
$this->addUserStatus($msgId, $data['recipient']);
|
||||||
|
return $msgId;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据拼装方法调用
|
||||||
|
*/
|
||||||
|
protected function assemblyProcess(string $op, mixed $data): mixed
|
||||||
|
{
|
||||||
|
$hook = "assembly{$op}";
|
||||||
|
return method_exists($this, $hook)
|
||||||
|
? $this->{$hook}($data)
|
||||||
|
: $data;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 默认数据拼装
|
||||||
|
*/
|
||||||
|
public function assemblyDefault(array $data)
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'sender_id' => isset($data['sender_id']) && $data['sender_id'] ? $data['sender_id'] : 0,
|
||||||
|
'sender_type' => isset($data['sender_type']) && $data['sender_type'] ? $data['sender_type'] : 'system',
|
||||||
|
'title' => isset($data['title']) && $data['title'] ? $data['title'] : '系统通知',
|
||||||
|
'content' => isset($data['content']) && $data['content'] ? $data['content'] : '系统通知-默认',
|
||||||
|
'scene' => isset($data['scene']) && $data['scene'] ? $data['scene'] : 1,
|
||||||
|
'recipient' => isset($data['recipient']) && count($data['recipient']) > 0 ? $data['recipient'] : $this->getAllUserIds(),
|
||||||
|
'push_method' => isset($data['push_method']) && $data['push_method'] ? $data['push_method'] : 0,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
// *** *** 可以扩展数据组拼装方法 *** ***
|
||||||
|
|
||||||
|
// ====== ====== ====== 通知操作的基础方法 ====== ====== ======
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建一条消息
|
* 创建一条消息
|
||||||
*/
|
*/
|
||||||
@@ -167,40 +273,4 @@ class MessageNotificationsService
|
|||||||
{
|
{
|
||||||
return User::where('status', 1)->pluck('id')->toArray();
|
return User::where('status', 1)->pluck('id')->toArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 批量推送消息
|
|
||||||
*/
|
|
||||||
public function pushNotice(array $data): int
|
|
||||||
{
|
|
||||||
// 数据结构:
|
|
||||||
// $data = [
|
|
||||||
// 'sender_id' => 0,
|
|
||||||
// 'sender_type' => 'system',
|
|
||||||
// 'title' => '测试消息 - 标题',
|
|
||||||
// 'content' => '测试消息 - 内容',
|
|
||||||
// 'scene' => 1,
|
|
||||||
// 'recipient' => [],
|
|
||||||
// ];
|
|
||||||
|
|
||||||
// 写消息
|
|
||||||
$msgId = $this->createMessage([
|
|
||||||
'sender_id' => $data['sender_id'],
|
|
||||||
'sender_type' => $data['sender_type'],
|
|
||||||
'title' => $data['title'],
|
|
||||||
'content' => $data['content'],
|
|
||||||
'scene' => $data['scene'],
|
|
||||||
'recipient' => $data['recipient'] ? implode(',', $data['recipient']) : '',
|
|
||||||
'push_method' => $data['push_method'],
|
|
||||||
]);
|
|
||||||
|
|
||||||
// 写用户状态
|
|
||||||
$uids = count($data['recipient']) > 0 ? $data['recipient'] : $this->getAllUserIds();
|
|
||||||
$this->addUserStatus($msgId, $uids);
|
|
||||||
|
|
||||||
// 执行推送
|
|
||||||
(new \app\common\utils\WsEvents())->pushNotice($msgId, $data['title'], $data['content'], $data['recipient']);
|
|
||||||
|
|
||||||
return $msgId;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace app\common\service;
|
namespace app\common\service;
|
||||||
|
|
||||||
|
use app\common\model\MessageNotifications;
|
||||||
use app\common\validate\MessageUserStatusValidate;
|
use app\common\validate\MessageUserStatusValidate;
|
||||||
use app\common\traits\crud\CrudServiceTrait;
|
use app\common\traits\crud\CrudServiceTrait;
|
||||||
use app\common\model\MessageUserStatus;
|
use app\common\model\MessageUserStatus;
|
||||||
@@ -41,21 +42,18 @@ class MessageUserStatusService
|
|||||||
switch ($scene) {
|
switch ($scene) {
|
||||||
case 'unread':
|
case 'unread':
|
||||||
$config['order']['id'] = 'desc';
|
$config['order']['id'] = 'desc';
|
||||||
// $config['with']['user'] = ['sn', 'name'];
|
$config['with']['message'] = ['sender_id', 'sender_type', 'msg_no', 'title', 'content', 'scene', 'created_at'];
|
||||||
$config['with']['message'] = ['sender_id', 'sender_type', 'msg_no', 'title', 'content', 'created_at'];
|
|
||||||
$config['where']['uid'] = $request->user->uid;
|
$config['where']['uid'] = $request->user->uid;
|
||||||
$config['where']['status'] = 0;
|
$config['where']['status'] = 0;
|
||||||
break;
|
break;
|
||||||
case 'index':
|
case 'index':
|
||||||
$config['order']['id'] = 'desc';
|
$config['order']['id'] = 'desc';
|
||||||
// $config['with']['user'] = ['sn', 'name'];
|
$config['with']['message'] = ['sender_id', 'sender_type', 'msg_no', 'title', 'content', 'scene', 'created_at'];
|
||||||
$config['with']['message'] = ['sender_id', 'sender_type', 'msg_no', 'title', 'content', 'created_at'];
|
|
||||||
$config['where']['uid'] = $request->user->uid;
|
$config['where']['uid'] = $request->user->uid;
|
||||||
break;
|
break;
|
||||||
case 'show':
|
case 'show':
|
||||||
$config['where']['id'] = $p['id'];
|
$config['where']['id'] = $p['id'];
|
||||||
// $config['with']['user'] = ['sn', 'name'];
|
$config['with']['message'] = ['sender_id', 'sender_type', 'msg_no', 'title', 'content', 'scene', 'created_at'];
|
||||||
$config['with']['message'] = ['sender_id', 'sender_type', 'msg_no', 'title', 'content', 'created_at'];
|
|
||||||
$config['where']['uid'] = $request->user->uid;
|
$config['where']['uid'] = $request->user->uid;
|
||||||
break;
|
break;
|
||||||
case 'store':
|
case 'store':
|
||||||
@@ -104,6 +102,7 @@ class MessageUserStatusService
|
|||||||
$res->each(function (&$item) {
|
$res->each(function (&$item) {
|
||||||
if (isset($item->message)) {
|
if (isset($item->message)) {
|
||||||
$item->message->sender_name = User::where('id', $item->message->sender_id)->value('name') ?: '系统';
|
$item->message->sender_name = User::where('id', $item->message->sender_id)->value('name') ?: '系统';
|
||||||
|
$item->message->scene_name = MessageNotifications::SCENE[$item->message->scene];
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
unset($item);
|
unset($item);
|
||||||
@@ -114,7 +113,24 @@ class MessageUserStatusService
|
|||||||
{
|
{
|
||||||
if (isset($res->message)) {
|
if (isset($res->message)) {
|
||||||
$res->message->sender_name = User::where('id', $res->message->sender_id)->value('name') ?: '系统';
|
$res->message->sender_name = User::where('id', $res->message->sender_id)->value('name') ?: '系统';
|
||||||
|
$res->message->scene_name = MessageNotifications::SCENE[$res->message->scene];
|
||||||
|
}
|
||||||
|
// 标记已读
|
||||||
|
if ($res->status == 0) {
|
||||||
|
$this->noticeRead([$res->msg_id], $res->uid);
|
||||||
}
|
}
|
||||||
return $res;
|
return $res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function afterServiceListUnRead($res)
|
||||||
|
{
|
||||||
|
$res->each(function (&$item) {
|
||||||
|
if (isset($item->message)) {
|
||||||
|
$item->message->sender_name = User::where('id', $item->message->sender_id)->value('name') ?: '系统';
|
||||||
|
$item->message->scene_name = MessageNotifications::SCENE[$item->message->scene];
|
||||||
|
}
|
||||||
|
});
|
||||||
|
unset($item);
|
||||||
|
return $res;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -74,16 +74,16 @@ class WsEvents
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 连接是否满足条件
|
// 连接是否满足条件
|
||||||
if ($payload['server']['HTTP_ORIGIN'] != 'https://wstool.js.org') {
|
// if ($payload['server']['HTTP_ORIGIN'] != 'https://wstool.js.org') {
|
||||||
flg('[x] [TOKEN认证] HTTP_ORIGIN不满足条件', false);
|
// flg('[x] [TOKEN认证] HTTP_ORIGIN不满足条件', false);
|
||||||
Gateway::sendToClient($client_id, WsResponse::error(
|
// Gateway::sendToClient($client_id, WsResponse::error(
|
||||||
'system.auth',
|
// 'system.auth',
|
||||||
1001,
|
// 1001,
|
||||||
'[x] [TOKEN认证] HTTP_ORIGIN不满足条件'
|
// '[x] [TOKEN认证] HTTP_ORIGIN不满足条件'
|
||||||
));
|
// ));
|
||||||
Gateway::closeClient($client_id);
|
// Gateway::closeClient($client_id);
|
||||||
return;
|
// return;
|
||||||
}
|
// }
|
||||||
|
|
||||||
// 签名检查
|
// 签名检查
|
||||||
if (!isset($payload['get']['token'])) {
|
if (!isset($payload['get']['token'])) {
|
||||||
@@ -226,24 +226,9 @@ class WsEvents
|
|||||||
/**
|
/**
|
||||||
* 推送消息通知
|
* 推送消息通知
|
||||||
*/
|
*/
|
||||||
public function pushNotice(int $msgId, string $title, string $content, array $recipient)
|
public function pushWsNotice(array $wsData, int $userId)
|
||||||
{
|
{
|
||||||
$payload = WsResponse::success('notify.receive', [
|
Gateway::sendToUid($userId, WsResponse::success('notify.receive', $wsData));
|
||||||
'msg_id' => $msgId,
|
|
||||||
'title' => $title,
|
|
||||||
'content' => $content,
|
|
||||||
'created_at' => time()
|
|
||||||
]);
|
|
||||||
|
|
||||||
if (count($recipient) > 0) {
|
|
||||||
// 推送给指定用户
|
|
||||||
foreach ($recipient as $uid) {
|
|
||||||
Gateway::sendToUid($uid, $payload);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// 推送给全体在线用户
|
|
||||||
Gateway::sendToAll($payload);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ================= 辅助方法 =================
|
// ================= 辅助方法 =================
|
||||||
|
|||||||
@@ -116,21 +116,27 @@ class IndexController
|
|||||||
// 'recipient' => [1],
|
// 'recipient' => [1],
|
||||||
// ];
|
// ];
|
||||||
|
|
||||||
|
$data['recipient'] = [1, 74];
|
||||||
// // 同步推送
|
// // 同步推送
|
||||||
// (new MessageNotificationsService())->pushNotice($data);
|
// (new MessageNotificationsService())->pushNotice($data);
|
||||||
|
|
||||||
// // 异步推送
|
|
||||||
// \Webman\RedisQueue\Redis::send('notice', $data);
|
|
||||||
|
// 异步推送
|
||||||
|
\Webman\RedisQueue\Redis::send('notice', $data);
|
||||||
|
|
||||||
|
|
||||||
|
return 'success';
|
||||||
|
|
||||||
// return json($data);
|
// return json($data);
|
||||||
|
|
||||||
$phone = '';
|
// $phone = '';
|
||||||
$email = '';
|
// $email = '';
|
||||||
|
|
||||||
$userByPhone = User::where('corp_id', 2)->where('phone', $phone)->where('phone', '!=', '')->first();
|
// $userByPhone = User::where('corp_id', 2)->where('phone', $phone)->where('phone', '!=', '')->first();
|
||||||
$userByEmail = User::where('corp_id', 2)->where('email', $email)->where('email', '!=', '')->first();
|
// $userByEmail = User::where('corp_id', 2)->where('email', $email)->where('email', '!=', '')->first();
|
||||||
$user = $userByPhone ?: $userByEmail;
|
// $user = $userByPhone ?: $userByEmail;
|
||||||
|
|
||||||
return json($user);
|
// return json($user);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user