fix: 统一所有平台使用同一userID,TG和QQ数据互通

This commit is contained in:
2026-02-15 07:03:00 +08:00
parent 0167b2ffbf
commit ebe8d92c75
2 changed files with 13 additions and 17 deletions

View File

@@ -12,6 +12,9 @@ import (
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
// DefaultUserID 统一用户ID使所有平台共享同一份账本
const DefaultUserID int64 = 1
type TGBot struct {
api *tgbotapi.BotAPI
finance *service.FinanceService
@@ -53,7 +56,6 @@ func (b *TGBot) Start(ctx context.Context) {
func (b *TGBot) handleMessage(msg *tgbotapi.Message) {
text := msg.Text
chatID := msg.Chat.ID
userID := msg.From.ID
var reply string
@@ -66,7 +68,7 @@ func (b *TGBot) handleMessage(msg *tgbotapi.Message) {
case text == "/today":
today := time.Now().Format("2006-01-02")
items, err := b.finance.GetTransactionsByDate(userID, today)
items, err := b.finance.GetTransactionsByDate(DefaultUserID, today)
if err != nil {
reply = "❌ 查询失败"
} else if len(items) == 0 {
@@ -84,7 +86,7 @@ func (b *TGBot) handleMessage(msg *tgbotapi.Message) {
}
case text == "/list":
items, err := b.finance.GetTransactions(userID, 10)
items, err := b.finance.GetTransactions(DefaultUserID, 10)
if err != nil {
reply = "❌ 查询失败"
} else if len(items) == 0 {
@@ -103,10 +105,10 @@ func (b *TGBot) handleMessage(msg *tgbotapi.Message) {
default:
// 记账逻辑
amount, category, err := b.finance.AddTransaction(userID, text)
amount, category, err := b.finance.AddTransaction(DefaultUserID, text)
if err != nil {
reply = "❌ 记账失败,请稍后重试"
log.Printf("记账失败 user=%d: %v", userID, err)
log.Printf("记账失败: %v", err)
} else if amount == 0 {
reply = "📍 没看到金额,这笔花了多少钱?"
} else {