common

package module
v0.0.0-...-000cbdd Latest Latest
Warning

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

Go to latest
Published: Dec 24, 2020 License: Apache-2.0 Imports: 38 Imported by: 0

README

duduCommon

收集的经常用到的一些轮子

感谢名单:

感谢beego的各位开发人员,提取了beego的模板函数
感谢各位分享的朋友,因为很多函数都是网上搜集而来的。

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Aes   DuduAes
	File  DuduFile
	Go    DuduGo
	Log   DuduLog
	Net   DuduNet
	OS    DuduOS
	Panic DuduPanic
	Rand  DuduRand
	Ret   DuduRet
	Rune  DuduRune
	Str   DuduStr
	Tpl   DuduTpl
	Zip   DuduZip
	Url   DuduUrl
)

Functions

func AesDecrypt

func AesDecrypt(crypted, key []byte) (res []byte, err error)

func AesEncrypt

func AesEncrypt(origData, key []byte) ([]byte, error)

func DeepFields

func DeepFields(ifaceType reflect.Type) []reflect.StructField

func PKCS5Padding

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

func PKCS5UnPadding

func PKCS5UnPadding(origData []byte) []byte

func StructCopy

func StructCopy(DstStructPtr interface{}, SrcStructPtr interface{})

func ZeroPadding

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

func ZeroUnPadding

func ZeroUnPadding(origData []byte) []byte

Types

type BaiDuData

type BaiDuData struct {
	Remain  int `json:"remain"`
	Success int `json:"success"`
}

百度主动提交

type DuduAes

type DuduAes byte

func (*DuduAes) Decrypt

func (*DuduAes) Decrypt(content string, securityKey string) (string, error)

解密算法(AES+CBC)

func (*DuduAes) Encrypt

func (*DuduAes) Encrypt(content string, securityKey string) (string, error)

加密算法(AES+CBC),结果转换成16进制,防止数据库出错

type DuduFile

type DuduFile byte

func (*DuduFile) CopyDir

func (*DuduFile) CopyDir(source string, dest string) (err error)

复制文件夹到指定位置

func (*DuduFile) CopyFile

func (*DuduFile) CopyFile(source string, dest string) (err error)

复制文件到目标地址

func (*DuduFile) GetAppName

func (*DuduFile) GetAppName() string

获取应用文件名

func (*DuduFile) GetConfigPath

func (*DuduFile) GetConfigPath() string

获取config路径

func (*DuduFile) GetCurrentDirectory

func (*DuduFile) GetCurrentDirectory() string

获取当前路径目录

func (*DuduFile) GetFileContentType

func (*DuduFile) GetFileContentType(out *os.File) (string, error)

获取文件类型

func (*DuduFile) GetFileSize

func (*DuduFile) GetFileSize(path string) (int64, error)

获取文件大小

func (*DuduFile) GetProjectPath

func (*DuduFile) GetProjectPath() string

获取项目路径

func (*DuduFile) IsBinary

func (*DuduFile) IsBinary(content string) bool

判断文件是否为二进制文件

func (*DuduFile) IsDir

func (*DuduFile) IsDir(path string) bool

判断是否为目录

func (*DuduFile) IsExist

func (*DuduFile) IsExist(path string) bool

判断文件是否存在

func (*DuduFile) Mkdir

func (*DuduFile) Mkdir(path string) bool

根据路据创建文件夹

type DuduGo

type DuduGo byte

func (*DuduGo) GetApiPath

func (*DuduGo) GetApiPath() string

GetAPIPath gets the Go source code path $GOROOT/src.

func (*DuduGo) GetExecutableInGOBIN

func (*DuduGo) GetExecutableInGOBIN(executable string) string

GetExecutableInGOBIN gets executable file under GOBIN path.

The specified executable should not with extension, this function will append .exe if on Windows.

func (*DuduGo) GetGoFormats

func (*DuduGo) GetGoFormats() []string

GetGoFormats gets Go format tools. It may return ["gofmt", "goimports"].

func (*DuduGo) IsAPI

func (*DuduGo) IsAPI(path string) bool

IsAPI determines whether the specified path belongs to Go API.

type DuduLog

type DuduLog byte

func (*DuduLog) Init

func (*DuduLog) Init(conf LogConfig) *zap.Logger

Init 初始化,生成的日志文件夹名字

func (*DuduLog) Instance

func (*DuduLog) Instance() *zap.Logger

Instance 唯一实例

func (*DuduLog) NewLogger

func (*DuduLog) NewLogger(opts ...Option) *zap.Logger

NewLogger 新建日志

type DuduNet

type DuduNet byte

func (*DuduNet) GetIp

func (*DuduNet) GetIp(req *http.Request) string

GetIp 返回远程客户端的 IP,如 192.168.1.1

func (*DuduNet) Ip2long

func (*DuduNet) Ip2long(ipstr string) uint32

Ip2long 将 IPv4 字符串形式转为 uint32

func (*DuduNet) LocalIP

func (*DuduNet) LocalIP() (string, error)

LocalIP gets the first NIC's IP address.

