mylomen-server/infrastructure/repository/g_login_token_repository.go

92 lines
2.6 KiB
Go
Raw Normal View History

2023-06-04 22:54:54 +08:00
package repository
import (
2024-02-21 12:04:40 +08:00
"context"
"gorm.io/gorm"
2023-06-04 22:54:54 +08:00
"gorm.io/gorm/clause"
"time"
)
2024-04-29 19:35:18 +08:00
// GLoginToken 全局用户登录token表
type GLoginToken struct {
Id uint64 `gorm:"column:id;type:bigint(20) unsigned;primary_key;AUTO_INCREMENT;comment:主键" json:"id"`
Uid uint64 `gorm:"column:uid;type:bigint(20) unsigned;default:0;comment:uid;NOT NULL" json:"uid"`
Platform string `gorm:"column:platform;type:varchar(8);default:web;comment:平台类型;NOT NULL" json:"platform"`
2023-06-04 22:54:54 +08:00
AccessToken string `gorm:"column:access_token;type:varchar(64);comment:访问token" json:"access_token"`
ExpireTime int64 `gorm:"column:expire_time;type:bigint(20) unsigned;comment:超时时间;NOT NULL" json:"expire_time"`
CreateTime time.Time `gorm:"column:create_time;type:timestamp;default:CURRENT_TIMESTAMP;comment:创建时间;NOT NULL" json:"create_time"`
UpdateTime time.Time `gorm:"column:update_time;type:timestamp;default:CURRENT_TIMESTAMP;comment:更新时间;NOT NULL" json:"update_time"`
}
2024-04-29 19:35:18 +08:00
func (m *GLoginToken) TableName() string {
return "g_login_token"
2023-06-04 22:54:54 +08:00
}
type thirdUserTokenRepositoryImpl struct {
}
2024-04-29 19:35:18 +08:00
func (thirdUserTokenRepositoryImpl) FindByToken(ctx context.Context, token string) *GLoginToken {
2024-02-21 12:04:40 +08:00
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
db = Db
2023-06-04 22:54:54 +08:00
}
2024-04-29 19:35:18 +08:00
var model GLoginToken
2023-06-04 22:54:54 +08:00
sqlErr := db.Model(&model).Where("access_token=?", token).First(&model).Error
if sqlErr != nil {
return nil
}
return &model
}
2024-04-29 19:35:18 +08:00
func (thirdUserTokenRepositoryImpl) FindByUid(ctx context.Context, uid uint64) *GLoginToken {
2024-02-21 12:04:40 +08:00
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
db = Db
2023-06-04 22:54:54 +08:00
}
2024-04-29 19:35:18 +08:00
var model GLoginToken
2024-08-18 20:42:44 +08:00
sqlErr := db.Model(&model).Where("uid=? and platform='web'", uid).First(&model).Error
2023-06-04 22:54:54 +08:00
if sqlErr != nil {
return nil
}
return &model
}
2024-04-29 19:35:18 +08:00
func (thirdUserTokenRepositoryImpl) SaveUserLoginToken(ctx context.Context, data *GLoginToken) error {
2024-02-21 12:04:40 +08:00
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
db = Db
2023-06-04 22:54:54 +08:00
}
return db.Clauses(clause.OnConflict{
2024-04-29 19:35:18 +08:00
Columns: []clause.Column{{Name: "uid"}, {Name: "platform"}},
2023-06-04 22:54:54 +08:00
DoUpdates: clause.AssignmentColumns([]string{"access_token", "expire_time"}),
}).Create(data).Error
}
2024-04-29 19:35:18 +08:00
func (thirdUserTokenRepositoryImpl) UpdateUserLoginToken(ctx context.Context, data *GLoginToken) error {
2024-02-21 12:04:40 +08:00
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
db = Db
2023-06-04 22:54:54 +08:00
}
var args []string
if data.AccessToken != "" {
args = append(args, "access_token")
}
if data.ExpireTime != 0 {
args = append(args, "expire_time")
}
sqlErr := db.Model(data).Select(args).Where("id=?", data.Id).Updates(*data).Error
if sqlErr != nil {
return sqlErr
}
return nil
}