103 lines
2.8 KiB
Go

package main
import (
"context"
"fmt"
"time"
"bindbox-game/configs"
"bindbox-game/internal/pkg/notify"
gormmysql "gorm.io/driver/mysql"
"gorm.io/gorm"
gormlogger "gorm.io/gorm/logger"
)
func main() {
// 配置会在 init 时自动加载
c := configs.Get()
fmt.Printf("========== 微信通知配置检查 ==========\n")
fmt.Printf("静态配置 (configs):\n")
fmt.Printf(" AppID: %s\n", maskStr(c.Wechat.AppID))
fmt.Printf(" AppSecret: %s\n", maskStr(c.Wechat.AppSecret))
fmt.Printf(" LotteryResultTemplateID: %s\n", c.Wechat.LotteryResultTemplateID)
// 连接数据库检查 system_configs
dsn := "root:bindbox2025kdy@tcp(106.54.232.2:3306)/bindbox_game?charset=utf8mb4&parseTime=True&loc=Local"
db, err := gorm.Open(gormmysql.Open(dsn), &gorm.Config{Logger: gormlogger.Default.LogMode(gormlogger.Silent)})
if err != nil {
panic("failed to connect database: " + err.Error())
}
// 检查 system_configs 中的模板 ID
type SystemConfig struct {
ConfigKey string
ConfigValue string
}
var cfg SystemConfig
err = db.Table("system_configs").Where("config_key = ?", "wechat.lottery_result_template_id").First(&cfg).Error
if err == nil {
fmt.Printf("\n动态配置 (system_configs):\n")
fmt.Printf(" wechat.lottery_result_template_id: %s\n", cfg.ConfigValue)
} else {
fmt.Printf("\n动态配置 (system_configs): 未配置 wechat.lottery_result_template_id\n")
fmt.Println("将使用静态配置的模板 ID")
}
// 确定要使用的模板 ID
templateID := c.Wechat.LotteryResultTemplateID
if cfg.ConfigValue != "" {
templateID = cfg.ConfigValue
}
if templateID == "" {
fmt.Println("\n❌ LotteryResultTemplateID 未配置!")
return
}
fmt.Printf("\n使用的模板 ID: %s\n", templateID)
// 获取一个有 openid 的用户进行测试
type User struct {
ID int64
Openid string
}
var user User
if err := db.Table("users").Where("openid != ''").First(&user).Error; err != nil {
fmt.Printf("\n❌ 没有找到有 openid 的用户: %v\n", err)
return
}
fmt.Printf("测试用户: ID=%d, Openid=%s\n", user.ID, maskStr(user.Openid))
// 尝试发送通知
fmt.Println("\n========== 发送测试通知 ==========")
notifyCfg := &notify.WechatNotifyConfig{
AppID: c.Wechat.AppID,
AppSecret: c.Wechat.AppSecret,
LotteryResultTemplateID: templateID,
}
err = notify.SendLotteryResultNotification(
context.Background(),
notifyCfg,
user.Openid,
"测试活动名称",
[]string{"测试奖品A", "测试奖品B"},
"TEST_ORDER_001",
time.Now(),
)
if err != nil {
fmt.Printf("\n❌ 发送失败: %v\n", err)
} else {
fmt.Println("\n✅ 发送成功!请检查微信是否收到通知。")
}
}
func maskStr(s string) string {
if len(s) <= 8 {
return s
}
return s[:4] + "****" + s[len(s)-4:]
}