func (*DuduNet) LocalMac

func (*DuduNet) LocalMac() (string, error)

LocalMac gets the first NIC's MAC address.

type DuduOS

type DuduOS byte

func (*DuduOS) Home

func (*DuduOS) Home() (string, error)

Home returns the home directory for the executing user.

This uses an OS-specific method for discovering the home directory. An error is returned if a home directory cannot be detected.

func (*DuduOS) IsWindows

func (*DuduOS) IsWindows() bool

IsWindows determines whether current OS is Windows.

func (*DuduOS) Pwd

func (*DuduOS) Pwd() string

Pwd gets the path of current working directory.

type DuduPanic

type DuduPanic byte

func (*DuduPanic) Recover

func (*DuduPanic) Recover(err *error)

Recover recovers a panic.

type DuduRand

type DuduRand byte

func (*DuduRand) Int

func (*DuduRand) Int(min, max int) int

func (*DuduRand) Int64

func (*DuduRand) Int64(min, max int64) int64

随机区间值

func (*DuduRand) IntAdd

func (*DuduRand) IntAdd(min, max int) int

func (*DuduRand) IntNoRepeat

func (*DuduRand) IntNoRepeat(start int, end int, count int) []int

生成count个[start,end)结束的不重复的随机数

func (*DuduRand) Ints

func (*DuduRand) Ints(from, to, size int) []int

Ints returns a random integer array with the specified from, to and size.

func (*DuduRand) String

func (*DuduRand) String(length int) string

String returns a random string ['a', 'z'] in the specified length

func (*DuduRand) StringByNum

func (*DuduRand) StringByNum(lenString int) string

随机几位字符串

func (*DuduRand) StringByStr

func (*DuduRand) StringByStr(lenString int) string

随机几位字符串

type DuduRet

type DuduRet byte

func (*DuduRet) NewResult

func (*DuduRet) NewResult() *Result

NewResult creates a result with Code=0, Msg="", Data=nil.

func (*DuduRet) RetGzJSON

func (*DuduRet) RetGzJSON(w http.ResponseWriter, r *http.Request, res map[string]interface{})

RetGzJSON writes HTTP response with "Content-Type, application/json" and "Content-Encoding, gzip".

func (*DuduRet) RetGzResult

func (*DuduRet) RetGzResult(w http.ResponseWriter, r *http.Request, res *Result)

RetGzResult writes HTTP response with "Content-Type, application/json" and "Content-Encoding, gzip".

func (*DuduRet) RetJSON

func (*DuduRet) RetJSON(w http.ResponseWriter, r *http.Request, res map[string]interface{})

RetJSON writes HTTP response with "Content-Type, application/json".

func (*DuduRet) RetResult

func (*DuduRet) RetResult(w http.ResponseWriter, r *http.Request, res *Result)

RetResult writes HTTP response with "Content-Type, application/json".

type DuduRune

type DuduRune byte

func (*DuduRune) IsLetter

func (*DuduRune) IsLetter(r rune) bool

IsLetter checks the specified rune is letter.

func (*DuduRune) IsNumOrLetter

func (*DuduRune) IsNumOrLetter(r rune) bool

IsNumOrLetter checks the specified rune is number or letter.

type DuduStr

type DuduStr byte

func (*DuduStr) Contains

func (*DuduStr) Contains(str string, strs []string) bool

Contains determines whether the str is in the strs.

func (*DuduStr) FromBytes

func (*DuduStr) FromBytes(bytes []byte) string

FromBytes converts the specified byte array to a string.

func (*DuduStr) IsMobile

func (*DuduStr) IsMobile(phone string) int32

判断是否为手机号码

func (*DuduStr) LCS

func (*DuduStr) LCS(s1 string, s2 string) string

LCS gets the longest common substring of s1 and s2.

Refers to http://en.wikibooks.org/wiki/Algorithm_Implementation/Strings/Longest_common_substring.

func (*DuduStr) Md5Str

func (*DuduStr) Md5Str(str string) string

字符串生成md5

func (*DuduStr) StringToHtml

func (*DuduStr) StringToHtml(text string) string

字符串转HTML编码

func (*DuduStr) ToBytes

func (*DuduStr) ToBytes(str string) []byte

Bytes converts the specified str to a byte array.

type DuduTpl

type DuduTpl byte

func (*DuduTpl) AssetsCSS

func (*DuduTpl) AssetsCSS(text string) template.HTML

AssetsCSS returns stylesheet link tag with src string.

func (*DuduTpl) AssetsJs

func (*DuduTpl) AssetsJs(text string) template.HTML

AssetsJs returns script tag with src string.

func (*DuduTpl) Compare

func (*DuduTpl) Compare(a, b interface{}) (equal bool)

Compare is a quick and dirty comparison function. It will convert whatever you give it to strings and see if the two values are equal. Whitespace is trimmed. Used by the template parser as "eq".

func (*DuduTpl) CompareNot

func (*DuduTpl) CompareNot(a, b interface{}) (equal bool)

CompareNot !Compare

func (*DuduTpl) Date

