utils

package
v0.0.0-...-79307e8 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2021 License: MIT Imports: 31 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TextBlack = iota + 30
	TextRed
	TextGreen
	TextYellow
	TextBlue
	TextMagenta
	TextCyan
	TextWhite
)
View Source
const (
	TrafficKey = "X-Request-Id"
	LoggerKey  = "_kobh-logger-request"
)

Variables

This section is empty.

Functions

func Assert

func Assert(condition bool, msg string, code ...int)

Assert 条件断言 当断言条件为 假 时触发 panic 对于当前请求不会再执行接下来的代码,并且返回指定格式的错误信息和错误码

func Base64ToImage

func Base64ToImage(imageBase64 string) ([]byte, error)

func Black

func Black(msg string) string

func Blue

func Blue(msg string) string

func CheckExist

func CheckExist(src string) bool

CheckExist 检查文件是否存在

func CheckPermission

func CheckPermission(src string) bool

CheckPermission 检查文件权限

func CompareHashAndPassword

func CompareHashAndPassword(e string, p string) (bool, error)

func Crc16Hash

func Crc16Hash(src string) string

Crc16Hash 用于16张分表

func Crc32Hash

func Crc32Hash(src string) string

Crc32Hash 用于32张分表

func Crc8Hash

func Crc8Hash(src string) string

Crc8Hash 用于8张分表

func CreateSubTable

func CreateSubTable(f func(string) string)

func Cyan

func Cyan(msg string) string

func DynamicTable

func DynamicTable(f func(string) string, baseTable, fieldValue string) func(db *gorm.DB) *gorm.DB

DynamicTable 动态表名 e.g. DB.Scopes(DynamicTable(Crc32Hash, "test", "小圈圈")).Find(&tests) 设置动态表名scope params: f 分表计算函数 baseTable 基础表名 fieldValue 参与分表字段

func FileCreate

func FileCreate(content bytes.Buffer, name string)

func FileMonitoringById

func FileMonitoringById(ctx context.Context, filePth string, id string, group string, hookfn func(context.Context, string, string, []byte))

func GenerateMsgIDFromContext

func GenerateMsgIDFromContext(c *gin.Context) string

GenerateMsgIDFromContext 生成msgID

func Get

func Get(url string) (string, error)

发送GET请求 url: 请求地址 response: 请求返回的内容

func GetClientIP

func GetClientIP(c *gin.Context) string

func GetCurrentPath

func GetCurrentPath() string

获取当前路径,比如:E:/abc/data/test

func GetCurrentTime

func GetCurrentTime() time.Time

func GetCurrentTimeStamp

func GetCurrentTimeStamp() int64

func GetCurrentTimeStr

func GetCurrentTimeStr() string

func GetDirFiles

func GetDirFiles(dir string) ([]string, error)

func GetExt

func GetExt(fileName string) string

GetExt 获取文件后缀

func GetFileSize

func GetFileSize(filename string) int64

获取文件大小

func GetImgType

func GetImgType(p string) (string, error)

GetImgType 获取Img文件类型

func GetLocaHonst

func GetLocaHonst() string

获取局域网ip地址

func GetLocation

func GetLocation(ip, key string) string

获取外网ip地址

func GetSize

func GetSize(f multipart.File) (int, error)

GetSize 获取文件大小

func GetType

func GetType(p string) (string, error)

GetType 获取文件类型

func GetUUID

func GetUUID() string

func Green

func Green(msg string) string

func HasError

func HasError(err error, msg string, code ...int)

HasError 错误断言 当 error 不为 nil 时触发 panic 对于当前请求不会再执行接下来的代码,并且返回指定格式的错误信息和错误码 若 msg 为空,则默认为 error 中的内容

func Hmac

func Hmac(data string) string

func Int64ToString

func Int64ToString(e int64) string

func IntToString

func IntToString(e int) string

func IsNotExistMkDir

func IsNotExistMkDir(src string) error

IsNotExistMkDir 检查文件夹是否存在 如果不存在则新建文件夹

func IsStringEmpty

func IsStringEmpty(str string) bool

func Magenta

func Magenta(msg string) string

func MkDir

func MkDir(src string) error

MkDir 新建文件夹

func Open

func Open(name string, flag int, perm os.FileMode) (*os.File, error)

Open 打开文件

func PathCreate

func PathCreate(dir string) error

func PathExist

func PathExist(addr string) bool

PathExist 判断目录是否存在

func PathExists

func PathExists(path string) bool

func Post

func Post(url string, data interface{}, contentType string) ([]byte, error)

发送POST请求 url: 请求地址 data: POST请求提交的数据 contentType: 请求体格式,如:application/json content: 请求放回的内容

func Red

func Red(msg string) string

func RemoveRepByMap

func RemoveRepByMap(slc []string) []string

slice去重

func Round

func Round(f float64, n int) float64

func SetColor

func SetColor(msg string, conf, bg, text int) string

func StringToInt

func StringToInt(e string) (int, error)

func StructToJsonStr

func StructToJsonStr(e interface{}) (string, error)

func Translate

func Translate(form, to interface{})

func UIntToString

func UIntToString(e uint) string

func White

func White(msg string) string

func Yellow

func Yellow(msg string) string

Types

type APIException

type APIException struct {
	Code      int         `json:"code"`
	Success   bool        `json:"success"`
	Msg       string      `json:"msg"`
	Timestamp int64       `json:"timestamp"`
	Result    interface{} `json:"result"`
}

api结构体

func AuthError

func AuthError(message string) *APIException

授权错误

func NotFound

func NotFound() *APIException

404 错误

func ParameterError

func ParameterError(message string) *APIException

参数错误

func ResponseJson

func ResponseJson(message string, data interface{}, success bool) *APIException

200

func ServerError

func ServerError() *APIException

500 错误处理

func UnknownError

func UnknownError(message string) *APIException

未知错误

func (*APIException) Error

func (e *APIException) Error() string

实现接口

type JSONDate

type JSONDate struct {
	time.Time
}

func (JSONDate) MarshalJSON

func (d JSONDate) MarshalJSON() ([]byte, error)

func (*JSONDate) UnmarshalJSON

func (d *JSONDate) UnmarshalJSON(b []byte) error

type JSONTime

type JSONTime struct {
	time.Time
}

JSONTime format json time field by myself

func (JSONTime) MarshalJSON

func (t JSONTime) MarshalJSON() ([]byte, error)

MarshalJSON on JSONTime format Time field with %Y-%m-%d %H:%M:%S

func (*JSONTime) Scan

func (t *JSONTime) Scan(v interface{}) error

Scan valueof time.Time

func (*JSONTime) UnmarshalJSON

func (t *JSONTime) UnmarshalJSON(b []byte) error

func (JSONTime) Value

func (t JSONTime) Value() (driver.Value, error)

Value insert timestamp into mysql need this function.

type Mode

type Mode string
const (
	ModeDev  Mode = "dev"    //开发模式
	ModeTest Mode = "test"   //测试模式
	ModeProd Mode = "prod"   //生产模式
	Mysql         = "mysql"  //mysql数据库标识
	Sqlite        = "sqlite" //sqlite
)

func (Mode) String

func (e Mode) String() string

type ReplaceHelper

type ReplaceHelper struct {
	Root    string //路径
	OldText string //需要替换的文本
	NewText string //新的文本
}

func (*ReplaceHelper) DoWrok

func (h *ReplaceHelper) DoWrok() error

Jump to

Keyboard shortcuts

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