From db83bd9d8afe6c6289b8eea369ee93636351f3ba Mon Sep 17 00:00:00 2001 From: chenlin Date: Mon, 26 Aug 2024 18:14:41 +0800 Subject: [PATCH] =?UTF-8?q?1.=E4=BF=AE=E6=94=B9GameCardInfo=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=EF=BC=8C=E6=9F=A5=E7=9C=8B=E8=AF=A6=E6=83=85=E6=97=A0?= =?UTF-8?q?=E9=9C=80=E7=94=A8=E6=88=B7=E7=99=BB=E5=BD=95=EF=BC=9B=202.?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=90=9C=E7=B4=A2=E7=9B=B8=E5=85=B3=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=EF=BC=8C=E6=90=9C=E7=B4=A2=E6=97=A0=E9=9C=80=E7=99=BB?= =?UTF-8?q?=E5=BD=95=EF=BC=9B=203.=E4=BF=AE=E6=94=B9=E7=A7=9F=E8=B5=81?= =?UTF-8?q?=E8=AE=A2=E5=8D=95=E6=8E=A5=E5=8F=A3=EF=BC=8C=E8=A7=A3=E5=86=B3?= =?UTF-8?q?=E9=87=8D=E5=A4=8D=E4=B8=8B=E5=8D=95=E7=BC=BA=E9=99=B7=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controller/game_card.go | 37 +++++++++++++++++++++++++++---------- controller/order.go | 38 ++++++++++++++++++++++++++++++-------- model/user.go | 17 +++++++++++++---- router/router_app.go | 4 ++-- 4 files changed, 72 insertions(+), 24 deletions(-) diff --git a/controller/game_card.go b/controller/game_card.go index 0622d1e..a4c602a 100644 --- a/controller/game_card.go +++ b/controller/game_card.go @@ -80,8 +80,20 @@ func GameCardInfo(c *gin.Context) { return } uc := auth.GetCurrentUser(c) - if uc == nil { - RespJson(c, status.Unauthorized, nil) + if uc == nil { // 如果没有登录,则只查看详情 + //RespJson(c, status.Unauthorized, nil) + //return + info, err := model.GetGameCardInfo(req.GameId, req.StoreId) + if err != nil { + logger.Error("err:", err) + RespJson(c, status.InternalServerError, nil) + return + } + ret := map[string]interface{}{ + "card_info": info, + "is_collection": false, + } + RespOK(c, ret) return } @@ -127,8 +139,8 @@ func GameCardSearch(c *gin.Context) { } uc := auth.GetCurrentUser(c) if uc == nil { - RespJson(c, status.Unauthorized, nil) - return + //RespJson(c, status.Unauthorized, nil) + //return } cardList, totalPage, err := model.GetGameCardSearch(req.Name, req.Page, req.PageSize, req.StoreId) @@ -137,12 +149,16 @@ func GameCardSearch(c *gin.Context) { RespJson(c, status.InternalServerError, nil) return } - err = model.SearchHistoryAdd(uc.Uid, req.Name) - if err != nil { - logger.Error("err:", err) - RespJson(c, status.InternalServerError, nil) - return + + if uc != nil { // 登录过的用户才记录搜索历史 + err = model.SearchHistoryAdd(uc.Uid, req.Name) + if err != nil { + logger.Error("err:", err) + RespJson(c, status.InternalServerError, nil) + return + } } + ret := map[string]interface{}{ "card_list": cardList, "cur_page": req.Page, @@ -154,7 +170,8 @@ func GameCardSearch(c *gin.Context) { func GameCardSearchHistory(c *gin.Context) { uc := auth.GetCurrentUser(c) if uc == nil { - RespJson(c, status.Unauthorized, nil) + //RespJson(c, status.Unauthorized, nil) + RespOK(c, []model.SearchHistory{}) return } historyList, err := model.GetSearchHistoryList(uc.Uid) diff --git a/controller/order.go b/controller/order.go index 93f4473..829a0ca 100644 --- a/controller/order.go +++ b/controller/order.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/codinl/go-logger" "github.com/gin-gonic/gin" + "github.com/jinzhu/gorm" "golang.org/x/sync/errgroup" "io/ioutil" "mh-server/kuaidi" @@ -241,19 +242,40 @@ func RentCardOrderCreate(c *gin.Context) { cardCount += v.Count } - rentCard := model.GetUserRentCard(uc.Uid) - if rentCard == nil { - //logger.Error(errors.New("GetUserByUid err")) - //RespJson(c, status.InternalServerError, nil) - //return - rentCard = &model.UserRentCard{LevelRentCount: memberConfig.CardMax, CanRentCount: memberConfig.CardMax} + var rentCard *model.UserRentCard + var tx *gorm.DB + if req.Price == 0 { + fmt.Println("*********** req.Price == 0 ***********") + tx = model.TransactionBegin() + rentCard = model.GetUserRentCard(tx, uc.Uid) + fmt.Println("*********** rentCard is:", rentCard) + if rentCard == nil { + //logger.Error(errors.New("GetUserByUid err")) + //RespJson(c, status.InternalServerError, nil) + //return + rentCard = &model.UserRentCard{LevelRentCount: memberConfig.CardMax, CanRentCount: memberConfig.CardMax} + } + + } else { + fmt.Println("*********** req.Price != 0 ***********") + rentCard = model.GetUserRentCard(nil, uc.Uid) + fmt.Println("*********** rentCard is:", rentCard) + if rentCard == nil { + //logger.Error(errors.New("GetUserByUid err")) + //RespJson(c, status.InternalServerError, nil) + //return + rentCard = &model.UserRentCard{LevelRentCount: memberConfig.CardMax, CanRentCount: memberConfig.CardMax} + } } //if uc.Uid == 45935373 { // rentCard.CanRentCount -= 1 //} if cardCount > rentCard.CanRentCount { - logger.Error("GetMemberConfig err:", err) + if req.Price == 0 { + tx.Rollback() + } + logger.Error("err:", "会员超过可借卡数") RespJson(c, status.OrderOutRentCount, nil) return } @@ -376,7 +398,7 @@ func RentCardOrderCreate(c *gin.Context) { //fmt.Println("PayPrice:", order.PayPrice) if req.Price == 0 { - tx := model.TransactionBegin() + //tx := model.TransactionBegin() order.PayStatus = model.PayStatusPaid fmt.Println("orderId:", order.PayStatus) err = order.OrderCreate(tx) diff --git a/model/user.go b/model/user.go index 272a1bf..4bd245f 100644 --- a/model/user.go +++ b/model/user.go @@ -362,12 +362,21 @@ func GetUserEffectiveStore(uid uint32) (*StoreInfo, error) { return &validStores[0], nil } -func GetUserRentCard(uid uint32) *UserRentCard { +func GetUserRentCard(db *gorm.DB, uid uint32) *UserRentCard { userRent := new(UserRentCard) - if err := NewUserRentCardQuerySet(DB).UidEq(uid).One(userRent); err != nil { - logger.Error(err, uid) - return nil + if db == nil { + if err := NewUserRentCardQuerySet(DB).UidEq(uid).One(userRent); err != nil { + logger.Error(err, uid) + return nil + } + } else { + // 手动加锁查询 + if err := db.Raw(`SELECT * FROM user_rent_card WHERE uid = ? FOR UPDATE`, uid).Scan(userRent).Error; err != nil { + logger.Error(err, uid) + return nil + } } + return userRent } diff --git a/router/router_app.go b/router/router_app.go index db5e7ab..c9ae9d6 100644 --- a/router/router_app.go +++ b/router/router_app.go @@ -58,7 +58,7 @@ func ConfigAppRouter(r gin.IRouter) { gameCard.POST("game_type", controller.GameCardTypes) // 游戏标签 gameCard.POST("type/list", controller.GameCardTypeList) // 游戏类型列表 - gameCard.Use(auth.UserAccessAuth) + //gameCard.Use(auth.UserAccessAuth) gameCard.POST("info", controller.GameCardInfo) // 游戏卡详情 gameCard.POST("list", controller.GameCardList) // 游戏卡列表 gameCard.POST("banner", controller.HomeCarouselList) // 轮播图 @@ -69,7 +69,7 @@ func ConfigAppRouter(r gin.IRouter) { search.POST("list", controller.GameCardSearch) // 游戏卡搜索列表 search.POST("hot", controller.GameCardHotSearch) // 游戏卡搜索列表 - search.Use(auth.UserAccessAuth) + //search.Use(auth.UserAccessAuth) search.POST("history", controller.GameCardSearchHistory) // 游戏卡搜索历史 }