2023-06-16 11:26:48 +00:00
|
|
|
package gmodel
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fusenapi/utils/auth"
|
2023-06-19 02:12:58 +00:00
|
|
|
"time"
|
|
|
|
|
2023-06-16 11:26:48 +00:00
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
2023-08-25 07:37:35 +00:00
|
|
|
func (m *FsGuestModel) GenerateGuestID(ctx context.Context, AccessSecret uint64) (authKey string, err error) {
|
2023-06-16 11:26:48 +00:00
|
|
|
|
|
|
|
err = m.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
2023-09-26 05:23:49 +00:00
|
|
|
now := time.Now().UTC()
|
2023-06-16 11:26:48 +00:00
|
|
|
var record = &FsGuest{}
|
|
|
|
tx.Create(record)
|
|
|
|
|
2023-09-26 05:23:49 +00:00
|
|
|
authKey, err = auth.GenerateJwtTokenUint64(AccessSecret, now.Unix(), 31536000, 0, int64(record.GuestId))
|
2023-06-16 11:26:48 +00:00
|
|
|
if err != nil {
|
|
|
|
logx.Error(err)
|
|
|
|
err = tx.Rollback().Error
|
|
|
|
if err != nil {
|
|
|
|
logx.Error(err)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
record.AuthKey = &authKey
|
2023-09-26 05:31:42 +00:00
|
|
|
record.Ctime = &now
|
2023-06-21 08:39:55 +00:00
|
|
|
err = tx.Updates(record).Error
|
|
|
|
if err != nil {
|
|
|
|
logx.Error(err)
|
|
|
|
return err
|
|
|
|
}
|
2023-10-16 07:19:25 +00:00
|
|
|
|
|
|
|
uinfo := &FsUserInfo{
|
2023-10-16 07:42:19 +00:00
|
|
|
Module: FsString("profile"),
|
2023-10-16 07:19:25 +00:00
|
|
|
UserId: FsInt64(0),
|
|
|
|
GuestId: &record.GuestId,
|
|
|
|
Metadata: FsBytes("{}"),
|
|
|
|
Ctime: &now,
|
|
|
|
Utime: &now,
|
|
|
|
}
|
2023-10-17 10:26:48 +00:00
|
|
|
return tx.Model(&FsUserInfo{}).Create(uinfo).Error
|
2023-06-16 11:26:48 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return authKey, nil
|
|
|
|
}
|