This commit is contained in:
@@ -1,8 +1,14 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"blazing/common/data"
|
||||
"blazing/cool"
|
||||
baseservice "blazing/modules/base/service"
|
||||
"blazing/modules/player/service"
|
||||
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
)
|
||||
|
||||
type ItemBagController struct {
|
||||
@@ -13,10 +19,54 @@ func init() {
|
||||
var task_info_controller = &ItemBagController{
|
||||
&cool.Controller{
|
||||
Prefix: "/admin/game/item",
|
||||
Api: []string{"Delete", "Update", "Info", "List", "Page"},
|
||||
Api: []string{"Add", "Delete", "Update", "Info", "List", "Page", "Grant"},
|
||||
Service: service.NewItemService(0), //因为page已经过滤,所以这里需要改成0
|
||||
},
|
||||
}
|
||||
// 注册路由
|
||||
cool.RegisterController(task_info_controller)
|
||||
}
|
||||
|
||||
type ItemGrantReq struct {
|
||||
g.Meta `path:"/grant" method:"POST"`
|
||||
Authorization string `json:"Authorization" in:"header"`
|
||||
PlayerID uint32 `json:"player_id"`
|
||||
Username string `json:"username"`
|
||||
ItemID int64 `json:"item_id" v:"required#请选择物品"`
|
||||
ItemCnt int64 `json:"item_cnt" v:"required|min:1#请输入数量|数量必须大于0"`
|
||||
}
|
||||
|
||||
func (c *ItemBagController) Grant(ctx context.Context, req *ItemGrantReq) (res *cool.BaseRes, err error) {
|
||||
playerID := req.PlayerID
|
||||
if playerID == 0 && req.Username != "" {
|
||||
user := baseservice.NewBaseSysUserService().GetByUsername(req.Username)
|
||||
if user == nil {
|
||||
return cool.Fail("用户不存在"), nil
|
||||
}
|
||||
playerID = uint32(user.ID)
|
||||
}
|
||||
|
||||
if playerID == 0 {
|
||||
return cool.Fail("请先选择玩家"), nil
|
||||
}
|
||||
|
||||
items, err := service.NewItemService(playerID).AddItems([]data.ItemInfo{
|
||||
{
|
||||
ItemId: req.ItemID,
|
||||
ItemCnt: req.ItemCnt,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return cool.Fail(err.Error()), nil
|
||||
}
|
||||
if len(items) == 0 {
|
||||
return cool.Fail("发放失败,可能已达到物品上限"), nil
|
||||
}
|
||||
|
||||
return cool.Ok(g.Map{
|
||||
"player_id": playerID,
|
||||
"item_id": req.ItemID,
|
||||
"item_cnt": req.ItemCnt,
|
||||
"items": items,
|
||||
}), nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user