fix: 修复数据库配置和事务一致性问题
数据库配置: - SQLite 开启 WAL 模式,读写可并发 - 设置 busy_timeout=5000ms 避免锁超时 - 设置 synchronous=NORMAL 平衡性能和安全 - SQLite MaxOpenConns 设为 1 避免并发写入冲突 事务修复: - auth.go: 注册流程(用户+设备+会话)加入事务 - auth.go: 密码重置(修改密码+标记验证码)加入事务 - cards.go: 卡密使用加入事务,补充 UsedAt 字段 - cards.go: 代理商批量生成(扣款+生成卡密)加入事务 - extension.go: 充值(修改余额+记录)加入事务 - extension.go: 扣费(修改余额+记录)加入事务 - storage_config.go: 设置默认存储加入事务
This commit is contained in:
@@ -272,7 +272,15 @@ func handleRechargeUser(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if err := database.DB.Save(&user).Error; err != nil {
|
||||
tx := database.DB.Begin()
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
tx.Rollback()
|
||||
}
|
||||
}()
|
||||
|
||||
if err := tx.Save(&user).Error; err != nil {
|
||||
tx.Rollback()
|
||||
service.LogVerification(c, &app.ID, &user.ID, "extension_recharge_failed", fmt.Sprintf("扩展API充值失败: 保存失败 - %s", user.Username), "", err)
|
||||
response.Error(c, 500, "充值失败")
|
||||
return
|
||||
@@ -286,7 +294,13 @@ func handleRechargeUser(c *gin.Context) {
|
||||
PaymentType: "extension_api",
|
||||
Remark: req.Description,
|
||||
}
|
||||
database.DB.Create(&record)
|
||||
if err := tx.Create(&record).Error; err != nil {
|
||||
tx.Rollback()
|
||||
response.Error(c, 500, "充值失败")
|
||||
return
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
|
||||
service.LogVerification(c, &app.ID, &user.ID, "extension_recharge", fmt.Sprintf("扩展API充值: 用户%s, 类型:%s, 数量:%d", user.Username, req.Type, req.Amount), "", nil)
|
||||
|
||||
@@ -355,7 +369,15 @@ func handleDeductUser(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if err := database.DB.Save(&user).Error; err != nil {
|
||||
tx := database.DB.Begin()
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
tx.Rollback()
|
||||
}
|
||||
}()
|
||||
|
||||
if err := tx.Save(&user).Error; err != nil {
|
||||
tx.Rollback()
|
||||
service.LogVerification(c, &app.ID, &user.ID, "extension_deduct_failed", fmt.Sprintf("扩展API扣费失败: 保存失败 - %s", user.Username), "", err)
|
||||
response.Error(c, 500, "扣除失败")
|
||||
return
|
||||
@@ -370,7 +392,13 @@ func handleDeductUser(c *gin.Context) {
|
||||
PaymentType: "extension_api",
|
||||
Remark: req.Description,
|
||||
}
|
||||
database.DB.Create(&record)
|
||||
if err := tx.Create(&record).Error; err != nil {
|
||||
tx.Rollback()
|
||||
response.Error(c, 500, "扣除失败")
|
||||
return
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
|
||||
service.LogVerification(c, &app.ID, &user.ID, "extension_deduct", fmt.Sprintf("扩展API扣费: 用户%s, 类型:%s, 数量:%d", user.Username, req.Type, req.Amount), "", nil)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user