fix: 修复域能管理操作无响应及公户账务错误

- 修复前端 energy.js api.post/get 回调调用为 Promise 链式调用,解决操作无响应
- 修复 AdminAdjust system_operation 模式错误扣减公户余额,现不碰公户
- 修复 AdminAdjust admin_transfer 模式公户按用户数量扣款(amount * len(userIDs))
- 修复 AdminAdjust 余额检查移入事务内并加行锁(SELECT FOR UPDATE),防 TOCTOU 竞态
- FundStore 接口及 FundRepo 新增 LockAndGetBalance 方法
This commit is contained in:
2026-06-01 15:26:25 +08:00
parent 26e557de8a
commit 4815060839
4 changed files with 39 additions and 30 deletions

View File

@ -379,34 +379,26 @@ func (s *EnergyService) DeductOnDeletePost(authorUserID uint, postID uint) error
}
// AdminAdjust 后台调整用户域能
// mode: "admin_transfer"(受公户余额约束)| "system_operation"(不受约束)
// mode: "admin_transfer"(受公户余额约束)| "system_operation"(不受约束,不碰公户
func (s *EnergyService) AdminAdjust(operatorUID uint, userIDs []uint, amount int, description string, mode string) error {
if mode == "" {
mode = model.FundTypeAdminTransfer
}
// admin_transfer 出账时检查公户余额
if mode == model.FundTypeAdminTransfer && amount > 0 && s.fundRepo != nil {
balance, err := s.fundRepo.GetBalance()
if err != nil {
return fmt.Errorf("查询公户余额失败: %w", err)
}
if balance < amount {
return common.ErrInsufficientFund
}
}
return s.db.Transaction(func(tx *gorm.DB) error {
txRepo := s.repo.WithTx(tx)
var txFundRepo FundStore
if s.fundRepo != nil {
txFundRepo = s.fundRepo.WithTx(tx)
}
// 公户余额调整(给用户 energy=+amount → 公户 -amount扣用户 energy=-amount → 公户 +amount
if txFundRepo != nil {
if err := txFundRepo.IncrBalance(-amount); err != nil {
return fmt.Errorf("公户余额调整失败: %w", err)
// admin_transfer公户出账需检查余额行锁防 TOCTOU 竞态
isAdminTransfer := mode == model.FundTypeAdminTransfer
if isAdminTransfer && amount > 0 && s.fundRepo != nil {
txFundRepo := s.fundRepo.WithTx(tx)
totalCost := amount * len(userIDs)
balance, err := txFundRepo.LockAndGetBalance()
if err != nil {
return fmt.Errorf("查询公户余额失败: %w", err)
}
if balance < totalCost {
return common.ErrInsufficientFund
}
}
@ -428,14 +420,20 @@ func (s *EnergyService) AdminAdjust(operatorUID uint, userIDs []uint, amount int
}
}
// 公户流水
if txFundRepo != nil {
amountDisplay := float64(amount) / 10
// admin_transfer公户扣款 + 公户流水system_operation 不碰公户)
if isAdminTransfer && s.fundRepo != nil {
txFundRepo := s.fundRepo.WithTx(tx)
totalCost := amount * len(userIDs)
if err := txFundRepo.IncrBalance(-totalCost); err != nil {
return fmt.Errorf("公户余额调整失败: %w", err)
}
totalDisplay := float64(totalCost) / 10
fundLog := &model.FundLog{
Amount: -amount,
Amount: -totalCost,
Type: mode,
OperatorUID: &operatorUID,
Description: fmt.Sprintf("%s调整用户域能 %.1f,说明:%s", getFundModeName(mode), amountDisplay, description),
Description: fmt.Sprintf("%s调整 %d 名用户域能 %.1f,说明:%s", getFundModeName(mode), len(userIDs), totalDisplay, description),
}
if len(userIDs) > 0 {
fundLog.RelatedType = "user"

View File

@ -114,6 +114,7 @@ type energyStore = EnergyStore
// FundStore 公户仓储接口ISP
type FundStore interface {
GetBalance() (int, error)
LockAndGetBalance() (int, error)
IncrBalance(amount int) error
CreateLog(log *model.FundLog) error
ListLogs(logType string, offset, limit int) ([]model.FundLog, int64, error)