This commit is contained in:
aozhiwei 2024-11-10 12:04:27 +08:00
parent ab83e6cb6f
commit 059a8db38a
2 changed files with 39 additions and 16 deletions

View File

@ -3,13 +3,11 @@ package user
import (
"q5"
"f5"
"errors"
"main/constant"
"main/common"
"main/model"
"main/vo"
"main/mt"
"gorm.io/gorm"
"github.com/gin-gonic/gin"
)
@ -27,12 +25,10 @@ func (this *UserApi) Login(c *gin.Context) {
UserInfo vo.User `json:"user_info"`
}{}
nowTime := f5.GetApp().GetRealSeconds()
if result := f5.GetApp().GetOrmDb(constant.WHEEL_DB).Table(user.TableName()).Take(
user, "account_id = ?", s.GetAccountId()); result.Error != nil &&
!errors.Is(result.Error, gorm.ErrRecordNotFound) {
if err, found := user.Find(s.GetAccountId(), nowTime); err != nil {
f5.RspErr(c, 500, "server internal error")
return
} else if result.RowsAffected <= 0 {
} else if !found {
user.AccountId = s.GetAccountId()
user.Avatar = ""
user.NickName = s.GetNickName()
@ -41,22 +37,13 @@ func (this *UserApi) Login(c *gin.Context) {
user.LastPresentDiceTime = q5.ToInt32(nowTime)
user.CreateTime = q5.ToInt32(nowTime)
user.ModifyTime = q5.ToInt32(nowTime)
if result := f5.GetApp().GetOrmDb(constant.WHEEL_DB).Create(user); result.Error != nil {
if user.Create() != nil {
f5.RspErr(c, 500, "server internal error")
return
}
f5.GetMsgQueue().FireEvent(constant.MSG_CREATE_USER, q5.Args{user})
}
f5.GetMsgQueue().FireEvent(constant.MSG_LOGIN, q5.Args{user})
if int32(q5.GetDaySeconds(nowTime, 0)) > user.LastPresentDiceTime {
user.Dice = mt.Table.Global.GetDailyDiceNum()
user.LastPresentDiceTime = q5.ToInt32(nowTime)
if result := f5.GetApp().GetOrmDb(constant.WHEEL_DB).Model(user).Select(
"dice", "last_present_dice_time").Updates(user); result.Error != nil {
f5.RspErr(c, 500, "server internal error")
return
}
}
rspObj.UserInfo.FromModel(user)
c.JSON(200, rspObj)
}

View File

@ -1,5 +1,14 @@
package model
import (
"q5"
"f5"
"errors"
"main/constant"
"main/mt"
"gorm.io/gorm"
)
type User struct {
Idx int64 `gorm:"column:idx;AUTO_INCREMENT"`
AccountId string `gorm:"column:account_id;primaryKey"`
@ -15,3 +24,30 @@ type User struct {
func (this *User) TableName() string {
return "t_user"
}
func (this *User) Find(accountId string, nowTime int64) (error, bool) {
if result := f5.GetApp().GetOrmDb(constant.WHEEL_DB).Table(this.TableName()).Take(
this, "account_id = ?", accountId); result.Error != nil &&
!errors.Is(result.Error, gorm.ErrRecordNotFound) {
return result.Error, false
} else {
if result.RowsAffected > 0 {
if int32(q5.GetDaySeconds(nowTime, 0)) > this.LastPresentDiceTime {
this.Dice = mt.Table.Global.GetDailyDiceNum()
this.LastPresentDiceTime = q5.ToInt32(nowTime)
if result := f5.GetApp().GetOrmDb(constant.WHEEL_DB).Model(this).Select(
"dice", "last_present_dice_time").Updates(this); result.Error != nil {
return result.Error, false
}
}
}
return nil, result.RowsAffected > 0
}
}
func (this *User) Create() error {
if result := f5.GetApp().GetOrmDb(constant.WHEEL_DB).Create(this); result.Error != nil {
return result.Error
}
return nil
}