```
All checks were successful
ci/woodpecker/push/my-first-workflow Pipeline was successful

feat(pet): 完善宠物购买功能的错误处理和数据验证

- 在控制器层添加错误返回值处理,确保购买宠物操作的错误能够正确传递
- 添加时间验证逻辑,防止使用过期或异常的数据进行购买操作
- 修正金币更新逻辑,确保玩家和系统金币扣除与增加的准确性
- 优化代码结构,增强代码可读性和维护性
```
This commit is contained in:
昔念
2026-03-10 23:24:33 +08:00
parent fc620d668f
commit ed8b1b71c1
2 changed files with 9 additions and 3 deletions

View File

@@ -110,7 +110,7 @@ func (c *PetBagController) BuyPet(ctx context.Context, req *BuyPetReq) (res *coo
res = &cool.BaseRes{}
service.NewPetService(uint32(admin.UserId)).BuyPet(req.Cid)
err = service.NewPetService(uint32(admin.UserId)).BuyPet(req.Cid)
return

View File

@@ -6,6 +6,8 @@ import (
"blazing/modules/player/model"
"context"
"fmt"
"github.com/gogf/gf/v2/os/gtime"
)
// 获取精灵信息 0是仓库,1是放生
@@ -65,14 +67,18 @@ func (s *PetService) BuyPet(pid uint32) error {
if tt.SalePrice == 0 {
return fmt.Errorf("未设置价格")
}
if !tt.UpdateTime.AddDate(0, 0, 1).Before(gtime.Now()) {
return fmt.Errorf("数据异常")
}
if service.NewBaseSysUserService().GetGold(uint(s.userid)) < int64(tt.SalePrice)*102 {
return fmt.Errorf("余额不足")
}
service.NewBaseSysUserService().UpdateGold(tt.PlayerID, int64(tt.SalePrice)*98)
service.NewBaseSysUserService().UpdateGold(s.userid, -int64(tt.SalePrice)*102)
s.PetAdd(&tt.Data)
NewPetService(tt.PlayerID).Pet_del(tt.CatchTime)
service.NewBaseSysUserService().UpdateGold(tt.PlayerID, int64(tt.SalePrice)*98)
s.PetAdd(&tt.Data)
return nil