util

package module
v0.0.0-...-e5fe9a1 Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2024 License: MIT Imports: 30 Imported by: 0

README

Util

Go Reference

工具库

Documentation

Overview

Package util 常用工具库

Index

Examples

Constants

View Source
const (
	CodeSuccess = 1
	CodeFail    = 0
	CodeError   = -1
)

Variables

View Source
var MsgCodeMap = map[int]string{
	CodeSuccess: "success",
	CodeFail:    "fail",
	CodeError:   "unknown error",
}

Functions

func AesDeCrypt

func AesDeCrypt(cypted []byte, key []byte) ([]byte, error)

AesDeCrypt 实现解密

func AesEncrypt

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

AesEncrypt 实现加密

func DataEncryption

func DataEncryption(password string) (string, error)

DataEncryption 数据加密

Example
package main

import (
	"fmt"

	"github.com/wxw9868/util"
)

func main() {
	str, err := util.DataEncryption("zh1234567")
	fmt.Println(str, err)
}
Output:

func FormatToUnix

func FormatToUnix(str string) int64
Example
package main

import (
	"fmt"

	"github.com/wxw9868/util"
)

func main() {
	fmt.Println(util.FormatToUnix("2006-01-02 15:04:05"))
}
Output:

func GenerateCode

func GenerateCode(width int) string

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 GenerateOrderSN

func GenerateOrderSN() string

GenerateOrderSN 订单ID

func GenerateQRCode

func GenerateQRCode(url string, recoveryLevel string, size int, filename string) error

GenerateQRCode 生成二维码

func GetExternalIP

func GetExternalIP() string

GetExternalIP 通过http://myexternalip.com/raw获取公网IP

func GetIntranetIp

func GetIntranetIp()

GetIntranetIp 获取本地ip

func GetIp

func GetIp() (string, error)

GetIp 获取客户端ip

func GetNowTime

func GetNowTime(timezone, value string) time.Time

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 GetPulicIP

func GetPulicIP() string

GetPulicIP 通过dns服务器8.8.8.8:80获取使用的ip

func INITCAP

func INITCAP(s, sep string) string

INITCAP 将用符号链接的英文字符的首字符转换为大写

Example
package main

import (
	"fmt"

	"github.com/wxw9868/util"
)

func main() {
	fmt.Println(util.INITCAP("a", "b"))
}
Output:

func IpBetween

func IpBetween(from net.IP, to net.IP, test net.IP) bool

IpBetween 判断ip地址区间

func IsPublicIP

func IsPublicIP(IP net.IP) bool

IsPublicIP 判断是否是公网ip

func Md5

func Md5(str string) string

Md5 md5加密

func PKCS7Padding

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

PKCS7Padding PKCS7 填充模式

func PKCS7UnPadding

func PKCS7UnPadding(origData []byte) ([]byte, error)

PKCS7UnPadding 填充的反向操作,删除填充字符串

func PathExists

func PathExists(path string) (bool, error)

PathExists 文件目录是否存在

func SaveUploadedFile

func SaveUploadedFile(file *multipart.FileHeader, dst string) error

SaveUploadedFile uploads the form file to specific dst.

func StringBuilder

func StringBuilder(s1, s2 string) string

StringBuilder 拼接字符串

Example
package main

import (
	"fmt"

	"github.com/wxw9868/util"
)

func main() {
	fmt.Println(util.StringBuilder("a", "b"))
}
Output:

func VerifyEmail

func VerifyEmail(str string) bool

VerifyEmail 验证邮箱

Example
package main

import (
	"fmt"

	"github.com/wxw9868/util"
)

func main() {
	fmt.Println(util.VerifyEmail("1234@163.com"))
}
Output:

func VerifyEnglish

func VerifyEnglish(str string) bool

VerifyEnglish 验证字符串是否全为英文

Example
package main

import (
	"fmt"

	"github.com/wxw9868/util"
)

func main() {
	fmt.Println(util.VerifyEnglish("we"))
}
Output:

