Compare commits

...

No commits in common. "main" and "e9601931519587f43361a5e5eeabf2c6ec47b68f" have entirely different histories.

3 changed files with 62 additions and 20 deletions

View File

@ -1,21 +1,40 @@
package main package main
import ( import (
"fmt"
"log" "log"
"time" "time"
"week/moonshot" "week/moonshot"
"week/qywechat" "week/qywechat"
"week/tools"
"github.com/jasonlvhit/gocron" "github.com/jasonlvhit/gocron"
) )
var maxRetries = 3 var maxRetries = 0
var now = time.Now()
func main() { func main() {
now := time.Now() fmt.Println("-----------------------------------------------------------------------------------------")
nowDate := now.Format("2006-01-02 15:04:05") + " " + now.Weekday().String() fmt.Println("-----------------------------------------------------------------------------------------")
log.Printf("等待执行定时任务中...当前时间是:%s", nowDate) log.Println(`
if maxRetries != 0 { _____
_/ ____\______ ____ _____ _____ ____ __ _ _______ ____ ____
\ __\\_ __ \/ _ \ / \ \__ \ / _ \ \ \/ \/ /\__ \ / \ / ___\
| | | | \( <_> ) Y Y \ / __ \( <_> ) \ / / __ \| | \/ /_/ >
|__| |__| \____/|__|_| / (____ /\____/ \/\_/ (____ /___| /\___ /
\/ \/ \/ \//_____/`)
fmt.Println("-----------------------------------------------------------------------------------------")
fmt.Println("-----------------------------------------------------------------------------------------")
if maxRetries < 3 {
for {
remainingTime := tools.RemainingTimeUntilNextFriday17()
if remainingTime <= 0 {
break // 当剩余时间小于等于0时跳出循环
}
fmt.Printf("\r距离执行定时任务还剩 %d小时 %d分钟 %d秒", int(remainingTime.Hours()), int(remainingTime.Minutes())%60, int(remainingTime.Seconds())%60)
time.Sleep(time.Second) // 等待一秒钟,然后更新
}
// 定义任务,每周五的五点执行 // 定义任务,每周五的五点执行
gocron.Every(1).Friday().At("17:00").Do(job) gocron.Every(1).Friday().At("17:00").Do(job)
// 开始定时任务 // 开始定时任务
@ -27,15 +46,23 @@ func main() {
} }
func job() { func job() {
nowDate := now.Format("2006-01-02 15:04:05") + " " + now.Weekday().String()
log.Printf("开始执行任务,当前时间是:%v", nowDate)
err := moonshot.CreateExcel() err := moonshot.CreateExcel()
if err != nil { if err != nil {
maxRetries++
log.Printf("执行失败,开始第%v次重试...", maxRetries)
log.Println("----------------------------------------------------------------------")
job() job()
maxRetries--
} }
repContent, err := moonshot.AiChat() repContent, err := moonshot.AiChat()
if err != nil { if err != nil {
maxRetries++
log.Printf("执行失败,开始第%v次重试...", maxRetries)
log.Println("----------------------------------------------------------------------")
job() job()
maxRetries--
} }
qywechat.Send(repContent) qywechat.Send(repContent)

View File

@ -24,10 +24,10 @@ func Send(repContent string) {
// 发送HTTP POST请求到企业微信机器人Webhook // 发送HTTP POST请求到企业微信机器人Webhook
resp, err := http.Post(webhookURL, "application/json", bytes.NewBuffer(requestBody)) resp, err := http.Post(webhookURL, "application/json", bytes.NewBuffer(requestBody))
if err != nil { if err != nil {
log.Fatalf("Failed to send message: %v", err) log.Fatalf("发送企业微信机器人失败,错误是: %v", err)
} }
defer resp.Body.Close() defer resp.Body.Close()
log.Println("Message sent successfully!") log.Println("发送企业微信机器人成功")
} }

View File

@ -1,18 +1,33 @@
package tools package tools
import ( import (
"fmt"
"time" "time"
) )
// 当前时间是否为周五5点 // 计算时间差值
func CheckTime() bool {
nowTime := time.Now() func RemainingTimeUntilNextFriday17() time.Duration {
if nowTime.Weekday().String() == "Friday" && nowTime.Format("15:04:05") == "17:00:00" { // 获取当前时间
fmt.Println("当前时间是周五5点") now := time.Now()
return true
} else { // 计算下一个周五的时间
fmt.Println("当前时间不是周五5点") nextFriday := GetNextWeekday(now, time.Friday)
return false
// 构造下一个周五 17 点的时间
nextFriday17 := time.Date(nextFriday.Year(), nextFriday.Month(), nextFriday.Day(), 17, 0, 0, 0, nextFriday.Location())
// 计算时间差
duration := nextFriday17.Sub(now)
return duration
} }
// 获取下一个指定星期几的时间
func GetNextWeekday(t time.Time, weekday time.Weekday) time.Time {
daysUntil := (int(weekday) - int(t.Weekday()) + 7) % 7
if daysUntil == 0 {
daysUntil = 7
}
return t.AddDate(0, 0, daysUntil)
} }