🎉初步完成用户
This commit is contained in:
67
model/base.go
Normal file
67
model/base.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"fmt"
|
||||
"github.com/google/uuid"
|
||||
"gorm.io/gorm"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Base
|
||||
// @description: 数据模型基类
|
||||
type Base struct {
|
||||
Id string `json:"id" gorm:"primaryKey;type:varchar(36);not null;comment:'主键'"`
|
||||
Timestamp
|
||||
}
|
||||
|
||||
func (b *Base) BeforeCreate(*gorm.DB) (err error) {
|
||||
if b.Id == "" {
|
||||
b.Id = strings.ReplaceAll(uuid.NewString(), "-", "")
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
type JsonTime struct {
|
||||
time.Time
|
||||
}
|
||||
|
||||
type Timestamp struct {
|
||||
CreatedAt JsonTime
|
||||
UpdatedAt JsonTime
|
||||
}
|
||||
|
||||
func (jt JsonTime) MarshalJSON() ([]byte, error) {
|
||||
if jt.IsZero() {
|
||||
return []byte(`""`), nil
|
||||
}
|
||||
output := fmt.Sprintf("\"%s\"", jt.Format("2006-01-02 15:04:05"))
|
||||
return []byte(output), nil
|
||||
}
|
||||
|
||||
func (jt JsonTime) Value() (driver.Value, error) {
|
||||
var zeroTime time.Time
|
||||
if jt.Time.UnixNano() == zeroTime.UnixNano() {
|
||||
return nil, nil
|
||||
}
|
||||
return jt.Time.Format("2006-01-02 15:04:05"), nil
|
||||
}
|
||||
|
||||
func (jt *JsonTime) Scan(v any) error {
|
||||
value, ok := v.(time.Time)
|
||||
if ok {
|
||||
*jt = JsonTime{Time: value}
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("can not convert %v to timestamp", v)
|
||||
}
|
||||
|
||||
func (jt JsonTime) String() string {
|
||||
if jt.IsZero() {
|
||||
return ""
|
||||
}
|
||||
output := fmt.Sprintf("%s", jt.Format("2006-01-02 15:04:05"))
|
||||
return output
|
||||
}
|
29
model/navigation.go
Normal file
29
model/navigation.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package model
|
||||
|
||||
// NavigationType
|
||||
// @description: 分类
|
||||
type NavigationType struct {
|
||||
Base
|
||||
Name string `gorm:"column:name;type:varchar(255);not null;comment:'名称'"`
|
||||
Icon string `gorm:"column:icon;type:varchar(255);not null;comment:'图标'"`
|
||||
}
|
||||
|
||||
func (NavigationType) TableName() string {
|
||||
return "t_navigation_type"
|
||||
}
|
||||
|
||||
// Navigation
|
||||
// @description: 导航
|
||||
type Navigation struct {
|
||||
Base
|
||||
TypeID string `gorm:"column:type_id;type:varchar(255);not null;comment:'分类ID'"`
|
||||
Title string `gorm:"column:title;type:varchar(255);not null;comment:'标题'"`
|
||||
Url string `gorm:"column:url;type:varchar(255);not null;comment:'链接'"`
|
||||
Description string `gorm:"column:description;type:varchar(255);not null;comment:'描述'"`
|
||||
Icon string `gorm:"column:icon;type:varchar(255);not null;comment:'图标'"`
|
||||
Enabled bool `gorm:"column:enabled;type:tinyint(1);not null;comment:'是否启用'"`
|
||||
}
|
||||
|
||||
func (Navigation) TableName() string {
|
||||
return "t_navigation"
|
||||
}
|
16
model/user.go
Normal file
16
model/user.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package model
|
||||
|
||||
// User
|
||||
// @description: 后台用户表
|
||||
type User struct {
|
||||
Base
|
||||
Account string `json:"account" gorm:"type:varchar(50);not null;index:idx_account;comment: '登陆账号'"`
|
||||
Password string `json:"password" gorm:"type:varchar(255);not null;comment: '密码'"`
|
||||
Avatar string `json:"avatar" gorm:"type:varchar(255);default null;comment: '头像'"`
|
||||
Nickname string `json:"nickname" gorm:"type:varchar(50);not null;comment: '昵称'"`
|
||||
Contact string `json:"contact" gorm:"type:varchar(255);default null;comment: '联系方式(邮箱|电话)'"`
|
||||
}
|
||||
|
||||
func (User) TableName() string {
|
||||
return "t_user"
|
||||
}
|
Reference in New Issue
Block a user