mirror of
https://github.com/ctwj/urldb.git
synced 2025-11-25 03:15:04 +08:00
Compare commits
11 Commits
feat_plugi
...
0e88374905
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0e88374905 | ||
|
|
081a3a7222 | ||
|
|
6b8d2b3cf0 | ||
|
|
9333f9da94 | ||
|
|
806a724fb5 | ||
|
|
487f5c9559 | ||
|
|
18b7f89c49 | ||
|
|
db902f3742 | ||
|
|
42baa891f8 | ||
|
|
02d5d00510 | ||
|
|
d95c69142a |
@@ -39,6 +39,7 @@
|
||||
- [服务器要求](https://ecn5khs4t956.feishu.cn/wiki/W8YBww1Mmiu4Cdkp5W4c8pFNnMf?from=from_copylink)
|
||||
- [QQ机器人](https://github.com/ctwj/astrbot_plugin_urldb)
|
||||
- [Telegram机器人](https://ecn5khs4t956.feishu.cn/wiki/SwkQw6AzRiFes7kxJXac3pd2ncb?from=from_copylink)
|
||||
- [微信公众号自动回复](https://ecn5khs4t956.feishu.cn/wiki/APOEwOyDYicKGHk7gTzcQKpynkf?from=from_copylink)
|
||||
|
||||
### v1.3.3
|
||||
1. 新增公众号自动回复
|
||||
|
||||
@@ -209,7 +209,15 @@ func createIndexes(db *gorm.DB) {
|
||||
db.Exec("CREATE INDEX IF NOT EXISTS idx_resource_tags_resource_id ON resource_tags(resource_id)")
|
||||
db.Exec("CREATE INDEX IF NOT EXISTS idx_resource_tags_tag_id ON resource_tags(tag_id)")
|
||||
|
||||
utils.Info("数据库索引创建完成(已移除全文搜索索引,准备使用Meilisearch)")
|
||||
// API访问日志表索引 - 高性能查询优化
|
||||
db.Exec("CREATE INDEX IF NOT EXISTS idx_api_access_logs_created_at ON api_access_logs(created_at DESC)")
|
||||
db.Exec("CREATE INDEX IF NOT EXISTS idx_api_access_logs_endpoint_status ON api_access_logs(endpoint, response_status)")
|
||||
db.Exec("CREATE INDEX IF NOT EXISTS idx_api_access_logs_ip_created ON api_access_logs(ip, created_at DESC)")
|
||||
db.Exec("CREATE INDEX IF NOT EXISTS idx_api_access_logs_method_endpoint ON api_access_logs(method, endpoint)")
|
||||
db.Exec("CREATE INDEX IF NOT EXISTS idx_api_access_logs_response_time ON api_access_logs(processing_time)")
|
||||
db.Exec("CREATE INDEX IF NOT EXISTS idx_api_access_logs_error_logs ON api_access_logs(response_status, created_at DESC) WHERE response_status >= 400")
|
||||
|
||||
utils.Info("数据库索引创建完成(已移除全文搜索索引,准备使用Meilisearch,新增API访问日志性能索引)")
|
||||
}
|
||||
|
||||
// insertDefaultDataIfEmpty 只在数据库为空时插入默认数据
|
||||
|
||||
@@ -72,19 +72,20 @@ type PanResponse struct {
|
||||
|
||||
// CksResponse Cookie响应
|
||||
type CksResponse struct {
|
||||
ID uint `json:"id"`
|
||||
PanID uint `json:"pan_id"`
|
||||
Idx int `json:"idx"`
|
||||
Ck string `json:"ck"`
|
||||
IsValid bool `json:"is_valid"`
|
||||
Space int64 `json:"space"`
|
||||
LeftSpace int64 `json:"left_space"`
|
||||
UsedSpace int64 `json:"used_space"`
|
||||
Username string `json:"username"`
|
||||
VipStatus bool `json:"vip_status"`
|
||||
ServiceType string `json:"service_type"`
|
||||
Remark string `json:"remark"`
|
||||
Pan *PanResponse `json:"pan,omitempty"`
|
||||
ID uint `json:"id"`
|
||||
PanID uint `json:"pan_id"`
|
||||
Idx int `json:"idx"`
|
||||
Ck string `json:"ck"`
|
||||
IsValid bool `json:"is_valid"`
|
||||
Space int64 `json:"space"`
|
||||
LeftSpace int64 `json:"left_space"`
|
||||
UsedSpace int64 `json:"used_space"`
|
||||
Username string `json:"username"`
|
||||
VipStatus bool `json:"vip_status"`
|
||||
ServiceType string `json:"service_type"`
|
||||
Remark string `json:"remark"`
|
||||
TransferredCount int64 `json:"transferred_count"` // 已转存资源数
|
||||
Pan *PanResponse `json:"pan,omitempty"`
|
||||
}
|
||||
|
||||
// ReadyResourceResponse 待处理资源响应
|
||||
|
||||
@@ -44,6 +44,8 @@ type ResourceRepository interface {
|
||||
MarkAllAsUnsyncedToMeilisearch() error
|
||||
FindAllWithPagination(page, limit int) ([]entity.Resource, int64, error)
|
||||
GetRandomResourceWithFilters(categoryFilter, tagFilter string, isPushSavedInfo bool) (*entity.Resource, error)
|
||||
DeleteRelatedResources(ckID uint) (int64, error)
|
||||
CountResourcesByCkID(ckID uint) (int64, error)
|
||||
}
|
||||
|
||||
// ResourceRepositoryImpl Resource的Repository实现
|
||||
@@ -277,6 +279,20 @@ func (r *ResourceRepositoryImpl) SearchWithFilters(params map[string]interface{}
|
||||
db = db.Where("pan_id = ?", panEntity.ID)
|
||||
}
|
||||
}
|
||||
case "exclude_ids": // 添加exclude_ids参数支持
|
||||
if excludeIDs, ok := value.([]uint); ok && len(excludeIDs) > 0 {
|
||||
// 限制排除ID的数量,避免SQL语句过长
|
||||
maxExcludeIDs := 5000 // 限制排除ID数量,避免SQL语句过长
|
||||
if len(excludeIDs) > maxExcludeIDs {
|
||||
// 只取最近的maxExcludeIDs个ID进行排除
|
||||
startIndex := len(excludeIDs) - maxExcludeIDs
|
||||
truncatedExcludeIDs := excludeIDs[startIndex:]
|
||||
db = db.Where("id NOT IN ?", truncatedExcludeIDs)
|
||||
utils.Debug("SearchWithFilters: 排除ID数量过多,截取最近%d个ID", len(truncatedExcludeIDs))
|
||||
} else {
|
||||
db = db.Where("id NOT IN ?", excludeIDs)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -650,3 +666,29 @@ func (r *ResourceRepositoryImpl) GetRandomResourceWithFilters(categoryFilter, ta
|
||||
|
||||
return &resource, nil
|
||||
}
|
||||
|
||||
// DeleteRelatedResources 删除关联资源,清空 fid、ck_id 和 save_url 三个字段
|
||||
func (r *ResourceRepositoryImpl) DeleteRelatedResources(ckID uint) (int64, error) {
|
||||
result := r.db.Model(&entity.Resource{}).
|
||||
Where("ck_id = ?", ckID).
|
||||
Updates(map[string]interface{}{
|
||||
"fid": nil, // 清空 fid 字段
|
||||
"ck_id": 0, // 清空 ck_id 字段
|
||||
"save_url": "", // 清空 save_url 字段
|
||||
})
|
||||
|
||||
if result.Error != nil {
|
||||
return 0, result.Error
|
||||
}
|
||||
|
||||
return result.RowsAffected, nil
|
||||
}
|
||||
|
||||
// CountResourcesByCkID 统计指定账号ID的资源数量
|
||||
func (r *ResourceRepositoryImpl) CountResourcesByCkID(ckID uint) (int64, error) {
|
||||
var count int64
|
||||
err := r.db.Model(&entity.Resource{}).
|
||||
Where("ck_id = ?", ckID).
|
||||
Count(&count).Error
|
||||
return count, err
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ services:
|
||||
- app-network
|
||||
|
||||
backend:
|
||||
image: ctwj/urldb-backend:1.3.3
|
||||
image: ctwj/urldb-backend:1.3.4
|
||||
environment:
|
||||
DB_HOST: postgres
|
||||
DB_PORT: 5432
|
||||
@@ -38,7 +38,7 @@ services:
|
||||
- app-network
|
||||
|
||||
frontend:
|
||||
image: ctwj/urldb-frontend:1.3.3
|
||||
image: ctwj/urldb-frontend:1.3.4
|
||||
environment:
|
||||
NODE_ENV: production
|
||||
NUXT_PUBLIC_API_SERVER: http://backend:8080/api
|
||||
|
||||
@@ -22,7 +22,49 @@ func GetCks(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
responses := converter.ToCksResponseList(cks)
|
||||
// 使用新的逻辑创建 CksResponse
|
||||
var responses []dto.CksResponse
|
||||
for _, ck := range cks {
|
||||
// 获取平台信息
|
||||
var pan *dto.PanResponse
|
||||
if ck.PanID != 0 {
|
||||
panEntity, err := repoManager.PanRepository.FindByID(ck.PanID)
|
||||
if err == nil && panEntity != nil {
|
||||
pan = &dto.PanResponse{
|
||||
ID: panEntity.ID,
|
||||
Name: panEntity.Name,
|
||||
Key: panEntity.Key,
|
||||
Icon: panEntity.Icon,
|
||||
Remark: panEntity.Remark,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 统计转存资源数
|
||||
count, err := repoManager.ResourceRepository.CountResourcesByCkID(ck.ID)
|
||||
if err != nil {
|
||||
count = 0 // 统计失败时设为0
|
||||
}
|
||||
|
||||
response := dto.CksResponse{
|
||||
ID: ck.ID,
|
||||
PanID: ck.PanID,
|
||||
Idx: ck.Idx,
|
||||
Ck: ck.Ck,
|
||||
IsValid: ck.IsValid,
|
||||
Space: ck.Space,
|
||||
LeftSpace: ck.LeftSpace,
|
||||
UsedSpace: ck.UsedSpace,
|
||||
Username: ck.Username,
|
||||
VipStatus: ck.VipStatus,
|
||||
ServiceType: ck.ServiceType,
|
||||
Remark: ck.Remark,
|
||||
TransferredCount: count,
|
||||
Pan: pan,
|
||||
}
|
||||
responses = append(responses, response)
|
||||
}
|
||||
|
||||
SuccessResponse(c, responses)
|
||||
}
|
||||
|
||||
@@ -380,3 +422,25 @@ func RefreshCapacity(c *gin.Context) {
|
||||
"cks": converter.ToCksResponse(cks),
|
||||
})
|
||||
}
|
||||
|
||||
// DeleteRelatedResources 删除关联资源
|
||||
func DeleteRelatedResources(c *gin.Context) {
|
||||
idStr := c.Param("id")
|
||||
id, err := strconv.ParseUint(idStr, 10, 32)
|
||||
if err != nil {
|
||||
ErrorResponse(c, "无效的ID", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// 调用资源库删除关联资源
|
||||
affectedRows, err := repoManager.ResourceRepository.DeleteRelatedResources(uint(id))
|
||||
if err != nil {
|
||||
ErrorResponse(c, "删除关联资源失败: "+err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
SuccessResponse(c, gin.H{
|
||||
"message": "关联资源删除成功",
|
||||
"affected_rows": affectedRows,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -91,30 +92,9 @@ func (h *PublicAPIHandler) logAPIAccess(c *gin.Context, startTime time.Time, pro
|
||||
}
|
||||
}
|
||||
|
||||
// 异步记录日志,避免影响API响应时间
|
||||
go func() {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
utils.Error("记录API访问日志时发生panic: %v", r)
|
||||
}
|
||||
}()
|
||||
|
||||
err := repoManager.APIAccessLogRepository.RecordAccess(
|
||||
ip,
|
||||
userAgent,
|
||||
endpoint,
|
||||
method,
|
||||
requestParams,
|
||||
c.Writer.Status(),
|
||||
responseData,
|
||||
processCount,
|
||||
errorMessage,
|
||||
processingTime,
|
||||
)
|
||||
if err != nil {
|
||||
utils.Error("记录API访问日志失败: %v", err)
|
||||
}
|
||||
}()
|
||||
// 记录API访问日志 - 使用简单日志记录
|
||||
h.recordAPIAccessToDB(ip, userAgent, endpoint, method, requestParams,
|
||||
c.Writer.Status(), responseData, processCount, errorMessage, processingTime)
|
||||
}
|
||||
|
||||
// AddBatchResources godoc
|
||||
@@ -466,3 +446,49 @@ func (h *PublicAPIHandler) GetHotDramas(c *gin.Context) {
|
||||
h.logAPIAccess(c, startTime, len(hotDramaResponses), responseData, "")
|
||||
SuccessResponse(c, responseData)
|
||||
}
|
||||
|
||||
// recordAPIAccessToDB 记录API访问日志到数据库
|
||||
func (h *PublicAPIHandler) recordAPIAccessToDB(ip, userAgent, endpoint, method string,
|
||||
requestParams interface{}, responseStatus int, responseData interface{},
|
||||
processCount int, errorMessage string, processingTime int64) {
|
||||
|
||||
// 只记录重要的API访问(有错误或处理时间较长的)
|
||||
if errorMessage == "" && processingTime < 1000 && responseStatus < 400 {
|
||||
return // 跳过正常的快速请求
|
||||
}
|
||||
|
||||
// 转换参数为JSON字符串
|
||||
var requestParamsStr, responseDataStr string
|
||||
if requestParams != nil {
|
||||
if jsonBytes, err := json.Marshal(requestParams); err == nil {
|
||||
requestParamsStr = string(jsonBytes)
|
||||
}
|
||||
}
|
||||
if responseData != nil {
|
||||
if jsonBytes, err := json.Marshal(responseData); err == nil {
|
||||
responseDataStr = string(jsonBytes)
|
||||
}
|
||||
}
|
||||
|
||||
// 创建日志记录
|
||||
logEntry := &entity.APIAccessLog{
|
||||
IP: ip,
|
||||
UserAgent: userAgent,
|
||||
Endpoint: endpoint,
|
||||
Method: method,
|
||||
RequestParams: requestParamsStr,
|
||||
ResponseStatus: responseStatus,
|
||||
ResponseData: responseDataStr,
|
||||
ProcessCount: processCount,
|
||||
ErrorMessage: errorMessage,
|
||||
ProcessingTime: processingTime,
|
||||
}
|
||||
|
||||
// 异步保存到数据库(避免影响API性能)
|
||||
go func() {
|
||||
if err := repoManager.APIAccessLogRepository.Create(logEntry); err != nil {
|
||||
// 记录失败只输出到系统日志,不影响API
|
||||
utils.Error("保存API访问日志失败: %v", err)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
@@ -145,27 +145,26 @@ func UpdateSystemConfig(c *gin.Context) {
|
||||
utils.Info("当前配置数量: %d", len(currentConfigs))
|
||||
}
|
||||
|
||||
// 验证参数 - 只验证提交的字段
|
||||
utils.Info("开始验证参数")
|
||||
// 验证参数 - 只验证提交的字段,仅在验证失败时记录日志
|
||||
if req.SiteTitle != nil {
|
||||
utils.Info("验证SiteTitle: '%s', 长度: %d", *req.SiteTitle, len(*req.SiteTitle))
|
||||
if len(*req.SiteTitle) < 1 || len(*req.SiteTitle) > 100 {
|
||||
utils.Warn("配置验证失败 - SiteTitle长度无效: %d", len(*req.SiteTitle))
|
||||
ErrorResponse(c, "网站标题长度必须在1-100字符之间", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if req.AutoProcessInterval != nil {
|
||||
utils.Info("验证AutoProcessInterval: %d", *req.AutoProcessInterval)
|
||||
if *req.AutoProcessInterval < 1 || *req.AutoProcessInterval > 1440 {
|
||||
utils.Warn("配置验证失败 - AutoProcessInterval超出范围: %d", *req.AutoProcessInterval)
|
||||
ErrorResponse(c, "自动处理间隔必须在1-1440分钟之间", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if req.PageSize != nil {
|
||||
utils.Info("验证PageSize: %d", *req.PageSize)
|
||||
if *req.PageSize < 10 || *req.PageSize > 500 {
|
||||
utils.Warn("配置验证失败 - PageSize超出范围: %d", *req.PageSize)
|
||||
ErrorResponse(c, "每页显示数量必须在10-500之间", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
@@ -173,16 +172,16 @@ func UpdateSystemConfig(c *gin.Context) {
|
||||
|
||||
// 验证自动转存配置
|
||||
if req.AutoTransferLimitDays != nil {
|
||||
utils.Info("验证AutoTransferLimitDays: %d", *req.AutoTransferLimitDays)
|
||||
if *req.AutoTransferLimitDays < 0 || *req.AutoTransferLimitDays > 365 {
|
||||
utils.Warn("配置验证失败 - AutoTransferLimitDays超出范围: %d", *req.AutoTransferLimitDays)
|
||||
ErrorResponse(c, "自动转存限制天数必须在0-365之间", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if req.AutoTransferMinSpace != nil {
|
||||
utils.Info("验证AutoTransferMinSpace: %d", *req.AutoTransferMinSpace)
|
||||
if *req.AutoTransferMinSpace < 100 || *req.AutoTransferMinSpace > 1024 {
|
||||
utils.Warn("配置验证失败 - AutoTransferMinSpace超出范围: %d", *req.AutoTransferMinSpace)
|
||||
ErrorResponse(c, "最小存储空间必须在100-1024GB之间", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
@@ -190,19 +189,17 @@ func UpdateSystemConfig(c *gin.Context) {
|
||||
|
||||
// 验证公告相关字段
|
||||
if req.Announcements != nil {
|
||||
utils.Info("验证Announcements: '%s'", *req.Announcements)
|
||||
// 可以在这里添加更详细的验证逻辑
|
||||
// 简化验证,仅在需要时添加逻辑
|
||||
}
|
||||
|
||||
// 转换为实体
|
||||
configs := converter.RequestToSystemConfig(&req)
|
||||
if configs == nil {
|
||||
utils.Error("配置数据转换失败")
|
||||
ErrorResponse(c, "数据转换失败", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
utils.Info("准备更新配置,配置项数量: %d", len(configs))
|
||||
|
||||
// 保存配置
|
||||
err = repoManager.SystemConfigRepository.UpsertConfigs(configs)
|
||||
if err != nil {
|
||||
@@ -211,7 +208,7 @@ func UpdateSystemConfig(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
utils.Info("配置保存成功")
|
||||
utils.Info("系统配置更新成功 - 更新项数: %d", len(configs))
|
||||
|
||||
// 安全刷新系统配置缓存
|
||||
if err := repoManager.SystemConfigRepository.SafeRefreshConfigCache(); err != nil {
|
||||
|
||||
@@ -80,16 +80,40 @@ func (h *TelegramHandler) UpdateBotConfig(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// 配置更新完成后,尝试启动机器人(如果未运行且配置有效)
|
||||
if startErr := h.telegramBotService.Start(); startErr != nil {
|
||||
utils.Warn("[TELEGRAM:HANDLER] 配置更新后尝试启动机器人失败: %v", startErr)
|
||||
// 启动失败不影响配置保存,只记录警告
|
||||
// 根据配置状态决定启动或停止机器人
|
||||
botEnabled := false
|
||||
for _, config := range configs {
|
||||
if config.Key == "telegram_bot_enabled" {
|
||||
botEnabled = config.Value == "true"
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if botEnabled {
|
||||
// 机器人已启用,尝试启动机器人
|
||||
if startErr := h.telegramBotService.Start(); startErr != nil {
|
||||
utils.Warn("[TELEGRAM:HANDLER] 配置更新后尝试启动机器人失败: %v", startErr)
|
||||
// 启动失败不影响配置保存,只记录警告
|
||||
}
|
||||
} else {
|
||||
// 机器人已禁用,停止机器人服务
|
||||
if stopErr := h.telegramBotService.Stop(); stopErr != nil {
|
||||
utils.Warn("[TELEGRAM:HANDLER] 配置更新后停止机器人失败: %v", stopErr)
|
||||
// 停止失败不影响配置保存,只记录警告
|
||||
}
|
||||
}
|
||||
|
||||
// 返回成功
|
||||
var message string
|
||||
if botEnabled {
|
||||
message = "配置更新成功,机器人已尝试启动"
|
||||
} else {
|
||||
message = "配置更新成功,机器人已停止"
|
||||
}
|
||||
|
||||
SuccessResponse(c, map[string]interface{}{
|
||||
"success": true,
|
||||
"message": "配置更新成功,机器人已尝试启动",
|
||||
"message": message,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
26
main.go
26
main.go
@@ -4,7 +4,9 @@ import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"os/signal"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/ctwj/urldb/config"
|
||||
@@ -38,7 +40,7 @@ func main() {
|
||||
}
|
||||
|
||||
// 初始化日志系统
|
||||
if err := utils.InitLogger(nil); err != nil {
|
||||
if err := utils.InitLogger(); err != nil {
|
||||
log.Fatal("初始化日志系统失败:", err)
|
||||
}
|
||||
|
||||
@@ -84,6 +86,8 @@ func main() {
|
||||
utils.Fatal("数据库连接失败: %v", err)
|
||||
}
|
||||
|
||||
// 日志系统已简化,无需额外初始化
|
||||
|
||||
// 创建Repository管理器
|
||||
repoManager := repo.NewRepositoryManager(db.DB)
|
||||
|
||||
@@ -265,6 +269,7 @@ func main() {
|
||||
api.DELETE("/cks/:id", middleware.AuthMiddleware(), middleware.AdminMiddleware(), handlers.DeleteCks)
|
||||
api.GET("/cks/:id", middleware.AuthMiddleware(), middleware.AdminMiddleware(), handlers.GetCksByID)
|
||||
api.POST("/cks/:id/refresh-capacity", middleware.AuthMiddleware(), middleware.AdminMiddleware(), handlers.RefreshCapacity)
|
||||
api.POST("/cks/:id/delete-related-resources", middleware.AuthMiddleware(), middleware.AdminMiddleware(), handlers.DeleteRelatedResources)
|
||||
|
||||
// 标签管理
|
||||
api.GET("/tags", handlers.GetTags)
|
||||
@@ -462,6 +467,21 @@ func main() {
|
||||
port = "8080"
|
||||
}
|
||||
|
||||
utils.Info("服务器启动在端口 %s", port)
|
||||
r.Run(":" + port)
|
||||
// 设置优雅关闭
|
||||
quit := make(chan os.Signal, 1)
|
||||
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
|
||||
|
||||
// 在goroutine中启动服务器
|
||||
go func() {
|
||||
utils.Info("服务器启动在端口 %s", port)
|
||||
if err := r.Run(":" + port); err != nil && err.Error() != "http: Server closed" {
|
||||
utils.Fatal("服务器启动失败: %v", err)
|
||||
}
|
||||
}()
|
||||
|
||||
// 等待信号
|
||||
<-quit
|
||||
utils.Info("收到关闭信号,开始优雅关闭...")
|
||||
|
||||
utils.Info("服务器已优雅关闭")
|
||||
}
|
||||
|
||||
@@ -34,7 +34,7 @@ func AuthMiddleware() gin.HandlerFunc {
|
||||
c.Request.Method, c.Request.URL.Path, clientIP, userAgent)
|
||||
|
||||
if authHeader == "" {
|
||||
utils.Warn("AuthMiddleware - 未提供认证令牌 - IP: %s, Path: %s", clientIP, c.Request.URL.Path)
|
||||
// utils.Warn("AuthMiddleware - 未提供认证令牌 - IP: %s, Path: %s", clientIP, c.Request.URL.Path)
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "未提供认证令牌"})
|
||||
c.Abort()
|
||||
return
|
||||
@@ -59,8 +59,8 @@ func AuthMiddleware() gin.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
utils.Info("AuthMiddleware - 认证成功 - 用户: %s(ID:%d), 角色: %s, IP: %s",
|
||||
claims.Username, claims.UserID, claims.Role, clientIP)
|
||||
// utils.Info("AuthMiddleware - 认证成功 - 用户: %s(ID:%d), 角色: %s, IP: %s",
|
||||
// claims.Username, claims.UserID, claims.Role, clientIP)
|
||||
|
||||
// 将用户信息存储到上下文中
|
||||
c.Set("user_id", claims.UserID)
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"bytes"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/ctwj/urldb/utils"
|
||||
@@ -55,41 +56,64 @@ func LoggingMiddleware(next http.Handler) http.Handler {
|
||||
})
|
||||
}
|
||||
|
||||
// logRequest 记录请求日志
|
||||
// logRequest 记录请求日志 - 优化后仅记录异常和关键请求
|
||||
func logRequest(r *http.Request, rw *responseWriter, duration time.Duration, requestBody []byte) {
|
||||
// 获取客户端IP
|
||||
clientIP := getClientIP(r)
|
||||
|
||||
// 获取用户代理
|
||||
userAgent := r.UserAgent()
|
||||
if userAgent == "" {
|
||||
userAgent = "Unknown"
|
||||
// 判断是否需要记录日志的条件
|
||||
shouldLog := rw.statusCode >= 400 || // 错误状态码
|
||||
duration > 5*time.Second || // 耗时过长
|
||||
shouldLogPath(r.URL.Path) || // 关键路径
|
||||
isAdminPath(r.URL.Path) // 管理员路径
|
||||
|
||||
if !shouldLog {
|
||||
return // 正常请求不记录日志,减少日志噪音
|
||||
}
|
||||
|
||||
// 记录请求信息
|
||||
utils.Info("HTTP请求 - %s %s - IP: %s - User-Agent: %s - 状态码: %d - 耗时: %v",
|
||||
r.Method, r.URL.Path, clientIP, userAgent, rw.statusCode, duration)
|
||||
|
||||
// 如果是错误状态码,记录详细信息
|
||||
// 简化的日志格式,移除User-Agent以减少噪音
|
||||
if rw.statusCode >= 400 {
|
||||
utils.Error("HTTP错误 - %s %s - 状态码: %d - 响应体: %s",
|
||||
r.Method, r.URL.Path, rw.statusCode, rw.body.String())
|
||||
// 错误请求记录详细信息
|
||||
utils.Error("HTTP异常 - %s %s - IP: %s - 状态码: %d - 耗时: %v",
|
||||
r.Method, r.URL.Path, clientIP, rw.statusCode, duration)
|
||||
|
||||
// 仅在错误状态下记录简要的请求信息
|
||||
if len(requestBody) > 0 && len(requestBody) <= 500 {
|
||||
utils.Error("请求详情: %s", string(requestBody))
|
||||
}
|
||||
} else if duration > 5*time.Second {
|
||||
// 慢请求警告
|
||||
utils.Warn("HTTP慢请求 - %s %s - IP: %s - 耗时: %v",
|
||||
r.Method, r.URL.Path, clientIP, duration)
|
||||
} else {
|
||||
// 关键路径的正常请求
|
||||
utils.Info("HTTP关键请求 - %s %s - IP: %s - 状态码: %d - 耗时: %v",
|
||||
r.Method, r.URL.Path, clientIP, rw.statusCode, duration)
|
||||
}
|
||||
}
|
||||
|
||||
// shouldLogPath 判断路径是否需要记录日志
|
||||
func shouldLogPath(path string) bool {
|
||||
// 定义需要记录日志的关键路径
|
||||
keyPaths := []string{
|
||||
"/api/public/resources",
|
||||
"/api/admin/config",
|
||||
"/api/admin/users",
|
||||
"/telegram/webhook",
|
||||
}
|
||||
|
||||
// 记录请求参数(仅对POST/PUT请求)
|
||||
if (r.Method == "POST" || r.Method == "PUT") && len(requestBody) > 0 {
|
||||
// 限制日志长度,避免日志文件过大
|
||||
if len(requestBody) > 1000 {
|
||||
utils.Debug("请求体(截断): %s...", string(requestBody[:1000]))
|
||||
} else {
|
||||
utils.Debug("请求体: %s", string(requestBody))
|
||||
for _, keyPath := range keyPaths {
|
||||
if strings.HasPrefix(path, keyPath) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// 记录查询参数
|
||||
if len(r.URL.RawQuery) > 0 {
|
||||
utils.Debug("查询参数: %s", r.URL.RawQuery)
|
||||
}
|
||||
// isAdminPath 判断是否为管理员路径
|
||||
func isAdminPath(path string) bool {
|
||||
return strings.HasPrefix(path, "/api/admin/") ||
|
||||
strings.HasPrefix(path, "/admin/")
|
||||
}
|
||||
|
||||
// getClientIP 获取客户端真实IP地址
|
||||
|
||||
@@ -52,6 +52,7 @@ type TelegramBotServiceImpl struct {
|
||||
config *TelegramBotConfig
|
||||
pushHistory map[int64][]uint // 每个频道的推送历史记录,最多100条
|
||||
mu sync.RWMutex // 用于保护pushHistory的读写锁
|
||||
stopChan chan struct{} // 用于停止消息循环的channel
|
||||
}
|
||||
|
||||
type TelegramBotConfig struct {
|
||||
@@ -84,6 +85,7 @@ func NewTelegramBotService(
|
||||
cronScheduler: cron.New(),
|
||||
config: &TelegramBotConfig{},
|
||||
pushHistory: make(map[int64][]uint),
|
||||
stopChan: make(chan struct{}),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -111,57 +113,55 @@ func (s *TelegramBotServiceImpl) loadConfig() error {
|
||||
s.config.ProxyUsername = ""
|
||||
s.config.ProxyPassword = ""
|
||||
|
||||
// 统计配置项数量,用于汇总日志
|
||||
configCount := 0
|
||||
|
||||
for _, config := range configs {
|
||||
switch config.Key {
|
||||
case entity.ConfigKeyTelegramBotEnabled:
|
||||
s.config.Enabled = config.Value == "true"
|
||||
utils.Info("[TELEGRAM:CONFIG] 加载配置 %s = %s (Enabled: %v)", config.Key, config.Value, s.config.Enabled)
|
||||
case entity.ConfigKeyTelegramBotApiKey:
|
||||
s.config.ApiKey = config.Value
|
||||
utils.Info("[TELEGRAM:CONFIG] 加载配置 %s = [HIDDEN]", config.Key)
|
||||
case entity.ConfigKeyTelegramAutoReplyEnabled:
|
||||
s.config.AutoReplyEnabled = config.Value == "true"
|
||||
utils.Info("[TELEGRAM:CONFIG] 加载配置 %s = %s (AutoReplyEnabled: %v)", config.Key, config.Value, s.config.AutoReplyEnabled)
|
||||
case entity.ConfigKeyTelegramAutoReplyTemplate:
|
||||
if config.Value != "" {
|
||||
s.config.AutoReplyTemplate = config.Value
|
||||
}
|
||||
utils.Info("[TELEGRAM:CONFIG] 加载配置 %s = %s", config.Key, config.Value)
|
||||
case entity.ConfigKeyTelegramAutoDeleteEnabled:
|
||||
s.config.AutoDeleteEnabled = config.Value == "true"
|
||||
utils.Info("[TELEGRAM:CONFIG] 加载配置 %s = %s (AutoDeleteEnabled: %v)", config.Key, config.Value, s.config.AutoDeleteEnabled)
|
||||
case entity.ConfigKeyTelegramAutoDeleteInterval:
|
||||
if config.Value != "" {
|
||||
fmt.Sscanf(config.Value, "%d", &s.config.AutoDeleteInterval)
|
||||
}
|
||||
utils.Info("[TELEGRAM:CONFIG] 加载配置 %s = %s (AutoDeleteInterval: %d)", config.Key, config.Value, s.config.AutoDeleteInterval)
|
||||
case entity.ConfigKeyTelegramProxyEnabled:
|
||||
s.config.ProxyEnabled = config.Value == "true"
|
||||
utils.Info("[TELEGRAM:CONFIG] 加载配置 %s = %s (ProxyEnabled: %v)", config.Key, config.Value, s.config.ProxyEnabled)
|
||||
case entity.ConfigKeyTelegramProxyType:
|
||||
s.config.ProxyType = config.Value
|
||||
utils.Info("[TELEGRAM:CONFIG] 加载配置 %s = %s (ProxyType: %s)", config.Key, config.Value, s.config.ProxyType)
|
||||
case entity.ConfigKeyTelegramProxyHost:
|
||||
s.config.ProxyHost = config.Value
|
||||
utils.Info("[TELEGRAM:CONFIG] 加载配置 %s = %s", config.Key, "[HIDDEN]")
|
||||
case entity.ConfigKeyTelegramProxyPort:
|
||||
if config.Value != "" {
|
||||
fmt.Sscanf(config.Value, "%d", &s.config.ProxyPort)
|
||||
}
|
||||
utils.Info("[TELEGRAM:CONFIG] 加载配置 %s = %s (ProxyPort: %d)", config.Key, config.Value, s.config.ProxyPort)
|
||||
case entity.ConfigKeyTelegramProxyUsername:
|
||||
s.config.ProxyUsername = config.Value
|
||||
utils.Info("[TELEGRAM:CONFIG] 加载配置 %s = %s", config.Key, "[HIDDEN]")
|
||||
case entity.ConfigKeyTelegramProxyPassword:
|
||||
s.config.ProxyPassword = config.Value
|
||||
utils.Info("[TELEGRAM:CONFIG] 加载配置 %s = %s", config.Key, "[HIDDEN]")
|
||||
default:
|
||||
utils.Debug("未知配置: %s = %s", config.Key, config.Value)
|
||||
utils.Debug("未知Telegram配置: %s", config.Key)
|
||||
}
|
||||
configCount++
|
||||
}
|
||||
|
||||
utils.Info("[TELEGRAM:SERVICE] Telegram Bot 配置加载完成: Enabled=%v, AutoReplyEnabled=%v, ApiKey长度=%d",
|
||||
s.config.Enabled, s.config.AutoReplyEnabled, len(s.config.ApiKey))
|
||||
// 汇总输出配置加载结果,避免逐项日志
|
||||
proxyStatus := "禁用"
|
||||
if s.config.ProxyEnabled {
|
||||
proxyStatus = "启用"
|
||||
}
|
||||
|
||||
utils.TelegramInfo("配置加载完成 - Bot启用: %v, 自动回复: %v, 代理: %s, 配置项数: %d",
|
||||
s.config.Enabled, s.config.AutoReplyEnabled, proxyStatus, configCount)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -185,6 +185,11 @@ func (s *TelegramBotServiceImpl) Start() error {
|
||||
|
||||
if !s.config.Enabled || s.config.ApiKey == "" {
|
||||
utils.Info("[TELEGRAM:SERVICE] Telegram Bot 未启用或 API Key 未配置")
|
||||
// 如果机器人当前正在运行,需要停止它
|
||||
if s.isRunning {
|
||||
utils.Info("[TELEGRAM:SERVICE] 机器人已被禁用,停止正在运行的服务")
|
||||
s.Stop()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -259,6 +264,9 @@ func (s *TelegramBotServiceImpl) Start() error {
|
||||
s.bot = bot
|
||||
s.isRunning = true
|
||||
|
||||
// 重置停止信号channel
|
||||
s.stopChan = make(chan struct{})
|
||||
|
||||
utils.Info("[TELEGRAM:SERVICE] Telegram Bot (@%s) 已启动", s.GetBotUsername())
|
||||
|
||||
// 启动推送调度器
|
||||
@@ -283,6 +291,15 @@ func (s *TelegramBotServiceImpl) Stop() error {
|
||||
|
||||
s.isRunning = false
|
||||
|
||||
// 安全地发送停止信号给消息循环
|
||||
select {
|
||||
case <-s.stopChan:
|
||||
// channel 已经关闭
|
||||
default:
|
||||
// channel 未关闭,安全关闭
|
||||
close(s.stopChan)
|
||||
}
|
||||
|
||||
if s.cronScheduler != nil {
|
||||
s.cronScheduler.Stop()
|
||||
}
|
||||
@@ -514,20 +531,34 @@ func (s *TelegramBotServiceImpl) messageLoop() {
|
||||
|
||||
utils.Info("[TELEGRAM:MESSAGE] 消息监听循环已启动,等待消息...")
|
||||
|
||||
for update := range updates {
|
||||
if update.Message != nil {
|
||||
utils.Info("[TELEGRAM:MESSAGE] 接收到新消息更新")
|
||||
s.handleMessage(update.Message)
|
||||
} else {
|
||||
utils.Debug("[TELEGRAM:MESSAGE] 接收到其他类型更新: %v", update)
|
||||
for {
|
||||
select {
|
||||
case <-s.stopChan:
|
||||
utils.Info("[TELEGRAM:MESSAGE] 收到停止信号,退出消息监听循环")
|
||||
return
|
||||
case update, ok := <-updates:
|
||||
if !ok {
|
||||
utils.Info("[TELEGRAM:MESSAGE] updates channel 已关闭,退出消息监听循环")
|
||||
return
|
||||
}
|
||||
if update.Message != nil {
|
||||
utils.Info("[TELEGRAM:MESSAGE] 接收到新消息更新")
|
||||
s.handleMessage(update.Message)
|
||||
} else {
|
||||
utils.Debug("[TELEGRAM:MESSAGE] 接收到其他类型更新: %v", update)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
utils.Info("[TELEGRAM:MESSAGE] 消息监听循环已结束")
|
||||
}
|
||||
|
||||
// handleMessage 处理接收到的消息
|
||||
func (s *TelegramBotServiceImpl) handleMessage(message *tgbotapi.Message) {
|
||||
// 检查机器人是否正在运行且已启用
|
||||
if !s.isRunning || !s.config.Enabled {
|
||||
utils.Info("[TELEGRAM:MESSAGE] 机器人已停止或禁用,跳过消息处理: ChatID=%d", message.Chat.ID)
|
||||
return
|
||||
}
|
||||
|
||||
chatID := message.Chat.ID
|
||||
text := strings.TrimSpace(message.Text)
|
||||
|
||||
@@ -965,8 +996,17 @@ func (s *TelegramBotServiceImpl) pushToChannel(channel entity.TelegramChannel) {
|
||||
|
||||
// 5. 记录推送的资源ID到历史记录,避免重复推送
|
||||
for _, resource := range resources {
|
||||
resourceEntity := resource.(entity.Resource)
|
||||
s.addPushedResourceID(channel.ChatID, resourceEntity.ID)
|
||||
var resourceID uint
|
||||
switch r := resource.(type) {
|
||||
case *entity.Resource:
|
||||
resourceID = r.ID
|
||||
case entity.Resource:
|
||||
resourceID = r.ID
|
||||
default:
|
||||
utils.Error("[TELEGRAM:PUSH] 无效的资源类型: %T", resource)
|
||||
continue
|
||||
}
|
||||
s.addPushedResourceID(channel.ChatID, resourceID)
|
||||
}
|
||||
|
||||
utils.Info("[TELEGRAM:PUSH:SUCCESS] 成功推送内容到频道: %s (%d 条资源)", channel.ChatName, len(resources))
|
||||
@@ -1009,16 +1049,16 @@ func (s *TelegramBotServiceImpl) findResourcesForChannel(channel entity.Telegram
|
||||
func (s *TelegramBotServiceImpl) findLatestResources(channel entity.TelegramChannel, excludeResourceIDs []uint) []interface{} {
|
||||
params := s.buildFilterParams(channel)
|
||||
|
||||
// 在数据库查询中排除已推送的资源
|
||||
if len(excludeResourceIDs) > 0 {
|
||||
params["exclude_ids"] = excludeResourceIDs
|
||||
}
|
||||
|
||||
// 使用现有的搜索功能,按更新时间倒序获取最新资源
|
||||
resources, _, err := s.resourceRepo.SearchWithFilters(params)
|
||||
if err != nil {
|
||||
utils.Error("[TELEGRAM:PUSH] 获取最新资源失败: %v", err)
|
||||
return []interface{}{}
|
||||
}
|
||||
|
||||
// 排除最近推送过的资源
|
||||
if len(excludeResourceIDs) > 0 {
|
||||
resources = s.excludePushedResources(resources, excludeResourceIDs)
|
||||
return s.findRandomResources(channel, excludeResourceIDs) // 回退到随机策略
|
||||
}
|
||||
|
||||
// 应用时间限制
|
||||
@@ -1027,13 +1067,13 @@ func (s *TelegramBotServiceImpl) findLatestResources(channel entity.TelegramChan
|
||||
}
|
||||
|
||||
if len(resources) == 0 {
|
||||
utils.Info("[TELEGRAM:PUSH] 没有找到符合条件的最新资源")
|
||||
return []interface{}{}
|
||||
utils.Info("[TELEGRAM:PUSH] 没有找到符合条件的最新资源,尝试获取随机资源")
|
||||
return s.findRandomResources(channel, excludeResourceIDs) // 回退到随机策略
|
||||
}
|
||||
|
||||
// 返回最新资源(第一条)
|
||||
utils.Info("[TELEGRAM:PUSH] 成功获取最新资源: %s", resources[0].Title)
|
||||
return []interface{}{resources[0]}
|
||||
return []interface{}{&resources[0]}
|
||||
}
|
||||
|
||||
// findTransferredResources 查找已转存资源
|
||||
@@ -1043,6 +1083,11 @@ func (s *TelegramBotServiceImpl) findTransferredResources(channel entity.Telegra
|
||||
// 添加转存链接条件
|
||||
params["has_save_url"] = true
|
||||
|
||||
// 在数据库查询中排除已推送的资源
|
||||
if len(excludeResourceIDs) > 0 {
|
||||
params["exclude_ids"] = excludeResourceIDs
|
||||
}
|
||||
|
||||
// 优先获取有转存链接的资源
|
||||
resources, _, err := s.resourceRepo.SearchWithFilters(params)
|
||||
if err != nil {
|
||||
@@ -1050,11 +1095,6 @@ func (s *TelegramBotServiceImpl) findTransferredResources(channel entity.Telegra
|
||||
return []interface{}{}
|
||||
}
|
||||
|
||||
// 排除最近推送过的资源
|
||||
if len(excludeResourceIDs) > 0 {
|
||||
resources = s.excludePushedResources(resources, excludeResourceIDs)
|
||||
}
|
||||
|
||||
// 应用时间限制
|
||||
if channel.TimeLimit != "none" && len(resources) > 0 {
|
||||
resources = s.applyTimeFilter(resources, channel.TimeLimit)
|
||||
@@ -1068,7 +1108,7 @@ func (s *TelegramBotServiceImpl) findTransferredResources(channel entity.Telegra
|
||||
|
||||
// 返回第一个有转存链接的资源
|
||||
utils.Info("[TELEGRAM:PUSH] 成功获取已转存资源: %s", resources[0].Title)
|
||||
return []interface{}{resources[0]}
|
||||
return []interface{}{&resources[0]}
|
||||
}
|
||||
|
||||
// findRandomResources 查找随机资源(原有逻辑)
|
||||
@@ -1078,23 +1118,19 @@ func (s *TelegramBotServiceImpl) findRandomResources(channel entity.TelegramChan
|
||||
// 如果是已转存优先策略但没有找到转存资源,这里会回退到随机策略
|
||||
// 此时不需要额外的转存链接条件,让随机函数处理
|
||||
|
||||
// 先尝试获取候选资源列表,然后从中排除已推送的资源
|
||||
var candidateResources []entity.Resource
|
||||
var err error
|
||||
// 在数据库查询中排除已推送的资源
|
||||
if len(excludeResourceIDs) > 0 {
|
||||
params["exclude_ids"] = excludeResourceIDs
|
||||
}
|
||||
|
||||
// 使用搜索功能获取候选资源,然后过滤
|
||||
params["limit"] = 100 // 获取更多候选资源
|
||||
candidateResources, _, err = s.resourceRepo.SearchWithFilters(params)
|
||||
candidateResources, _, err := s.resourceRepo.SearchWithFilters(params)
|
||||
if err != nil {
|
||||
utils.Error("[TELEGRAM:PUSH] 获取候选资源失败: %v", err)
|
||||
return []interface{}{}
|
||||
}
|
||||
|
||||
// 排除最近推送过的资源
|
||||
if len(excludeResourceIDs) > 0 {
|
||||
candidateResources = s.excludePushedResources(candidateResources, excludeResourceIDs)
|
||||
}
|
||||
|
||||
// 应用时间限制
|
||||
if channel.TimeLimit != "none" && len(candidateResources) > 0 {
|
||||
candidateResources = s.applyTimeFilter(candidateResources, channel.TimeLimit)
|
||||
@@ -1108,7 +1144,7 @@ func (s *TelegramBotServiceImpl) findRandomResources(channel entity.TelegramChan
|
||||
|
||||
utils.Info("[TELEGRAM:PUSH] 成功获取随机资源: %s (从 %d 个候选资源中选择)",
|
||||
selectedResource.Title, len(candidateResources))
|
||||
return []interface{}{selectedResource}
|
||||
return []interface{}{&selectedResource}
|
||||
}
|
||||
|
||||
// 如果候选资源不足,回退到数据库随机函数
|
||||
@@ -1184,7 +1220,18 @@ func (s *TelegramBotServiceImpl) buildFilterParams(channel entity.TelegramChanne
|
||||
|
||||
// buildPushMessage 构建推送消息
|
||||
func (s *TelegramBotServiceImpl) buildPushMessage(channel entity.TelegramChannel, resources []interface{}) (string, string) {
|
||||
resource := resources[0].(entity.Resource)
|
||||
var resource *entity.Resource
|
||||
|
||||
// 处理两种可能的类型:*entity.Resource 或 entity.Resource
|
||||
switch r := resources[0].(type) {
|
||||
case *entity.Resource:
|
||||
resource = r
|
||||
case entity.Resource:
|
||||
resource = &r
|
||||
default:
|
||||
utils.Error("[TELEGRAM:PUSH] 无效的资源类型: %T", resources[0])
|
||||
return "", ""
|
||||
}
|
||||
|
||||
message := fmt.Sprintf("🆕 <b>%s</b>\n", s.cleanMessageTextForHTML(resource.Title))
|
||||
|
||||
@@ -1243,6 +1290,12 @@ func (s *TelegramBotServiceImpl) GetBotUsername() string {
|
||||
|
||||
// SendMessage 发送消息(默认使用 HTML 格式)
|
||||
func (s *TelegramBotServiceImpl) SendMessage(chatID int64, text string, img string) error {
|
||||
// 检查机器人是否正在运行且已启用
|
||||
if !s.isRunning || !s.config.Enabled {
|
||||
utils.Info("[TELEGRAM:MESSAGE] 机器人已停止或禁用,跳过发送消息: ChatID=%d", chatID)
|
||||
return fmt.Errorf("机器人已停止或禁用")
|
||||
}
|
||||
|
||||
if img == "" {
|
||||
msg := tgbotapi.NewMessage(chatID, text)
|
||||
msg.ParseMode = "HTML"
|
||||
@@ -1752,11 +1805,12 @@ func (s *TelegramBotServiceImpl) addPushedResourceID(chatID int64, resourceID ui
|
||||
history = []uint{}
|
||||
}
|
||||
|
||||
// 检查是否已经超过100条记录
|
||||
if len(history) >= 10000 {
|
||||
// 清空历史记录,重新开始
|
||||
history = []uint{}
|
||||
utils.Info("[TELEGRAM:PUSH] 频道 %d 推送历史记录已满(10000条),清空重置", chatID)
|
||||
// 检查是否已经超过5000条记录
|
||||
if len(history) >= 5000 {
|
||||
// 移除旧的2500条记录,保留最新的2500条记录
|
||||
startIndex := len(history) - 2500
|
||||
history = history[startIndex:]
|
||||
utils.Info("[TELEGRAM:PUSH] 频道 %d 推送历史记录已满(5000条),移除旧的2500条记录,保留最新的2500条", chatID)
|
||||
}
|
||||
|
||||
// 添加新的资源ID到历史记录
|
||||
@@ -1839,10 +1893,11 @@ func (s *TelegramBotServiceImpl) loadPushHistory() error {
|
||||
resourceIDs = append(resourceIDs, uint(resourceID))
|
||||
}
|
||||
|
||||
// 只保留最多100条记录
|
||||
if len(resourceIDs) > 100 {
|
||||
// 保留最新的100条记录
|
||||
resourceIDs = resourceIDs[len(resourceIDs)-100:]
|
||||
// 只保留最多5000条记录
|
||||
if len(resourceIDs) > 5000 {
|
||||
// 保留最新的5000条记录
|
||||
startIndex := len(resourceIDs) - 5000
|
||||
resourceIDs = resourceIDs[startIndex:]
|
||||
}
|
||||
|
||||
s.pushHistory[chatID] = resourceIDs
|
||||
|
||||
@@ -189,12 +189,22 @@ func (tm *TaskManager) StopTask(taskID uint) error {
|
||||
// processTask 处理任务
|
||||
func (tm *TaskManager) processTask(ctx context.Context, task *entity.Task, processor TaskProcessor) {
|
||||
startTime := utils.GetCurrentTime()
|
||||
|
||||
// 记录任务开始
|
||||
utils.Info("任务开始 - ID: %d, 类型: %s", task.ID, task.Type)
|
||||
|
||||
defer func() {
|
||||
tm.mu.Lock()
|
||||
delete(tm.running, task.ID)
|
||||
tm.mu.Unlock()
|
||||
|
||||
elapsedTime := time.Since(startTime)
|
||||
utils.Info("processTask: 任务 %d 处理完成,耗时: %v,清理资源", task.ID, elapsedTime)
|
||||
// 使用业务事件记录任务完成,只有异常情况才输出详细日志
|
||||
if elapsedTime > 30*time.Second {
|
||||
utils.Warn("任务处理耗时较长 - ID: %d, 类型: %s, 耗时: %v", task.ID, task.Type, elapsedTime)
|
||||
}
|
||||
|
||||
utils.Info("任务完成 - ID: %d, 类型: %s, 耗时: %v", task.ID, task.Type, elapsedTime)
|
||||
}()
|
||||
|
||||
utils.InfoWithFields(map[string]interface{}{
|
||||
|
||||
514
utils/logger.go
514
utils/logger.go
@@ -1,7 +1,6 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
@@ -10,7 +9,6 @@ import (
|
||||
"runtime"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// LogLevel 日志级别
|
||||
@@ -24,7 +22,7 @@ const (
|
||||
FATAL
|
||||
)
|
||||
|
||||
// String 返回日志级别的字符串表示
|
||||
// String 返回级别的字符串表示
|
||||
func (l LogLevel) String() string {
|
||||
switch l {
|
||||
case DEBUG:
|
||||
@@ -42,280 +40,76 @@ func (l LogLevel) String() string {
|
||||
}
|
||||
}
|
||||
|
||||
// StructuredLogEntry 结构化日志条目
|
||||
type StructuredLogEntry struct {
|
||||
Timestamp time.Time `json:"timestamp"`
|
||||
Level string `json:"level"`
|
||||
Message string `json:"message"`
|
||||
Caller string `json:"caller"`
|
||||
Module string `json:"module"`
|
||||
Fields map[string]interface{} `json:"fields,omitempty"`
|
||||
}
|
||||
|
||||
// Logger 统一日志器
|
||||
// Logger 简化的日志器
|
||||
type Logger struct {
|
||||
debugLogger *log.Logger
|
||||
infoLogger *log.Logger
|
||||
warnLogger *log.Logger
|
||||
errorLogger *log.Logger
|
||||
fatalLogger *log.Logger
|
||||
|
||||
level LogLevel
|
||||
logger *log.Logger
|
||||
file *os.File
|
||||
mu sync.Mutex
|
||||
config *LogConfig
|
||||
}
|
||||
|
||||
// LogConfig 日志配置
|
||||
type LogConfig struct {
|
||||
LogDir string // 日志目录
|
||||
LogLevel LogLevel // 日志级别
|
||||
MaxFileSize int64 // 单个日志文件最大大小(MB)
|
||||
MaxBackups int // 最大备份文件数
|
||||
MaxAge int // 日志文件最大保留天数
|
||||
EnableConsole bool // 是否启用控制台输出
|
||||
EnableFile bool // 是否启用文件输出
|
||||
EnableRotation bool // 是否启用日志轮转
|
||||
StructuredLog bool // 是否启用结构化日志格式
|
||||
}
|
||||
|
||||
// DefaultConfig 默认配置
|
||||
func DefaultConfig() *LogConfig {
|
||||
// 从环境变量获取日志级别,默认为INFO
|
||||
logLevel := getLogLevelFromEnv()
|
||||
|
||||
return &LogConfig{
|
||||
LogDir: "logs",
|
||||
LogLevel: logLevel,
|
||||
MaxFileSize: 100, // 100MB
|
||||
MaxBackups: 5,
|
||||
MaxAge: 30, // 30天
|
||||
EnableConsole: true,
|
||||
EnableFile: true,
|
||||
EnableRotation: true,
|
||||
StructuredLog: os.Getenv("STRUCTURED_LOG") == "true", // 从环境变量控制结构化日志
|
||||
}
|
||||
}
|
||||
|
||||
// getLogLevelFromEnv 从环境变量获取日志级别
|
||||
func getLogLevelFromEnv() LogLevel {
|
||||
envLogLevel := os.Getenv("LOG_LEVEL")
|
||||
envDebug := os.Getenv("DEBUG")
|
||||
|
||||
// 如果设置了DEBUG环境变量为true,则使用DEBUG级别
|
||||
if envDebug == "true" || envDebug == "1" {
|
||||
return DEBUG
|
||||
}
|
||||
|
||||
// 根据LOG_LEVEL环境变量设置日志级别
|
||||
switch strings.ToUpper(envLogLevel) {
|
||||
case "DEBUG":
|
||||
return DEBUG
|
||||
case "INFO":
|
||||
return INFO
|
||||
case "WARN", "WARNING":
|
||||
return WARN
|
||||
case "ERROR":
|
||||
return ERROR
|
||||
case "FATAL":
|
||||
return FATAL
|
||||
default:
|
||||
// 根据运行环境设置默认级别:开发环境DEBUG,生产环境INFO
|
||||
if isDevelopment() {
|
||||
return DEBUG
|
||||
}
|
||||
return INFO
|
||||
}
|
||||
}
|
||||
|
||||
// isDevelopment 判断是否为开发环境
|
||||
func isDevelopment() bool {
|
||||
env := os.Getenv("GO_ENV")
|
||||
return env == "development" || env == "dev" || env == "local" || env == "test"
|
||||
}
|
||||
|
||||
// getEnvironment 获取当前环境类型
|
||||
func (l *Logger) getEnvironment() string {
|
||||
if isDevelopment() {
|
||||
return "development"
|
||||
}
|
||||
return "production"
|
||||
mu sync.RWMutex
|
||||
}
|
||||
|
||||
var (
|
||||
globalLogger *Logger
|
||||
onceLogger sync.Once
|
||||
loggerOnce sync.Once
|
||||
)
|
||||
|
||||
// InitLogger 初始化全局日志器
|
||||
func InitLogger(config *LogConfig) error {
|
||||
// InitLogger 初始化日志器
|
||||
func InitLogger() error {
|
||||
var err error
|
||||
onceLogger.Do(func() {
|
||||
if config == nil {
|
||||
config = DefaultConfig()
|
||||
loggerOnce.Do(func() {
|
||||
globalLogger = &Logger{
|
||||
level: INFO,
|
||||
logger: log.New(os.Stdout, "", log.LstdFlags),
|
||||
}
|
||||
|
||||
globalLogger, err = NewLogger(config)
|
||||
// 创建日志目录
|
||||
logDir := "logs"
|
||||
if err = os.MkdirAll(logDir, 0755); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// 创建日志文件
|
||||
logFile := filepath.Join(logDir, "app.log")
|
||||
globalLogger.file, err = os.OpenFile(logFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// 同时输出到控制台和文件
|
||||
globalLogger.logger = log.New(io.MultiWriter(os.Stdout, globalLogger.file), "", log.LstdFlags)
|
||||
})
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// GetLogger 获取全局日志器
|
||||
func GetLogger() *Logger {
|
||||
if globalLogger == nil {
|
||||
InitLogger(nil)
|
||||
InitLogger()
|
||||
}
|
||||
return globalLogger
|
||||
}
|
||||
|
||||
// NewLogger 创建新的日志器
|
||||
func NewLogger(config *LogConfig) (*Logger, error) {
|
||||
if config == nil {
|
||||
config = DefaultConfig()
|
||||
}
|
||||
|
||||
logger := &Logger{
|
||||
config: config,
|
||||
}
|
||||
|
||||
// 创建日志目录
|
||||
if config.EnableFile {
|
||||
if err := os.MkdirAll(config.LogDir, 0755); err != nil {
|
||||
return nil, fmt.Errorf("创建日志目录失败: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// 初始化日志文件
|
||||
if err := logger.initLogFile(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 初始化日志器
|
||||
logger.initLoggers()
|
||||
|
||||
// 启动日志轮转检查
|
||||
if config.EnableRotation {
|
||||
go logger.startRotationCheck()
|
||||
}
|
||||
|
||||
// 打印日志配置信息
|
||||
logger.Info("日志系统初始化完成 - 级别: %s, 环境: %s",
|
||||
config.LogLevel.String(),
|
||||
logger.getEnvironment())
|
||||
|
||||
return logger, nil
|
||||
}
|
||||
|
||||
// initLogFile 初始化日志文件
|
||||
func (l *Logger) initLogFile() error {
|
||||
if !l.config.EnableFile {
|
||||
return nil
|
||||
}
|
||||
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
|
||||
// 关闭现有文件
|
||||
if l.file != nil {
|
||||
l.file.Close()
|
||||
}
|
||||
|
||||
// 创建新的日志文件
|
||||
logFile := filepath.Join(l.config.LogDir, fmt.Sprintf("app_%s.log", GetCurrentTime().Format("2006-01-02")))
|
||||
file, err := os.OpenFile(logFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
|
||||
if err != nil {
|
||||
return fmt.Errorf("创建日志文件失败: %v", err)
|
||||
}
|
||||
|
||||
l.file = file
|
||||
return nil
|
||||
}
|
||||
|
||||
// initLoggers 初始化各个级别的日志器
|
||||
func (l *Logger) initLoggers() {
|
||||
var writers []io.Writer
|
||||
|
||||
// 添加控制台输出
|
||||
if l.config.EnableConsole {
|
||||
writers = append(writers, os.Stdout)
|
||||
}
|
||||
|
||||
// 添加文件输出
|
||||
if l.config.EnableFile && l.file != nil {
|
||||
writers = append(writers, l.file)
|
||||
}
|
||||
|
||||
multiWriter := io.MultiWriter(writers...)
|
||||
|
||||
// 创建各个级别的日志器
|
||||
l.debugLogger = log.New(multiWriter, "[DEBUG] ", log.LstdFlags)
|
||||
l.infoLogger = log.New(multiWriter, "[INFO] ", log.LstdFlags)
|
||||
l.warnLogger = log.New(multiWriter, "[WARN] ", log.LstdFlags)
|
||||
l.errorLogger = log.New(multiWriter, "[ERROR] ", log.LstdFlags)
|
||||
l.fatalLogger = log.New(multiWriter, "[FATAL] ", log.LstdFlags)
|
||||
}
|
||||
|
||||
// log 内部日志方法
|
||||
func (l *Logger) log(level LogLevel, format string, args ...interface{}) {
|
||||
if level < l.config.LogLevel {
|
||||
if level < l.level {
|
||||
return
|
||||
}
|
||||
|
||||
// 获取调用者信息
|
||||
_, file, line, ok := runtime.Caller(2)
|
||||
if !ok {
|
||||
file = "unknown"
|
||||
line = 0
|
||||
caller := "unknown"
|
||||
if ok {
|
||||
caller = fmt.Sprintf("%s:%d", filepath.Base(file), line)
|
||||
}
|
||||
|
||||
// 提取文件名作为模块名
|
||||
fileName := filepath.Base(file)
|
||||
moduleName := strings.TrimSuffix(fileName, filepath.Ext(fileName))
|
||||
|
||||
// 格式化消息
|
||||
message := fmt.Sprintf(format, args...)
|
||||
logMessage := fmt.Sprintf("[%s] [%s] %s", level.String(), caller, message)
|
||||
|
||||
// 添加调用位置信息
|
||||
caller := fmt.Sprintf("%s:%d", fileName, line)
|
||||
l.logger.Println(logMessage)
|
||||
|
||||
if l.config.StructuredLog {
|
||||
// 结构化日志格式
|
||||
entry := StructuredLogEntry{
|
||||
Timestamp: GetCurrentTime(),
|
||||
Level: level.String(),
|
||||
Message: message,
|
||||
Caller: caller,
|
||||
Module: moduleName,
|
||||
}
|
||||
|
||||
jsonBytes, err := json.Marshal(entry)
|
||||
if err != nil {
|
||||
// 如果JSON序列化失败,回退到普通格式
|
||||
fullMessage := fmt.Sprintf("[%s] [%s:%d] %s", level.String(), fileName, line, message)
|
||||
l.logToLevel(level, fullMessage)
|
||||
return
|
||||
}
|
||||
|
||||
l.logToLevel(level, string(jsonBytes))
|
||||
} else {
|
||||
// 普通文本格式
|
||||
fullMessage := fmt.Sprintf("[%s] [%s:%d] %s", level.String(), fileName, line, message)
|
||||
l.logToLevel(level, fullMessage)
|
||||
}
|
||||
}
|
||||
|
||||
// logToLevel 根据级别输出日志
|
||||
func (l *Logger) logToLevel(level LogLevel, message string) {
|
||||
switch level {
|
||||
case DEBUG:
|
||||
l.debugLogger.Println(message)
|
||||
case INFO:
|
||||
l.infoLogger.Println(message)
|
||||
case WARN:
|
||||
l.warnLogger.Println(message)
|
||||
case ERROR:
|
||||
l.errorLogger.Println(message)
|
||||
case FATAL:
|
||||
l.fatalLogger.Println(message)
|
||||
// Fatal级别终止程序
|
||||
if level == FATAL {
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
@@ -345,162 +139,72 @@ func (l *Logger) Fatal(format string, args ...interface{}) {
|
||||
l.log(FATAL, format, args...)
|
||||
}
|
||||
|
||||
// startRotationCheck 启动日志轮转检查
|
||||
func (l *Logger) startRotationCheck() {
|
||||
ticker := time.NewTicker(1 * time.Hour) // 每小时检查一次
|
||||
defer ticker.Stop()
|
||||
|
||||
for range ticker.C {
|
||||
l.checkRotation()
|
||||
}
|
||||
// TelegramDebug Telegram调试日志
|
||||
func (l *Logger) TelegramDebug(format string, args ...interface{}) {
|
||||
l.log(DEBUG, "[TELEGRAM] "+format, args...)
|
||||
}
|
||||
|
||||
// checkRotation 检查是否需要轮转日志
|
||||
func (l *Logger) checkRotation() {
|
||||
if !l.config.EnableFile || l.file == nil {
|
||||
return
|
||||
}
|
||||
|
||||
// 检查文件大小
|
||||
fileInfo, err := l.file.Stat()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// 如果文件超过最大大小,进行轮转
|
||||
if fileInfo.Size() > l.config.MaxFileSize*1024*1024 {
|
||||
l.rotateLog()
|
||||
}
|
||||
|
||||
// 清理旧日志文件
|
||||
l.cleanOldLogs()
|
||||
// TelegramInfo Telegram信息日志
|
||||
func (l *Logger) TelegramInfo(format string, args ...interface{}) {
|
||||
l.log(INFO, "[TELEGRAM] "+format, args...)
|
||||
}
|
||||
|
||||
// rotateLog 轮转日志文件
|
||||
func (l *Logger) rotateLog() {
|
||||
// TelegramWarn Telegram警告日志
|
||||
func (l *Logger) TelegramWarn(format string, args ...interface{}) {
|
||||
l.log(WARN, "[TELEGRAM] "+format, args...)
|
||||
}
|
||||
|
||||
// TelegramError Telegram错误日志
|
||||
func (l *Logger) TelegramError(format string, args ...interface{}) {
|
||||
l.log(ERROR, "[TELEGRAM] "+format, args...)
|
||||
}
|
||||
|
||||
// DebugWithFields 带字段的调试日志
|
||||
func (l *Logger) DebugWithFields(fields map[string]interface{}, format string, args ...interface{}) {
|
||||
message := fmt.Sprintf(format, args...)
|
||||
if len(fields) > 0 {
|
||||
var fieldStrs []string
|
||||
for k, v := range fields {
|
||||
fieldStrs = append(fieldStrs, fmt.Sprintf("%s=%v", k, v))
|
||||
}
|
||||
message = fmt.Sprintf("%s [%s]", message, strings.Join(fieldStrs, ", "))
|
||||
}
|
||||
l.log(DEBUG, message)
|
||||
}
|
||||
|
||||
// InfoWithFields 带字段的信息日志
|
||||
func (l *Logger) InfoWithFields(fields map[string]interface{}, format string, args ...interface{}) {
|
||||
message := fmt.Sprintf(format, args...)
|
||||
if len(fields) > 0 {
|
||||
var fieldStrs []string
|
||||
for k, v := range fields {
|
||||
fieldStrs = append(fieldStrs, fmt.Sprintf("%s=%v", k, v))
|
||||
}
|
||||
message = fmt.Sprintf("%s [%s]", message, strings.Join(fieldStrs, ", "))
|
||||
}
|
||||
l.log(INFO, message)
|
||||
}
|
||||
|
||||
// ErrorWithFields 带字段的错误日志
|
||||
func (l *Logger) ErrorWithFields(fields map[string]interface{}, format string, args ...interface{}) {
|
||||
message := fmt.Sprintf(format, args...)
|
||||
if len(fields) > 0 {
|
||||
var fieldStrs []string
|
||||
for k, v := range fields {
|
||||
fieldStrs = append(fieldStrs, fmt.Sprintf("%s=%v", k, v))
|
||||
}
|
||||
message = fmt.Sprintf("%s [%s]", message, strings.Join(fieldStrs, ", "))
|
||||
}
|
||||
l.log(ERROR, message)
|
||||
}
|
||||
|
||||
// Close 关闭日志文件
|
||||
func (l *Logger) Close() {
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
|
||||
// 关闭当前文件
|
||||
if l.file != nil {
|
||||
l.file.Close()
|
||||
}
|
||||
|
||||
// 重命名当前日志文件
|
||||
currentLogFile := filepath.Join(l.config.LogDir, fmt.Sprintf("app_%s.log", GetCurrentTime().Format("2006-01-02")))
|
||||
backupLogFile := filepath.Join(l.config.LogDir, fmt.Sprintf("app_%s_%s.log", GetCurrentTime().Format("2006-01-02"), GetCurrentTime().Format("15-04-05")))
|
||||
|
||||
if _, err := os.Stat(currentLogFile); err == nil {
|
||||
os.Rename(currentLogFile, backupLogFile)
|
||||
}
|
||||
|
||||
// 创建新的日志文件
|
||||
l.initLogFile()
|
||||
l.initLoggers()
|
||||
}
|
||||
|
||||
// cleanOldLogs 清理旧日志文件
|
||||
func (l *Logger) cleanOldLogs() {
|
||||
if l.config.MaxAge <= 0 {
|
||||
return
|
||||
}
|
||||
|
||||
files, err := filepath.Glob(filepath.Join(l.config.LogDir, "app_*.log"))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
cutoffTime := GetCurrentTime().AddDate(0, 0, -l.config.MaxAge)
|
||||
|
||||
for _, file := range files {
|
||||
fileInfo, err := os.Stat(file)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
if fileInfo.ModTime().Before(cutoffTime) {
|
||||
os.Remove(file)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Min 返回两个整数中的较小值
|
||||
func Min(a, b int) int {
|
||||
if a < b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
// 结构化日志方法
|
||||
func (l *Logger) DebugWithFields(fields map[string]interface{}, format string, args ...interface{}) {
|
||||
l.logWithFields(DEBUG, fields, format, args...)
|
||||
}
|
||||
|
||||
func (l *Logger) InfoWithFields(fields map[string]interface{}, format string, args ...interface{}) {
|
||||
l.logWithFields(INFO, fields, format, args...)
|
||||
}
|
||||
|
||||
func (l *Logger) WarnWithFields(fields map[string]interface{}, format string, args ...interface{}) {
|
||||
l.logWithFields(WARN, fields, format, args...)
|
||||
}
|
||||
|
||||
func (l *Logger) ErrorWithFields(fields map[string]interface{}, format string, args ...interface{}) {
|
||||
l.logWithFields(ERROR, fields, format, args...)
|
||||
}
|
||||
|
||||
func (l *Logger) FatalWithFields(fields map[string]interface{}, format string, args ...interface{}) {
|
||||
l.logWithFields(FATAL, fields, format, args...)
|
||||
}
|
||||
|
||||
// logWithFields 带字段的结构化日志方法
|
||||
func (l *Logger) logWithFields(level LogLevel, fields map[string]interface{}, format string, args ...interface{}) {
|
||||
if level < l.config.LogLevel {
|
||||
return
|
||||
}
|
||||
|
||||
// 获取调用者信息
|
||||
_, file, line, ok := runtime.Caller(2)
|
||||
if !ok {
|
||||
file = "unknown"
|
||||
line = 0
|
||||
}
|
||||
|
||||
// 提取文件名作为模块名
|
||||
fileName := filepath.Base(file)
|
||||
moduleName := strings.TrimSuffix(fileName, filepath.Ext(fileName))
|
||||
|
||||
// 格式化消息
|
||||
message := fmt.Sprintf(format, args...)
|
||||
|
||||
// 添加调用位置信息
|
||||
caller := fmt.Sprintf("%s:%d", fileName, line)
|
||||
|
||||
if l.config.StructuredLog {
|
||||
// 结构化日志格式
|
||||
entry := StructuredLogEntry{
|
||||
Timestamp: GetCurrentTime(),
|
||||
Level: level.String(),
|
||||
Message: message,
|
||||
Caller: caller,
|
||||
Module: moduleName,
|
||||
Fields: fields,
|
||||
}
|
||||
|
||||
jsonBytes, err := json.Marshal(entry)
|
||||
if err != nil {
|
||||
// 如果JSON序列化失败,回退到普通格式
|
||||
fullMessage := fmt.Sprintf("[%s] [%s:%d] %s - Fields: %v", level.String(), fileName, line, message, fields)
|
||||
l.logToLevel(level, fullMessage)
|
||||
return
|
||||
}
|
||||
|
||||
l.logToLevel(level, string(jsonBytes))
|
||||
} else {
|
||||
// 普通文本格式
|
||||
fullMessage := fmt.Sprintf("[%s] [%s:%d] %s - Fields: %v", level.String(), fileName, line, message, fields)
|
||||
l.logToLevel(level, fullMessage)
|
||||
}
|
||||
}
|
||||
|
||||
// 全局便捷函数
|
||||
@@ -524,7 +228,22 @@ func Fatal(format string, args ...interface{}) {
|
||||
GetLogger().Fatal(format, args...)
|
||||
}
|
||||
|
||||
// 全局结构化日志便捷函数
|
||||
func TelegramDebug(format string, args ...interface{}) {
|
||||
GetLogger().TelegramDebug(format, args...)
|
||||
}
|
||||
|
||||
func TelegramInfo(format string, args ...interface{}) {
|
||||
GetLogger().TelegramInfo(format, args...)
|
||||
}
|
||||
|
||||
func TelegramWarn(format string, args ...interface{}) {
|
||||
GetLogger().TelegramWarn(format, args...)
|
||||
}
|
||||
|
||||
func TelegramError(format string, args ...interface{}) {
|
||||
GetLogger().TelegramError(format, args...)
|
||||
}
|
||||
|
||||
func DebugWithFields(fields map[string]interface{}, format string, args ...interface{}) {
|
||||
GetLogger().DebugWithFields(fields, format, args...)
|
||||
}
|
||||
@@ -533,14 +252,15 @@ func InfoWithFields(fields map[string]interface{}, format string, args ...interf
|
||||
GetLogger().InfoWithFields(fields, format, args...)
|
||||
}
|
||||
|
||||
func WarnWithFields(fields map[string]interface{}, format string, args ...interface{}) {
|
||||
GetLogger().WarnWithFields(fields, format, args...)
|
||||
}
|
||||
|
||||
func ErrorWithFields(fields map[string]interface{}, format string, args ...interface{}) {
|
||||
GetLogger().ErrorWithFields(fields, format, args...)
|
||||
}
|
||||
|
||||
func FatalWithFields(fields map[string]interface{}, format string, args ...interface{}) {
|
||||
GetLogger().FatalWithFields(fields, format, args...)
|
||||
// Min 返回两个整数中的较小值
|
||||
func Min(a, b int) int {
|
||||
if a < b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
|
||||
@@ -96,7 +96,8 @@ export const useCksApi = () => {
|
||||
const updateCks = (id: number, data: any) => useApiFetch(`/cks/${id}`, { method: 'PUT', body: data }).then(parseApiResponse)
|
||||
const deleteCks = (id: number) => useApiFetch(`/cks/${id}`, { method: 'DELETE' }).then(parseApiResponse)
|
||||
const refreshCapacity = (id: number) => useApiFetch(`/cks/${id}/refresh-capacity`, { method: 'POST' }).then(parseApiResponse)
|
||||
return { getCks, getCksByID, createCks, updateCks, deleteCks, refreshCapacity }
|
||||
const deleteRelatedResources = (id: number) => useApiFetch(`/cks/${id}/delete-related-resources`, { method: 'POST' }).then(parseApiResponse)
|
||||
return { getCks, getCksByID, createCks, updateCks, deleteCks, refreshCapacity, deleteRelatedResources }
|
||||
}
|
||||
|
||||
export const useTagApi = () => {
|
||||
|
||||
@@ -18,7 +18,7 @@ interface VersionResponse {
|
||||
|
||||
export const useVersion = () => {
|
||||
const versionInfo = ref<VersionInfo>({
|
||||
version: '1.3.3',
|
||||
version: '1.3.4',
|
||||
build_time: '',
|
||||
git_commit: 'unknown',
|
||||
git_branch: 'unknown',
|
||||
|
||||
@@ -64,10 +64,11 @@ const injectRawScript = (rawScriptString: string) => {
|
||||
const container = document.createElement('div');
|
||||
container.innerHTML = rawScriptString.trim();
|
||||
|
||||
// 获取解析后的 script 元素
|
||||
const script = container.querySelector('script');
|
||||
// 获取解析后的所有 script 元素
|
||||
const scripts = container.querySelectorAll('script');
|
||||
|
||||
if (script) {
|
||||
// 遍历并注入所有脚本
|
||||
scripts.forEach((script) => {
|
||||
// 创建新的 script 元素
|
||||
const newScript = document.createElement('script');
|
||||
|
||||
@@ -83,7 +84,7 @@ const injectRawScript = (rawScriptString: string) => {
|
||||
|
||||
// 插入到 DOM
|
||||
document.head.appendChild(newScript);
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "res-db-web",
|
||||
"version": "1.3.3",
|
||||
"version": "1.3.4",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
|
||||
@@ -133,6 +133,9 @@
|
||||
<span class="text-xs text-gray-500 dark:text-gray-400">
|
||||
剩余: {{ formatFileSize(Math.max(0, item.left_space)) }}
|
||||
</span>
|
||||
<span class="text-xs text-gray-500 dark:text-gray-400">
|
||||
已转存: {{ item.transferred_count || 0 }}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<!-- 备注 -->
|
||||
@@ -146,25 +149,20 @@
|
||||
<!-- 右侧操作按钮 -->
|
||||
<div class="flex items-center space-x-2 ml-4">
|
||||
<n-button size="small" :type="item.is_valid ? 'warning' : 'success'" @click="toggleStatus(item)"
|
||||
:title="item.is_valid ? '禁用账号' : '启用账号'">
|
||||
<template #icon>
|
||||
<i :class="item.is_valid ? 'fas fa-ban' : 'fas fa-check'"></i>
|
||||
</template>
|
||||
:title="item.is_valid ? '禁用账号' : '启用账号'" text>
|
||||
{{ item.is_valid ? '禁用' : '启用' }}
|
||||
</n-button>
|
||||
<n-button size="small" type="info" @click="refreshCapacity(item.id)" title="刷新容量">
|
||||
<template #icon>
|
||||
<i class="fas fa-sync-alt"></i>
|
||||
</template>
|
||||
<n-button size="small" type="info" @click="refreshCapacity(item.id)" title="刷新容量" text>
|
||||
刷新容量
|
||||
</n-button>
|
||||
<n-button size="small" type="primary" @click="editCks(item)" title="编辑账号">
|
||||
<template #icon>
|
||||
<i class="fas fa-edit"></i>
|
||||
</template>
|
||||
<n-button size="small" type="primary" @click="editCks(item)" title="编辑账号" text>
|
||||
编辑
|
||||
</n-button>
|
||||
<n-button size="small" type="error" @click="deleteCks(item.id)" title="删除账号">
|
||||
<template #icon>
|
||||
<i class="fas fa-trash"></i>
|
||||
</template>
|
||||
<n-button size="small" type="error" @click="deleteCks(item.id)" title="删除账号" text>
|
||||
删除
|
||||
</n-button>
|
||||
<n-button size="small" type="warning" @click="deleteRelatedResources(item.id)" title="删除关联资源" text>
|
||||
删除关联
|
||||
</n-button>
|
||||
</div>
|
||||
</div>
|
||||
@@ -241,9 +239,15 @@
|
||||
<template #footer>
|
||||
<div class="flex justify-end space-x-3">
|
||||
<n-button type="tertiary" @click="closeModal">
|
||||
<template #icon>
|
||||
<i class="fas fa-times"></i>
|
||||
</template>
|
||||
取消
|
||||
</n-button>
|
||||
<n-button type="primary" :loading="submitting" @click="handleSubmit">
|
||||
<template #icon>
|
||||
<i class="fas fa-check"></i>
|
||||
</template>
|
||||
{{ showEditModal ? '更新' : '创建' }}
|
||||
</n-button>
|
||||
</div>
|
||||
@@ -428,6 +432,36 @@ const deleteCks = async (id) => {
|
||||
})
|
||||
}
|
||||
|
||||
// 删除关联资源
|
||||
const deleteRelatedResources = async (id) => {
|
||||
dialog.warning({
|
||||
title: '警告',
|
||||
content: '确定要删除与此账号关联的所有资源吗?这将清空这些资源的转存信息,变为未转存状态。',
|
||||
positiveText: '确定',
|
||||
negativeText: '取消',
|
||||
draggable: true,
|
||||
onPositiveClick: async () => {
|
||||
try {
|
||||
// 调用API删除关联资源
|
||||
await cksApi.deleteRelatedResources(id)
|
||||
await fetchCks()
|
||||
notification.success({
|
||||
title: '成功',
|
||||
content: '关联资源已删除!',
|
||||
duration: 3000
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('删除关联资源失败:', error)
|
||||
notification.error({
|
||||
title: '失败',
|
||||
content: '删除关联资源失败: ' + (error.message || '未知错误'),
|
||||
duration: 3000
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// 刷新容量
|
||||
const refreshCapacity = async (id) => {
|
||||
dialog.warning({
|
||||
|
||||
@@ -150,6 +150,10 @@
|
||||
<i class="fas fa-eye mr-1"></i>
|
||||
{{ resource.view_count || 0 }}
|
||||
</span>
|
||||
<span>
|
||||
<i class="fas fa-clock mr-1"></i>
|
||||
{{ resource.updated_at }}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<div v-if="resource.tags && resource.tags.length > 0" class="mt-2">
|
||||
@@ -670,20 +674,20 @@ const handleEditSubmit = async () => {
|
||||
try {
|
||||
editing.value = true
|
||||
await editFormRef.value?.validate()
|
||||
|
||||
|
||||
await resourceApi.updateResource(editingResource.value!.id, editForm.value)
|
||||
|
||||
|
||||
notification.success({
|
||||
content: '更新成功',
|
||||
duration: 3000
|
||||
})
|
||||
|
||||
|
||||
// 更新本地数据
|
||||
const resourceId = editingResource.value?.id
|
||||
const index = resources.value.findIndex(r => r.id === resourceId)
|
||||
if (index > -1) {
|
||||
resources.value[index] = {
|
||||
...resources.value[index],
|
||||
resources.value[index] = {
|
||||
...resources.value[index],
|
||||
title: editForm.value.title,
|
||||
description: editForm.value.description,
|
||||
url: editForm.value.url,
|
||||
@@ -692,7 +696,7 @@ const handleEditSubmit = async () => {
|
||||
tag_ids: editForm.value.tag_ids
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
showEditModal.value = false
|
||||
editingResource.value = null
|
||||
} catch (error) {
|
||||
|
||||
@@ -14,13 +14,13 @@ export const useSystemConfigStore = defineStore('systemConfig', {
|
||||
// 根据上下文选择API:管理员页面使用管理员API,其他页面使用公开API
|
||||
const apiUrl = useAdminApi ? '/system/config' : '/public/system-config'
|
||||
const response = await useApiFetch(apiUrl)
|
||||
console.log('Store API响应:', response) // 调试信息
|
||||
// console.log('Store API响应:', response) // 调试信息
|
||||
|
||||
// 使用parseApiResponse正确解析API响应
|
||||
const data = parseApiResponse(response)
|
||||
console.log('Store 处理后的数据:', data) // 调试信息
|
||||
console.log('Store 自动处理状态:', data.auto_process_ready_resources)
|
||||
console.log('Store 自动转存状态:', data.auto_transfer_enabled)
|
||||
// console.log('Store 处理后的数据:', data) // 调试信息
|
||||
// console.log('Store 自动处理状态:', data.auto_process_ready_resources)
|
||||
// console.log('Store 自动转存状态:', data.auto_transfer_enabled)
|
||||
|
||||
this.config = data
|
||||
this.initialized = true
|
||||
|
||||
Reference in New Issue
Block a user