From 2438f565e9495a4f7c04e5d9892b53feabee197b Mon Sep 17 00:00:00 2001 From: sunmoon Date: Mon, 16 Mar 2020 11:18:06 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=AD=97=E7=AC=A6?= =?UTF-8?q?=E4=B8=B2ip=E5=9C=B0=E5=9D=80=E4=B8=8ELong=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E4=BA=92=E8=BD=AC=E7=9A=84=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- util/gconv/gconv.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/util/gconv/gconv.go b/util/gconv/gconv.go index c34d0a569..045176cdc 100644 --- a/util/gconv/gconv.go +++ b/util/gconv/gconv.go @@ -8,9 +8,11 @@ package gconv import ( + "encoding/binary" "encoding/json" "fmt" "github.com/gogf/gf/os/gtime" + "net" "reflect" "strconv" "strings" @@ -165,6 +167,23 @@ func Runes(i interface{}) []rune { } return []rune(String(i)) } +// convert ip to long, ex ip 106.8.36.164 to 1778918564 +func Ip2long(ipStr string) uint32 { + ip := net.ParseIP(ipStr) + if ip == nil { + return 0 + } + ip = ip.To4() + return binary.BigEndian.Uint32(ip) +} +// convert long to ip string, ex int 1778918564 to ip 106.8.36.164 +func Long2ip(ipLong uint32) string { + ipByte := make([]byte, 4) + binary.BigEndian.PutUint32(ipByte, ipLong) + ip := net.IP(ipByte) + return ip.String() +} + // String converts to string. // It's most common used converting function. From 2812a247aa4f5c894c259a1e9ed25aa281e338c3 Mon Sep 17 00:00:00 2001 From: sunmoon Date: Tue, 17 Mar 2020 11:19:43 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=AA=8C=E8=AF=81?= =?UTF-8?q?=E4=B8=AD=E8=8B=B1=E6=96=87=E6=B7=B7=E5=90=88=E5=AD=97=E7=AC=A6?= =?UTF-8?q?=E4=B8=B2=E9=95=BF=E5=BA=A6=E7=9A=84=E5=88=A4=E6=96=AD=E8=A7=84?= =?UTF-8?q?=E5=88=99,=E6=8C=89utf8=E8=AE=A1=E7=AE=97,=E4=B8=80=E4=B8=AA?= =?UTF-8?q?=E4=B8=AD=E6=96=87=E7=AE=97=E4=B8=80=E4=B8=AA=E5=AD=97=E7=AC=A6?= =?UTF-8?q?,cn-length,cn-min-length,cn-max-length?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- util/gvalid/gvalid_check.go | 77 +++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/util/gvalid/gvalid_check.go b/util/gvalid/gvalid_check.go index 59e3eadcc..ef020d44d 100644 --- a/util/gvalid/gvalid_check.go +++ b/util/gvalid/gvalid_check.go @@ -10,6 +10,7 @@ import ( "regexp" "strconv" "strings" + "unicode/utf8" "github.com/gogf/gf/container/gmap" "github.com/gogf/gf/encoding/gjson" @@ -78,6 +79,9 @@ var ( "length": {}, "min-length": {}, "max-length": {}, + "cn-length": {}, + "cn-min-length": {}, + "cn-max-length": {}, "between": {}, "min": {}, "max": {}, @@ -192,6 +196,17 @@ func Check(value interface{}, rules string, msgs interface{}, params ...interfac } else { match = true } + // 中英文混合长度判断 + case "cn-length": + fallthrough + case "cn-min-length": + fallthrough + case "cn-max-length": + if msg := checkCnLength(val, ruleKey, ruleVal, customMsgMap); msg != "" { + errorMsgs[ruleKey] = msg + } else { + match = true + } // 大小范围 case "min": @@ -522,7 +537,69 @@ func checkRequired(value, ruleKey, ruleVal string, params map[string]string) boo return true } } +// 对中英文混合字段值长度进行检测 +func checkCnLength(value, ruleKey, ruleVal string, customMsgMap map[string]string) string { + msg := "" + strLen := utf8.RuneCountInString(value) + switch ruleKey { + // 长度范围 + case "cn-length": + array := strings.Split(ruleVal, ",") + min := 0 + max := 0 + if len(array) > 0 { + if v, err := strconv.Atoi(strings.TrimSpace(array[0])); err == nil { + min = v + } + } + if len(array) > 1 { + if v, err := strconv.Atoi(strings.TrimSpace(array[1])); err == nil { + max = v + } + } + if strLen < min || strLen > max { + if v, ok := customMsgMap[ruleKey]; !ok { + msg = errorMsgMap.Get(ruleKey) + } else { + msg = v + } + msg = strings.Replace(msg, ":min", strconv.Itoa(min), -1) + msg = strings.Replace(msg, ":max", strconv.Itoa(max), -1) + return msg + } + // 最小长度 + case "cn-min-length": + if min, err := strconv.Atoi(ruleVal); err == nil { + if strLen < min { + if v, ok := customMsgMap[ruleKey]; !ok { + msg = errorMsgMap.Get(ruleKey) + } else { + msg = v + } + msg = strings.Replace(msg, ":min", strconv.Itoa(min), -1) + } + } else { + msg = "校验参数[" + ruleVal + "]应当为整数类型" + } + + // 最大长度 + case "cn-max-length": + if max, err := strconv.Atoi(ruleVal); err == nil { + if strLen > max { + if v, ok := customMsgMap[ruleKey]; !ok { + msg = errorMsgMap.Get(ruleKey) + } else { + msg = v + } + msg = strings.Replace(msg, ":max", strconv.Itoa(max), -1) + } + } else { + msg = "校验参数[" + ruleVal + "]应当为整数类型" + } + } + return msg +} // 对字段值长度进行检测 func checkLength(value, ruleKey, ruleVal string, customMsgMap map[string]string) string { msg := "" From 4fb01e68f7f0e781a25b4a1eb7168376094a67a2 Mon Sep 17 00:00:00 2001 From: sunmoon Date: Tue, 17 Mar 2020 14:26:36 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E7=A7=BB=E9=99=A4=E5=AD=97=E7=AC=A6?= =?UTF-8?q?=E4=B8=B2ip=E5=9C=B0=E5=9D=80=E4=B8=8ELong=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E4=BA=92=E8=BD=AC=E7=9A=84=E6=96=B9=E6=B3=95,=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E4=B8=AD=E6=96=87=E9=95=BF=E5=BA=A6=E6=A0=A1=E9=AA=8C?= =?UTF-8?q?=E7=9A=84=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- util/gconv/gconv.go | 19 -------- util/gvalid/gvalid_check.go | 94 ++++--------------------------------- 2 files changed, 8 insertions(+), 105 deletions(-) diff --git a/util/gconv/gconv.go b/util/gconv/gconv.go index 045176cdc..c34d0a569 100644 --- a/util/gconv/gconv.go +++ b/util/gconv/gconv.go @@ -8,11 +8,9 @@ package gconv import ( - "encoding/binary" "encoding/json" "fmt" "github.com/gogf/gf/os/gtime" - "net" "reflect" "strconv" "strings" @@ -167,23 +165,6 @@ func Runes(i interface{}) []rune { } return []rune(String(i)) } -// convert ip to long, ex ip 106.8.36.164 to 1778918564 -func Ip2long(ipStr string) uint32 { - ip := net.ParseIP(ipStr) - if ip == nil { - return 0 - } - ip = ip.To4() - return binary.BigEndian.Uint32(ip) -} -// convert long to ip string, ex int 1778918564 to ip 106.8.36.164 -func Long2ip(ipLong uint32) string { - ipByte := make([]byte, 4) - binary.BigEndian.PutUint32(ipByte, ipLong) - ip := net.IP(ipByte) - return ip.String() -} - // String converts to string. // It's most common used converting function. diff --git a/util/gvalid/gvalid_check.go b/util/gvalid/gvalid_check.go index ef020d44d..0992ce8fc 100644 --- a/util/gvalid/gvalid_check.go +++ b/util/gvalid/gvalid_check.go @@ -7,11 +7,6 @@ package gvalid import ( - "regexp" - "strconv" - "strings" - "unicode/utf8" - "github.com/gogf/gf/container/gmap" "github.com/gogf/gf/encoding/gjson" "github.com/gogf/gf/net/gipv4" @@ -19,6 +14,9 @@ import ( "github.com/gogf/gf/os/gtime" "github.com/gogf/gf/text/gregex" "github.com/gogf/gf/util/gconv" + "regexp" + "strconv" + "strings" ) const ( @@ -79,9 +77,6 @@ var ( "length": {}, "min-length": {}, "max-length": {}, - "cn-length": {}, - "cn-min-length": {}, - "cn-max-length": {}, "between": {}, "min": {}, "max": {}, @@ -196,18 +191,6 @@ func Check(value interface{}, rules string, msgs interface{}, params ...interfac } else { match = true } - // 中英文混合长度判断 - case "cn-length": - fallthrough - case "cn-min-length": - fallthrough - case "cn-max-length": - if msg := checkCnLength(val, ruleKey, ruleVal, customMsgMap); msg != "" { - errorMsgs[ruleKey] = msg - } else { - match = true - } - // 大小范围 case "min": fallthrough @@ -537,72 +520,11 @@ func checkRequired(value, ruleKey, ruleVal string, params map[string]string) boo return true } } -// 对中英文混合字段值长度进行检测 -func checkCnLength(value, ruleKey, ruleVal string, customMsgMap map[string]string) string { - msg := "" - strLen := utf8.RuneCountInString(value) - switch ruleKey { - // 长度范围 - case "cn-length": - array := strings.Split(ruleVal, ",") - min := 0 - max := 0 - if len(array) > 0 { - if v, err := strconv.Atoi(strings.TrimSpace(array[0])); err == nil { - min = v - } - } - if len(array) > 1 { - if v, err := strconv.Atoi(strings.TrimSpace(array[1])); err == nil { - max = v - } - } - if strLen < min || strLen > max { - if v, ok := customMsgMap[ruleKey]; !ok { - msg = errorMsgMap.Get(ruleKey) - } else { - msg = v - } - msg = strings.Replace(msg, ":min", strconv.Itoa(min), -1) - msg = strings.Replace(msg, ":max", strconv.Itoa(max), -1) - return msg - } - - // 最小长度 - case "cn-min-length": - if min, err := strconv.Atoi(ruleVal); err == nil { - if strLen < min { - if v, ok := customMsgMap[ruleKey]; !ok { - msg = errorMsgMap.Get(ruleKey) - } else { - msg = v - } - msg = strings.Replace(msg, ":min", strconv.Itoa(min), -1) - } - } else { - msg = "校验参数[" + ruleVal + "]应当为整数类型" - } - - // 最大长度 - case "cn-max-length": - if max, err := strconv.Atoi(ruleVal); err == nil { - if strLen > max { - if v, ok := customMsgMap[ruleKey]; !ok { - msg = errorMsgMap.Get(ruleKey) - } else { - msg = v - } - msg = strings.Replace(msg, ":max", strconv.Itoa(max), -1) - } - } else { - msg = "校验参数[" + ruleVal + "]应当为整数类型" - } - } - return msg -} // 对字段值长度进行检测 func checkLength(value, ruleKey, ruleVal string, customMsgMap map[string]string) string { msg := "" + runeArray := gconv.Runes(value) + valueLen := len(runeArray) switch ruleKey { // 长度范围 case "length": @@ -619,7 +541,7 @@ func checkLength(value, ruleKey, ruleVal string, customMsgMap map[string]string) max = v } } - if len(value) < min || len(value) > max { + if valueLen < min || valueLen > max { if v, ok := customMsgMap[ruleKey]; !ok { msg = errorMsgMap.Get(ruleKey) } else { @@ -633,7 +555,7 @@ func checkLength(value, ruleKey, ruleVal string, customMsgMap map[string]string) // 最小长度 case "min-length": if min, err := strconv.Atoi(ruleVal); err == nil { - if len(value) < min { + if valueLen < min { if v, ok := customMsgMap[ruleKey]; !ok { msg = errorMsgMap.Get(ruleKey) } else { @@ -648,7 +570,7 @@ func checkLength(value, ruleKey, ruleVal string, customMsgMap map[string]string) // 最大长度 case "max-length": if max, err := strconv.Atoi(ruleVal); err == nil { - if len(value) > max { + if valueLen > max { if v, ok := customMsgMap[ruleKey]; !ok { msg = errorMsgMap.Get(ruleKey) } else {