base

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

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

Go to latest
Published: Nov 1, 2022 License: MulanPSL-2.0 Imports: 19 Imported by: 0

Documentation

Overview

*

@author: sugar
@since: 2022/10/30
@desc: //TODO

*

Index

Constants

This section is empty.

Variables

View Source
var CheckCnEnNumStr = func(str interface{}) bool {

	match, _ := regexp.MatchString(`^[\w\p{Han}\s\,\.\!\?\+\-\(\)\\、,。!?()@#¥%/|]+$`, fmt.Sprint(str))
	return match
}

CheckCnEnNumStr 验证英文、数字、中文、常用符号字符串

View Source
var CheckDirExistsWithCreate = func(p string) bool {
	if !PathExists(p) {
		return MakeDirs(p)
	}
	return true
}

检测文件夹是否存在;不存在创建

View Source
var CheckEmail = func(str interface{}) bool {
	match, _ := regexp.MatchString(`^[a-zA-Z0-9]+([._\-][a-zA-Z0-9]+)*@([a-zA-Z0-9]+([._\-][a-zA-Z0-9]+))+$`, fmt.Sprint(str))
	return match
}

CheckEmail 验证电子邮箱

View Source
var CheckEnStr = func(str interface{}) bool {
	match, _ := regexp.MatchString(`^[a-zA-Z\d]$+`, fmt.Sprint(str))
	return match
}
View Source
var CheckInt = func(str interface{}) bool {
	match, _ := regexp.MatchString(`^\d+$`, fmt.Sprint(str))
	return match
}

CheckInt 验证数字

View Source
var CheckInts = func(str interface{}) bool {
	match, _ := regexp.MatchString(`^\d*(\d+\,)*\d+$`, fmt.Sprint(str))
	return match
}

CheckInts 验证数字,多个逗号拼接的IDs

View Source
var CheckPhone = func(str interface{}) bool {
	match, _ := regexp.MatchString(`^1[\d]{10}$`, fmt.Sprint(str))
	return match
}

CheckPhone 验证手机号码

View Source
var CheckTimestamp = func(str interface{}) bool {
	match, _ := regexp.MatchString(`^\d{10}$`, fmt.Sprint(str))
	return match
}

CheckTimestamp 验证10位秒时间戳

View Source
var CheckUniqueStr = func(str interface{}) bool {
	match, _ := regexp.MatchString(`^[\w\-]+$`, fmt.Sprint(str))
	return match
}

CheckUniqueStr 验证\w的字符

View Source
var CheckUniqueStrWithLen = func(str interface{}, len int) bool {
	match, _ := regexp.MatchString(fmt.Sprintf(`^[\w\-]{%d}$`, len), fmt.Sprint(str))
	return match
}

CheckUniqueStrWithLen 验证\w的指定长度的字符

View Source
var CheckUniqueStrWithRan = func(str interface{}, min int, max int) bool {
	match, _ := regexp.MatchString(fmt.Sprintf(`^[\w\-]{%d,%d}$`, min, max), fmt.Sprint(str))
	return match
}

CheckUniqueStrWithRan 验证\w的指定范围长度的字符

View Source
var CheckWStr = func(str interface{}) bool {
	match, _ := regexp.MatchString(`^\w+$`, fmt.Sprint(str))
	return match
}

CheckWStr 验证\w的字符

View Source
var CheckWStrWithLen = func(str interface{}, len int) bool {
	match, _ := regexp.MatchString(fmt.Sprintf(`^\w{%d}$`, len), fmt.Sprint(str))
	return match
}

CheckWStrWithLen 验证\w的指定长度的字符

View Source
var CheckWStrWithRan = func(str interface{}, min int, max int) bool {
	match, _ := regexp.MatchString(fmt.Sprintf(`^\w{%d,%d}$`, min, max), fmt.Sprint(str))
	return match
}

CheckWStrWithRan 验证\w的指定范围长度的字符

