nie

package module
v1.5.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 20, 2026 License: GPL-3.0 Imports: 22 Imported by: 0

README

nie-go

Documentation

Index

Constants

View Source
const (
	CtxUidKey          = "uid"
	CtxNickNameKey     = "nickName"
	CtxEnterpriseIdKey = "enterpriseId"
	CtxUnameKey        = "uname"
	CtxRoleKey         = "role"
	CtxOfficeIdKey     = "officeId"
)

Variables

View Source
var (
	// Deprecated: it will be removed
	CaptchaPrefix = "captcha:" // 验证码前缀
	// Deprecated: it will be removed
	AccessTokenPrefix = "accessToken:" // accessToken前缀
	// Deprecated: it will be removed
	RefreshTokenPrefix = "refreshToken:" // refreshToken前缀
)
View Source
var CopierConverters = getAllConverters()

CopierConverters 定义 copier.Option 中 Converters 转换器列表

View Source
var StrictTimeParse bool

StrictTimeParse 控制 string->time.Time/sql.NullTime 解析失败时的行为。

- false(默认):兼容历史行为,解析失败返回零值/Valid=false,不抛错。 - true:解析失败直接返回 error,便于上层快速定位“日期格式错误”。

Functions

func Copier4Bff

func Copier4Bff(to interface{}, from interface{}) error

Copier4Bff 使用 copier 深拷贝

用于将 *structpb.Struct 和 []*structpb.Struct 字段转换为自定义结构体

func Copier4Ent

func Copier4Ent(to interface{}, from interface{}) error

Copier4Ent 使用 copier 深拷贝,加载自定义转换器

用于entity结构体和pb结构体之间的转换

func CtxEnterpriseId added in v1.0.2

func CtxEnterpriseId(ctx context.Context) int64

CtxEnterpriseId 从上下文中获取企业ID

func CtxGlobalInt added in v1.0.2

func CtxGlobalInt(ctx context.Context, name string) (int64, error)

CtxGlobalInt 从上下文中获取元数据

func CtxGlobalString added in v1.0.2

func CtxGlobalString(ctx context.Context, name string) (string, error)

CtxGlobalString 从上下文中获取元数据

func CtxNickName added in v1.0.2

func CtxNickName(ctx context.Context) string

CtxNickName 从上下文中获取用户昵称

func CtxOfficeId added in v1.1.0

func CtxOfficeId(ctx context.Context) int64

CtxOfficeId 从上下文中获取单位ID

func CtxRoleKeys added in v1.0.3

func CtxRoleKeys(ctx context.Context) []string

CtxRoleKeys 从上下文中获取角色

func CtxUid added in v1.0.2

func CtxUid(ctx context.Context) int64

CtxUid 从上下文中获取用户ID

func CtxUname added in v1.0.2

func CtxUname(ctx context.Context) string

CtxUname 从上下文中获取用户名

func GetAllowFields

func GetAllowFields(obj interface{}, options ...FieldOptions) []string

GetAllowFields 提取结构体字段名的函数,返回字段名切片

model.SelectCreateFields 方法中已添加 CreateId, CreateBy

model.SelectUpdateFields 方法中已添加 UpdateId, UpdateBy

func GetJSONConverters

func GetJSONConverters() []copier.TypeConverter

GetJSONConverters 获取 datatypes.JSON ←→ []string 转换器

func GetNullTimeConverters

func GetNullTimeConverters() []copier.TypeConverter

GetNullTimeConverters 获取 sql.NullTime ←→ string 转换器

func GetStructPBConverters

func GetStructPBConverters() []copier.TypeConverter

GetStructPBConverters 返回 *structpb.Struct ←→ datatypes.JSON 的转换器

func GetStructPBSliceConverters

func GetStructPBSliceConverters() []copier.TypeConverter

GetStructPBSliceConverters 定义 []*structpb.Struct ←→ datatypes.JSON 的转换器

func GetTimeConverters

func GetTimeConverters() []copier.TypeConverter

GetTimeConverters 获取 time.Time ←→ string 转换器

func InitCache added in v1.0.5

func InitCache(client redis.Cmdable)

InitCache 初始化全局 DefaultCache

func SelectCreateFields

func SelectCreateFields(db *gorm.DB, allowFields []string) *gorm.DB

SelectCreateFields 构建允许创建字段

func SelectUpdateFields

func SelectUpdateFields(db *gorm.DB, allowFields []string) *gorm.DB

SelectUpdateFields 构建允许更新字段

func SetDefaultTimeLocation added in v1.5.2

func SetDefaultTimeLocation(loc *time.Location)