func VerifyFloat

func VerifyFloat(f interface{}) bool

VerifyFloat 验证浮点数最多有两位小数

func VerifyFloat2f

func VerifyFloat2f(str string) bool

VerifyFloat2f 验证浮点数最多有两位小数

Example
package main

import (
	"fmt"

	"github.com/wxw9868/util"
)

func main() {
	fmt.Println(util.VerifyFloat2f("1.11"))
}
Output:

func VerifyMobile

func VerifyMobile(str string) bool

VerifyMobile 验证手机号

Example
package main

import (
	"fmt"

	"github.com/wxw9868/util"
)

func main() {
	fmt.Println(util.VerifyMobile("18201108888"))
}
Output:

func VerifyName

func VerifyName(name string) bool
Example
package main

import (
	"fmt"

	"github.com/wxw9868/util"
)

func main() {
	fmt.Println(util.VerifyName("18201108888"))
}
Output:

func VerifyPassword

func VerifyPassword(str string) error

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

func VerifyPayPassword(str string) (bool, error)

VerifyPayPassword 支付密码验证规则:6个数字

Example
package main

import (
	"fmt"

	"github.com/wxw9868/util"
)

func main() {
	fmt.Println(util.VerifyPayPassword("123456"))
}
Output:

func VerifyString

func VerifyString(sn string) bool

VerifyString 验证字符串

Example
package main

import (
	"fmt"

	"github.com/wxw9868/util"
)

func main() {
	fmt.Println(util.VerifyString("18201108888"))
}
Output:

func VerifyTelephone

func VerifyTelephone(str string) bool

VerifyTelephone 座机号格式校验

Example
package main

import (
	"fmt"

	"github.com/wxw9868/util"
)

func main() {
	fmt.Println(util.VerifyTelephone("028-02866250077"))
}
Output:

func VideoFileMode

func VideoFileMode(ext string) bool

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.

func NewFile

func NewFile(r *http.Request) *Context

func (*Context) FormFile

func (c *Context) FormFile(name string) (*multipart.FileHeader, error)

FormFile returns the first file for the provided form key.

type IP

type IP struct {
	Country   string `json:"country"`
	CountryId string `json:"country_id"`
	Area      string `json:"area"`
	AreaId    string `json:"area_id"`
	Region    string `json:"region"`
	RegionId  string `json:"region_id"`
	City      string `json:"city"`
	CityId    string `json:"city_id"`
	Isp       string `json:"isp"`
}

type IPInfo

type IPInfo struct {
	Code int `json:"code"`
	Data IP  `json:"data"`
}

IPInfo 通过淘宝接口根据公网ip获取国家运营商等信息

func TabaoAPI

func TabaoAPI(ip string) *IPInfo

type Message

type Message struct {
	Code   int         `json:"code"`
	Status bool        `json:"status"`
	Msg    string      `json:"message"`
	Data   interface{} `json:"data"`
}

func CodeMsg

func CodeMsg(status bool, code int, data interface{}) Message

CodeMsg 匹配状态码和信息

func Fail

func Fail(msg string) Message

Fail 默认失败

func Msg

func Msg(status bool, code int, msg string, data interface{}) Message

Msg 序列化消息

func Success

func Success(msg string, data interface{}) Message

Success 默认成功

type Validate

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

func NewValidate

func NewValidate(tag string) *Validate

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

func (v *Validate) FieldError(field interface{}, tag string) error

FieldError 字段验证

func (*Validate) GetValidateTrans

func (v *Validate) GetValidateTrans() (*validator.Validate, ut.Translator, error)

GetValidateTrans 获取配置

func (*Validate) GinError

func (v *Validate) GinError(err error) (ret string)

func (*Validate) GinVar

func (v *Validate) GinVar(f interface{}, t string) error

func (*Validate) InitValidateGin

func (v *Validate) InitValidateGin()

func (*Validate) StructError

func (v *Validate) StructError(s interface{}) error

StructError 结构体验证

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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