Selaa lähdekoodia

修改send-fcm-script.ts脚本,支持多个发送

guoziyun 9 kuukautta sitten
vanhempi
sitoutus
0dac3389c5
1 muutettua tiedostoa jossa 24 lisäystä ja 21 poistoa
  1. 24 21
      oms/src/scripts/send-fcm-script.ts

+ 24 - 21
oms/src/scripts/send-fcm-script.ts

@@ -7,7 +7,8 @@ import userController from "../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 分钟
 
 // --- 消息内容 ---
@@ -18,7 +19,7 @@ const MESSAGE_CONTENT = {
 };
 
 // 消息序号,从 0 开始
-let messageCounter = 7;
+let messageCounter = 0;
 let intervalId: NodeJS.Timeout | null = null;
 
 /**
@@ -42,29 +43,31 @@ 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,
-    },
-  } as Request; // 类型断言为 Request
-
-  const mockResponse = new MockResponse() as unknown as Response; // 类型断言为 Response
-
-  console.log(`\n[${new Date().toLocaleString()}] 正在调用后端方法发送第 ${messageCounter} 条消息给 ${TARGET_UID}...`);
-
-  try {
-    await userController.sendDirectMessage(mockRequest, mockResponse);
-  } catch (error: any) {
-    console.error(`[${new Date().toLocaleString()}] 调用 sendDirectMessage 方法失败:`, error);
+  for (const uid of TARGET_UIDS) {
+    // 模拟一个 Express Request 对象
+    const mockRequest = {
+      body: {
+        uid: uid,
+        title: dynamicTitle,
+        ...MESSAGE_CONTENT,
+      },
+    } as Request; // 类型断言为 Request
+
+    const mockResponse = new MockResponse() as unknown as Response; // 类型断言为 Response
+
+    console.log(`\n[${new Date().toLocaleString()}] 正在调用后端方法发送第 ${messageCounter} 条消息给用户:${uid}...`);
+
+    try {
+      await userController.sendDirectMessage(mockRequest, mockResponse);
+    } catch (error: any) {
+      console.error(`[${new Date().toLocaleString()}] 调用 sendDirectMessage 方法失败,用户:${uid},错误:`, error);
+    }
   }
 }
 
@@ -83,7 +86,7 @@ async function startScript() {
     // 设置定时器,每 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) {
     console.error("[初始化失败] 无法连接到 MongoDB 或启动脚本:", error);