View Source
var FileMd5 = func(path string) string {
	f, e := os.Open(path)
	if e != nil {
		return ""
	}
	defer f.Close()
	m := md5.New()
	io.Copy(m, f)
	return hex.EncodeToString(m.Sum(nil))
}

FileMd5 文件md5值

View Source
var InterfaceToFloat32 = func(val interface{}) float32 {
	return float32(InterfaceToFloat64(val))
}

InterfaceToFloat32 interface类型转float32

View Source
var InterfaceToFloat64 = func(val interface{}) float64 {
	f, _ := strconv.ParseFloat(InterfaceToString(val), 64)
	return f
}

InterfaceToFloat64 interface类型转float64

View Source
var InterfaceToInt = func(val interface{}) int {
	return int(InterfaceToInt64(val))
}

InterfaceToInt interface类型转int

View Source
var InterfaceToInt32 = func(val interface{}) int32 {
	return int32(InterfaceToInt64(val))
}

InterfaceToInt32 interface类型转int32

View Source
var InterfaceToInt64 = func(val interface{}) int64 {
	i, _ := strconv.ParseInt(InterfaceToString(val), 10, 64)
	return i
}

InterfaceToInt64 interface类型转int64

View Source
var InterfaceToMapWithStringInterface = func(val interface{}) map[string]interface{} {

	if reflect.TypeOf(val).Kind() == reflect.Map {
		return val.(map[string]interface{})
	}
	return map[string]interface{}{}
}

InterfaceToMapWithStringInterface interface类型转为map

View Source
var InterfaceToString = func(val interface{}) string {
	tf := reflect.TypeOf(val)
	if tf == nil {
		return ""
	}
	switch tf.Kind() {
	case reflect.Map:
		fmt.Println("Map:", val)
		return ""
	case reflect.Chan:
		fmt.Println("Map:", val)
		return ""
	}
	fmt.Println("反射类型", reflect.TypeOf(val))
	fmt.Println("反射类型", reflect.TypeOf(val).Kind())
	return fmt.Sprint(val)
}

InterfaceToString interface类型转换String

View Source
var MakeDirs = func(path string) bool {
	err := os.MkdirAll(path, os.ModePerm)
	if err != nil {
		return false
	}
	return true
}

MakeDirs 创建目录;多层创建

View Source
var PathExists = func(path string) bool {
	_, err := os.Stat(path)
	if err != nil {
		if os.IsNotExist(err) {
			return false
		}
	}
	return true
}

PathExists 检测文件路径是否存在

View Source
var RebuildScienceNumToString = func(numStr string) string {
	if strings.Contains(numStr, "e+") {

		decimalNum, err := decimal.NewFromString(numStr)
		if err != nil {
			Error("获取科学计数数值出错", err)
			return numStr
		}
		decimalStr := decimalNum.String()
		return decimalStr
	}
	return numStr
}
View Source
var RunRootPath = func() (string, error) {
	f, e := os.Executable()

	if e != nil {
		return "", e
	}
	p := filepath.Dir(f)
	return p, nil
}

RunRootPath 获取程序运行目录

Functions

func CheckDateTime

func CheckDateTime(str interface{}) bool

CheckDateTime 验证时间 格式 2020-04-24 15:00:00

func Debug

func Debug(v ...interface{})

Debug 调式日志

func Error

func Error(v ...interface{})

Error 错误日志

func GetNowMsTimeStamp

func GetNowMsTimeStamp() int64

GetNowMsTimeStamp 返回当前时间的毫秒时间戳 13位数字

func GetNowNsTimeStamp

func GetNowNsTimeStamp() int64

GetNowNsTimeStamp 返回当前时间的纳秒时间戳 19位数字

func GetNowTimeStamp

func GetNowTimeStamp() int64

GetNowTimeStamp 返回当前时间的时间戳(秒) 10位数字

func GetNowUsTimeStamp

func GetNowUsTimeStamp() int64

GetNowUsTimeStamp 返回当前时间的微秒时间戳 16位数字

func Info

func Info(v ...interface{})

Info 普通日志

func InitLogs

func InitLogs()

init 初始化信息

func StringContains

func StringContains(str, sub string) int

