Просмотр исходного кода

修改推送时间在早十到晚十

wuyi 2 лет назад
Родитель
Сommit
e7ced7eda4
1 измененных файлов с 20 добавлено и 8 удалено
  1. 20 8
      src/moments/moments.service.ts

+ 20 - 8
src/moments/moments.service.ts

@@ -164,17 +164,29 @@ export class MomentsService {
             .getMany()
             .getMany()
 
 
         momentsList.forEach(moments => {
         momentsList.forEach(moments => {
+
+            const targetTime = new Date();
+            targetTime.setHours(Math.floor(Math.random() * 12) + 10);
+            targetTime.setMinutes(Math.floor(Math.random() * 60));
+
+            const now = new Date();
+            const delayTime = targetTime.getTime() - now.getTime();
+
+
             const hours = Math.floor(Math.random() * 12)
             const hours = Math.floor(Math.random() * 12)
             const minutes = Math.floor(Math.random() * 60)
             const minutes = Math.floor(Math.random() * 60)
             console.log(moments.userId + '的动态将在' + hours + '小时' + minutes + '分后发布')
             console.log(moments.userId + '的动态将在' + hours + '小时' + minutes + '分后发布')
-            setTimeout(async () => {
-                moments.isPush = true
-                moments.createdAt = new Date()
-                await this.momentsRepository.save(moments)
-                const chatRole = await this.chatRoleRepository.findOneBy({ id: moments.userId })
-                chatRole.dynamicNumber += 1
-                await this.chatRoleRepository.save(chatRole)
-            }, hours * 60 * 60 * 1000 + minutes * 60 * 1000)
+
+            if (delayTime > 0) {
+                setTimeout(async () => {
+                    moments.isPush = true
+                    moments.createdAt = new Date()
+                    await this.momentsRepository.save(moments)
+                    const chatRole = await this.chatRoleRepository.findOneBy({ id: moments.userId })
+                    chatRole.dynamicNumber += 1
+                    await this.chatRoleRepository.save(chatRole)
+                }, delayTime)
+            }
         })
         })
     }
     }