kerbalwzygo

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2021 License: GPL-3.0 Imports: 27 Imported by: 0

README

kerbalwzygo

个人使用Golang进行后端开发时封装的一些工具对象

这里封装都是写工具的初级原型, 具体融合到项目中时可能需要做一些修改

懒得封装到兼容性很高, 我个人认为纯粹调包开发非常无聊而且包内有些功能也用不上, 抽空自己造一造轮子其实挺有意思的


  • u_csv
    • func ReadCSV(filepath string) ([][]string, error) 读取CSV文件, 去除了空行
  • u_excel
    • ExcelIllegalCharactersRe 非法字符串正则匹配器
    • ExcelSheet 分页数据对象
    • func (obj *ExcelSheet) Len() int 获取数据行数
    • func (obj *ExcelSheet) SetSafeLimit(n int) 设置单页安全数据行数上限值(最大值=1048576)
    • func (obj *ExcelSheet) Safe() []ExcelSheet 将分页数据对象进行安全转换
    • func MakeExcelFp(data ...ExcelSheet) (*excelize.File, error) 创建Excel文件对象
    • func SafeMakeExcelFp(data ...ExcelSheet) (*excelize.File, error) 安全的创建Excel文件对象
  • u_file
    • func PathOk(path string) (bool, error) 判断文件或者文件夹路径是否正常
    • func ValidFileUTF8(filepath string, checkLines int) (bool, error) 验证文件是否能被UTF8解码
    • func ListDirFiles(dirPath, suffix string) ([]string, error) 获取目录下是所有文件的绝对路径(不含文件夹, 并且可以通过suffix过滤, 当suffix为空字符串或者"*"时表示配匹所有文件尾缀)
  • u_jwt
    • type CustomJWTClaims struct 自定义JWT数据载体
    • func CreateJWTToken(claims CustomJWTClaims, salt []byte) (string, error) 创建JWT-Token字符串
    • func ParseJWTToken(tokenStr string, salt []byte) (*CustomJWTClaims, error) 解析JWT-Token字符串
    • func RefreshJWTToken(tokenStr string, salt []byte, survivalTime time.Duration) (string, error) 刷新JWT-Token字符串;
  • u_logger
    • type Level int 日志级别类型
    • type XLogger struct 日志记录器结构体, 继承了标准库的log.Logger
    • func (obj *XLogger) SetLevel(level Level) 设置日志级别
    • func (obj *XLogger) Level() Level 获取日志级别
    • func (obj *XLogger) Debug(msg ...interface{}) 输出Debug级别的日志
    • func (obj *XLogger) Info(msg ...interface{}) 输出Info级别的日志
    • func (obj *XLogger) Warn(msg ...interface{}) 输出Warn级别的日志
    • func (obj *XLogger) Error(msg ...interface{}) 输出Error级别的日志
    • func GetLogger() *XLogger 获取日志记录器对象,单例模式,默认格式与输出
  • u_rotate_file
    • type RotateFileWriter struct 循环文件写入器, 可以帮助我们自己实现循环文件日志
    • func (obj *RotateFileWriter) Write(p []byte) (n int, err error) 往文件写入数据
    • func NewRotateFileWriter(fileName, dirPath string, maxCount int, maxSize int64) *RotateFileWriter 创建新的循环文件写入对象;
  • u_string
    • func BytesMD5Hash(data []byte) string 获取字节数组的MD5签名
    • func StringMD5Hash(data string) string 获取字符串的MD5签名
    • func MultiStringMD5Hash(data ...string) string 获取多个字符串的MD5签名
    • func StringContainsHan(data string) bool 检查字符串是否包含了中文字符
    • func StringContainsSpace(data string) 检查字符串是否包含了空白字符
    • func SafeSliceString(data string, start, end int) (string, error) 安全的字符串切片
  • u_time
    • BJS 北京时间时区
    • func NowTimestamp() int64 当前时间戳
    • func Timestamp2Datetime(timestamp int64, tz *time.Location) time.Time 时间戳转时间对象
    • func Timestamp2DatetimeStr(timestamp int64, tz *time.Location) string 时间戳转时间字符串
    • func BJSNowDatetimeStr() string 当前北京时间字符串
    • func UTCNowDatetimeStr() string 当前UTC时间字符串
    • func BJSTodayDateStr() string 当前北京时间日期字符串
    • func UTCTodayDateStr() string 当前UTC时间日期字符串
    • func Time2BJS(value time.Time) time.Time 时间对象转换到北京时区;

Documentation

Index

Constants

View Source
const ExcelMaxRowCount = 1048576

Excel单个sheet最多只能有1048576行,超出的行数据将保存到复制了名称的sheet

Variables

View Source
var (
	TokenExpired     = errors.New(" Token is expired")
	TokenNotValidYet = errors.New(" Token not active yet")
	TokenMalformed   = errors.New(" That's not even a token")
	TokenInvalid     = errors.New(" Couldn't handle this token:")
)

Error notes

View Source
var BJS = time.FixedZone("BJS", 8*3600)

Set time zone at UTC/GMT+08:00, China Standard Time UTC+8:00

View Source
var ErrIndexOutOfRange = errors.New("the 'start' or 'end' index value is out of range")
View Source
var ErrStartLargeThenEnd = errors.New("the 'start' index value is over then 'end'")
View Source
var ExcelIllegalCharactersRe = regexp.MustCompile(`[\000-\010]|[\013-\014]|[\016-\037]`)