func (*DuduTpl) Date(t time.Time, format string) string

Date takes a PHP like date func to Go's time format.

func (*DuduTpl) DateDayRand

func (*DuduTpl) DateDayRand(t time.Time, format string) string

func (*DuduTpl) DateFormat

func (*DuduTpl) DateFormat(t time.Time, layout string) (datestring string)

DateFormat takes a time and a layout string and returns a string with the formatted date. Used by the template parser as "dateformat"

func (*DuduTpl) DateParse

func (*DuduTpl) DateParse(dateString, format string) (time.Time, error)

DateParse Parse Date use PHP time format.

func (*DuduTpl) HTML2str

func (*DuduTpl) HTML2str(html string) string

HTML2str returns escaping text convert from html.

func (*DuduTpl) HighLight

func (*DuduTpl) HighLight(content string) string

自动高亮

func (*DuduTpl) HtmlFilter

func (*DuduTpl) HtmlFilter(content string) string

替换把换行符与回车符,换成BR标签

func (*DuduTpl) Htmlquote

func (*DuduTpl) Htmlquote(text string) string

Htmlquote returns quoted html string.

func (*DuduTpl) Htmlunquote

func (*DuduTpl) Htmlunquote(text string) string

Htmlunquote returns unquoted html string.

func (*DuduTpl) MapGet

func (*DuduTpl) MapGet(arg1 interface{}, arg2 ...interface{}) (interface{}, error)

MapGet getting value from map by keys usage:

Data["m"] = M{
    "a": 1,
    "1": map[string]float64{
        "c": 4,
    },
}

{{ map_get m "a" }} // return 1 {{ map_get m 1 "c" }} // return 4

func (*DuduTpl) NotNil

func (*DuduTpl) NotNil(a interface{}) (isNil bool)

NotNil the same as CompareNot

func (*DuduTpl) ParseForm

func (*DuduTpl) ParseForm(form url.Values, obj interface{}) error

ParseForm will parse form values to struct via tag.

func (*DuduTpl) RenderForm

func (*DuduTpl) RenderForm(obj interface{}) template.HTML

RenderForm will render object to form html. obj must be a struct pointer.

func (*DuduTpl) Str2html

func (*DuduTpl) Str2html(raw string) template.HTML

Str2html Convert string to template.HTML type.

func (*DuduTpl) Substr

func (*DuduTpl) Substr(s string, start, length int) string

Substr returns the substr from start to length.

type DuduUrl

type DuduUrl byte

func (*DuduUrl) GetBaiDu

func (*DuduUrl) GetBaiDu(domain string, urls []string) bool

百度ping提交

func (*DuduUrl) GetDomain

func (*DuduUrl) GetDomain(url string) (string, string, string)

分离域名,提取二级域名,主域名,端口号

func (*DuduUrl) GetDomainByRegexp

func (*DuduUrl) GetDomainByRegexp(url string, domainList []string) (string, string, string, error)

通过正则分离域名,提取二级域名,主域名,端口号

func (*DuduUrl) PostBaiDu

func (*DuduUrl) PostBaiDu(dataType int, domain, token string, urls []string) (int, int)

func (*DuduUrl) PostSm

func (*DuduUrl) PostSm(userName, domain, token string, urls []string) int

type DuduZip

type DuduZip byte

func (*DuduZip) Create

func (*DuduZip) Create(filename string) (*ZipFile, error)

Create creates a zip file with the specified filename.

func (*DuduZip) Unzip

func (*DuduZip) Unzip(zipFilePath, destination string) error

Unzip extracts a zip file specified by the zipFilePath to the destination.

type LogConfig

type LogConfig struct {
	Path    string `yaml:"path"`
	Name    string `yaml:"name"`
	Level   string `yaml:"level"`
	MaxSize int    `yaml:"max_size"`
	MaxAge  int    `yaml:"max_age"`

	Stacktrace string `yaml:"stacktrace"`
	Stdout     bool   `yaml:"stdout"`
}

type Option

type Option func(*LogConfig)

type Result

type Result struct {
	Code int         `json:"code"` // return code
	Msg  string      `json:"msg"`  // message
	Data interface{} `json:"data"` // data object
}

Result represents a common-used result struct.

type SmData

type SmData struct {
	ReturnCode int `json:"returnCode"`
}

百度主动提交

type ZipFile

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

ZipFile represents a zip file.

func (*ZipFile) AddDirectory

func (z *ZipFile) AddDirectory(path, dirName string) error

AddDirectory adds a directory.

func (*ZipFile) AddDirectoryN

func (z *ZipFile) AddDirectoryN(path string, names ...string) error

AddDirectoryN adds directories.

func (*ZipFile) AddEntry

func (z *ZipFile) AddEntry(path, name string) error

AddEntry adds a entry.

func (*ZipFile) AddEntryN

func (z *ZipFile) AddEntryN(path string, names ...string) error

AddEntryN adds entries.

func (*ZipFile) Close

func (z *ZipFile) Close() error

Close closes the zip file writer.

Jump to

Keyboard shortcuts

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