91 lines
2.8 KiB
Go
91 lines
2.8 KiB
Go
package repository
|
|
|
|
import (
|
|
"code.freebrio.com/fb-go/lib/context"
|
|
"gorm.io/gorm/clause"
|
|
"time"
|
|
)
|
|
|
|
// FThirdUserTokenDO 第三方用户token表
|
|
type FThirdUserTokenDO struct {
|
|
Id uint64 `gorm:"column:id;type:bigint(20) unsigned;primary_key;AUTO_INCREMENT" json:"id"`
|
|
CorpId uint64 `gorm:"column:corp_id;type:bigint(20) unsigned;comment:企业id;NOT NULL" json:"corp_id"`
|
|
BizId uint64 `gorm:"column:biz_id;type:bigint(20);comment:业务id。 " json:"biz_id"`
|
|
CorpUid uint64 `gorm:"column:corp_uid;type:bigint(20) unsigned;comment:企业uid;NOT NULL" json:"corp_uid"`
|
|
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 *FThirdUserTokenDO) TableName() string {
|
|
return "f_third_user_token"
|
|
}
|
|
|
|
type thirdUserTokenRepositoryImpl struct {
|
|
}
|
|
|
|
func (thirdUserTokenRepositoryImpl) FindByToken(ctx context.GormWithZap, token string) *FThirdUserTokenDO {
|
|
db, err := ctx.GormWithContext()
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
|
|
var model FThirdUserTokenDO
|
|
sqlErr := db.Model(&model).Where("access_token=?", token).First(&model).Error
|
|
|
|
if sqlErr != nil {
|
|
return nil
|
|
}
|
|
|
|
return &model
|
|
}
|
|
func (thirdUserTokenRepositoryImpl) FindByBizAndCorpId(ctx context.GormWithZap, bizId, corpUid uint64) *FThirdUserTokenDO {
|
|
db, err := ctx.GormWithContext()
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
|
|
var model FThirdUserTokenDO
|
|
sqlErr := db.Model(&model).Where("biz_id=? and corp_uid=?", bizId, corpUid).First(&model).Error
|
|
|
|
if sqlErr != nil {
|
|
return nil
|
|
}
|
|
|
|
return &model
|
|
}
|
|
|
|
func (thirdUserTokenRepositoryImpl) SaveUserLoginToken(ctx context.GormWithZap, data *FThirdUserTokenDO) error {
|
|
db, err := ctx.GormWithContext()
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
|
|
return db.Clauses(clause.OnConflict{
|
|
Columns: []clause.Column{{Name: "corp_uid"}, {Name: "biz_id"}},
|
|
DoUpdates: clause.AssignmentColumns([]string{"access_token", "expire_time"}),
|
|
}).Create(data).Error
|
|
}
|
|
|
|
func (thirdUserTokenRepositoryImpl) UpdateUserLoginToken(ctx context.GormWithZap, data *FThirdUserTokenDO) error {
|
|
db, err := ctx.GormWithContext()
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
|
|
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
|
|
}
|