Functions

func BJSNowDatetimeStr

func BJSNowDatetimeStr() string

func BJSTodayDateStr

func BJSTodayDateStr() string

func BytesMD5Hash

func BytesMD5Hash(data []byte) string

Get the MD5 hash value of bytes

func CreateJWTToken

func CreateJWTToken(claims CustomJWTClaims, salt []byte) (string, error)

生成JWT TOKEN: claims 载体数据, salt加密盐值

func GenerateQrCode

func GenerateQrCode(content string, width, height int) ([]byte, error)

func ListDirFiles

func ListDirFiles(dirPath, suffix string) ([]string, error)

获取目录下是所有文件的绝对路径(不含文件夹, 并且可以通过suffix过滤, 当suffix为空字符串或者"*"时表示配匹所有文件尾缀)

func MakeExcelFp

func MakeExcelFp(data ...ExcelSheet) (*excelize.File, error)

func MultiStringMD5Hash

func MultiStringMD5Hash(data ...string) string

Get the MD5 hash value of multi string

func NowTimestamp

func NowTimestamp() int64

func PathOk

func PathOk(path string) (bool, error)

判断文件或者文件夹路径是否存在

func ReadCSV

func ReadCSV(filepath string) ([][]string, error)

读取csv文件(PS: 去除了空行)

func RefreshJWTToken

func RefreshJWTToken(tokenStr string, salt []byte, survivalTime time.Duration) (string, error)

刷新JWT TOKEN tokenStr TOKEN字符串, salt加密盐值, survivalTime存活时间

func SafeMakeExcelFp

func SafeMakeExcelFp(data ...ExcelSheet) (*excelize.File, error)

func SafeSliceString

func SafeSliceString(data string, start, end int) (string, error)

安全的切割字符串: data原字符串, start切片起点, end切片终点+1 Direct cutting of string may cause character scrambling, because some character could be 3 or 4 byte. Translate the 'string' into 'rune' before cutting could make safe

func StringContainsHan

func StringContainsHan(data string) bool

检查字符串是否包含中文字符

func StringContainsSpace

func StringContainsSpace(data string) bool

检查字符串是否包含空白字符

func StringMD5Hash

func StringMD5Hash(data string) string

Get the MD5 hash value of string

func Time2BJS

func Time2BJS(value time.Time) time.Time

func Timestamp2Datetime

func Timestamp2Datetime(timestamp int64, tz *time.Location) time.Time

func Timestamp2DatetimeStr

func Timestamp2DatetimeStr(timestamp int64, tz *time.Location) string

func UTCNowDatetimeStr

func UTCNowDatetimeStr() string

func UTCTodayDateStr

func UTCTodayDateStr() string

func ValidFileUTF8

func ValidFileUTF8(filepath string, checkLines int) (bool, error)

按行读取文件内容, 并判断是否为UTF8编码

Types

type CustomJWTClaims

type CustomJWTClaims struct {
	CustomData interface{} `json:"custom_data"`
	jwt.StandardClaims
}

自定义载体, CustomData用于保存自定义的数据; jwt.StandardClaims用于存储载体附属信息, 特别是过期时间

func ParseJWTToken

func ParseJWTToken(tokenStr string, salt []byte) (*CustomJWTClaims, error)

解析JWT TOKEN: tokenStr TOKEN字符串, salt加密盐值

type ExcelSheet

type ExcelSheet struct {
	Name    string
	Content [][]interface{}
	// contains filtered or unexported fields
}

func (*ExcelSheet) Len

func (obj *ExcelSheet) Len() int

func (*ExcelSheet) Safe

func (obj *ExcelSheet) Safe() []ExcelSheet

func (*ExcelSheet) SetSafeLimit

func (obj *ExcelSheet) SetSafeLimit(n int)

type Level

type Level int
var (
	Debug Level = 0
	Info  Level = 1
	Warn  Level = 2
	Error Level = 3
)

type RotateFileWriter

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

Custom rotate file writer.

func NewRotateFileWriter

func NewRotateFileWriter(fileName, dirPath string, maxCount int, maxSize int64) *RotateFileWriter

循环文件写入器: fileName基本文件名, dirPath文件夹路径, maxCount最大文件数量, maxSize最大文件体积

func (*RotateFileWriter) Init

func (obj *RotateFileWriter) Init()

Initial method, check whether the log dir existed and have history log files

func (*RotateFileWriter) Write

func (obj *RotateFileWriter) Write(p []byte) (n int, err error)

type XLogger

type XLogger struct {
	log.Logger
	// contains filtered or unexported fields
}

func GetLogger

func GetLogger() *XLogger

func (*XLogger) Debug

func (obj *XLogger) Debug(msg ...interface{})

func (*XLogger) Error

func (obj *XLogger) Error(msg ...interface{})

func (*XLogger) Info

func (obj *XLogger) Info(msg ...interface{})

func (*XLogger) Level

func (obj *XLogger) Level() Level

func (*XLogger) Printf

func (obj *XLogger) Printf(format string, v ...interface{})

Printf rewrite for correct 'calldepth' value

func (*XLogger) SetLevel

func (obj *XLogger) SetLevel(level Level)

func (*XLogger) Warn

func (obj *XLogger) Warn(msg ...interface{})

Jump to

Keyboard shortcuts

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