Documentation
¶
Overview ¶
Package util 常用工具库
Index ¶
- Constants
- Variables
- func AesDeCrypt(cypted []byte, key []byte) ([]byte, error)
- func AesEncrypt(origData []byte, key []byte) ([]byte, error)
- func DataEncryption(password string) (string, error)
- func FormatToUnix(str string) int64
- func GenerateCode(width int) string
- func GenerateOrderSN() string
- func GenerateQRCode(url string, recoveryLevel string, size int, filename string) error
- func GetExternalIP() string
- func GetIntranetIp()
- func GetIp() (string, error)
- func GetNowTime(timezone, value string) time.Time
- func GetPulicIP() string
- func INITCAP(s, sep string) string
- func IpBetween(from net.IP, to net.IP, test net.IP) bool
- func IsPublicIP(IP net.IP) bool
- func Md5(str string) string
- func PKCS7Padding(ciphertext []byte, blockSize int) []byte
- func PKCS7UnPadding(origData []byte) ([]byte, error)
- func PathExists(path string) (bool, error)
- func SaveUploadedFile(file *multipart.FileHeader, dst string) error
- func StringBuilder(s1, s2 string) string
- func VerifyEmail(str string) bool
- func VerifyEnglish(str string) bool
- func VerifyFloat(f interface{}) bool
- func VerifyFloat2f(str string) bool
- func VerifyMobile(str string) bool
- func VerifyName(name string) bool
- func VerifyPassword(str string) error
- func VerifyPayPassword(str string) (bool, error)
- func VerifyString(sn string) bool
- func VerifyTelephone(str string) bool
- func VideoFileMode(ext string) bool
- type Context
- type IP
- type IPInfo
- type Message
- type Validate
- func (v *Validate) FieldError(field interface{}, tag string) error
- func (v *Validate) GetValidateTrans() (*validator.Validate, ut.Translator, error)
- func (v *Validate) GinError(err error) (ret string)
- func (v *Validate) GinVar(f interface{}, t string) error
- func (v *Validate) InitValidateGin()
- func (v *Validate) StructError(s interface{}) error
Examples ¶
Constants ¶
const ( CodeSuccess = 1 CodeFail = 0 CodeError = -1 )
Variables ¶
var MsgCodeMap = map[int]string{ CodeSuccess: "success", CodeFail: "fail", CodeError: "unknown error", }
Functions ¶
func DataEncryption ¶
DataEncryption 数据加密
Example ¶
package main
import (
"fmt"
"github.com/wxw9868/util"
)
func main() {
str, err := util.DataEncryption("zh1234567")
fmt.Println(str, err)
}
Output:
func FormatToUnix ¶
Example ¶
package main
import (
"fmt"
"github.com/wxw9868/util"
)
func main() {
fmt.Println(util.FormatToUnix("2006-01-02 15:04:05"))
}
Output:
func GenerateCode ¶
GenerateCode 生成6位数字码
Example ¶
package main
import (
"fmt"
"github.com/wxw9868/util"
)
func main() {
for i := 0; i < 10000; i++ {
code := util.GenerateCode(6)
fmt.Println(code)
}
}
Output:
func GenerateQRCode ¶
GenerateQRCode 生成二维码
func GetNowTime ¶
GetNowTime 获取对应时区的实时时间
Example ¶
package main
import (
"fmt"
"github.com/wxw9868/util"
)
func main() {
fmt.Println(util.GetNowTime("Asia/Shanghai", "2021-09-30 15:58:17"))
}
Output:
func INITCAP ¶
INITCAP 将用符号链接的英文字符的首字符转换为大写
Example ¶
package main
import (
"fmt"
"github.com/wxw9868/util"
)
func main() {
fmt.Println(util.INITCAP("a", "b"))
}
Output:
func PKCS7Padding ¶
PKCS7Padding PKCS7 填充模式
func PKCS7UnPadding ¶
PKCS7UnPadding 填充的反向操作,删除填充字符串
func SaveUploadedFile ¶
func SaveUploadedFile(file *multipart.FileHeader, dst string) error
SaveUploadedFile uploads the form file to specific dst.
func StringBuilder ¶
StringBuilder 拼接字符串
Example ¶
package main
import (
"fmt"
"github.com/wxw9868/util"
)
func main() {
fmt.Println(util.StringBuilder("a", "b"))
}
Output:
func VerifyEmail ¶
VerifyEmail 验证邮箱
Example ¶
package main
import (
"fmt"
"github.com/wxw9868/util"
)
func main() {
fmt.Println(util.VerifyEmail("1234@163.com"))
}
Output:
func VerifyEnglish ¶
VerifyEnglish 验证字符串是否全为英文
Example ¶
package main
import (
"fmt"
"github.com/wxw9868/util"
)
func main() {
fmt.Println(util.VerifyEnglish("we"))
}
Output:
func VerifyFloat2f ¶
VerifyFloat2f 验证浮点数最多有两位小数
Example ¶
package main
import (
"fmt"
"github.com/wxw9868/util"
)
func main() {
fmt.Println(util.VerifyFloat2f("1.11"))
}
Output:
func VerifyMobile ¶
VerifyMobile 验证手机号
Example ¶
package main
import (
"fmt"
"github.com/wxw9868/util"
)
func main() {
fmt.Println(util.VerifyMobile("18201108888"))
}
Output:
func VerifyName ¶
Example ¶
package main
import (
"fmt"
"github.com/wxw9868/util"
)
func main() {
fmt.Println(util.VerifyName("18201108888"))
}
Output:
func VerifyPassword ¶
VerifyPassword 密码必须由字⺟、数字和_~!.@#$%^&*?-符号组成,长度为6 ~ 20个字符 pattern := `^[\d|a-zA-Z]+[\d|a-zA-Z]+[_~!.@#$%^&*?-]+$`
if len(str) < 6 || len(str) > 20 {
return errors.New("密码长度为6 ~ 20个字符")
}
Example ¶
package main
import (
"fmt"
"github.com/wxw9868/util"
)
func main() {
err := util.VerifyPassword("99999.")
fmt.Println(err)
}
Output:
func VerifyPayPassword ¶
VerifyPayPassword 支付密码验证规则:6个数字
Example ¶
package main
import (
"fmt"
"github.com/wxw9868/util"
)
func main() {
fmt.Println(util.VerifyPayPassword("123456"))
}
Output:
func VerifyString ¶
VerifyString 验证字符串
Example ¶
package main
import (
"fmt"
"github.com/wxw9868/util"
)
func main() {
fmt.Println(util.VerifyString("18201108888"))
}
Output:
func VerifyTelephone ¶
VerifyTelephone 座机号格式校验
Example ¶
package main
import (
"fmt"
"github.com/wxw9868/util"
)
func main() {
fmt.Println(util.VerifyTelephone("028-02866250077"))
}
Output:
func VideoFileMode ¶
VideoFileMode 视频文件类型
Example ¶
package main
import (
"github.com/wxw9868/util"
)
func main() {
util.VideoFileMode("mp4")
}
Output: true
Types ¶
type Context ¶
type Context struct {
Request *http.Request
// Value of 'maxMemory' param that is given to http.Request's ParseMultipartForm
// method call.
MaxMultipartMemory int64
}
Context is the most important part of gin. It allows us to pass variables between middleware, manage the flow, validate the JSON of a request and render a JSON response for example.
type Message ¶
type Validate ¶
type Validate struct {
// contains filtered or unexported fields
}
func NewValidate ¶
NewValidate 初始化 Validate
Example ¶
package main
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/wxw9868/util"
)
type RegisterRequest struct {
Mobile string `json:"mobile" validate:"required,number,len=11" label:"手机号"`
UserType int `json:"user_type" validate:"required,number,len=1" label:"用户类别"`
Captcha
}
type Captcha struct {
VerifyCode string `json:"verify_code" validate:"required,number,len=6" label:"验证码"`
}
func main() {
engine := gin.Default()
engine.POST("/register", register)
_ = engine.Run()
}
func register(c *gin.Context) {
r := new(RegisterRequest)
if err := util.NewValidate("label").StructError(r); err != nil {
c.JSON(http.StatusNotFound, util.Msg(false, 0, err.Error(), nil))
return
}
c.JSON(http.StatusOK, util.Msg(true, 1, "注册成功", nil))
}
Output:
func (*Validate) FieldError ¶
FieldError 字段验证
func (*Validate) GetValidateTrans ¶
GetValidateTrans 获取配置
func (*Validate) InitValidateGin ¶
func (v *Validate) InitValidateGin()
func (*Validate) StructError ¶
StructError 结构体验证