SetDefaultTimeLocation 设置“无时区日期字符串”的默认解析时区。

说明: - 仅影响类似 "2026-01-19" / "2026-01-19 08:00:00" 这类不带时区信息的字符串。 - 对 RFC3339(如 "2026-01-19T08:00:00+08:00")不做强制覆盖,保持其绝对时间语义。

func SetDefaultTimeLocationName added in v1.5.2

func SetDefaultTimeLocationName(name string) error

SetDefaultTimeLocationName 用 IANA 时区名设置默认解析时区,例如 "Asia/Shanghai"。

Types

type BaseModel

type BaseModel struct {
	CreatedAt time.Time      `gorm:"type:datetime;column:create_time;comment:创建时间" json:"createTime" copier:"CreateTime"`    // 创建时间
	UpdatedAt time.Time      `gorm:"type:datetime;column:update_time;comment:更新时间" json:"updateTime" copier:"UpdateTime"`    // 更新时间
	DeletedAt gorm.DeletedAt `gorm:"type:datetime;column:delete_time;comment:删除时间" sql:"index" json:"-" copier:"DeleteTime"` // 删除时间
	CreateId  int64          `gorm:"type:bigint;column:create_id;comment:创建人id" json:"createId"`                             // 创建人id
	UpdateId  int64          `gorm:"type:bigint;column:update_id;comment:更新人id" json:"updateId"`                             // 更新人id
	DeleteId  int64          `gorm:"type:bigint;column:delete_id;comment:删除人id" json:"deleteId"`                             // 删除人id
	CreateBy  string         `gorm:"type:varchar(64);column:create_by;comment:创建人" json:"createBy"`                          // 创建人
	UpdateBy  string         `gorm:"type:varchar(64);column:update_by;comment:更新人" json:"updateBy"`                          // 更新人
	DeleteBy  string         `gorm:"type:varchar(64);column:delete_by;comment:删除人" json:"deleteBy"`                          // 删除人
}

BaseModel 基础模型

带增删改时间、ID、姓名

type BaseStruct

type BaseStruct struct {
	CreateId   int64  // 创建人ID
	UpdateId   int64  // 更新人ID
	DeleteId   int64  // 删除人ID
	CreateBy   string // 创建人
	UpdateBy   string // 更新人
	DeleteBy   string // 删除人
	CreateTime string // 创建时间
	UpdateTime string // 更新时间
	DeleteTime string // 删除时间
}

BaseStruct 公共完整解构体

带增删改时间、ID、姓名

type Cache added in v1.0.5

type Cache struct {
	// contains filtered or unexported fields
}

Cache 用于管理 Redis 缓存

var DefaultCache *Cache

func NewCache added in v1.0.5

func NewCache(redis redis.Cmdable) *Cache

NewCache 使用已有的 Redis 客户端初始化 Cache

func (*Cache) AsyncDelRedis added in v1.1.2

func (c *Cache) AsyncDelRedis(key string, logHelper *log.Helper)

AsyncDelRedis 异步删除缓存

func (*Cache) AsyncSetRedis added in v1.1.2

func (c *Cache) AsyncSetRedis(key string, data interface{}, expiration time.Duration, logHelper *log.Helper)

AsyncSetRedis 异步设置缓存

func (*Cache) DelRedis added in v1.0.5

func (c *Cache) DelRedis(ctx context.Context, key string) error

DelRedis 删除单个缓存 key

func (*Cache) DelRedisMulti added in v1.4.2

func (c *Cache) DelRedisMulti(ctx context.Context, keys ...string) error

DelRedisMulti 删除多个缓存 key(可变参数版) 便于批量删除,用于 SCAN 前缀删除等场景

func (*Cache) GetRedis added in v1.0.5

func (c *Cache) GetRedis(ctx context.Context, key string, result ...interface{}) (string, error)

GetRedis 获取缓存

func (*Cache) IncrRedis added in v1.5.3

func (c *Cache) IncrRedis(ctx context.Context, key string, expiration time.Duration) (int64, error)

IncrRedis 对 key 执行原子自增并设置/刷新过期时间,返回自增后的值 适用于频率限制、登录失败计数等场景

func (*Cache) ScanRedis added in v1.4.2

func (c *Cache) ScanRedis(ctx context.Context, cursor uint64, pattern string, count int64) ([]string, uint64, error)

ScanRedis 使用 Redis SCAN 按模式分页扫描 key 返回本次扫描到的 keys、下一次游标值和错误

func (*Cache) SetRedis added in v1.0.5

