fix promotion

This commit is contained in:
liangzai 2025-06-20 15:12:34 +08:00
parent c062908aae
commit 9aa6913883
3 changed files with 45 additions and 4 deletions

View File

@ -143,7 +143,26 @@ func (l *PaymentLogic) QueryOrderPayment(req *types.PaymentReq, session sqlx.Ses
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "生成订单, 获取保存订单ID失败: %+v", lastInsertIdErr)
}
orderID = insertedOrderID
promoteKey, ok := l.ctx.Value("promoteKey").(string)
if ok && promoteKey != "" {
url := fmt.Sprintf("%s/%s", l.svcCtx.Config.AdminPromotion.URLDomain, promoteKey)
promoteLink, err := l.svcCtx.AdminPromotionLinkModel.FindOneByUrl(l.ctx, url)
if err != nil && !errors.Is(err, model.ErrNotFound) {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "生成订单, 查找推广链接失败: %+v", err)
}
if promoteLink != nil {
promoteOrder := &model.AdminPromotionOrder{
OrderId: orderID,
LinkId: promoteLink.Id,
UserId: userID,
AdminUserId: promoteLink.AdminUserId,
}
_, insertPromoteOrderErr := l.svcCtx.AdminPromotionOrderModel.Insert(l.ctx, nil, promoteOrder)
if insertPromoteOrderErr != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "生成订单, 保存推广订单失败: %+v", insertPromoteOrderErr)
}
}
}
if data.AgentIdentifier != "" {
agent, parsingErr := l.agentParsing(data.AgentIdentifier)
if parsingErr != nil {

View File

@ -7,19 +7,22 @@ import (
const (
PlatformKey = "X-Platform"
PromoteKey = "X-Promote-Key"
)
func GlobalSourceInterceptor(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// 获取请求头 X-Platform 的值
platform := r.Header.Get(PlatformKey)
promoteValue := r.Header.Get(PromoteKey)
// 将值放入新的 context 中
ctx := r.Context()
if platform != "" {
ctx = context.WithValue(ctx, "platform", platform)
}
if promoteValue != "" {
ctx = context.WithValue(ctx, "promoteKey", promoteValue)
}
// 通过 r.WithContext 将更新后的 ctx 传递给后续的处理函数
r = r.WithContext(ctx)

View File

@ -5,6 +5,7 @@ import (
"database/sql"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"os"
"regexp"
@ -139,7 +140,10 @@ func (l *PaySuccessNotifyUserHandler) ProcessTask(ctx context.Context, t *asynq.
if err != nil {
return l.handleError(ctx, err, order, query)
}
err = l.promotionOrderStats(ctx, order)
if err != nil {
logx.Errorf("处理推广订单统计失败订单ID: %d, 错误: %v", order.Id, err)
}
_, delErr := l.svcCtx.Redis.DelCtx(ctx, redisKey)
if delErr != nil {
logx.Errorf("删除Redis缓存失败但任务已成功处理订单ID: %d, 错误: %v", order.Id, delErr)
@ -296,6 +300,21 @@ func (l *PaySuccessNotifyUserHandler) createNullString(value string) sql.NullStr
}
}
// 处理推广订单统计
func (l *PaySuccessNotifyUserHandler) promotionOrderStats(ctx context.Context, order *model.Order) error {
promotionOrder, err := l.svcCtx.AdminPromotionOrderModel.FindOneByOrderId(ctx, order.Id)
if err != nil && !errors.Is(err, model.ErrNotFound) {
return fmt.Errorf("获取推广订单失败: %+v", err)
}
if promotionOrder != nil {
err = l.svcCtx.AdminPromotionLinkStatsService.UpdatePaymentStats(ctx, promotionOrder.LinkId, float64(order.Amount))
if err != nil {
return fmt.Errorf("更新推广链接支付统计失败: %+v", err)
}
}
return nil
}
// desensitizeParams 对敏感数据进行脱敏处理
func (l *PaySuccessNotifyUserHandler) desensitizeParams(data []byte) ([]byte, error) {
// 解析JSON数据到map