StringContains 检测str是否包含sub

Types

type Api

type Api struct {
	beego.Controller
	//请求的Header参数
	HttpRequestHeaderData map[string]interface{}
	//请求的Get参数
	HttpRequestGetData map[string]interface{}
	//请求的form表单参数
	HttpRequestPostFormData map[string]interface{}
	//请求的JSON参数
	HttpRequestPostJsonData map[string]interface{}
}

func (*Api) InitRequestData

func (a *Api) InitRequestData()

initRequestData 初始化请求信息

type BeegoLogs

type BeegoLogs struct {
}

type BeegoLogsConf

type BeegoLogsConf struct {
	//保存文件名
	LogsFileName string `json:"filename,omitempty"`
	//每个文件保存的最大行数
	LogsMaxLines int `json:"maxlines,omitempty"`
	//每个文件保存的最大尺寸,默认值是 1 << 28, //256 MB
	LogsMaxSize int `json:"maxsize,omitempty"`
	//是否按照每天 logrotate,默认是 true
	LogsDaily interface{} `json:"daily,omitempty"`
	//文件最多保存多少天,默认保存 7 天
	LogsMaxDays int `json:"maxdays,omitempty"`
	//是否开启 logrotate,默认是 true
	LogsRotate interface{} `json:"rotate,omitempty"`
	//日志保存的时候的级别,默认是 Trace 级别
	LogsLevel int `json:"level,omitempty"`
	//日志文件权限
	LogsPerm int `json:"perm,omitempty"`
	//需要单独写入文件的日志级别,设置后命名类似 test.error.log
	LogsSeparate []string `json:"separate,omitempty"`
	//是否开启打印日志彩色打印(需环境支持彩色输出)
	LogsColor interface{} `json:"color,omitempty"`
	//
	//是否每次链接都重新打开链接,默认是 false
	LogsReconnectOnMsg interface{} `json:"reconnectOnMsg,omitempty"`
	//是否自动重新链接地址
	LogsReconnect interface{} `json:"reconnect,omitempty"`
	//网络链接的方式
	LogsNet string `json:"net,omitempty"`
	//网络链接的地址
	LogsAddr string `json:"addr,omitempty"`
	//
	//验证的用户名
	LogsUsername string `json:"username,omitempty"`
	//验证密码
	LogsPassword string `json:"password,omitempty"`
	//发送的邮箱地址
	LogsHost string `json:"host,omitempty"`
	// 邮件需要发送的人,支持多个
	LogsSendTos string `json:"sendTos,omitempty"`
	//发送邮件的标题,默认是 Diagnostic message from server
	LogsSubject string `json:"subject,omitempty"`
	//
	//DNS
	LogsDns string `json:"dns,omitempty"`
}

type HttpClient

type HttpClient struct {
	Url           string                 //请求地址
	Header        map[string]string      //请求头
	RequestData   map[string]interface{} //请求数据
	FormData      map[string]string
	FormFile      map[string]string
	CheckRedirect func(req *http.Request, via []*http.Request) error //检查重定向
	Transport     *http.Transport                                    //Transport
}

HttpClient @Description: Http请求客户端

func NewHttpClient

func NewHttpClient() *HttpClient

func (*HttpClient) Get

func (that *HttpClient) Get() (res *HttpResponse)

Get @Description: Get请求 @receiver that 当前结构体 @return res *HttpResponse 返回信息

func (*HttpClient) PostForm

func (that *HttpClient) PostForm() (res *HttpResponse)

PostForm @Description: Post data请求 @receiver that 当前结构体 @return res *HttpResponse 返回信息

func (*HttpClient) PostJson

func (that *HttpClient) PostJson() (res *HttpResponse)

PostJson @Description: Post json请求 @receiver that 当前结构体 @return res *HttpResponse 返回信息

type HttpResponse

type HttpResponse struct {
	Status int    //http 状态码
	Body   []byte // 返回body
	Error  error  // 错误信息
}

HttpResponse @Description: http请求返回信息

Jump to

Keyboard shortcuts

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