mirror of
https://github.com/bufanyun/hotgo.git
synced 2025-08-26 16:46:14 +08:00
发布代码生成、更新20+表单组件,优化数据字典,gf版本更新到2.3.1
This commit is contained in:
24
server/utility/validate/filter.go
Normal file
24
server/utility/validate/filter.go
Normal file
@@ -0,0 +1,24 @@
|
||||
// Package validate
|
||||
// @Link https://github.com/bufanyun/hotgo
|
||||
// @Copyright Copyright (c) 2022 HotGo CLI
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
||||
//
|
||||
package validate
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
type Filter interface {
|
||||
// Filter gf效验规则 https://goframe.org/pages/viewpage.action?pageId=1114367
|
||||
Filter(ctx context.Context) error
|
||||
}
|
||||
|
||||
// PreFilter 预过滤
|
||||
func PreFilter(ctx context.Context, in interface{}) error {
|
||||
if c, ok := in.(Filter); ok {
|
||||
return c.Filter(ctx)
|
||||
}
|
||||
return nil
|
||||
}
|
72
server/utility/validate/include.go
Normal file
72
server/utility/validate/include.go
Normal file
@@ -0,0 +1,72 @@
|
||||
// Package validate
|
||||
// @Link https://github.com/bufanyun/hotgo
|
||||
// @Copyright Copyright (c) 2022 HotGo CLI
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
||||
//
|
||||
package validate
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/v2/text/gstr"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
// InSameDay 是否为同一天
|
||||
func InSameDay(t1, t2 int64) bool {
|
||||
y1, m1, d1 := time.Unix(t1, 0).Date()
|
||||
y2, m2, d2 := time.Unix(t2, 0).Date()
|
||||
return y1 == y2 && m1 == m2 && d1 == d2
|
||||
}
|
||||
|
||||
// InSameMinute 是否为同一分钟
|
||||
func InSameMinute(t1, t2 int64) bool {
|
||||
d1 := time.Unix(t1, 0).Format("2006-01-02 15:04")
|
||||
d2 := time.Unix(t2, 0).Format("2006-01-02 15:04")
|
||||
return d1 == d2
|
||||
}
|
||||
|
||||
// InSliceExistStr 判断字符或切片字符是否存在指定字符
|
||||
func InSliceExistStr(elems interface{}, search string) bool {
|
||||
switch elems.(type) {
|
||||
case []string:
|
||||
elem := gconv.Strings(elems)
|
||||
for i := 0; i < len(elem); i++ {
|
||||
if gconv.String(elem[i]) == search {
|
||||
return true
|
||||
}
|
||||
}
|
||||
default:
|
||||
return gconv.String(elems) == search
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// InSliceInt64 元素是否存在于切片中
|
||||
func InSliceInt64(slice []int64, key int64) bool {
|
||||
if len(slice) == 0 {
|
||||
return false
|
||||
}
|
||||
for i := 0; i < len(slice); i++ {
|
||||
if slice[i] == key {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func InSliceInt(slice []int, key int) bool {
|
||||
if len(slice) == 0 {
|
||||
return false
|
||||
}
|
||||
for i := 0; i < len(slice); i++ {
|
||||
if slice[i] == key {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func InSliceString(slice []string, key string) bool {
|
||||
return gstr.InArray(slice, key)
|
||||
}
|
@@ -7,13 +7,37 @@
|
||||
package validate
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
"context"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
"github.com/gogf/gf/v2/text/gstr"
|
||||
"net"
|
||||
"net/url"
|
||||
"regexp"
|
||||
"time"
|
||||
)
|
||||
|
||||
func IsDNSName(s string) bool {
|
||||
DNSName := `^([a-zA-Z0-9_]{1}[a-zA-Z0-9_-]{0,62}){1}(\.[a-zA-Z0-9_]{1}[a-zA-Z0-9_-]{0,62})*[\._]?$`
|
||||
rxDNSName := regexp.MustCompile(DNSName)
|
||||
return s != "" && rxDNSName.MatchString(s)
|
||||
}
|
||||
|
||||
func IsHTTPS(ctx context.Context) bool {
|
||||
r := ghttp.RequestFromCtx(ctx)
|
||||
if r == nil {
|
||||
g.Log().Warningf(ctx, "IsHTTPS ctx not request")
|
||||
return false
|
||||
}
|
||||
var (
|
||||
proto = r.Header.Get("X-Forwarded-Proto")
|
||||
)
|
||||
|
||||
if r.TLS != nil || gstr.Equal(proto, "https") {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// IsIp 是否为ipv4
|
||||
func IsIp(ip string) bool {
|
||||
if net.ParseIP(ip) != nil {
|
||||
@@ -22,48 +46,37 @@ func IsIp(ip string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// IsPublicIp 是否是公网IP
|
||||
func IsPublicIp(Ip string) bool {
|
||||
ip := net.ParseIP(Ip)
|
||||
|
||||
if ip.IsLoopback() || ip.IsPrivate() || ip.IsMulticast() || ip.IsUnspecified() || ip.IsLinkLocalUnicast() || ip.IsLinkLocalMulticast() {
|
||||
return false
|
||||
}
|
||||
|
||||
if ip4 := ip.To4(); ip4 != nil {
|
||||
return !ip.Equal(net.IPv4bcast)
|
||||
}
|
||||
|
||||
return true
|
||||
|
||||
}
|
||||
|
||||
// IsMobile 是否为手机号码
|
||||
func IsMobile(mobile string) bool {
|
||||
pattern := `^(1[2|3|4|5|6|7|8|9][0-9]\d{4,8})$`
|
||||
reg := regexp.MustCompile(pattern)
|
||||
return reg.MatchString(mobile)
|
||||
}
|
||||
|
||||
// IsEmail 是否为邮箱地址
|
||||
func IsEmail(email string) bool {
|
||||
//pattern := `\w+([-+.]\w+)@\w+([-.]\w+).\w+([-.]\w+)*` //匹配电子邮箱
|
||||
pattern := `^[0-9a-z][_.0-9a-z-]{0,31}@([0-9a-z][0-9a-z-]{0,30}[0-9a-z].){1,4}[a-z]{2,4}$`
|
||||
|
||||
reg := regexp.MustCompile(pattern)
|
||||
return reg.MatchString(email)
|
||||
}
|
||||
|
||||
// InSameDay 是否为同一天
|
||||
func InSameDay(t1, t2 int64) bool {
|
||||
y1, m1, d1 := time.Unix(t1, 0).Date()
|
||||
y2, m2, d2 := time.Unix(t2, 0).Date()
|
||||
|
||||
return y1 == y2 && m1 == m2 && d1 == d2
|
||||
}
|
||||
|
||||
// InSameMinute 是否为同一分钟
|
||||
func InSameMinute(t1, t2 int64) bool {
|
||||
d1 := time.Unix(t1, 0).Format("2006-01-02 15:04")
|
||||
d2 := time.Unix(t2, 0).Format("2006-01-02 15:04")
|
||||
|
||||
return d1 == d2
|
||||
}
|
||||
|
||||
// InSliceExistStr 判断字符或切片字符是否存在指定字符
|
||||
func InSliceExistStr(elems interface{}, search string) bool {
|
||||
switch elems.(type) {
|
||||
case []string:
|
||||
elem := gconv.Strings(elems)
|
||||
for i := 0; i < len(elem); i++ {
|
||||
if gconv.String(elem[i]) == search {
|
||||
return true
|
||||
}
|
||||
}
|
||||
default:
|
||||
return gconv.String(elems) == search
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// IsURL 是否是url地址
|
||||
func IsURL(u string) bool {
|
||||
_, err := url.ParseRequestURI(u)
|
||||
@@ -76,3 +89,19 @@ func IsURL(u string) bool {
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// IsIDCard 是否为身份证
|
||||
func IsIDCard(idCard string) bool {
|
||||
sz := len(idCard)
|
||||
if sz != 18 {
|
||||
return false
|
||||
}
|
||||
weight := []int{7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2}
|
||||
validate := []byte{'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'}
|
||||
sum := 0
|
||||
for i := 0; i < len(weight); i++ {
|
||||
sum += weight[i] * int(byte(idCard[i])-'0')
|
||||
}
|
||||
m := sum % 11
|
||||
return validate[m] == idCard[sz-1]
|
||||
}
|
||||
|
Reference in New Issue
Block a user