🎨 优化支付回调

This commit is contained in:
2025-10-11 16:16:12 +08:00
parent 91ec3a2601
commit 86c7d443cb
2 changed files with 78 additions and 22 deletions

View File

@@ -211,17 +211,45 @@ func (s *OrderService) BalancePay(p request.BalancePay) error {
ids := strings.Split(order.TeacherVipId, ",")
for _, id := range ids {
teacherVip := app.UserTeacherVip{}
teacherVip.TeacherId = uint(order.TeacherId)
// 将id转为uint
teacherVipId, _ := strconv.ParseUint(id, 10, 64)
teacherVip.TeacherVipId = uint(teacherVipId)
teacherVip.UserId = uint(order.UserId)
teacherVip.ExpireAt = time.Now().AddDate(0, 1, 0).Format("2006-01-02 15:04:05") // 会员有效期一个月
teacherVip.IsExpire = 1 // 设置为未过期
err = global.GVA_DB.Create(&teacherVip).Error
if err != nil {
global.GVA_LOG.Error("购买讲师会员回调处理失败:", zap.Error(err))
return err
err = global.GVA_DB.Model(&app.UserTeacherVip{}).
Where("teacher_id = ? AND user_id = ? AND teacher_vip_id = ?", order.TeacherId, order.UserId, id).
Order("id desc"). // 取最新一条
First(&teacherVip).Error
now := time.Now()
var newExpireAt time.Time
if err == nil {
// 找到记录,判断是否过期
expireTime, _ := time.Parse("2006-01-02 15:04:05", teacherVip.ExpireAt)
if teacherVip.IsExpire == 1 && expireTime.After(now) {
// 未过期,在原有基础上加一个月
newExpireAt = expireTime.AddDate(0, 1, 0)
} else {
// 已过期,从当前时间加一个月
newExpireAt = now.AddDate(0, 1, 0)
}
teacherVip.ExpireAt = newExpireAt.Format("2006-01-02 15:04:05")
teacherVip.IsExpire = 1 // 设置为未过期
err = global.GVA_DB.Save(&teacherVip).Error
if err != nil {
global.GVA_LOG.Error("更新用户讲师会员信息失败", zap.Error(err))
return err
}
} else {
// 没有购买过,直接新建
teacherVip := app.UserTeacherVip{
TeacherId: uint(order.TeacherId),
UserId: uint(order.UserId),
TeacherVipId: func() uint { v, _ := strconv.ParseUint(id, 10, 64); return uint(v) }(),
ExpireAt: now.AddDate(0, 1, 0).Format("2006-01-02 15:04:05"),
IsExpire: 1,
}
err = global.GVA_DB.Create(&teacherVip).Error
if err != nil {
global.GVA_LOG.Error("购买讲师会员回调处理失败:", zap.Error(err))
return err
}
}
}
// 计算分成比例,按比例增加讲师余额

View File

@@ -274,17 +274,45 @@ func NotifyHandle(ctx *gin.Context) error {
ids := strings.Split(order.TeacherVipId, ",")
for _, id := range ids {
teacherVip := app.UserTeacherVip{}
teacherVip.TeacherId = uint(order.TeacherId)
// 将id转为uint
teacherVipId, _ := strconv.ParseUint(id, 10, 64)
teacherVip.TeacherVipId = uint(teacherVipId)
teacherVip.UserId = uint(order.UserId)
teacherVip.ExpireAt = time.Now().AddDate(0, 1, 0).Format("2006-01-02 15:04:05") // 会员有效期一个月
teacherVip.IsExpire = 1 // 设置为未过期
err = global.GVA_DB.Create(&teacherVip).Error
if err != nil {
global.GVA_LOG.Error("购买讲师会员回调处理失败:", zap.Error(err))
return err
err = global.GVA_DB.Model(&app.UserTeacherVip{}).
Where("teacher_id = ? AND user_id = ? AND teacher_vip_id = ?", order.TeacherId, order.UserId, id).
Order("id desc"). // 取最新一条
First(&teacherVip).Error
now := time.Now()
var newExpireAt time.Time
if err == nil {
// 找到记录,判断是否过期
expireTime, _ := time.Parse("2006-01-02 15:04:05", teacherVip.ExpireAt)
if teacherVip.IsExpire == 1 && expireTime.After(now) {
// 未过期,在原有基础上加一个月
newExpireAt = expireTime.AddDate(0, 1, 0)
} else {
// 已过期,从当前时间加一个月
newExpireAt = now.AddDate(0, 1, 0)
}
teacherVip.ExpireAt = newExpireAt.Format("2006-01-02 15:04:05")
teacherVip.IsExpire = 1 // 设置为未过期
err = global.GVA_DB.Save(&teacherVip).Error
if err != nil {
global.GVA_LOG.Error("更新用户讲师会员信息失败", zap.Error(err))
return err
}
} else {
// 没有购买过,直接新建
teacherVip := app.UserTeacherVip{
TeacherId: uint(order.TeacherId),
UserId: uint(order.UserId),
TeacherVipId: func() uint { v, _ := strconv.ParseUint(id, 10, 64); return uint(v) }(),
ExpireAt: now.AddDate(0, 1, 0).Format("2006-01-02 15:04:05"),
IsExpire: 1,
}
err = global.GVA_DB.Create(&teacherVip).Error
if err != nil {
global.GVA_LOG.Error("购买讲师会员回调处理失败:", zap.Error(err))
return err
}
}
}