feat(internal/timermanger): timermanger
This commit is contained in:
@@ -18,6 +18,12 @@ func GetBasicInfo() string {
|
||||
now := time.Now()
|
||||
lastSignalTime := now.Add(-time.Duration(config.DisconnectDuration) * time.Hour).Format("2006-01-02 15:04:05")
|
||||
|
||||
return fmt.Sprintf("%s(QQ号:%s)已于%s至%s(UTC+8)之间去世,享年%d岁,死因可能为%s(此死因为推测最可能的死因,请勿完全相信,请等待后续消息)",
|
||||
config.Basic.Name, config.Basic.QQNumber, lastSignalTime, time.Now().Format("2006-01-02 15:04:05"), config.Basic.Age, config.Basic.CauseStop)
|
||||
if config.Debug {
|
||||
return fmt.Sprintf("此消息为测试消息\n\n%s(QQ号:%s)已于%s至%s(UTC+8)之间去世,享年%d岁,死因可能为%s(此死因为推测最可能的死因,请勿完全相信,请等待后续消息)",
|
||||
config.Basic.Name, config.Basic.QQNumber, lastSignalTime, time.Now().Format("2006-01-02 15:04:05"), config.Basic.Age, config.Basic.CauseStop)
|
||||
} else {
|
||||
return fmt.Sprintf("%s(QQ号:%s,网名:%s)已于%s至%s(UTC+8)之间去世,享年%d岁,死因可能为%s(此死因为推测最可能的死因,请勿完全相信,请等待后续消息)",
|
||||
config.Basic.Name, config.Basic.NickName, config.Basic.QQNumber, lastSignalTime, time.Now().Format("2006-01-02 15:04:05"), config.Basic.Age, config.Basic.CauseStop)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -12,6 +12,22 @@ import (
|
||||
"github.com/ssdomei232/goodBaby/configs"
|
||||
)
|
||||
|
||||
func EnsureTmpDirectory() error {
|
||||
_, err := os.Stat("tmp")
|
||||
if os.IsNotExist(err) {
|
||||
err = os.Mkdir("tmp", 0755)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
log.Println("已创建tmp目录")
|
||||
} else if err != nil {
|
||||
return err
|
||||
} else {
|
||||
log.Println("tmp目录已存在")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func isCookiesExpired(cookiesString string) bool {
|
||||
// 解析cookie中的过期时间
|
||||
lines := strings.SplitSeq(cookiesString, "\n")
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
// internal/timer_manager.go
|
||||
package internal
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type TimerManager struct {
|
||||
duration time.Duration
|
||||
resetTime time.Time
|
||||
mutex sync.RWMutex
|
||||
}
|
||||
|
||||
var GlobalTimerManager *TimerManager
|
||||
|
||||
func InitTimerManager(duration time.Duration) {
|
||||
GlobalTimerManager = &TimerManager{
|
||||
duration: duration,
|
||||
resetTime: time.Now(),
|
||||
}
|
||||
}
|
||||
|
||||
func (tm *TimerManager) Reset() {
|
||||
tm.mutex.Lock()
|
||||
defer tm.mutex.Unlock()
|
||||
tm.resetTime = time.Now()
|
||||
}
|
||||
|
||||
func (tm *TimerManager) GetRemainingTime() time.Duration {
|
||||
tm.mutex.RLock()
|
||||
defer tm.mutex.RUnlock()
|
||||
|
||||
elapsed := time.Since(tm.resetTime)
|
||||
remaining := tm.duration - elapsed
|
||||
if remaining < 0 {
|
||||
return 0
|
||||
}
|
||||
return remaining
|
||||
}
|
||||
|
||||
func HandleTimerStatus(c *gin.Context) {
|
||||
remainingTime := GlobalTimerManager.GetRemainingTime()
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"code": 200,
|
||||
"remaining_time": remainingTime.Seconds(), // 返回剩余秒数
|
||||
"message": "ok",
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user