utils

package
v0.0.0-...-a4d5418 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2019 License: MIT Imports: 25 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var MessageTmpls = map[string]string{
	"Required":        "不能为空",
	"Max":             "最大为%v",
	"Min":             "最小为%v",
	"Range":           "范围为%v到%v",
	"MinSize":         "最小长度为%v",
	"MaxSize":         "最大长度为%v",
	"Length":          "长度必须为%v",
	"Alpha":           "必须为字母",
	"Numeric":         "必须为数字",
	"AlphaNumeric":    "必须为字母、数字",
	"AlphaDash":       "必须为字母、数字、-或_",
	"Email":           "必须为有效的邮箱地址",
	"IP":              "必须为有效的IP地址",
	"Mobile":          "必须为有效的手机号码",
	"Tel":             "必须为有效的固定电话",
	"ZipCode":         "必须是有效的zipcode",
	"UploadExt":       "文件后缀名只能为 %v",
	"UploadSize":      "文件大小不能超过 %dMB",
	"Mac":             "必须是有效的mac地址",
	"SpecialChar":     "不允许字符包括`~!@#$%^&*()=+[]{}\\|;:.'\",<>/?",
	"RuneLength":      "不能超过15个字符",
	"ChnDash":         "只支持数字,字母,汉字,-或_的组合",
	"ChnAlphaNumeric": "只支持数字,字母,汉字的组合",
	"Chn":             "只支持汉字",
	"Sensitive":       "中包含敏感词:%s,请修改。",
	"OpenTaskName":    "只支持数字,字母,汉字,-或_或.的组合",
	"SubnetMask":      "子网掩码必须为有效的IP地址",
}

验证函数未通过,对应的错误提示

Functions

func AESDecrypt

func AESDecrypt(key []byte, cryptoText string) (string, bool)

Decrypt from base64 to decrypted string

func AESEncrypt

func AESEncrypt(key []byte, text string) (string, bool)

Encrypt string to base64 crypto using AES

func Alpha

func Alpha(validValue interface{}, params ...string) bool

alpha字符(全字母),有效类型:string

func AlphaDash

func AlphaDash(validValue interface{}, params ...string) bool

alpha 字符或数字或横杠 -_,有效类型:string,

func AlphaNumeric

func AlphaNumeric(validValue interface{}, params ...string) bool

alpha 字符(字母)或数字,有效类型:string

func ChnAlphaNumeric

func ChnAlphaNumeric(validValue interface{}, params ...string) bool

中文,数字,字母

func ChnDash

func ChnDash(validValue interface{}, params ...string) bool

中文,数字,字母,下划线

func Deserialize

func Deserialize(data []byte, to interface{}) error

用gob进行数据反序列化

func Email

func Email(validValue interface{}, params ...string) bool

邮箱格式,有效类型:string

func FileExist

func FileExist(filename string) bool

判定文件是否存在

func GetNotZeroFields

func GetNotZeroFields(obj interface{}, fields ...string) []string

获取结构体的非零值字段

func GetStructTV

func GetStructTV(obj interface{}) (reflect.Type, reflect.Value, error)

获取结构体或者指针的类型和值

func IP

func IP(validValue interface{}, params ...string) bool

IP 格式,目前只支持 IPv4 格式验证,有效类型:string

func InArray

func InArray(item interface{}, array interface{}) bool

func IsSatisfied

func IsSatisfied(obj interface{}) bool

IsSatisfied judge whether obj has value

func IsStruct

func IsStruct(t reflect.Type) bool

判定是否为结构体

func IsStructPtr

func IsStructPtr(t reflect.Type) bool

判定是否为结构体指针

func Join

func Join(arr interface{}, sep string) string

强大的join

func JsonPrint

func JsonPrint(obj interface{})

json方式打印结构体

func Length

func Length(validValue interface{}, params ...string) bool

指定长度,有效类型:string slice

func Mac

func Mac(validValue interface{}, params ...string) bool

mac地址校验

func Max

func Max(validValue interface{}, params ...string) bool

限制数字最大值

func MaxSize

func MaxSize(validValue interface{}, params ...string) bool

最大长度,有效类型:string slice,

func Md5

func Md5(str string) string

将字符串加密成 md5

func Min

func Min(validValue interface{}, params ...string) bool

限制数字最小值

func MinSize

func MinSize(validValue interface{}, params ...string) bool

最小长度 有效类型:string slice

func Mobile

