feat(pet): 完善宠物购买功能的错误处理和数据验证 - 在控制器层添加错误返回值处理,确保购买宠物操作的错误能够正确传递 - 添加时间验证逻辑,防止使用过期或异常的数据进行购买操作 - 修正金币更新逻辑,确保玩家和系统金币扣除与增加的准确性 - 优化代码结构,增强代码可读性和维护性 ```
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user