完成基础model部分

This commit is contained in:
2022-05-25 17:30:46 +08:00
parent ddec628641
commit 9bb43f4484
18 changed files with 498 additions and 0 deletions

View File

@@ -0,0 +1,6 @@
package constant
const (
OAuth2RedisKey = "oauth:token:" // Token缓存前缀
OAuth2UserCacheKey = "oauth:user:" // 用户缓存前缀
)

38
common/constant/user.go Normal file
View File

@@ -0,0 +1,38 @@
package constant
// UserStatus 用户状态
type UserStatus string
const (
UserStatusActive UserStatus = "NORMAL" // 用户状态正常
UserStatusDisabled UserStatus = "DISABLE" // 已禁用用户
)
// 状态对应的描述
var userStatusMap = map[UserStatus]string{
UserStatusActive: "正常",
UserStatusDisabled: "已禁用",
}
// 处理为看得懂的状态
func (s UserStatus) String() string {
if str, ok := userStatusMap[s]; ok {
return str
}
return string(s)
}
// =====================================================================================================================
// UserIdentity 用户身份
type UserIdentity string
const (
UserIdentityAdmin UserIdentity = "admin" // 管理员
UserIdentityUser UserIdentity = "user" // 普通用户
)
// String implements the Stringer interface.
func (t UserIdentity) String() string {
return string(t)
}

6
common/default_keys.go Normal file
View File

@@ -0,0 +1,6 @@
package common
const (
SmsSendKey = "sms:send:" // 短信发送缓存前缀
Oauth2RedisKey = "oauth:token:" // Token缓存前缀
)

105
common/types/date.go Normal file
View File

@@ -0,0 +1,105 @@
package types
import (
"database/sql/driver"
"fmt"
"time"
)
// 默认时间格式
const dateFormat = "2006-01-02 15:04:05.000"
// DateTime 自定义时间类型
type DateTime time.Time
// Scan implements the Scanner interface.
func (dt *DateTime) Scan(value interface{}) error {
// mysql 内部日期的格式可能是 2006-01-02 15:04:05 +0800 CST 格式,所以检出的时候还需要进行一次格式化
tTime, _ := time.Parse("2006-01-02 15:04:05 +0800 CST", value.(time.Time).String())
*dt = DateTime(tTime)
return nil
}
// Value implements the driver Valuer interface.
func (dt DateTime) Value() (driver.Value, error) {
// 0001-01-01 00:00:00 属于空值,遇到空值解析成 null 即可
if dt.String() == "0001-01-01 00:00:00.000" {
return nil, nil
}
return []byte(dt.Format(dateFormat)), nil
}
// 用于 fmt.Println 和后续验证场景
func (dt DateTime) String() string {
return dt.Format(dateFormat)
}
// Format 格式化
func (dt DateTime) Format(fm string) string {
return time.Time(dt).Format(fm)
}
// After 时间比较
func (dt *DateTime) After(now time.Time) bool {
return time.Time(*dt).After(now)
}
// Before 时间比较
func (dt *DateTime) Before(now time.Time) bool {
return time.Time(*dt).Before(now)
}
// IBefore 时间比较
func (dt *DateTime) IBefore(now DateTime) bool {
return dt.Before(time.Time(now))
}
// SubTime 对比
func (dt DateTime) SubTime(t time.Time) time.Duration {
return dt.ToTime().Sub(t)
}
// Sub 对比
func (dt DateTime) Sub(t DateTime) time.Duration {
return dt.ToTime().Sub(t.ToTime())
}
// ToTime 转换为golang的时间类型
func (dt DateTime) ToTime() time.Time {
return time.Time(dt)
}
// IsNil 是否为空值
func (dt DateTime) IsNil() bool {
return dt.Format(dateFormat) == "0001-01-01 00:00:00.000"
}
// Unix 实现Unix函数
func (dt DateTime) Unix() int64 {
return dt.ToTime().Unix()
}
// ======== 序列化 ========
// MarshalJSON 时间到字符串
func (dt DateTime) MarshalJSON() ([]byte, error) {
// 过滤掉空数据
if dt.IsNil() {
return []byte("\"\""), nil
}
output := fmt.Sprintf(`"%s"`, dt.Format("2006-01-02 15:04:05"))
return []byte(output), nil
}
// UnmarshalJSON 字符串到时间
func (dt *DateTime) UnmarshalJSON(b []byte) error {
if len(b) == 2 {
*dt = DateTime{}
return nil
}
// 解析指定的格式
//now, err := time.ParseInLocation(`"`+dateFormat+`"`, string(b), time.Local)
now, err := time.ParseInLocation(dateFormat, string(b), time.Local)
*dt = DateTime(now)
return err
}

20
common/types/model.go Normal file
View File

@@ -0,0 +1,20 @@
package types
import (
"github.com/google/uuid"
"gorm.io/gorm"
)
// BaseDbModel 数据库通用字段
type BaseDbModel struct {
Id string `json:"id" gorm:"type:varchar(50);primarykey"`
CreatedAt DateTime `json:"createdAt"`
UpdatedAt DateTime `json:"updatedAt"`
DeletedAt gorm.DeletedAt `json:"-" gorm:"index:deleted"`
}
// BeforeCreate 创建数据库对象之前生成UUID
func (m *BaseDbModel) BeforeCreate(*gorm.DB) (err error) {
m.Id = uuid.New().String()
return
}