fix: 统一所有平台使用同一userID,TG和QQ数据互通
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -3,7 +3,6 @@ package qq
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"hash/fnv"
|
||||
"log"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -18,6 +17,9 @@ import (
|
||||
"github.com/tencent-connect/botgo/token"
|
||||
)
|
||||
|
||||
// DefaultUserID 统一用户ID,使所有平台共享同一份账本
|
||||
const DefaultUserID int64 = 1
|
||||
|
||||
type QQBot struct {
|
||||
api openapi.OpenAPI
|
||||
finance *service.FinanceService
|
||||
@@ -34,13 +36,6 @@ func NewQQBot(appID string, secret string, finance *service.FinanceService) *QQB
|
||||
}
|
||||
}
|
||||
|
||||
// hashUserID 将 QQ 的字符串用户标识转为 int64
|
||||
func hashUserID(authorID string) int64 {
|
||||
h := fnv.New64a()
|
||||
h.Write([]byte(authorID))
|
||||
return int64(h.Sum64())
|
||||
}
|
||||
|
||||
func (b *QQBot) Start(ctx context.Context) {
|
||||
// 创建 token source 并启动自动刷新
|
||||
tokenSource := token.NewQQBotTokenSource(b.credentials)
|
||||
@@ -89,7 +84,6 @@ func isCommand(text string, keywords ...string) bool {
|
||||
|
||||
// processAndReply 通用记账处理
|
||||
func (b *QQBot) processAndReply(userID string, content string) string {
|
||||
uid := hashUserID(userID)
|
||||
text := strings.TrimSpace(message.ETLInput(content))
|
||||
if text == "" {
|
||||
return ""
|
||||
@@ -111,7 +105,7 @@ func (b *QQBot) processAndReply(userID string, content string) string {
|
||||
"• 帮助 — 本帮助信息"
|
||||
|
||||
case isCommand(text, "查看", "记录", "列表", "list", "/list", "最近"):
|
||||
items, err := b.finance.GetTransactions(uid, 10)
|
||||
items, err := b.finance.GetTransactions(DefaultUserID, 10)
|
||||
if err != nil {
|
||||
return "❌ 查询失败"
|
||||
}
|
||||
@@ -126,7 +120,7 @@ func (b *QQBot) processAndReply(userID string, content string) string {
|
||||
return sb.String()
|
||||
|
||||
case isCommand(text, "今日", "今天", "today"):
|
||||
items, err := b.finance.GetTransactionsByDate(uid, today)
|
||||
items, err := b.finance.GetTransactionsByDate(DefaultUserID, today)
|
||||
if err != nil {
|
||||
return "❌ 查询失败"
|
||||
}
|
||||
@@ -144,7 +138,7 @@ func (b *QQBot) processAndReply(userID string, content string) string {
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
amount, category, err := b.finance.AddTransaction(uid, text)
|
||||
amount, category, err := b.finance.AddTransaction(DefaultUserID, text)
|
||||
if err != nil {
|
||||
log.Printf("QQ记账失败 user=%s: %v", userID, err)
|
||||
return "❌ 记账失败,请稍后重试"
|
||||
|
||||
Reference in New Issue
Block a user