48 lines
1.1 KiB
Go
48 lines
1.1 KiB
Go
package transfercastile
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"nova_task/internal/pkg/utils"
|
|
"time"
|
|
|
|
"github.com/zeromicro/go-zero/rest/httpx"
|
|
"nova_task/internal/logic/transfercastile"
|
|
"nova_task/internal/svc"
|
|
"nova_task/internal/types"
|
|
)
|
|
|
|
// 提取castile到游戏
|
|
func TransferCastileToGameHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
var req types.TransferCastileToGameReq
|
|
if err := httpx.Parse(r, &req); err != nil {
|
|
httpx.ErrorCtx(r.Context(), w, err)
|
|
return
|
|
}
|
|
|
|
uid := utils.GetUidUint(r.Context())
|
|
redisKey := fmt.Sprintf("transferCastile:uid:%d", uid)
|
|
//加锁
|
|
lock, err := svcCtx.Redis.SetnxEx(redisKey, time.Now().String(), 120)
|
|
if !lock || err != nil {
|
|
httpx.ErrorCtx(r.Context(), w, errors.New("operation frequently"))
|
|
return
|
|
}
|
|
|
|
l := transfercastile.NewTransferCastileToGameLogic(r.Context(), svcCtx)
|
|
|
|
resp, err := l.TransferCastileToGame(&req)
|
|
|
|
//删除redis KEY
|
|
_, _ = svcCtx.Redis.Del(redisKey)
|
|
|
|
if err != nil {
|
|
httpx.ErrorCtx(r.Context(), w, err)
|
|
} else {
|
|
httpx.OkJsonCtx(r.Context(), w, resp)
|
|
}
|
|
}
|
|
}
|