func (c *Cache) SetRedis(ctx context.Context, key string, value interface{}, expiration time.Duration) error

SetRedis 设置缓存

func (*Cache) TTLRefresh added in v1.4.0

func (c *Cache) TTLRefresh(ctx context.Context, key string, expiration time.Duration) error

TTLRefresh 刷新缓存过期时间

type CreateStruct

type CreateStruct struct {
	AllowFields []string // 允许字段
	CreateId    int64    // 创建人ID
	CreateBy    string   // 创建人
	CreateTime  string   // 创建时间
}

CreateStruct 公共创建结构体

带创建时间、ID、姓名、允许字段

type FieldOptions

type FieldOptions struct {
	Adds                interface{} // 可追加的字段,string 或 []string
	Filters             interface{} // 可过滤的字段,string 或 []string
	AddEnterpriseId     bool        // 是否添加EnterpriseId字段
	FiltersEnterpriseId bool        // 是否过滤EnterpriseId字段
	OnlyFields          interface{} // 仅保留的字段(忽略obj),string 或 []string
}

FieldOptions 定义 GetAllowFields 参数结构体,用于传递可选参数

type FullModel

type FullModel struct {
	CreatedAt   time.Time      `gorm:"type:datetime;column:create_time;comment:创建时间" json:"createTime" copier:"CreateTime"`    // 创建时间
	UpdatedAt   time.Time      `gorm:"type:datetime;column:update_time;comment:更新时间" json:"updateTime" copier:"UpdateTime"`    // 更新时间
	DeletedAt   gorm.DeletedAt `gorm:"type:datetime;column:delete_time;comment:删除时间" sql:"index" json:"-" copier:"DeleteTime"` // 删除时间
	CreateId    int64          `gorm:"type:bigint;column:create_id;comment:创建人id" json:"createId"`                             // 创建人id
	UpdateId    int64          `gorm:"type:bigint;column:update_id;comment:更新人id" json:"updateId"`                             // 更新人id
	DeleteId    int64          `gorm:"type:bigint;column:delete_id;comment:删除人id" json:"deleteId"`                             // 删除人id
	CreateBy    string         `gorm:"type:varchar(64);column:create_by;comment:创建人" json:"createBy"`                          // 创建人
	UpdateBy    string         `gorm:"type:varchar(64);column:update_by;comment:更新人" json:"updateBy"`                          // 更新人
	DeleteBy    string         `gorm:"type:varchar(64);column:delete_by;comment:删除人" json:"deleteBy"`                          // 删除人
	AllowFields []string       `gorm:"-" json:"allowFields"`                                                                   // 允许修改的字段
}

FullModel 完整模型

带增删改时间、ID、姓名、允许字段

type FullStruct

type FullStruct struct {
	AllowFields []string // 允许字段
	CreateId    int64    // 创建人ID
	UpdateId    int64    // 更新人ID
	DeleteId    int64    // 删除人ID
	CreateBy    string   // 创建人
	UpdateBy    string   // 更新人
	DeleteBy    string   // 删除人
	CreateTime  string   // 创建时间
	UpdateTime  string   // 更新时间
	DeleteTime  string   // 删除时间
}

FullStruct 公共完整解构体

带增删改时间、ID、姓名、允许字段

type HardDModel

type HardDModel struct {
	CreatedAt time.Time `gorm:"column:create_time" json:"createTime"`
	UpdatedAt time.Time `gorm:"column:update_time" json:"updateTime"`
}

HardDModel 硬删除

type TimeModel

type TimeModel struct {
	CreatedAt time.Time      `gorm:"type:datetime;column:create_time;comment:创建时间" json:"createTime" copier:"CreateTime"`    // 创建时间
	UpdatedAt time.Time      `gorm:"type:datetime;column:update_time;comment:更新时间" json:"updateTime" copier:"UpdateTime"`    // 更新时间
	DeletedAt gorm.DeletedAt `gorm:"type:datetime;column:delete_time;comment:删除时间" sql:"index" json:"-" copier:"DeleteTime"` // 删除时间
}

TimeModel 时间模型

type TimeStruct

type TimeStruct struct {
	CreateTime string // 创建时间
	UpdateTime string // 更新时间
	DeleteTime string // 删除时间
}

TimeStruct 公共时间结构体

type UpdateStruct

type UpdateStruct struct {
	AllowFields []string // 允许字段
	UpdateId    int64    // 更新人ID
	UpdateBy    string   // 更新人
	UpdateTime  string   // 更新时间
}

UpdateStruct 公共更新结构体

带更新时间、ID、姓名、允许字段

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL