utils

package
v0.0.0-...-1fffc49 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2025 License: MIT Imports: 32 Imported by: 0

Documentation

Index

Constants

View Source
const (
	FMT_DATE_TIME    = "2006-01-02 15:04:05"
	FMT_DATE         = "2006-01-02"
	FMT_TIME         = "15:04:05"
	FMT_DATE_TIME_CN = "2006年01月02日 15时04分05秒"
	FMT_DATE_CN      = "2006年01月02日"
	FMT_TIME_CN      = "15时04分05秒"
)
View Source
const (
	Minute = 60
	Hour   = 60 * Minute
	Day    = 24 * Hour
	Week   = 7 * Day
	Month  = 30 * Day
	Year   = 12 * Month
)

Seconds-based time units

View Source
const CODE62 = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
View Source
const CodeLength = 62

Variables

View Source
var (
	ErrorTopicNotFound    = NewError(-1, "话题不存在")
	ErrorArticleNotFound  = NewError(-2, "文章不存在")
	ErrorTagNotFound      = NewError(-3, "标签不存在")
	ErrorPermissionDenied = NewError(-100, "Permission denied.")
	ErrorNotLogin         = NewError(-200, "请先登录")
)
View Source
var CodeMap = map[string]int64{"0": 0, "1": 1, "2": 2, "3": 3, "4": 4, "5": 5, "6": 6, "7": 7, "8": 8, "9": 9, "a": 10,
	"b": 11, "redis": 12, "d": 13, "e": 14, "f": 15, "g": 16, "h": 17, "i": 18, "j": 19, "k": 20, "l": 21, "m": 22,
	"n": 23, "o": 24, "p": 25, "q": 26, "r": 27, "s": 28, "t": 29, "u": 30, "v": 31, "w": 32, "x": 33, "y": 34,
	"z": 35, "A": 36, "B": 37, "C": 38, "D": 39, "E": 40, "F": 41, "G": 42, "H": 43, "I": 44, "J": 45, "K": 46,
	"L": 47, "M": 48, "N": 49, "O": 50, "P": 51, "Q": 52, "R": 53, "S": 54, "T": 55, "U": 56, "V": 57, "W": 58,
	"X": 59, "Y": 60, "Z": 61}
View Source
var MediaTypeSuffixMap = map[string]string{
	"image/png":       "png",
	"image/jpeg":      "jpeg",
	"image/bmp":       "bmp",
	"video/avi":       "avi",
	"video/mp4":       "mp4",
	"audio/mpeg":      "mp3",
	"audio/wave":      "wav",
	"application/pdf": "pdf",
}

Functions

func AppendLine

func AppendLine(path string, content string) errors.WTError

func ApplyImageStyle

func ApplyImageStyle(imageUrl, styleName string) string

应用图片样式

func CheckIPInList

func CheckIPInList(ip string, ipList []string, canAll bool, canIP bool, canCIDR bool) bool

func Close

func Close(f Closer)

func Contains

func Contains(search interface{}, target interface{}) bool

func ContainsIgnoreCase

func ContainsIgnoreCase(search string, target []string) bool

func Decode62

func Decode62(str string) int64

*

  • 解码字符串为整数

func DecodeFromBase62

func DecodeFromBase62(encoded string) []byte

func Encode62

func Encode62(number int64) string

*

  • 编码 整数 为 base62 字符串

func EncodeBase62

func EncodeBase62(num uint64) string

func EncodeToBase62

func EncodeToBase62(data []byte) string

func FormatJson

func FormatJson(obj interface{}) (str string, err errors.WTError)

func GenerateRandomInt

func GenerateRandomInt(min, max int) int64

func GenerateRandomText

func GenerateRandomText(length int) string

func GenerateUniqueNumber

func GenerateUniqueNumber(length int) string

func GetDay

func GetDay(time time.Time) int

return yyyyMMdd

func GetHtmlSummary

func GetHtmlSummary(text string) string

截取html摘要

func GetMediaType

func GetMediaType(data []byte) string

func GetSummary

func GetSummary(content string) (summary string)

func GetTargetIP

func GetTargetIP(r *http.Request) (ip string)

func GetUserName

func GetUserName(defaultName string, username sql.NullString, nickname sql.NullString) string

func GetUserNameByPhone

func GetUserNameByPhone(phone string, username sql.NullString, nickname sql.NullString) string

func HashSHA1

func HashSHA1(s string) string

func HashSHA256

func HashSHA256(str string) string

func HashSHA256WithBase62

func HashSHA256WithBase62(str string) string

func InList

func InList[T string | int64](lst []T, element T) bool

func IndexOf

func IndexOf(userIds []int64, userId int64) int

index of

func IsAcceptType

func IsAcceptType(fileType string) bool

func IsAudio

func IsAudio(fileType string) bool

func IsEmpty

func IsEmpty(a interface{}) bool

func IsIPInCIDR

func IsIPInCIDR(ip string, cidr string) bool

func IsImage

func IsImage(fileType string) bool

func IsJsonString

func IsJsonString(data string) bool

func IsLocalIP

func IsLocalIP(ip string) bool

func IsNetClose

func IsNetClose(err error) bool

func IsPdf

func IsPdf(fileType string) bool

func IsProd

func IsProd() bool

是否是正式环境

func IsValidateEmail

func IsValidateEmail(email string) (err errors.WTError)

验证是否是合法的邮箱

func IsValidatePassword

func IsValidatePassword(password, rePassword string) errors.WTError

是否是合法的密码

func IsValidateUrl

func IsValidateUrl(url string) errors.WTError

是否是合法的URL

func IsValidateUsername

