utils

package
v0.0.0-...-b97b37b Latest Latest
Warning

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

Go to latest
Published: Jul 21, 2023 License: Apache-2.0 Imports: 29 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// 2023-07-19T14:44:45.439291+08:00
	DatetimeFormat  = "2006-01-02 15:04:05"
	DatetimeFormat1 = "2006-01-02 15:04:05.0"
	DatetimeFormat2 = "2006-01-02 15:04:05.00"
	DatetimeFormat3 = "2006-01-02 15:04:05.000"
	DatetimeFormat4 = "2006-01-02 15:04:05.0000"
	DatetimeFormat5 = "2006-01-02 15:04:05.00000"
	DatetimeFormat6 = "2006-01-02 15:04:05.000000"

	NationalDatetimeFormat  = "2006-01-02T15:04:05Z07:00"
	NationalDatetimeFormat1 = "2006-01-02T15:04:05.0Z07:00"
	NationalDatetimeFormat2 = "2006-01-02T15:04:05.00Z07:00"
	NationalDatetimeFormat3 = "2006-01-02T15:04:05.000Z07:00"
	NationalDatetimeFormat4 = "2006-01-02T15:04:05.0000Z07:00"
	NationalDatetimeFormat5 = "2006-01-02T15:04:05.00000Z07:00"
	NationalDatetimeFormat6 = "2006-01-02T15:04:05.000000Z07:00"

	DateFormat = "2006-01-02"

	TimeFormat  = "15:04:05"
	TimeFormat1 = "15:04:05.0"
	TimeFormat2 = "15:04:05.00"
	TimeFormat3 = "15:04:05.000"
	TimeFormat4 = "15:04:05.0000"
	TimeFormat5 = "15:04:05.00000"
	TimeFormat6 = "15:04:05.000000"
)
View Source
const (
	ResponseCodeOk       = 100000
	ResponseCodeTokenErr = 300000
	ResponseCodeErr      = 500000
)
View Source
const (
	DefaultTimeout = 30
)
View Source
const (
	JS_SAFE_MAX_INT = 9007199254740992
)

Variables

View Source
var (
	TokenExpired     error  = errors.New("Token 过期")
	TokenNotValidYet error  = errors.New("Token 没有激活")
	TokenMalformed   error  = errors.New("Token 格式不正确")
	TokenInvalid     error  = errors.New("Token 无效")
	TokenNotFound    error  = errors.New("Token 获取不到")
	SignKey          string = "easydb_api_20211105"
)

一些常量

Functions

func AddrI64

func AddrI64(host string, port int64) string

func AddrToHostPort

func AddrToHostPort(addr string) (string, int64, bool)

获取host port, 是否是host port

func CamelStr

func CamelStr(s string) string

camel string, xx_yy to XxYy

func CamelStrs

func CamelStrs(changeStrs []string) []string

camel string, xx_yy to XxYy

func CheckAndCreateDir

func CheckAndCreateDir(dir string) error

检测和创建目录是否存在

func ConvertAssign

func ConvertAssign(bytes sql.RawBytes, typ *sql.ColumnType) (interface{}, error)

func CopyStruct

func CopyStruct(src, dst interface{})

func Decrypt

func Decrypt(crypted string) (string, error)

AES解密

func DeduplicationStrs

func DeduplicationStrs(strs []string) []string

func Encrypt

func Encrypt(origData string) (string, error)

AES加密

func ErrorsToError

func ErrorsToError(errs ...error) error

func FileAbs

func FileAbs(fileName string) (string, error)

获取文件绝对路径

func FileDir

func FileDir(fileName string) string

func FileInfo

func FileInfo(filePath string) (os.FileInfo, error)

func FileLineCount

func FileLineCount(name string) (int64, error)

func Filename

func Filename(path string) string

func ForwardRequest

func ForwardRequest(c *gin.Context, targetUrl string, timeout int64)

func GenerateHashByString

func GenerateHashByString(_value string) int
通过字符串计算出一个数字的hash值

Params:

_value: 输入的字符串值

func GetExistsStrs

func GetExistsStrs(from []string, to []string) []string

func GetExistsStrsPrefix

func GetExistsStrsPrefix(from []string, to []string, toPrefix string) []string

func GetInterfaceFieldValue

func GetInterfaceFieldValue(v interface{}, fieldName string) (interface{}, error)

func GetRandStr

func GetRandStr(strs []string) string

随机获取一个字符串

func GetSignKey

func GetSignKey() string

获取signKey

func GetStructFieldNames

func GetStructFieldNames(v interface{}) ([]string, error)

func GetStructFieldNamesWithPrefix

func GetStructFieldNamesWithPrefix(v interface{}, prefix string) ([]string, error)

func GetSuffix

func GetSuffix(str string, suffixSep string) string

func GetURLRaw

func GetURLRaw(url, query string, hearders map[string]string) ([]byte, error)

Get请求

func HasPrefixAll

func HasPrefixAll(strs []string, prefix string) bool

func Hostname

func Hostname() (string, error)

func InterfaceToStruct

func InterfaceToStruct(i interface{}, mode interface{}) (interface{}, error)

