44 lines
854 B
Go
44 lines
854 B
Go
|
package gmodel
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"fusenapi/utils/auth"
|
||
|
"github.com/zeromicro/go-zero/core/logx"
|
||
|
"gorm.io/gorm"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
func (m *FsGuestModel) GenerateGuestID(ctx context.Context, AccessSecret *string) (authKey string, err error) {
|
||
|
var record = &FsGuest{}
|
||
|
err = m.db.Create(record).Error
|
||
|
if err != nil {
|
||
|
logx.Error(err)
|
||
|
}
|
||
|
|
||
|
err = m.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||
|
now := time.Now().Unix()
|
||
|
var record = &FsGuest{}
|
||
|
tx.Create(record)
|
||
|
|
||
|
authKey, err = auth.GenerateJwtToken(AccessSecret, now, 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.CreatedAt = &now
|
||
|
tx.Updates(record)
|
||
|
return nil
|
||
|
})
|
||
|
|
||
|
if err != nil {
|
||
|
return "", err
|
||
|
}
|
||
|
|
||
|
return authKey, nil
|
||
|
}
|