From d7c8e7c9cb9ccafae897b4c8e6d0b0111c20d760 Mon Sep 17 00:00:00 2001 From: Tenko Date: Thu, 22 Aug 2024 15:13:36 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=98=8E=E7=A1=AE=E7=9A=84=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E6=8F=90=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/web/base.go | 3 +++ src/core/web/box.go | 3 +++ src/core/web/box_detail.go | 4 ++++ src/core/web/calendar.go | 5 +++++ src/core/web/card.go | 10 +++++++++- src/core/web/depot.go | 3 +++ src/core/web/missing.go | 3 +++ src/core/web/state.go | 4 ++++ src/plugins/enemy/enemy_handle.go | 6 +++--- src/plugins/material/material_handle.go | 13 ++++--------- src/plugins/operator/operator_handle.go | 6 +++--- src/plugins/player/base_handle.go | 6 +++--- src/plugins/player/box_detail_handle.go | 6 +++--- src/plugins/player/box_handle.go | 6 +++--- src/plugins/player/card_handle.go | 6 +++--- src/plugins/player/depot_handle.go | 6 +++--- src/plugins/player/gacha_handle.go | 6 +++--- src/plugins/player/missing_handle.go | 6 +++--- src/plugins/player/state_handle.go | 6 +++--- src/plugins/skland/auth.go | 2 +- src/plugins/skland/skland_request.go | 4 ++-- src/plugins/system/calendar_handle.go | 16 +++++----------- src/plugins/system/headhunt_handle.go | 6 +++--- src/plugins/system/help_handle.go | 6 +++--- src/plugins/system/recruit_handle.go | 7 ++++--- src/utils/base_utils.go | 17 ++++++++++++----- 26 files changed, 98 insertions(+), 68 deletions(-) diff --git a/src/core/web/base.go b/src/core/web/base.go index a8deb63..06a7faa 100644 --- a/src/core/web/base.go +++ b/src/core/web/base.go @@ -144,6 +144,8 @@ func init() { func Base(r *gin.Engine) { r.GET("/base", func(c *gin.Context) { + utils.WebC = make(chan error, 10) + defer close(utils.WebC) r.LoadHTMLFiles("./template/Base.tmpl") var playerBase PlayerBase var userAccount account.UserAccount @@ -158,6 +160,7 @@ func Base(r *gin.Engine) { playerData, skAccount, err := skland.GetPlayerInfo(uid, skAccount) if err != nil { log.Println(err) + utils.WebC <- err return } diff --git a/src/core/web/box.go b/src/core/web/box.go index 16c36e3..0ffc1dd 100644 --- a/src/core/web/box.go +++ b/src/core/web/box.go @@ -32,6 +32,8 @@ type Char struct { func Box(r *gin.Engine) { r.GET("/box", func(c *gin.Context) { + utils.WebC = make(chan error, 10) + defer close(utils.WebC) r.LoadHTMLFiles("./template/Box.tmpl") var box BoxInfo var userAccount account.UserAccount @@ -47,6 +49,7 @@ func Box(r *gin.Engine) { playerData, _, err := skland.GetPlayerInfo(uid, skAccount) if err != nil { log.Println(err) + utils.WebC <- err return } diff --git a/src/core/web/box_detail.go b/src/core/web/box_detail.go index 45ef446..6270528 100644 --- a/src/core/web/box_detail.go +++ b/src/core/web/box_detail.go @@ -34,6 +34,8 @@ type Equip struct { func BoxDetail(r *gin.Engine) { r.GET("/boxDetail", func(c *gin.Context) { + utils.WebC = make(chan error, 10) + defer close(utils.WebC) r.LoadHTMLFiles("./template/BoxDetail.tmpl") var detailList []Detail var userAccount account.UserAccount @@ -49,11 +51,13 @@ func BoxDetail(r *gin.Engine) { playerCultivate, err := skland.GetPlayerCultivate(uid, skAccount) if err != nil { log.Println(err) + utils.WebC <- err return } playerData, _, err := skland.GetPlayerInfo(uid, skAccount) if err != nil { log.Println(err) + utils.WebC <- err return } diff --git a/src/core/web/calendar.go b/src/core/web/calendar.go index 1e35094..7d41abd 100644 --- a/src/core/web/calendar.go +++ b/src/core/web/calendar.go @@ -1,6 +1,7 @@ package web import ( + "arknights_bot/utils" "fmt" "github.com/PuerkitoBio/goquery" "github.com/gin-gonic/gin" @@ -23,16 +24,20 @@ type CalendarInfo struct { func Calendar(r *gin.Engine) { r.GET("/calendar", func(c *gin.Context) { + utils.WebC = make(chan error, 10) + defer close(utils.WebC) r.LoadHTMLFiles("./template/Calendar.tmpl") var calendarMap = make(map[string]template.HTML) resp, err := http.Get(viper.GetString("api.calendar")) if err != nil { log.Println(err) + utils.WebC <- err return } doc, err := goquery.NewDocumentFromReader(resp.Body) if err != nil { log.Println(err) + utils.WebC <- err return } text := doc.Text() diff --git a/src/core/web/card.go b/src/core/web/card.go index e71a07b..e895dea 100644 --- a/src/core/web/card.go +++ b/src/core/web/card.go @@ -74,24 +74,32 @@ func init() { func Card(r *gin.Engine) { r.GET("/card", func(c *gin.Context) { + utils.WebC = make(chan error, 10) + defer close(utils.WebC) r.LoadHTMLFiles("./template/Card.tmpl") userId, _ := strconv.ParseInt(c.Query("userId"), 10, 64) uid := c.Query("uid") sklandId := c.Query("sklandId") playerCard, err := cardData(userId, sklandId, uid) if err != nil { + log.Println(err) + utils.WebC <- err return } c.HTML(http.StatusOK, "Card.tmpl", playerCard) }) r.GET("/oldCard", func(c *gin.Context) { + utils.WebC = make(chan error, 10) + defer close(utils.WebC) r.LoadHTMLFiles("./template/OldCard.tmpl") userId, _ := strconv.ParseInt(c.Query("userId"), 10, 64) uid := c.Query("uid") sklandId := c.Query("sklandId") playerCard, err := cardData(userId, sklandId, uid) if err != nil { + log.Println(err) + utils.WebC <- err return } c.HTML(http.StatusOK, "OldCard.tmpl", playerCard) @@ -113,7 +121,7 @@ func cardData(userId int64, sklandId, uid string) (PlayerCard, error) { playerData, skAccount, err := skland.GetPlayerInfo(uid, skAccount) if err != nil { log.Println(err) - return playerCard, fmt.Errorf("获取名片信息失败") + return playerCard, err } charMap := playerData.CharInfoMap diff --git a/src/core/web/depot.go b/src/core/web/depot.go index d8ec006..25c74c3 100644 --- a/src/core/web/depot.go +++ b/src/core/web/depot.go @@ -43,6 +43,8 @@ func init() { func Depot(r *gin.Engine) { r.GET("/depot", func(c *gin.Context) { + utils.WebC = make(chan error, 10) + defer close(utils.WebC) r.LoadHTMLFiles("./template/Depot.tmpl") var depotItems []DepotItem var userAccount account.UserAccount @@ -57,6 +59,7 @@ func Depot(r *gin.Engine) { playerCultivate, err := skland.GetPlayerCultivate(uid, skAccount) if err != nil { log.Println(err) + utils.WebC <- err return } for _, item := range playerCultivate.Items { diff --git a/src/core/web/missing.go b/src/core/web/missing.go index ceba28a..6256109 100644 --- a/src/core/web/missing.go +++ b/src/core/web/missing.go @@ -25,6 +25,8 @@ type MissingChar struct { func Missing(r *gin.Engine) { r.GET("/missing", func(c *gin.Context) { + utils.WebC = make(chan error, 10) + defer close(utils.WebC) r.LoadHTMLFiles("./template/Missing.tmpl") var missingInfo MissingInfo param := c.Query("param") @@ -40,6 +42,7 @@ func Missing(r *gin.Engine) { playerData, _, err := skland.GetPlayerInfo(uid, skAccount) if err != nil { log.Println(err) + utils.WebC <- err return } diff --git a/src/core/web/state.go b/src/core/web/state.go index 4284d51..388dea2 100644 --- a/src/core/web/state.go +++ b/src/core/web/state.go @@ -12,6 +12,8 @@ import ( func State(r *gin.Engine) { r.GET("/state", func(c *gin.Context) { + utils.WebC = make(chan error, 10) + defer close(utils.WebC) r.LoadHTMLFiles("./template/State.tmpl") var userAccount account.UserAccount var skAccount skland.Account @@ -25,11 +27,13 @@ func State(r *gin.Engine) { playerData, skAccount, err := skland.GetPlayerInfo(uid, skAccount) if err != nil { log.Println(err) + utils.WebC <- err return } playStatistic, _, err := skland.GetPlayerStatistic(uid, skAccount) if err != nil { log.Println(err) + utils.WebC <- err return } diff --git a/src/plugins/enemy/enemy_handle.go b/src/plugins/enemy/enemy_handle.go index b60d00b..f994026 100644 --- a/src/plugins/enemy/enemy_handle.go +++ b/src/plugins/enemy/enemy_handle.go @@ -80,9 +80,9 @@ func EnemyHandle(update tgbotapi.Update) error { } port := viper.GetString("http.port") - pic := utils.Screenshot(fmt.Sprintf("http://localhost:%s/enemy?name=%s", port, name), 0, 1.5) - if pic == nil { - sendMessage := tgbotapi.NewMessage(chatId, "生成图片失败,请重试。") + pic, err := utils.Screenshot(fmt.Sprintf("http://localhost:%s/enemy?name=%s", port, name), 0, 1.5) + if err != nil { + sendMessage := tgbotapi.NewMessage(chatId, err.Error()) sendMessage.ReplyToMessageID = messageId bot.Arknights.Send(sendMessage) return nil diff --git a/src/plugins/material/material_handle.go b/src/plugins/material/material_handle.go index 96a92fd..975950a 100644 --- a/src/plugins/material/material_handle.go +++ b/src/plugins/material/material_handle.go @@ -7,7 +7,6 @@ import ( "fmt" tgbotapi "github.com/ijnkawakaze/telegram-bot-api" "github.com/spf13/viper" - "log" ) // MaterialHandle 材料查询 @@ -52,19 +51,15 @@ func MaterialHandle(update tgbotapi.Update) error { bot.Arknights.Send(sendAction) port := viper.GetString("http.port") - pic := utils.Screenshot(fmt.Sprintf("http://localhost:%s/material?name=%s", port, name), 0, 1.5) - if pic == nil { - sendMessage := tgbotapi.NewMessage(chatId, "生成图片失败,请重试。") + pic, err := utils.Screenshot(fmt.Sprintf("http://localhost:%s/material?name=%s", port, name), 0, 1.5) + if err != nil { + sendMessage := tgbotapi.NewMessage(chatId, err.Error()) sendMessage.ReplyToMessageID = messageId bot.Arknights.Send(sendMessage) return nil } sendPhoto := tgbotapi.NewPhoto(chatId, tgbotapi.FileBytes{Bytes: pic}) sendPhoto.ReplyToMessageID = messageId - _, err := bot.Arknights.Send(sendPhoto) - if err != nil { - log.Println(err) - return err - } + bot.Arknights.Send(sendPhoto) return nil } diff --git a/src/plugins/operator/operator_handle.go b/src/plugins/operator/operator_handle.go index 2b8640c..21630dc 100644 --- a/src/plugins/operator/operator_handle.go +++ b/src/plugins/operator/operator_handle.go @@ -76,9 +76,9 @@ func OperatorHandle(update tgbotapi.Update) error { } port := viper.GetString("http.port") - pic := utils.Screenshot(fmt.Sprintf("http://localhost:%s/operator?name=%s", port, name), 0, 1.5) - if pic == nil { - sendMessage := tgbotapi.NewMessage(chatId, "生成图片失败,请重试。") + pic, err := utils.Screenshot(fmt.Sprintf("http://localhost:%s/operator?name=%s", port, name), 0, 1.5) + if err != nil { + sendMessage := tgbotapi.NewMessage(chatId, err.Error()) sendMessage.ReplyToMessageID = messageId bot.Arknights.Send(sendMessage) return nil diff --git a/src/plugins/player/base_handle.go b/src/plugins/player/base_handle.go index 128aaeb..38d06f3 100644 --- a/src/plugins/player/base_handle.go +++ b/src/plugins/player/base_handle.go @@ -21,9 +21,9 @@ func (_ PlayerOperationBase) Run(uid string, userAccount account.UserAccount, ch bot.Arknights.Send(sendAction) port := viper.GetString("http.port") - pic := utils.Screenshot(fmt.Sprintf("http://localhost:%s/base?userId=%d&uid=%s&sklandId=%s", port, userAccount.UserNumber, uid, userAccount.SklandId), 0, 1.5) - if pic == nil { - sendMessage := tgbotapi.NewMessage(chatId, fmt.Sprintf("生成图片失败,token可能已失效请[重设token](https://t.me/%s)。", viper.GetString("bot.name"))) + pic, err := utils.Screenshot(fmt.Sprintf("http://localhost:%s/base?userId=%d&uid=%s&sklandId=%s", port, userAccount.UserNumber, uid, userAccount.SklandId), 0, 1.5) + if err != nil { + sendMessage := tgbotapi.NewMessage(chatId, err.Error()) sendMessage.ParseMode = tgbotapi.ModeMarkdownV2 sendMessage.ReplyToMessageID = messageId bot.Arknights.Send(sendMessage) diff --git a/src/plugins/player/box_detail_handle.go b/src/plugins/player/box_detail_handle.go index 0035dec..1b4967b 100644 --- a/src/plugins/player/box_detail_handle.go +++ b/src/plugins/player/box_detail_handle.go @@ -32,9 +32,9 @@ func (_ PlayerOperationBoxDetail) Run(uid string, userAccount account.UserAccoun } port := viper.GetString("http.port") - pic := utils.Screenshot(fmt.Sprintf("http://localhost:%s/boxDetail?userId=%d&uid=%s¶m=%s&sklandId=%s", port, userAccount.UserNumber, uid, param, userAccount.SklandId), 0, 1.5) - if pic == nil { - sendMessage := tgbotapi.NewMessage(chatId, fmt.Sprintf("生成图片失败,token可能已失效请[重设token](https://t.me/%s)。", viper.GetString("bot.name"))) + pic, err := utils.Screenshot(fmt.Sprintf("http://localhost:%s/boxDetail?userId=%d&uid=%s¶m=%s&sklandId=%s", port, userAccount.UserNumber, uid, param, userAccount.SklandId), 0, 1.5) + if err != nil { + sendMessage := tgbotapi.NewMessage(chatId, err.Error()) sendMessage.ParseMode = tgbotapi.ModeMarkdownV2 sendMessage.ReplyToMessageID = messageId bot.Arknights.Send(sendMessage) diff --git a/src/plugins/player/box_handle.go b/src/plugins/player/box_handle.go index b0f2de7..1fdb9e3 100644 --- a/src/plugins/player/box_handle.go +++ b/src/plugins/player/box_handle.go @@ -32,9 +32,9 @@ func (_ PlayerOperationBox) Run(uid string, userAccount account.UserAccount, cha } port := viper.GetString("http.port") - pic := utils.Screenshot(fmt.Sprintf("http://localhost:%s/box?userId=%d&uid=%s¶m=%s&sklandId=%s", port, userAccount.UserNumber, uid, param, userAccount.SklandId), 0, 1.5) - if pic == nil { - sendMessage := tgbotapi.NewMessage(chatId, fmt.Sprintf("生成图片失败,token可能已失效请[重设token](https://t.me/%s)。", viper.GetString("bot.name"))) + pic, err := utils.Screenshot(fmt.Sprintf("http://localhost:%s/box?userId=%d&uid=%s¶m=%s&sklandId=%s", port, userAccount.UserNumber, uid, param, userAccount.SklandId), 0, 1.5) + if err != nil { + sendMessage := tgbotapi.NewMessage(chatId, err.Error()) sendMessage.ParseMode = tgbotapi.ModeMarkdownV2 sendMessage.ReplyToMessageID = messageId bot.Arknights.Send(sendMessage) diff --git a/src/plugins/player/card_handle.go b/src/plugins/player/card_handle.go index 7625a9d..ba1ef9a 100644 --- a/src/plugins/player/card_handle.go +++ b/src/plugins/player/card_handle.go @@ -26,9 +26,9 @@ func (_ PlayerOperationCard) Run(uid string, userAccount account.UserAccount, ch r = "oldCard" } port := viper.GetString("http.port") - pic := utils.Screenshot(fmt.Sprintf("http://localhost:%s/%s?userId=%d&uid=%s&sklandId=%s", port, r, userAccount.UserNumber, uid, userAccount.SklandId), 0, 1) - if pic == nil { - sendMessage := tgbotapi.NewMessage(chatId, fmt.Sprintf("生成图片失败,token可能已失效请[重设token](https://t.me/%s)。", viper.GetString("bot.name"))) + pic, err := utils.Screenshot(fmt.Sprintf("http://localhost:%s/%s?userId=%d&uid=%s&sklandId=%s", port, r, userAccount.UserNumber, uid, userAccount.SklandId), 0, 1) + if err != nil { + sendMessage := tgbotapi.NewMessage(chatId, err.Error()) sendMessage.ParseMode = tgbotapi.ModeMarkdownV2 sendMessage.ReplyToMessageID = messageId bot.Arknights.Send(sendMessage) diff --git a/src/plugins/player/depot_handle.go b/src/plugins/player/depot_handle.go index 00b6979..3199e9d 100644 --- a/src/plugins/player/depot_handle.go +++ b/src/plugins/player/depot_handle.go @@ -22,9 +22,9 @@ func (_ PlayerOperationDepot) Run(uid string, userAccount account.UserAccount, c bot.Arknights.Send(sendAction) port := viper.GetString("http.port") - pic := utils.Screenshot(fmt.Sprintf("http://localhost:%s/depot?userId=%d&uid=%s&sklandId=%s", port, userAccount.UserNumber, uid, userAccount.SklandId), 0, 1.5) - if pic == nil { - sendMessage := tgbotapi.NewMessage(chatId, fmt.Sprintf("生成图片失败,token可能已失效请[重设token](https://t.me/%s)。", viper.GetString("bot.name"))) + pic, err := utils.Screenshot(fmt.Sprintf("http://localhost:%s/depot?userId=%d&uid=%s&sklandId=%s", port, userAccount.UserNumber, uid, userAccount.SklandId), 0, 1.5) + if err != nil { + sendMessage := tgbotapi.NewMessage(chatId, err.Error()) sendMessage.ParseMode = tgbotapi.ModeMarkdownV2 sendMessage.ReplyToMessageID = messageId bot.Arknights.Send(sendMessage) diff --git a/src/plugins/player/gacha_handle.go b/src/plugins/player/gacha_handle.go index edf3195..b9d67f1 100644 --- a/src/plugins/player/gacha_handle.go +++ b/src/plugins/player/gacha_handle.go @@ -107,9 +107,9 @@ func (_ PlayerOperationGacha) Run(uid string, userAccount account.UserAccount, c bot.Arknights.Send(sendAction) port := viper.GetString("http.port") - pic := utils.Screenshot(fmt.Sprintf("http://localhost:%s/gacha?userId=%d&uid=%s", port, userAccount.UserNumber, uid), 3000, 1.5) - if pic == nil { - sendMessage := tgbotapi.NewMessage(chatId, "生成图片失败,请重试。") + pic, e := utils.Screenshot(fmt.Sprintf("http://localhost:%s/gacha?userId=%d&uid=%s", port, userAccount.UserNumber, uid), 3000, 1.5) + if e != nil { + sendMessage := tgbotapi.NewMessage(chatId, e.Error()) sendMessage.ReplyToMessageID = messageId bot.Arknights.Send(sendMessage) return nil diff --git a/src/plugins/player/missing_handle.go b/src/plugins/player/missing_handle.go index c10a5a7..97d7852 100644 --- a/src/plugins/player/missing_handle.go +++ b/src/plugins/player/missing_handle.go @@ -31,9 +31,9 @@ func (_ PlayerOperationMissing) Run(uid string, userAccount account.UserAccount, } port := viper.GetString("http.port") - pic := utils.Screenshot(fmt.Sprintf("http://localhost:%s/missing?userId=%d&uid=%s¶m=%s&sklandId=%s", port, userAccount.UserNumber, uid, param, userAccount.SklandId), 0, 1.5) - if pic == nil { - sendMessage := tgbotapi.NewMessage(chatId, fmt.Sprintf("生成图片失败,token可能已失效请[重设token](https://t.me/%s)。", viper.GetString("bot.name"))) + pic, err := utils.Screenshot(fmt.Sprintf("http://localhost:%s/missing?userId=%d&uid=%s¶m=%s&sklandId=%s", port, userAccount.UserNumber, uid, param, userAccount.SklandId), 0, 1.5) + if err != nil { + sendMessage := tgbotapi.NewMessage(chatId, err.Error()) sendMessage.ParseMode = tgbotapi.ModeMarkdownV2 sendMessage.ReplyToMessageID = messageId bot.Arknights.Send(sendMessage) diff --git a/src/plugins/player/state_handle.go b/src/plugins/player/state_handle.go index 8886683..c964d83 100644 --- a/src/plugins/player/state_handle.go +++ b/src/plugins/player/state_handle.go @@ -20,9 +20,9 @@ func (_ PlayerOperationState) Run(uid string, userAccount account.UserAccount, c bot.Arknights.Send(sendAction) port := viper.GetString("http.port") - pic := utils.Screenshot(fmt.Sprintf("http://localhost:%s/state?userId=%d&uid=%s&sklandId=%s", port, userAccount.UserNumber, uid, userAccount.SklandId), 0, 1) - if pic == nil { - sendMessage := tgbotapi.NewMessage(chatId, fmt.Sprintf("生成图片失败,token可能已失效请[重设token](https://t.me/%s)。", viper.GetString("bot.name"))) + pic, err := utils.Screenshot(fmt.Sprintf("http://localhost:%s/state?userId=%d&uid=%s&sklandId=%s", port, userAccount.UserNumber, uid, userAccount.SklandId), 0, 1) + if err != nil { + sendMessage := tgbotapi.NewMessage(chatId, err.Error()) sendMessage.ParseMode = tgbotapi.ModeMarkdownV2 sendMessage.ReplyToMessageID = messageId bot.Arknights.Send(sendMessage) diff --git a/src/plugins/skland/auth.go b/src/plugins/skland/auth.go index 30d05b3..8de8f74 100644 --- a/src/plugins/skland/auth.go +++ b/src/plugins/skland/auth.go @@ -117,7 +117,7 @@ func CheckUser(cred string) (bool, error) { return false, err } if res.StatusCode() == 401 { - return false, fmt.Errorf("[skland] %s", "用户未登录!") + return false, fmt.Errorf("无效的cred,请重新登录获取。") } return true, nil } diff --git a/src/plugins/skland/skland_request.go b/src/plugins/skland/skland_request.go index 4afa12d..37a531f 100644 --- a/src/plugins/skland/skland_request.go +++ b/src/plugins/skland/skland_request.go @@ -42,7 +42,7 @@ func SklandRequest[T any](r *resty.Request, method, path string, vs ...any) (t T resp, respErr := r.SetError(&SKBaseResp[any]{}).SetResult(&SKBaseResp[T]{}).Execute(method, sklandAddr+path) if resp.StatusCode() == 405 { log.Println(string(resp.Body())) - return t, fmt.Errorf("[skland] %s", "服务器被墙了!") + return t, fmt.Errorf("服务器被墙了!") } res, err := resty.ParseResp[*SKBaseResp[any], *SKBaseResp[T]]( resp, respErr, @@ -67,7 +67,7 @@ func SklandRequestPlayerData(r *resty.Request, method, path string, vs ...any) ( } if res.StatusCode() == 405 { log.Println(string(res.Body())) - return d, fmt.Errorf("[skland] %s", "服务器被墙了!") + return d, fmt.Errorf("服务器被墙了!") } return string(res.Body()), nil } diff --git a/src/plugins/system/calendar_handle.go b/src/plugins/system/calendar_handle.go index 0d213f2..cac4399 100644 --- a/src/plugins/system/calendar_handle.go +++ b/src/plugins/system/calendar_handle.go @@ -17,22 +17,16 @@ func CalendarHandle(update tgbotapi.Update) error { sendAction := tgbotapi.NewChatAction(chatId, "upload_photo") bot.Arknights.Send(sendAction) port := viper.GetString("http.port") - pic := utils.Screenshot(fmt.Sprintf("http://localhost:%s/calendar", port), 0, 1.5) - if pic == nil { - sendMessage := tgbotapi.NewMessage(chatId, "生成图片失败,请重试。") + pic, err := utils.Screenshot(fmt.Sprintf("http://localhost:%s/calendar", port), 0, 1.5) + if err != nil { + sendMessage := tgbotapi.NewMessage(chatId, err.Error()) sendMessage.ReplyToMessageID = messageId - msg, err := bot.Arknights.Send(sendMessage) - if err != nil { - return err - } + msg, _ := bot.Arknights.Send(sendMessage) messagecleaner.AddDelQueue(chatId, msg.MessageID, 5) return nil } sendDocument := tgbotapi.NewDocument(chatId, tgbotapi.FileBytes{Bytes: pic, Name: "calendar.jpg"}) sendDocument.ReplyToMessageID = messageId - _, err := bot.Arknights.Send(sendDocument) - if err != nil { - return err - } + bot.Arknights.Send(sendDocument) return nil } diff --git a/src/plugins/system/headhunt_handle.go b/src/plugins/system/headhunt_handle.go index 9603541..7cdb8a2 100644 --- a/src/plugins/system/headhunt_handle.go +++ b/src/plugins/system/headhunt_handle.go @@ -82,9 +82,9 @@ func HeadhuntHandle(update tgbotapi.Update) error { sendAction := tgbotapi.NewChatAction(chatId, "upload_photo") bot.Arknights.Send(sendAction) port := viper.GetString("http.port") - pic := utils.Screenshot(fmt.Sprintf("http://localhost:%s/headhunt?userId=%d", port, userId), 0, 1) - if pic == nil { - sendMessage := tgbotapi.NewMessage(chatId, "生成图片失败,请重试。") + pic, err := utils.Screenshot(fmt.Sprintf("http://localhost:%s/headhunt?userId=%d", port, userId), 0, 1) + if err != nil { + sendMessage := tgbotapi.NewMessage(chatId, err.Error()) sendMessage.ReplyToMessageID = messageId msg, err := bot.Arknights.Send(sendMessage) times, _ := strconv.Atoi(utils.RedisGet(key)) diff --git a/src/plugins/system/help_handle.go b/src/plugins/system/help_handle.go index 6e01992..53c5a97 100644 --- a/src/plugins/system/help_handle.go +++ b/src/plugins/system/help_handle.go @@ -20,9 +20,9 @@ func HelpHandle(update tgbotapi.Update) error { if fileId == "" { port := viper.GetString("http.port") - pic := utils.Screenshot("http://localhost:"+port+"/help", 0, 1.5) - if pic == nil { - sendMessage := tgbotapi.NewMessage(chatId, "生成图片失败,请重试。") + pic, err := utils.Screenshot("http://localhost:"+port+"/help", 0, 1.5) + if err != nil { + sendMessage := tgbotapi.NewMessage(chatId, err.Error()) sendMessage.ReplyToMessageID = messageId bot.Arknights.Send(sendMessage) return nil diff --git a/src/plugins/system/recruit_handle.go b/src/plugins/system/recruit_handle.go index a6d7dfc..a375487 100644 --- a/src/plugins/system/recruit_handle.go +++ b/src/plugins/system/recruit_handle.go @@ -47,6 +47,7 @@ func recruit(chatId int64, messageId int, param, fileId string) error { } for _, result := range results { if bot.RecruitTagMap[result] != "" { + log.Println(bot.RecruitTagMap[result]) tags = append(tags, bot.RecruitTagMap[result]) } } @@ -63,9 +64,9 @@ func recruit(chatId int64, messageId int, param, fileId string) error { bot.Arknights.Send(sendAction) port := viper.GetString("http.port") - pic := utils.Screenshot(fmt.Sprintf("http://localhost:%s/recruit?tags=%s&client=%s", port, strings.Join(tags, " "), param), 0, 1.5) - if pic == nil { - sendMessage := tgbotapi.NewMessage(chatId, "生成图片失败,请重试。") + pic, err := utils.Screenshot(fmt.Sprintf("http://localhost:%s/recruit?tags=%s&client=%s", port, strings.Join(tags, " "), param), 0, 1.5) + if err != nil { + sendMessage := tgbotapi.NewMessage(chatId, err.Error()) sendMessage.ReplyToMessageID = messageId _, err := bot.Arknights.Send(sendMessage) if err != nil { diff --git a/src/utils/base_utils.go b/src/utils/base_utils.go index cf90f80..120ca04 100644 --- a/src/utils/base_utils.go +++ b/src/utils/base_utils.go @@ -6,6 +6,7 @@ import ( "context" "crypto/md5" "encoding/hex" + "fmt" "github.com/go-redis/redis/v8" tgbotapi "github.com/ijnkawakaze/telegram-bot-api" gonanoid "github.com/matoous/go-nanoid/v2" @@ -27,6 +28,8 @@ import ( var ctx = context.Background() +var WebC chan error + var browser playwright.Browser type GroupInvite struct { @@ -250,7 +253,7 @@ func RedisDelSetItem(key string, val string) { } // Screenshot 屏幕截图 -func Screenshot(url string, waitTime float64, scale float64) []byte { +func Screenshot(url string, waitTime float64, scale float64) ([]byte, error) { if browser == nil { pw, err := playwright.Run() if err != nil { @@ -261,7 +264,7 @@ func Screenshot(url string, waitTime float64, scale float64) []byte { browser, err = pw.Chromium.Launch() if err != nil { log.Println(err) - return nil + return nil, fmt.Errorf("playerwright启动失败") } } page, _ := browser.NewPage(playwright.BrowserNewContextOptions{DeviceScaleFactor: &scale}) @@ -273,18 +276,22 @@ func Screenshot(url string, waitTime float64, scale float64) []byte { page.Goto(url, playwright.PageGotoOptions{ WaitUntil: playwright.WaitUntilStateNetworkidle, }) + e := <-WebC + if e != nil { + return nil, e + } page.WaitForTimeout(waitTime) locator, _ := page.Locator("#main") if v, _ := locator.IsVisible(); !v { log.Println("元素未加载取消截图操作") - return nil + return nil, fmt.Errorf("元素未加载") } screenshot, err := locator.Screenshot(playwright.LocatorScreenshotOptions{Type: playwright.ScreenshotTypeJpeg}) if err != nil { - return nil + return nil, fmt.Errorf("截图失败") } log.Println("截图完成...") - return screenshot + return screenshot, nil } // ReverseSlice 反转切片