func InterfaceToUrlQuery

func InterfaceToUrlQuery(data interface{}, ignoreEmpty bool) (string, error)

func IsNil

func IsNil(i interface{}) bool

func MakeDirAll

func MakeDirAll(dir string) error

func ObjToMap

func ObjToMap(data interface{}) (map[string]interface{}, error)

func PKCS5Padding

func PKCS5Padding(ciphertext []byte, blockSize int) []byte

func PKCS5UnPadding

func PKCS5UnPadding(origData []byte) []byte

func PathExists

func PathExists(p string) (bool, error)

文件/目录 是否存在

func PostURLRaw

func PostURLRaw(url string, data interface{}, hearders map[string]string, timeout int64) ([]byte, error)

Post请求

func PostURLRaw2xx

func PostURLRaw2xx(url string, data interface{}, hearders map[string]string) ([]byte, error)

func PostUploadRaw

func PostUploadRaw(url string, filename string, hearders map[string]string) ([]byte, error)

func RandN

func RandN(n int) int

func RandNInt64

func RandNInt64(n int64) int64

func RandString

func RandString(n int) string

func ReadBinaryFile

func ReadBinaryFile(filename string) ([]byte, error)

func ReflectSetNotDefaultFieldValue

func ReflectSetNotDefaultFieldValue(prefix string, baseObj, modifyObj interface{}) error

将不是默认字段值, 设置为值定的字段值

func RemoveFile

func RemoveFile(filePath string) error

func ReplaceAllStrs

func ReplaceAllStrs(oldStrs []string, old string, new string) []string

func ReturnError

func ReturnError(c *gin.Context, code int, err error)

返回错误

func ReturnFile

func ReturnFile(c *gin.Context, fileName string, path string)

返回文件信息

func ReturnList

func ReturnList(c *gin.Context, obj interface{}, total int)

成功并放回数据

func ReturnListError

func ReturnListError(c *gin.Context, code int, err error)

返回列表数据的错误

func ReturnSuccess

func ReturnSuccess(c *gin.Context, obj interface{})

成功并放回数据

func SetInterfaceFieldValue

func SetInterfaceFieldValue(v interface{}, fieldName string, fieldValue interface{}) error

func SetObjFieldFromOtherObj

func SetObjFieldFromOtherObj(fieldName string, fromObj interface{}, toObj interface{}) error

func SetObjFieldsFromOtherObj

func SetObjFieldsFromOtherObj(fieldNames []string, fromObj interface{}, toObj interface{}) error

func SetSignKey

func SetSignKey(key string) string

这是SignKey

func SnakeStr

func SnakeStr(s string) string

XxYy to xx_yy , XxYY to xx_yy

func SnakeStrs

func SnakeStrs(changeStrs []string) []string

XxYy to xx_yy , XxYY to xx_yy

func SplitStringAndFilterEmpty

func SplitStringAndFilterEmpty(data string, sep string) []string

func SqlConvertStrToNormalDateStr

func SqlConvertStrToNormalDateStr(ori string, scale int64) (string, error)

func SqlConvertStrToNormalDatetimeStr

func SqlConvertStrToNormalDatetimeStr(ori string, scale int64) (string, error)

func SqlConvertStrToNormalTimeStr

func SqlConvertStrToNormalTimeStr(ori string, scale int64) (string, error)

func StrToInt64

func StrToInt64(data string) (int64, error)

func StrToTime

func StrToTime(formatStr, timeStr string, zone *time.Location) (time.Time, error)

func StrsToInt64s

func StrsToInt64s(datas []string) ([]int64, error)

func StrsToMap

func StrsToMap(strs []string) map[string]struct{}

func StrsToUnique

func StrsToUnique(strs []string) []string

func SuffixIsNumAll

func SuffixIsNumAll(strs []string, suffixSep string) bool

func ToJsonStr

func ToJsonStr(data interface{}) string

func ToJsonStrPretty

func ToJsonStrPretty(data interface{}) string

func WriteDataToFile

func WriteDataToFile(filePath string, data []byte) (int, error)

func WriteStringsToFile

func WriteStringsToFile(datas []string, filename string) error

func ZipCompress

func ZipCompress(filePath, dest string) error

Types

type CustomClaims

type CustomClaims struct {
	Username string `json:"username" request:"username"`
	Password string `json:"password" request:"password"`
	Role     string `json:"role" request:"role"`
	jwt.StandardClaims
}

载荷,可以加一些自己需要的信息

func NewCustomClaims

func NewCustomClaims(username, password, role string) *CustomClaims

func TokenToClaims

func TokenToClaims(token string) (*CustomClaims, error)

type JWT

type JWT struct {
	SigningKey []byte
}

JWT 签名结构

func NewJWT

func NewJWT() *JWT

func (*JWT) CreateToken

func (j *JWT) CreateToken(claims CustomClaims) (string, error)

CreateToken 生成一个token

func (*JWT) ParseToken

func (j *JWT) ParseToken(tokenString string) (*CustomClaims, error)

解析Tokne

func (*JWT) RefreshToken

func (j *JWT) RefreshToken(tokenString string) (string, error)

更新token

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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