func Mobile(validValue interface{}, params ...string) bool

手机号,有效类型:string

func Numeric

func Numeric(validValue interface{}, params ...string) bool

数字,有效类型:string

func OpenTaskName

func OpenTaskName(validValue interface{}, params ...string) bool

中文,数字,字母,下划线,点

func RandomString

func RandomString(length int) string

RandomString 在数字、大写字母、小写字母范围内生成num位的随机字符串

func Range

func Range(validValue interface{}, params ...string) bool

范围

func Serialize

func Serialize(data interface{}) ([]byte, error)

用gob进行数据序列化

func SortSlice

func SortSlice(slicePtr interface{}, sortFields string) error

多字段排序 SliceSort([]User ,"Name desc,Id aes"})

func Tel

func Tel(validValue interface{}, params ...string) bool

固定电话号,有效类型:string

func ToSlice

func ToSlice(arr interface{}) []interface{}

slice interface 变数组

func TrimStruct

func TrimStruct(obj interface{}, field ...string)

去空,传了field 只处理field里面的字段

func ZipCode

func ZipCode(validValue interface{}, params ...string) bool

邮政编码,有效类型:string

Types

type Request

type Request struct {
	Code string //认证字符串
	Time string //请求的时间
	Data string //传输的数据:请求数据的json格式数据
}

请求的数据格式

type Response

type Response struct {
	Code    uint32 `json:"Code"`    //错误码
	Version string `json:"Version"` //客户端版本号
	Msg     string `json:"Msg"`     //错误信息
	Data    []byte `json:"Data"`    //响应的ProtoBuf数据
}

响应的数据格式

type Tools

type Tools struct {
	Debug bool
}

lwyserver 接口服务

func (*Tools) ApiPost

func (this *Tools) ApiPost(url string, postParams Request) (bodyByte []byte, err error)

*

  • 提交POST的请求
  • url 请求地址
  • 请求参数

func (*Tools) GetRequest

func (this *Tools) GetRequest(requestDat []byte, ret interface{}) error

func (*Tools) GetResponse

func (this *Tools) GetResponse(requestData interface{}) ([]byte, error)

func (*Tools) Post

func (this *Tools) Post(url string, requestData interface{}, response *Response) error

获取数据 url 请求地址 requestData 请求数据 response 响应数据

func (*Tools) PostUnmarshal

func (this *Tools) PostUnmarshal(url string, req interface{}, ack interface{}) error

json,协议byte数据

type ValidFun

type ValidFun func(validValue interface{}, params ...string) bool

type Validation

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

A Validation context manages data validation and error messages.

func NewValidation

func NewValidation() *Validation

new

func (*Validation) FieldTag

func (this *Validation) FieldTag(fieldTag map[string]string) *Validation

手动设置FieldTag

func (*Validation) GetStructTV

func (this *Validation) GetStructTV(obj interface{}) (reflect.Type, reflect.Value, error)

获取结构体或者指针的类型和值

func (*Validation) InSliceString

func (this *Validation) InSliceString(field string, arr []string) bool

判定某个值是否在数组里面

func (*Validation) IsEmpty

func (this *Validation) IsEmpty(obj interface{}) bool

判定一个interface的值是否为空值

func (*Validation) IsStruct

func (this *Validation) IsStruct(t reflect.Type) bool

判定是否为结构体

func (*Validation) IsStructPtr

func (this *Validation) IsStructPtr(t reflect.Type) bool

判定是否为结构体指针

func (*Validation) Priority

func (this *Validation) Priority(fields ...string) *Validation

设置优先验证字段,可通过此方法调整字段验证顺序

func (*Validation) RegisterFun

func (this *Validation) RegisterFun(funcName string, validFunc ValidFun) *Validation

注册函数

func (*Validation) Require

func (this *Validation) Require(fields ...string) *Validation

指定不为空字段

func (*Validation) SetFailMessages

func (this *Validation) SetFailMessages(failMessages map[string]string) *Validation

设定字段出错时的错误消息

func (*Validation) SetMessageTmpls

func (this *Validation) SetMessageTmpls(messageTmpls map[string]string) *Validation

设定函数对应的模板消息

func (*Validation) Valid

func (this *Validation) Valid(obj interface{}) error

结构体验证遇到一个验证不通过就会退出

func (*Validation) ValidAll

func (this *Validation) ValidAll(obj interface{}) map[string]error

会验证所有的字段

Jump to

Keyboard shortcuts

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