x
Build docker and publish / build (20.15.1) (push) Successful in 8m20s

This commit is contained in:
2026-03-28 09:06:24 -07:00
parent 2db2bc0860
commit 507ee16a30
4 changed files with 22 additions and 246 deletions
-143
View File
@@ -7,7 +7,6 @@ import (
"encoding/json"
"errors"
"fmt"
"strconv"
"time"
"github.com/perfect-panel/server/internal/logic/admin/group"
@@ -15,10 +14,8 @@ import (
"github.com/perfect-panel/server/pkg/constant"
"github.com/perfect-panel/server/pkg/logger"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/google/uuid"
"github.com/hibiken/asynq"
"github.com/perfect-panel/server/internal/logic/telegram"
"github.com/perfect-panel/server/internal/model/order"
internaltypes "github.com/perfect-panel/server/internal/types"
"github.com/perfect-panel/server/internal/model/redemption"
@@ -333,9 +330,6 @@ func (l *ActivateOrderLogic) NewPurchase(ctx context.Context, orderInfo *order.O
// Clear cache
l.clearServerCache(ctx, sub)
// Send notifications
l.sendNotifications(ctx, orderInfo, userInfo, sub, userSub, telegram.PurchaseNotify)
logger.WithContext(ctx).Info("Insert user subscribe success")
return nil
}
@@ -889,9 +883,6 @@ func (l *ActivateOrderLogic) Renewal(ctx context.Context, orderInfo *order.Order
// Handle commission
go l.handleCommission(context.Background(), userInfo, orderInfo)
// Send notifications
l.sendNotifications(ctx, orderInfo, userInfo, sub, userSub, telegram.RenewalNotify)
return nil
}
@@ -1041,9 +1032,6 @@ func (l *ActivateOrderLogic) ResetTraffic(ctx context.Context, orderInfo *order.
logger.WithContext(ctx).Error("[Order Queue]Insert reset subscribe log failed", logger.Field("error", err.Error()))
}
// Send notifications
l.sendNotifications(ctx, orderInfo, userInfo, sub, userSub, telegram.ResetTrafficNotify)
return nil
}
@@ -1108,140 +1096,9 @@ func (l *ActivateOrderLogic) Recharge(ctx context.Context, orderInfo *order.Orde
return err
}
// Send notifications
l.sendRechargeNotifications(ctx, orderInfo, userInfo)
return nil
}
// sendNotifications sends both user and admin notifications for order completion
func (l *ActivateOrderLogic) sendNotifications(ctx context.Context, orderInfo *order.Order, userInfo *user.User, sub *subscribe.Subscribe, userSub *user.Subscribe, notifyType string) {
// Send user notification
if telegramId, ok := findTelegram(userInfo); ok {
templateData := l.buildUserNotificationData(orderInfo, sub, userSub)
if text, err := tool.RenderTemplateToString(notifyType, templateData); err == nil {
l.sendUserNotifyWithTelegram(telegramId, text)
}
}
// Send admin notification
adminData := l.buildAdminNotificationData(orderInfo, sub)
if text, err := tool.RenderTemplateToString(telegram.AdminOrderNotify, adminData); err == nil {
l.sendAdminNotifyWithTelegram(ctx, text)
}
}
// sendRechargeNotifications sends specific notifications for balance recharge orders
func (l *ActivateOrderLogic) sendRechargeNotifications(ctx context.Context, orderInfo *order.Order, userInfo *user.User) {
// Send user notification
if telegramId, ok := findTelegram(userInfo); ok {
templateData := map[string]string{
"OrderAmount": fmt.Sprintf("%.2f", float64(orderInfo.Price)/100),
"PaymentMethod": orderInfo.Method,
"Time": orderInfo.CreatedAt.Format("2006-01-02 15:04:05"),
"Balance": fmt.Sprintf("%.2f", float64(userInfo.Balance)/100),
}
if text, err := tool.RenderTemplateToString(telegram.RechargeNotify, templateData); err == nil {
l.sendUserNotifyWithTelegram(telegramId, text)
}
}
// Send admin notification
adminData := map[string]string{
"OrderNo": orderInfo.OrderNo,
"TradeNo": orderInfo.TradeNo,
"OrderAmount": fmt.Sprintf("%.2f", float64(orderInfo.Price)/100),
"SubscribeName": "余额充值",
"OrderStatus": "已支付",
"OrderTime": orderInfo.CreatedAt.Format("2006-01-02 15:04:05"),
"PaymentMethod": orderInfo.Method,
}
if text, err := tool.RenderTemplateToString(telegram.AdminOrderNotify, adminData); err == nil {
l.sendAdminNotifyWithTelegram(ctx, text)
}
}
// buildUserNotificationData creates template data for user notifications
func (l *ActivateOrderLogic) buildUserNotificationData(orderInfo *order.Order, sub *subscribe.Subscribe, userSub *user.Subscribe) map[string]string {
data := map[string]string{
"OrderNo": orderInfo.OrderNo,
"SubscribeName": sub.Name,
"OrderAmount": fmt.Sprintf("%.2f", float64(orderInfo.Price)/100),
}
if userSub != nil {
data["ExpireTime"] = userSub.ExpireTime.Format("2006-01-02 15:04:05")
data["ResetTime"] = time.Now().Format("2006-01-02 15:04:05")
}
return data
}
// buildAdminNotificationData creates template data for admin notifications
func (l *ActivateOrderLogic) buildAdminNotificationData(orderInfo *order.Order, sub *subscribe.Subscribe) map[string]string {
subscribeName := sub.Name
if orderInfo.Type == OrderTypeResetTraffic {
subscribeName = "流量重置"
}
return map[string]string{
"OrderNo": orderInfo.OrderNo,
"TradeNo": orderInfo.TradeNo,
"SubscribeName": subscribeName,
"OrderAmount": fmt.Sprintf("%.2f", float64(orderInfo.Price)/100),
"OrderStatus": "已支付",
"OrderTime": orderInfo.CreatedAt.Format("2006-01-02 15:04:05"),
"PaymentMethod": orderInfo.Method,
}
}
// sendUserNotifyWithTelegram sends a notification message to a user via Telegram
func (l *ActivateOrderLogic) sendUserNotifyWithTelegram(chatId int64, text string) {
if l.svc.TelegramBot == nil {
return
}
msg := tgbotapi.NewMessage(chatId, text)
msg.ParseMode = "markdown"
if _, err := l.svc.TelegramBot.Send(msg); err != nil {
logger.Error("Send telegram user message failed", logger.Field("error", err.Error()))
}
}
// sendAdminNotifyWithTelegram sends a notification message to all admin users via Telegram
func (l *ActivateOrderLogic) sendAdminNotifyWithTelegram(ctx context.Context, text string) {
if l.svc.TelegramBot == nil {
return
}
admins, err := l.svc.UserModel.QueryAdminUsers(ctx)
if err != nil {
logger.WithContext(ctx).Error("Query admin users failed", logger.Field("error", err.Error()))
return
}
for _, admin := range admins {
if telegramId, ok := findTelegram(admin); ok {
msg := tgbotapi.NewMessage(telegramId, text)
msg.ParseMode = "markdown"
if _, err := l.svc.TelegramBot.Send(msg); err != nil {
logger.WithContext(ctx).Error("Send telegram admin message failed", logger.Field("error", err.Error()))
}
}
}
}
// findTelegram extracts Telegram chat ID from user authentication methods.
// Returns the chat ID and a boolean indicating if Telegram auth was found.
func findTelegram(u *user.User) (int64, bool) {
for _, item := range u.AuthMethods {
if item.AuthType == "telegram" {
if telegramId, err := strconv.ParseInt(item.AuthIdentifier, 10, 64); err == nil {
return telegramId, true
}
}
}
return 0, false
}
// RedemptionActivate handles redemption code activation including subscription creation,
// redemption record creation, used count update, cache clearing, and notifications
func (l *ActivateOrderLogic) RedemptionActivate(ctx context.Context, orderInfo *order.Order) error {