feat: channels/audit UI unify, apply flow hardening, bump v1.1.12

This commit is contained in:
2026-03-10 03:32:40 +08:00
parent 52b0d742a7
commit 8b2557b2bf
15 changed files with 2311 additions and 262 deletions

View File

@@ -9,8 +9,10 @@ import (
xchart "xiaji-go/internal/chart"
"xiaji-go/internal/service"
"xiaji-go/models"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"gorm.io/gorm"
)
// DefaultUserID 统一用户ID使所有平台共享同一份账本
@@ -19,14 +21,15 @@ const DefaultUserID int64 = 1
type TGBot struct {
api *tgbotapi.BotAPI
finance *service.FinanceService
db *gorm.DB
}
func NewTGBot(token string, finance *service.FinanceService) (*TGBot, error) {
func NewTGBot(db *gorm.DB, token string, finance *service.FinanceService) (*TGBot, error) {
bot, err := tgbotapi.NewBotAPI(token)
if err != nil {
return nil, err
}
return &TGBot{api: bot, finance: finance}, nil
return &TGBot{api: bot, finance: finance, db: db}, nil
}
func (b *TGBot) Start(ctx context.Context) {
@@ -49,11 +52,29 @@ func (b *TGBot) Start(ctx context.Context) {
if update.Message == nil || update.Message.Text == "" {
continue
}
eventID := fmt.Sprintf("tg:%d", update.UpdateID)
if b.isDuplicate(eventID) {
continue
}
log.Printf("📩 inbound platform=telegram event=%s chat=%d user=%d text=%q", eventID, update.Message.Chat.ID, update.Message.From.ID, strings.TrimSpace(update.Message.Text))
b.handleMessage(update.Message)
}
}
}
func (b *TGBot) isDuplicate(eventID string) bool {
if b.db == nil || strings.TrimSpace(eventID) == "" {
return false
}
var existed models.MessageDedup
if err := b.db.Where("platform = ? AND event_id = ?", "telegram", eventID).First(&existed).Error; err == nil {
return true
}
_ = b.db.Create(&models.MessageDedup{Platform: "telegram", EventID: eventID, ProcessedAt: time.Now()}).Error
return false
}
func (b *TGBot) handleMessage(msg *tgbotapi.Message) {
text := msg.Text
chatID := msg.Chat.ID
@@ -113,7 +134,6 @@ func (b *TGBot) handleMessage(msg *tgbotapi.Message) {
reply = "❓ 未知命令,输入 /help 查看帮助"
default:
// 记账逻辑
amount, category, err := b.finance.AddTransaction(DefaultUserID, text)
if err != nil {
reply = "❌ 记账失败,请稍后重试"
@@ -132,7 +152,6 @@ func (b *TGBot) handleMessage(msg *tgbotapi.Message) {
}
}
// sendMonthlyChart 发送本月分类饼图
func (b *TGBot) sendMonthlyChart(chatID int64) {
now := time.Now()
dateFrom := now.Format("2006-01") + "-01"
@@ -154,7 +173,6 @@ func (b *TGBot) sendMonthlyChart(chatID int64) {
return
}
// 计算总计文字
var total int64
var totalCount int
for _, s := range stats {
@@ -170,7 +188,6 @@ func (b *TGBot) sendMonthlyChart(chatID int64) {
}
}
// sendWeeklyChart 发送近7天每日消费柱状图
func (b *TGBot) sendWeeklyChart(chatID int64) {
now := time.Now()
dateFrom := now.AddDate(0, 0, -6).Format("2006-01-02")
@@ -192,7 +209,6 @@ func (b *TGBot) sendWeeklyChart(chatID int64) {
return
}
// 总计
var total int64
var totalCount int
for _, s := range stats {