移除字符串ip地址与Long类型互转的方法,修改中文长度校验的方法

This commit is contained in:
sunmoon
2020-03-17 14:26:36 +08:00
parent 2812a247aa
commit 4fb01e68f7
2 changed files with 8 additions and 105 deletions

View File

@ -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 <i> to string.
// It's most common used converting function.

View File

@ -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 {