92 lines
2.6 KiB
Go
92 lines
2.6 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"gorm.io/gorm"
|
|
"gorm.io/gorm/clause"
|
|
"time"
|
|
)
|
|
|
|
// 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"`
|
|
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"`
|
|
}
|
|
|
|
func (m *GLoginToken) TableName() string {
|
|
return "g_login_token"
|
|
}
|
|
|
|
type thirdUserTokenRepositoryImpl struct {
|
|
}
|
|
|
|
func (thirdUserTokenRepositoryImpl) FindByToken(ctx context.Context, token string) *GLoginToken {
|
|
db, ok := ctx.Value("db").(*gorm.DB)
|
|
if !ok {
|
|
db = Db
|
|
}
|
|
|
|
var model GLoginToken
|
|
sqlErr := db.Model(&model).Where("access_token=?", token).First(&model).Error
|
|
|
|
if sqlErr != nil {
|
|
return nil
|
|
}
|
|
|
|
return &model
|
|
}
|
|
|
|
func (thirdUserTokenRepositoryImpl) FindByUid(ctx context.Context, uid uint64) *GLoginToken {
|
|
db, ok := ctx.Value("db").(*gorm.DB)
|
|
if !ok {
|
|
db = Db
|
|
}
|
|
|
|
var model GLoginToken
|
|
sqlErr := db.Model(&model).Where("uid=? and platform='web'", uid).First(&model).Error
|
|
|
|
if sqlErr != nil {
|
|
return nil
|
|
}
|
|
|
|
return &model
|
|
}
|
|
|
|
func (thirdUserTokenRepositoryImpl) SaveUserLoginToken(ctx context.Context, data *GLoginToken) error {
|
|
db, ok := ctx.Value("db").(*gorm.DB)
|
|
if !ok {
|
|
db = Db
|
|
}
|
|
|
|
return db.Clauses(clause.OnConflict{
|
|
Columns: []clause.Column{{Name: "uid"}, {Name: "platform"}},
|
|
DoUpdates: clause.AssignmentColumns([]string{"access_token", "expire_time"}),
|
|
}).Create(data).Error
|
|
}
|
|
|
|
func (thirdUserTokenRepositoryImpl) UpdateUserLoginToken(ctx context.Context, data *GLoginToken) error {
|
|
db, ok := ctx.Value("db").(*gorm.DB)
|
|
if !ok {
|
|
db = Db
|
|
}
|
|
|
|
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
|
|
}
|