🎉 完成基本的演示和样例
This commit is contained in:
11
model/authorization.go
Normal file
11
model/authorization.go
Normal file
@ -0,0 +1,11 @@
|
||||
package model
|
||||
|
||||
type AuthorizationRecord struct {
|
||||
Id uint64 `json:"id" gorm:"primaryKey;column:id"`
|
||||
CreatedAt int64 `json:"created_at" gorm:"column:created_at;autoCreateTime:milli"`
|
||||
UpdatedAt int64 `json:"updated_at" gorm:"column:updated_at;autoUpdateTime:milli"`
|
||||
DeletedAt int64 `json:"deleted_at" gorm:"index;column:deleted_at;default:0"`
|
||||
|
||||
UserId uint64 `json:"user_id" gorm:"index:unique_idx,unique;column:user_id"`
|
||||
ClientId uint64 `json:"client_id" gorm:"index:unique_idx,unique;column:client_id"`
|
||||
}
|
13
model/client.go
Normal file
13
model/client.go
Normal file
@ -0,0 +1,13 @@
|
||||
package model
|
||||
|
||||
type Client struct {
|
||||
Id uint64 `json:"id" gorm:"primaryKey;column:id"`
|
||||
CreatedAt int64 `json:"created_at" gorm:"column:created_at;autoCreateTime:milli"`
|
||||
UpdatedAt int64 `json:"updated_at" gorm:"column:updated_at;autoUpdateTime:milli"`
|
||||
DeletedAt int64 `json:"deleted_at" gorm:"index;column:deleted_at;default:0"`
|
||||
|
||||
ClientId string `json:"client_id" gorm:"unique;column:client_id;type:varchar(128)"`
|
||||
ClientSecret string `json:"client_secret" gorm:"column:client_secret;type:varchar(32)"`
|
||||
Name string `json:"name" gorm:"column:name;type:varchar(256)"`
|
||||
Icon string `json:"icon" gorm:"column:icon"`
|
||||
}
|
30
model/init.go
Normal file
30
model/init.go
Normal file
@ -0,0 +1,30 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
"uauth/internal/tool"
|
||||
)
|
||||
|
||||
func Init(tx *gorm.DB) error {
|
||||
var err error
|
||||
if err = tx.AutoMigrate(
|
||||
&User{},
|
||||
&Client{},
|
||||
&AuthorizationRecord{},
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = tx.Clauses(clause.OnConflict{DoNothing: true}).
|
||||
Create(&User{Username: "admin", Nickname: "admin", Password: tool.NewPassword("Foobar123")}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = tx.Clauses(clause.OnConflict{DoNothing: true}).
|
||||
Create(&Client{ClientId: "test", ClientSecret: "Foobar123", Name: "测试", Icon: "https://picsum.photos/seed/loveuer/200"}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
53
model/user.go
Normal file
53
model/user.go
Normal file
@ -0,0 +1,53 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
"github.com/loveuer/nf/nft/log"
|
||||
"time"
|
||||
"uauth/internal/opt"
|
||||
)
|
||||
|
||||
type Status int64
|
||||
|
||||
type User struct {
|
||||
Id uint64 `json:"id" gorm:"primaryKey;column:id"`
|
||||
CreatedAt int64 `json:"created_at" gorm:"column:created_at;autoCreateTime:milli"`
|
||||
UpdatedAt int64 `json:"updated_at" gorm:"column:updated_at;autoUpdateTime:milli"`
|
||||
DeletedAt int64 `json:"deleted_at" gorm:"index;column:deleted_at;default:0"`
|
||||
|
||||
Username string `json:"username" gorm:"column:username;type:varchar(64);unique"`
|
||||
Password string `json:"-" gorm:"column:password;type:varchar(256)"`
|
||||
|
||||
Status Status `json:"status" gorm:"column:status;default:0"`
|
||||
|
||||
Nickname string `json:"nickname" gorm:"column:nickname;type:varchar(64)"`
|
||||
Avatar string `json:"avatar" gorm:"column:avatar;type:varchar(256)"`
|
||||
Comment string `json:"comment" gorm:"column:comment"`
|
||||
|
||||
CreatedById uint64 `json:"created_by_id" gorm:"column:created_by_id"`
|
||||
CreatedByName string `json:"created_by_name" gorm:"column:created_by_name;type:varchar(64)"`
|
||||
|
||||
LoginAt int64 `json:"login_at" gorm:"-"`
|
||||
}
|
||||
|
||||
func (u *User) JwtEncode() (token string, err error) {
|
||||
|
||||
now := time.Now()
|
||||
|
||||
jwtToken := jwt.NewWithClaims(jwt.SigningMethodHS512, jwt.MapClaims{
|
||||
"id": u.Id,
|
||||
"username": u.Username,
|
||||
"nickname": u.Nickname,
|
||||
"status": u.Status,
|
||||
"login_at": now.UnixMilli(),
|
||||
})
|
||||
|
||||
if token, err = jwtToken.SignedString([]byte(opt.JwtTokenSecret)); err != nil {
|
||||
err = fmt.Errorf("JwtEncode: jwt token signed secret err: %v", err)
|
||||
log.Error(err.Error())
|
||||
return "", nil
|
||||
}
|
||||
|
||||
return
|
||||
}
|
Reference in New Issue
Block a user