diff --git a/app/common/controller/MessageUserStatusController.php b/app/common/controller/MessageUserStatusController.php index a9ccb11..9a896a1 100644 --- a/app/common/controller/MessageUserStatusController.php +++ b/app/common/controller/MessageUserStatusController.php @@ -51,16 +51,6 @@ class MessageUserStatusController extends BaseController 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) { if (isset($item['read_at']) && $item['read_at'] !== null) { $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']); } } - var_dump($data); return $data; } @@ -94,6 +83,10 @@ class MessageUserStatusController extends BaseController $data = $this->transformData($raw, method_exists($this, 'unReadFormat') ? [$this, 'unReadFormat'] : null); + $data = [ + 'list' => $data, + 'counts' => count($data) + ]; return $this->success($data); } catch (\Throwable $e) { return $this->error($e->getMessage()); diff --git a/app/common/service/MessageNotificationsService.php b/app/common/service/MessageNotificationsService.php index 4e8162c..806bc1d 100644 --- a/app/common/service/MessageNotificationsService.php +++ b/app/common/service/MessageNotificationsService.php @@ -6,6 +6,7 @@ use app\common\logic\MessageNotificationsLogic; use app\common\validate\MessageNotificationsValidate; use app\common\traits\crud\CrudServiceTrait; use app\common\model\MessageNotifications; +use app\common\model\MessageUserStatus; use app\common\model\User; use support\Db; use support\Request; @@ -106,6 +107,111 @@ class MessageNotificationsService 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(); } - - /** - * 批量推送消息 - */ - 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; - } } \ No newline at end of file diff --git a/app/common/service/MessageUserStatusService.php b/app/common/service/MessageUserStatusService.php index 27ba060..2162f86 100644 --- a/app/common/service/MessageUserStatusService.php +++ b/app/common/service/MessageUserStatusService.php @@ -2,6 +2,7 @@ namespace app\common\service; +use app\common\model\MessageNotifications; use app\common\validate\MessageUserStatusValidate; use app\common\traits\crud\CrudServiceTrait; use app\common\model\MessageUserStatus; @@ -41,21 +42,18 @@ class MessageUserStatusService switch ($scene) { case 'unread': $config['order']['id'] = 'desc'; - // $config['with']['user'] = ['sn', 'name']; - $config['with']['message'] = ['sender_id', 'sender_type', 'msg_no', 'title', 'content', 'created_at']; + $config['with']['message'] = ['sender_id', 'sender_type', 'msg_no', 'title', 'content', 'scene', 'created_at']; $config['where']['uid'] = $request->user->uid; $config['where']['status'] = 0; break; case 'index': $config['order']['id'] = 'desc'; - // $config['with']['user'] = ['sn', 'name']; - $config['with']['message'] = ['sender_id', 'sender_type', 'msg_no', 'title', 'content', 'created_at']; + $config['with']['message'] = ['sender_id', 'sender_type', 'msg_no', 'title', 'content', 'scene', 'created_at']; $config['where']['uid'] = $request->user->uid; break; case 'show': $config['where']['id'] = $p['id']; - // $config['with']['user'] = ['sn', 'name']; - $config['with']['message'] = ['sender_id', 'sender_type', 'msg_no', 'title', 'content', 'created_at']; + $config['with']['message'] = ['sender_id', 'sender_type', 'msg_no', 'title', 'content', 'scene', 'created_at']; $config['where']['uid'] = $request->user->uid; break; case 'store': @@ -104,6 +102,7 @@ class MessageUserStatusService $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); @@ -114,7 +113,24 @@ class MessageUserStatusService { if (isset($res->message)) { $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; } + + 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; + } } \ No newline at end of file diff --git a/app/common/utils/WsEvents.php b/app/common/utils/WsEvents.php index 50f82bb..2a65f3e 100644 --- a/app/common/utils/WsEvents.php +++ b/app/common/utils/WsEvents.php @@ -74,16 +74,16 @@ class WsEvents } // 连接是否满足条件 - if ($payload['server']['HTTP_ORIGIN'] != 'https://wstool.js.org') { - flg('[x] [TOKEN认证] HTTP_ORIGIN不满足条件', false); - Gateway::sendToClient($client_id, WsResponse::error( - 'system.auth', - 1001, - '[x] [TOKEN认证] HTTP_ORIGIN不满足条件' - )); - Gateway::closeClient($client_id); - return; - } + // if ($payload['server']['HTTP_ORIGIN'] != 'https://wstool.js.org') { + // flg('[x] [TOKEN认证] HTTP_ORIGIN不满足条件', false); + // Gateway::sendToClient($client_id, WsResponse::error( + // 'system.auth', + // 1001, + // '[x] [TOKEN认证] HTTP_ORIGIN不满足条件' + // )); + // Gateway::closeClient($client_id); + // return; + // } // 签名检查 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', [ - '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); - } + Gateway::sendToUid($userId, WsResponse::success('notify.receive', $wsData)); } // ================= 辅助方法 ================= diff --git a/app/controller/IndexController.php b/app/controller/IndexController.php index c8427cd..8e15d2a 100644 --- a/app/controller/IndexController.php +++ b/app/controller/IndexController.php @@ -116,21 +116,27 @@ class IndexController // 'recipient' => [1], // ]; + $data['recipient'] = [1, 74]; // // 同步推送 // (new MessageNotificationsService())->pushNotice($data); - // // 异步推送 - // \Webman\RedisQueue\Redis::send('notice', $data); + + + // 异步推送 + \Webman\RedisQueue\Redis::send('notice', $data); + + + return 'success'; // return json($data); - $phone = ''; - $email = ''; + // $phone = ''; + // $email = ''; - $userByPhone = User::where('corp_id', 2)->where('phone', $phone)->where('phone', '!=', '')->first(); - $userByEmail = User::where('corp_id', 2)->where('email', $email)->where('email', '!=', '')->first(); - $user = $userByPhone ?: $userByEmail; + // $userByPhone = User::where('corp_id', 2)->where('phone', $phone)->where('phone', '!=', '')->first(); + // $userByEmail = User::where('corp_id', 2)->where('email', $email)->where('email', '!=', '')->first(); + // $user = $userByPhone ?: $userByEmail; - return json($user); + // return json($user); } } \ No newline at end of file