fusenapi/model/gmodel/fs_guest_logic.go
2023-10-17 18:26:48 +08:00

53 lines
1.0 KiB
Go
Executable File

package gmodel
import (
"context"
"fusenapi/utils/auth"
"time"
"github.com/zeromicro/go-zero/core/logx"
"gorm.io/gorm"
)
func (m *FsGuestModel) GenerateGuestID(ctx context.Context, AccessSecret uint64) (authKey string, err error) {
err = m.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
now := time.Now().UTC()
var record = &FsGuest{}
tx.Create(record)
authKey, err = auth.GenerateJwtTokenUint64(AccessSecret, now.Unix(), 31536000, 0, int64(record.GuestId))
if err != nil {
logx.Error(err)
err = tx.Rollback().Error
if err != nil {
logx.Error(err)
}
return err
}
record.AuthKey = &authKey
record.Ctime = &now
err = tx.Updates(record).Error
if err != nil {
logx.Error(err)
return err
}
uinfo := &FsUserInfo{
Module: FsString("profile"),
UserId: FsInt64(0),
GuestId: &record.GuestId,
Metadata: FsBytes("{}"),
Ctime: &now,
Utime: &now,
}
return tx.Model(&FsUserInfo{}).Create(uinfo).Error
})
if err != nil {
return "", err
}
return authKey, nil
}