func IsValidateUsername(username string) errors.WTError

验证用户名合法性,用户名必须由5-12位(数字、字母、_、-)组成,且必须以字母开头。

func IsVideo

func IsVideo(fileType string) bool

func JoinIntToString

func JoinIntToString(lst []int64, seq string, removeDuplicates bool) string

func JoinStringToString

func JoinStringToString(lst []string, seq string, removeDuplicates bool) string

func JsonMarshal

func JsonMarshal(v interface{}) ([]byte, errors.WTError)

func JsonUnmarshal

func JsonUnmarshal(data []byte, v interface{}) errors.WTError

func JustAudio

func JustAudio(fileType string) bool

func JustImage

func JustImage(fileType string) bool

func JustVideo

func JustVideo(fileType string) bool

func ListConvert

func ListConvert[T1, T2 string | int64](lst []T1, m map[T1]T2) []T2

func ListConvertMust

func ListConvertMust[T1, T2 string | int64](lst []T1, m map[T1]T2) ([]T2, errors.WTError)

func MD5

func MD5(str string) string

func MD5Bytes

func MD5Bytes(data []byte) string

func MapToStruct

func MapToStruct(obj interface{}, data map[string]interface{}) errors.WTError

func MurmurHashWithBase62

func MurmurHashWithBase62(s string) string

编码用的字符集

func MustNotError

func MustNotError(err error)

func NowTimestamp

func NowTimestamp() int64

毫秒时间戳

func NowUnix

func NowUnix() int64

秒时间戳

func OnceFunc

func OnceFunc(f func()) func()

func ParseJson

func ParseJson(str string, t interface{}) errors.WTError

func ParseTagsToArray

func ParseTagsToArray(tags string) []string

处理标签

func PathExists

func PathExists(path string) (bool, errors.WTError)

func ReadRsaPrivateKey

func ReadRsaPrivateKey(c []byte) (*rsa.PrivateKey, errors.WTError)

func ReadRsaPubKeyFromCert

func ReadRsaPubKeyFromCert(c []byte) (*rsa.PublicKey, errors.WTError)

func ReadRsaPublicKey

func ReadRsaPublicKey(c []byte) (*rsa.PublicKey, errors.WTError)

func Recover

func Recover(logger Logger, err *errors.WTError, msg string)

func RemoveDuplicates

func RemoveDuplicates[T string | int64](lst []T) []T

func RemoveTrailingNil

func RemoveTrailingNil[T any](lst []T, isZero func(T) bool) []T

func SignRsaHash256Sign

func SignRsaHash256Sign(data string, privateKey *rsa.PrivateKey) ([]byte, errors.WTError)

func SplitStringToInt

func SplitStringToInt(lst string, seq string) []int64

func SqlNullString

func SqlNullString(value string) sql.NullString

func StringIn

func StringIn(dst string, lst []string) bool

func StructFields

func StructFields(s interface{}) []reflect.StructField

获取struct字段

func StructName

func StructName(s interface{}) string

获取struct name

func StructToMap

func StructToMap(obj interface{}, excludes ...string) map[string]interface{}

func StructTypeOf

func StructTypeOf(s interface{}) reflect.Type

func TimeFormat

func TimeFormat(time time.Time, layout string) string

时间格式化

func TimeFromTimestamp

func TimeFromTimestamp(timestamp int64) time.Time

毫秒时间戳转时间

func TimeFromUnix

func TimeFromUnix(unix int64) time.Time

秒时间戳转时间

func TimeParse

func TimeParse(timeStr, layout string) (time.Time, errors.WTError)

字符串时间转时间类型

func TimeSincePro

func TimeSincePro(then time.Time) string

TimeSincePro calculates the time interval and generate full user-friendly string.

func Timestamp

func Timestamp(t time.Time) int64

毫秒时间戳

func TruncateString

func TruncateString(s string, n int64) string

func VerifyRsaHash256Sign

func VerifyRsaHash256Sign(data string, signature []byte, publicKey *rsa.PublicKey) errors.WTError

func WithTimeAsStartOfDay

func WithTimeAsStartOfDay(t time.Time) time.Time

返回指定时间当天的开始时间

func WriteString

func WriteString(path string, content string, append bool) errors.WTError

Types

type BaseFunc

type BaseFunc func()

func RecoverFunc

func RecoverFunc(f BaseFunc, l Logger, msg string) BaseFunc

type Closer

type Closer interface {
	Close() error
}

type CodeError

type CodeError struct {
	errors.WTError
	Data interface{}
}

func FromError

func FromError(err error) *CodeError

func NewError

func NewError(code int, text string) *CodeError

func NewErrorData

func NewErrorData(code int, text string, data interface{}) *CodeError

func NewErrorMsg

func NewErrorMsg(text string) *CodeError

func (*CodeError) CodeInt

func (e *CodeError) CodeInt() int64

type Logger

type Logger interface {
	Error(string, ...any)
}

type UrlBuilder

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

func ParseUrl

func ParseUrl(rawUrl string) *UrlBuilder

func (*UrlBuilder) AddQueries

func (builder *UrlBuilder) AddQueries(queries map[string]string) *UrlBuilder

func (*UrlBuilder) AddQuery

func (builder *UrlBuilder) AddQuery(name, value string) *UrlBuilder

func (*UrlBuilder) Build

func (builder *UrlBuilder) Build() *url.URL

func (*UrlBuilder) BuildStr

func (builder *UrlBuilder) BuildStr() string

func (*UrlBuilder) GetQuery

func (builder *UrlBuilder) GetQuery() url.Values

func (*UrlBuilder) GetURL

func (builder *UrlBuilder) GetURL() *url.URL

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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