🎨 优化支付回调

This commit is contained in:
2025-09-08 20:09:04 +08:00
parent f5948a366b
commit 2bc3eb26fd
3 changed files with 45 additions and 14 deletions

View File

@@ -14,14 +14,15 @@ type User struct {
Status int8 `gorm:"column:status;default:1;NOT NULL;comment:'用户状态 0 封禁 1 正常'" json:"status" form:"status"`
//邀请码
InviteCode *string `json:"invite_code" form:"invite_code" gorm:"type:varchar(255) comment '用户专属邀请码'"`
Balance float32 `json:"balance" form:"balance" gorm:"type:decimal(10,2);comment:学员余额"`
Balance float64 `json:"balance" form:"balance" gorm:"type:decimal(10,2);comment:学员余额"`
CommenderId int `json:"commender_id" form:"commender_id" gorm:"comment:推荐人ID"`
UserLabel int64 `json:"user_label" form:"user_label" gorm:"comment:用户标签 1 普通用户 2 Vip 3 Svip 4 到期会员"`
UserType int8 `gorm:"column:user_type;default:1;NOT NULL;comment:'用户类型 1 用户 2 讲师'" json:"user_type" form:"user_type" `
IsVip int8 `gorm:"column:is_vip;default:0;NOT NULL;comment:'是否是VIP 0 否 1 是'" json:"is_vip" form:"is_vip"`
VipExpireTime string `json:"vip_expire_time" form:"vip_expire_time" gorm:"comment:VIP过期时间"`
//权重
Weight int `json:"weight" form:"weight" gorm:"comment:用户权重"`
Weight int `json:"weight" form:"weight" gorm:"comment:用户权重"`
ExpectRate int `json:"expect_rate" form:"expect_rate" gorm:"comment:讲师分成比例"`
}
func (User) TableName() string {

View File

@@ -7,3 +7,4 @@ import (
type RouterGroup struct{ UserRouter }
var userApi = api.ApiGroupApp.UserApiGroup
var teacherVipApi = api.ApiGroupApp.AppApiGroup.TeacherVip

View File

@@ -203,8 +203,30 @@ func NotifyHandle(ctx *gin.Context) error {
}
order.Status = 2 // 设置订单状态为已支付
// 全站vip订单
if order.OrderType == 2 {
switch order.OrderType {
case 1: // 课程订单
err = global.GVA_DB.Save(&order).Error
if err != nil {
global.GVA_LOG.Error("更新订单状态失败", zap.Error(err))
return nil
}
// 计算分成比例,按比例增加讲师余额
teacher := user.User{}
err = global.GVA_DB.Model(&user.User{}).Where("id = ?", order.TeacherId).First(&teacher).Error
if err != nil {
global.GVA_LOG.Error("查询讲师信息失败", zap.Error(err))
return err
}
// 计算分成金额
amount := float64(order.Price) * float64(teacher.ExpectRate) / 100.0
teacher.Balance = teacher.Balance + amount
err = global.GVA_DB.Save(&teacher).Error
if err != nil {
global.GVA_LOG.Error("更新讲师余额失败", zap.Error(err))
return err
}
case 2: // 全站VIP订单
userInfo := user.User{}
err = global.GVA_DB.Model(&user.User{}).Where("id = ?", order.UserId).First(&userInfo).Error
if err != nil {
@@ -239,11 +261,7 @@ func NotifyHandle(ctx *gin.Context) error {
global.GVA_LOG.Error("更新用户会员状态失败", zap.Error(err))
return nil
}
}
// 如果是讲师包月订单,更新讲师的会员状态
if order.OrderType == 3 {
// 逗号分割字符串
case 3: // 讲师包月订单
ids := strings.Split(order.TeacherVipId, ",")
for _, id := range ids {
teacherVip := app.UserTeacherVip{}
@@ -260,13 +278,24 @@ func NotifyHandle(ctx *gin.Context) error {
return err
}
}
// 计算分成比例,按比例增加讲师余额
teacher := user.User{}
err = global.GVA_DB.Model(&user.User{}).Where("id = ?", order.TeacherId).First(&teacher).Error
if err != nil {
global.GVA_LOG.Error("查询讲师信息失败", zap.Error(err))
return err
}
// 计算分成金额
amount := float64(order.Price) * float64(teacher.ExpectRate) / 100.0
teacher.Balance = teacher.Balance + amount
err = global.GVA_DB.Save(&teacher).Error
if err != nil {
global.GVA_LOG.Error("更新讲师余额失败", zap.Error(err))
return err
}
}
err = global.GVA_DB.Save(&order).Error
if err != nil {
global.GVA_LOG.Error("更新订单状态失败", zap.Error(err))
return nil
}
log.Printf("订单号:%s 支付成功", transaction.OutTradeNo)
} else {
// 因为微信这个回调不存在订单号,所以可以告诉微信我还没处理成功,等会它会重新发起通知