guoziyun il y a 9 mois
Parent
commit
920a4cae46
1 fichiers modifiés avec 22 ajouts et 19 suppressions
  1. 22 19
      oms/dist/src/scripts/send-fcm-script.js

+ 22 - 19
oms/dist/src/scripts/send-fcm-script.js

@@ -42,7 +42,8 @@ const mongoose_1 = __importDefault(require("mongoose"));
 const userController_1 = __importDefault(require("../controllers/userController")); // 确保路径正确
 // --- 环境配置 ---
 const OMS_MONGO_URI = process.env.MONGO_URI || "mongodb://oms:oms123.@localhost:27717/omsdb?authSource=admin";
-const TARGET_UID = "my-samsumg";
+// 定义一个目标用户UID的数组,您可以随时添加或移除用户。
+const TARGET_UIDS = ["my-samsumg", "chenxinmiao"];
 const SEND_INTERVAL_MS = 10 * 60 * 1000; // 10 分钟
 // --- 消息内容 ---
 const MESSAGE_CONTENT = {
@@ -51,7 +52,7 @@ const MESSAGE_CONTENT = {
     bigger: true,
 };
 // 消息序号,从 0 开始
-let messageCounter = 7;
+let messageCounter = 0;
 let intervalId = null;
 /**
  * 模拟一个简化的 Express Response 对象,用于传递给控制器方法。
@@ -73,26 +74,28 @@ class MockResponse {
 }
 /**
  * 异步函数,直接调用 userController.sendDirectMessage 方法。
- * 每次调用都会递增消息序号,并构建相应的请求体。
+ * 每次调用都会递增消息序号,并为每个目标用户构建相应的请求体。
  */
 async function sendFCMMessage() {
     messageCounter++;
     const dynamicTitle = `${messageCounter}. ❤️ Time to Relax!`;
-    // 模拟一个 Express Request 对象
-    const mockRequest = {
-        body: {
-            uid: TARGET_UID,
-            title: dynamicTitle,
-            ...MESSAGE_CONTENT,
-        },
-    }; // 类型断言为 Request
-    const mockResponse = new MockResponse(); // 类型断言为 Response
-    console.log(`\n[${new Date().toLocaleString()}] 正在调用后端方法发送第 ${messageCounter} 条消息给 ${TARGET_UID}...`);
-    try {
-        await userController_1.default.sendDirectMessage(mockRequest, mockResponse);
-    }
-    catch (error) {
-        console.error(`[${new Date().toLocaleString()}] 调用 sendDirectMessage 方法失败:`, error);
+    for (const uid of TARGET_UIDS) {
+        // 模拟一个 Express Request 对象
+        const mockRequest = {
+            body: {
+                uid: uid,
+                title: dynamicTitle,
+                ...MESSAGE_CONTENT,
+            },
+        }; // 类型断言为 Request
+        const mockResponse = new MockResponse(); // 类型断言为 Response
+        console.log(`\n[${new Date().toLocaleString()}] 正在调用后端方法发送第 ${messageCounter} 条消息给用户:${uid}...`);
+        try {
+            await userController_1.default.sendDirectMessage(mockRequest, mockResponse);
+        }
+        catch (error) {
+            console.error(`[${new Date().toLocaleString()}] 调用 sendDirectMessage 方法失败,用户:${uid},错误:`, error);
+        }
     }
 }
 /**
@@ -107,7 +110,7 @@ async function startScript() {
         await sendFCMMessage();
         // 设置定时器,每 10 分钟运行一次
         intervalId = setInterval(sendFCMMessage, SEND_INTERVAL_MS);
-        console.log(`\n脚本已启动。将每隔 10 分钟向用户 ${TARGET_UID} 发送一次 FCM 消息。`);
+        console.log(`\n脚本已启动。将每隔 10 分钟向 ${TARGET_UIDS.length} 位用户发送一次 FCM 消息。`);
         console.log(`按下 Ctrl + C 停止脚本。`);
     }
     catch (error) {