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 {