mirror of
https://github.com/unti-io/go-utils.git
synced 2025-10-18 22:24:31 +08:00
v1.0.0
This commit is contained in:
@@ -15,25 +15,25 @@ import (
|
|||||||
|
|
||||||
// CurlRequest - 发起请求的结构体
|
// CurlRequest - 发起请求的结构体
|
||||||
type CurlRequest struct {
|
type CurlRequest struct {
|
||||||
Body any
|
Body any
|
||||||
Url string
|
Url string
|
||||||
Method string
|
Method string
|
||||||
Client *http.Client
|
Client *http.Client
|
||||||
Data map[string]string
|
Data map[string]any
|
||||||
Query map[string]string
|
Query map[string]any
|
||||||
Headers map[string]string
|
Headers map[string]any
|
||||||
}
|
}
|
||||||
|
|
||||||
// CurlResponse - 响应的结构体
|
// CurlResponse - 响应的结构体
|
||||||
type CurlResponse struct {
|
type CurlResponse struct {
|
||||||
StatusCode int
|
StatusCode int
|
||||||
Request *http.Request
|
Request *http.Request
|
||||||
Headers *http.Header
|
Headers *http.Header
|
||||||
Body *io.ReadCloser
|
Body *io.ReadCloser
|
||||||
Byte []byte
|
Byte []byte
|
||||||
Text string
|
Text string
|
||||||
Json map[string]any
|
Json map[string]any
|
||||||
Error error
|
Error error
|
||||||
}
|
}
|
||||||
|
|
||||||
// CurlStruct - Curl 结构体
|
// CurlStruct - Curl 结构体
|
||||||
@@ -54,15 +54,15 @@ func Curl(request ...CurlRequest) *CurlStruct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if IsEmpty(request[0].Data) {
|
if IsEmpty(request[0].Data) {
|
||||||
request[0].Data = make(map[string]string)
|
request[0].Data = make(map[string]any)
|
||||||
}
|
}
|
||||||
|
|
||||||
if IsEmpty(request[0].Query) {
|
if IsEmpty(request[0].Query) {
|
||||||
request[0].Query = make(map[string]string)
|
request[0].Query = make(map[string]any)
|
||||||
}
|
}
|
||||||
|
|
||||||
if IsEmpty(request[0].Headers) {
|
if IsEmpty(request[0].Headers) {
|
||||||
request[0].Headers = make(map[string]string)
|
request[0].Headers = make(map[string]any)
|
||||||
}
|
}
|
||||||
|
|
||||||
if IsEmpty(request[0].Client) {
|
if IsEmpty(request[0].Client) {
|
||||||
@@ -70,44 +70,44 @@ func Curl(request ...CurlRequest) *CurlStruct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return &CurlStruct{
|
return &CurlStruct{
|
||||||
request : &request[0],
|
request: &request[0],
|
||||||
response: &CurlResponse{
|
response: &CurlResponse{
|
||||||
Json : make(map[string]any),
|
Json: make(map[string]any),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get - 发起 GET 请求
|
// Get - 发起 GET 请求
|
||||||
func (this *CurlStruct) Get(url string) *CurlStruct {
|
func (this *CurlStruct) Get(url string) *CurlStruct {
|
||||||
this.request.Url = url
|
this.request.Url = url
|
||||||
this.request.Method = "GET"
|
this.request.Method = "GET"
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
// Post - 发起 POST 请求
|
// Post - 发起 POST 请求
|
||||||
func (this *CurlStruct) Post(url string) *CurlStruct {
|
func (this *CurlStruct) Post(url string) *CurlStruct {
|
||||||
this.request.Url = url
|
this.request.Url = url
|
||||||
this.request.Method = "POST"
|
this.request.Method = "POST"
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
// Put - 发起 PUT 请求
|
// Put - 发起 PUT 请求
|
||||||
func (this *CurlStruct) Put(url string) *CurlStruct {
|
func (this *CurlStruct) Put(url string) *CurlStruct {
|
||||||
this.request.Url = url
|
this.request.Url = url
|
||||||
this.request.Method = "PUT"
|
this.request.Method = "PUT"
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
// Patch - 发起 PATCH 请求
|
// Patch - 发起 PATCH 请求
|
||||||
func (this *CurlStruct) Patch(url string) *CurlStruct {
|
func (this *CurlStruct) Patch(url string) *CurlStruct {
|
||||||
this.request.Url = url
|
this.request.Url = url
|
||||||
this.request.Method = "PATCH"
|
this.request.Method = "PATCH"
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete - 发起 DELETE 请求
|
// Delete - 发起 DELETE 请求
|
||||||
func (this *CurlStruct) Delete(url string) *CurlStruct {
|
func (this *CurlStruct) Delete(url string) *CurlStruct {
|
||||||
this.request.Url = url
|
this.request.Url = url
|
||||||
this.request.Method = "DELETE"
|
this.request.Method = "DELETE"
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
@@ -190,7 +190,7 @@ func (this *CurlStruct) Send() *CurlResponse {
|
|||||||
if len(this.request.Query) > 0 {
|
if len(this.request.Query) > 0 {
|
||||||
query := url.Values{}
|
query := url.Values{}
|
||||||
for key, val := range this.request.Query {
|
for key, val := range this.request.Query {
|
||||||
query.Add(key, val)
|
query.Add(key, cast.ToString(val))
|
||||||
}
|
}
|
||||||
this.request.Url += "?" + query.Encode()
|
this.request.Url += "?" + query.Encode()
|
||||||
}
|
}
|
||||||
@@ -200,19 +200,19 @@ func (this *CurlStruct) Send() *CurlResponse {
|
|||||||
contentType, ok := this.request.Headers["Content-Type"]
|
contentType, ok := this.request.Headers["Content-Type"]
|
||||||
if ok {
|
if ok {
|
||||||
switch {
|
switch {
|
||||||
case strings.Contains(contentType, "application/json"):
|
case strings.Contains(cast.ToString(contentType), "application/json"):
|
||||||
buffer, _ = json.Marshal(this.request.Body)
|
buffer, _ = json.Marshal(this.request.Body)
|
||||||
case strings.Contains(contentType, "application/x-www-form-urlencoded"):
|
case strings.Contains(cast.ToString(contentType), "application/x-www-form-urlencoded"):
|
||||||
form := url.Values{}
|
form := url.Values{}
|
||||||
for key, val := range this.request.Data {
|
for key, val := range this.request.Data {
|
||||||
form.Add(key, val)
|
form.Add(key, cast.ToString(val))
|
||||||
}
|
}
|
||||||
buffer = []byte(form.Encode())
|
buffer = []byte(form.Encode())
|
||||||
case strings.Contains(contentType, "multipart/form-data"):
|
case strings.Contains(cast.ToString(contentType), "multipart/form-data"):
|
||||||
body := &bytes.Buffer{}
|
body := &bytes.Buffer{}
|
||||||
writer := multipart.NewWriter(body)
|
writer := multipart.NewWriter(body)
|
||||||
for key, val := range this.request.Data {
|
for key, val := range this.request.Data {
|
||||||
err := writer.WriteField(key, val)
|
err := writer.WriteField(key, cast.ToString(val))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
this.response.Error = err
|
this.response.Error = err
|
||||||
return this.response
|
return this.response
|
||||||
@@ -262,7 +262,7 @@ func (this *CurlStruct) Send() *CurlResponse {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for key, val := range this.request.Headers {
|
for key, val := range this.request.Headers {
|
||||||
req.Header.Set(key, val)
|
req.Header.Set(key, cast.ToString(val))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make HTTP request
|
// Make HTTP request
|
||||||
@@ -287,12 +287,12 @@ func (this *CurlStruct) Send() *CurlResponse {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Set response
|
// Set response
|
||||||
this.response.Byte = body
|
this.response.Byte = body
|
||||||
this.response.Body = &response.Body
|
this.response.Body = &response.Body
|
||||||
this.response.Text = string(body)
|
this.response.Text = string(body)
|
||||||
this.response.Headers = &response.Header
|
this.response.Headers = &response.Header
|
||||||
this.response.Request = response.Request
|
this.response.Request = response.Request
|
||||||
this.response.Json = cast.ToStringMap(JsonDecode(string(body)))
|
this.response.Json = cast.ToStringMap(JsonDecode(string(body)))
|
||||||
this.response.StatusCode = response.StatusCode
|
this.response.StatusCode = response.StatusCode
|
||||||
|
|
||||||
return this.response
|
return this.response
|
||||||
@@ -324,4 +324,4 @@ func Redirect(url any) (result string) {
|
|||||||
result = item.Request.URL.String()
|
result = item.Request.URL.String()
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
107
utils/is.go
107
utils/is.go
@@ -8,16 +8,25 @@ import (
|
|||||||
|
|
||||||
// IsEmail - 是否为邮箱
|
// IsEmail - 是否为邮箱
|
||||||
func IsEmail(email any) (ok bool) {
|
func IsEmail(email any) (ok bool) {
|
||||||
|
if email == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
return regexp.MustCompile(`\w[-\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\.)+[A-Za-z]{2,14}`).MatchString(cast.ToString(email))
|
return regexp.MustCompile(`\w[-\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\.)+[A-Za-z]{2,14}`).MatchString(cast.ToString(email))
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsPhone - 是否为手机号
|
// IsPhone - 是否为手机号
|
||||||
func IsPhone(phone any) (ok bool) {
|
func IsPhone(phone any) (ok bool) {
|
||||||
|
if phone == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
return regexp.MustCompile(`^1[3456789]\d{9}$`).MatchString(cast.ToString(phone))
|
return regexp.MustCompile(`^1[3456789]\d{9}$`).MatchString(cast.ToString(phone))
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsMobile - 是否为手机号
|
// IsMobile - 是否为手机号
|
||||||
func IsMobile(value any) (ok bool) {
|
func IsMobile(value any) (ok bool) {
|
||||||
|
if value == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
return regexp.MustCompile(`^1[3456789]\d{9}$`).MatchString(cast.ToString(value))
|
return regexp.MustCompile(`^1[3456789]\d{9}$`).MatchString(cast.ToString(value))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -29,6 +38,9 @@ func IsEmpty(value any) (ok bool) {
|
|||||||
|
|
||||||
// IsDomain - 是否为域名
|
// IsDomain - 是否为域名
|
||||||
func IsDomain(domain any) (ok bool) {
|
func IsDomain(domain any) (ok bool) {
|
||||||
|
if domain == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
return regexp.MustCompile(`^((https|http|ftp|rtsp|mms)?:\/\/)[^\s]+`).MatchString(cast.ToString(domain))
|
return regexp.MustCompile(`^((https|http|ftp|rtsp|mms)?:\/\/)[^\s]+`).MatchString(cast.ToString(domain))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -44,156 +56,245 @@ func IsFalse(value any) (ok bool) {
|
|||||||
|
|
||||||
// IsNumber - 是否为数字
|
// IsNumber - 是否为数字
|
||||||
func IsNumber(value any) (ok bool) {
|
func IsNumber(value any) (ok bool) {
|
||||||
|
if value == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
return regexp.MustCompile(`^[0-9]+$`).MatchString(cast.ToString(value))
|
return regexp.MustCompile(`^[0-9]+$`).MatchString(cast.ToString(value))
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsFloat - 是否为浮点数
|
// IsFloat - 是否为浮点数
|
||||||
func IsFloat(value any) (ok bool) {
|
func IsFloat(value any) (ok bool) {
|
||||||
|
if value == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
return regexp.MustCompile(`^[0-9]+(.[0-9]+)?$`).MatchString(cast.ToString(value))
|
return regexp.MustCompile(`^[0-9]+(.[0-9]+)?$`).MatchString(cast.ToString(value))
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsBool - 是否为bool
|
// IsBool - 是否为bool
|
||||||
func IsBool(value any) (ok bool) {
|
func IsBool(value any) (ok bool) {
|
||||||
_, check := value.(bool)
|
return cast.ToBool(value)
|
||||||
return check
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsAccepted - 验证某个字段是否为为 yes, on, 或是 1
|
// IsAccepted - 验证某个字段是否为为 yes, on, 或是 1
|
||||||
func IsAccepted(value any) (ok bool) {
|
func IsAccepted(value any) (ok bool) {
|
||||||
|
if value == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
return regexp.MustCompile(`^(yes|on|1)$`).MatchString(cast.ToString(value))
|
return regexp.MustCompile(`^(yes|on|1)$`).MatchString(cast.ToString(value))
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsDate - 是否为日期类型
|
// IsDate - 是否为日期类型
|
||||||
func IsDate(date any) (ok bool) {
|
func IsDate(date any) (ok bool) {
|
||||||
|
if date == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
return regexp.MustCompile(`^\d{4}-\d{1,2}-\d{1,2}$`).MatchString(cast.ToString(date))
|
return regexp.MustCompile(`^\d{4}-\d{1,2}-\d{1,2}$`).MatchString(cast.ToString(date))
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsAlpha - 只能包含字母
|
// IsAlpha - 只能包含字母
|
||||||
func IsAlpha(value any) (ok bool) {
|
func IsAlpha(value any) (ok bool) {
|
||||||
|
if value == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
return regexp.MustCompile(`^[a-zA-Z]+$`).MatchString(cast.ToString(value))
|
return regexp.MustCompile(`^[a-zA-Z]+$`).MatchString(cast.ToString(value))
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsAlphaNum - 只能包含字母和数字
|
// IsAlphaNum - 只能包含字母和数字
|
||||||
func IsAlphaNum(value any) (ok bool) {
|
func IsAlphaNum(value any) (ok bool) {
|
||||||
|
if value == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
return regexp.MustCompile(`^[a-zA-Z0-9]+$`).MatchString(cast.ToString(value))
|
return regexp.MustCompile(`^[a-zA-Z0-9]+$`).MatchString(cast.ToString(value))
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsAlphaDash - 只能包含字母、数字和下划线_及破折号-
|
// IsAlphaDash - 只能包含字母、数字和下划线_及破折号-
|
||||||
func IsAlphaDash(value any) (ok bool) {
|
func IsAlphaDash(value any) (ok bool) {
|
||||||
|
if value == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
return regexp.MustCompile(`^[a-zA-Z0-9_-]+$`).MatchString(cast.ToString(value))
|
return regexp.MustCompile(`^[a-zA-Z0-9_-]+$`).MatchString(cast.ToString(value))
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsChs - 是否为汉字
|
// IsChs - 是否为汉字
|
||||||
func IsChs(value any) (ok bool) {
|
func IsChs(value any) (ok bool) {
|
||||||
|
if value == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
return regexp.MustCompile(`^[\x{4e00}-\x{9fa5}]+$`).MatchString(cast.ToString(value))
|
return regexp.MustCompile(`^[\x{4e00}-\x{9fa5}]+$`).MatchString(cast.ToString(value))
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsChsAlpha - 只能是汉字、字母
|
// IsChsAlpha - 只能是汉字、字母
|
||||||
func IsChsAlpha(value any) (ok bool) {
|
func IsChsAlpha(value any) (ok bool) {
|
||||||
|
if value == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
return regexp.MustCompile(`^[\x{4e00}-\x{9fa5}a-zA-Z]+$`).MatchString(cast.ToString(value))
|
return regexp.MustCompile(`^[\x{4e00}-\x{9fa5}a-zA-Z]+$`).MatchString(cast.ToString(value))
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsChsAlphaNum - 只能是汉字、字母和数字
|
// IsChsAlphaNum - 只能是汉字、字母和数字
|
||||||
func IsChsAlphaNum(value any) (ok bool) {
|
func IsChsAlphaNum(value any) (ok bool) {
|
||||||
|
if value == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
return regexp.MustCompile(`^[\x{4e00}-\x{9fa5}a-zA-Z0-9]+$`).MatchString(cast.ToString(value))
|
return regexp.MustCompile(`^[\x{4e00}-\x{9fa5}a-zA-Z0-9]+$`).MatchString(cast.ToString(value))
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsChsDash - 只能是汉字、字母、数字和下划线_及破折号-
|
// IsChsDash - 只能是汉字、字母、数字和下划线_及破折号-
|
||||||
func IsChsDash(value any) (ok bool) {
|
func IsChsDash(value any) (ok bool) {
|
||||||
|
if value == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
return regexp.MustCompile(`^[\x{4e00}-\x{9fa5}a-zA-Z0-9_-]+$`).MatchString(cast.ToString(value))
|
return regexp.MustCompile(`^[\x{4e00}-\x{9fa5}a-zA-Z0-9_-]+$`).MatchString(cast.ToString(value))
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsCntrl - 是否为控制字符 - (换行、缩进、空格)
|
// IsCntrl - 是否为控制字符 - (换行、缩进、空格)
|
||||||
func IsCntrl(value any) (ok bool) {
|
func IsCntrl(value any) (ok bool) {
|
||||||
|
if value == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
return regexp.MustCompile(`^[\x00-\x1F\x7F]+$`).MatchString(cast.ToString(value))
|
return regexp.MustCompile(`^[\x00-\x1F\x7F]+$`).MatchString(cast.ToString(value))
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsGraph - 是否为可见字符 - (除空格外的所有可打印字符)
|
// IsGraph - 是否为可见字符 - (除空格外的所有可打印字符)
|
||||||
func IsGraph(value any) (ok bool) {
|
func IsGraph(value any) (ok bool) {
|
||||||
|
if value == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
return regexp.MustCompile(`^[\x21-\x7E]+$`).MatchString(cast.ToString(value))
|
return regexp.MustCompile(`^[\x21-\x7E]+$`).MatchString(cast.ToString(value))
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsLower - 是否为小写字母
|
// IsLower - 是否为小写字母
|
||||||
func IsLower(value any) (ok bool) {
|
func IsLower(value any) (ok bool) {
|
||||||
|
if value == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
return regexp.MustCompile(`^[a-z]+$`).MatchString(cast.ToString(value))
|
return regexp.MustCompile(`^[a-z]+$`).MatchString(cast.ToString(value))
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsUpper - 是否为大写字母
|
// IsUpper - 是否为大写字母
|
||||||
func IsUpper(value any) (ok bool) {
|
func IsUpper(value any) (ok bool) {
|
||||||
|
if value == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
return regexp.MustCompile(`^[A-Z]+$`).MatchString(cast.ToString(value))
|
return regexp.MustCompile(`^[A-Z]+$`).MatchString(cast.ToString(value))
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsSpace - 是否为空白字符 - (空格、制表符、换页符等)
|
// IsSpace - 是否为空白字符 - (空格、制表符、换页符等)
|
||||||
func IsSpace(value any) (ok bool) {
|
func IsSpace(value any) (ok bool) {
|
||||||
|
if value == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
return regexp.MustCompile(`^[\s]+$`).MatchString(cast.ToString(value))
|
return regexp.MustCompile(`^[\s]+$`).MatchString(cast.ToString(value))
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsXdigit - 是否为十六进制字符 - (0-9、a-f、A-F)
|
// IsXdigit - 是否为十六进制字符 - (0-9、a-f、A-F)
|
||||||
func IsXdigit(value any) (ok bool) {
|
func IsXdigit(value any) (ok bool) {
|
||||||
|
if value == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
return regexp.MustCompile(`^[\da-fA-F]+$`).MatchString(cast.ToString(value))
|
return regexp.MustCompile(`^[\da-fA-F]+$`).MatchString(cast.ToString(value))
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsActiveUrl - 是否为有效的域名或者IP
|
// IsActiveUrl - 是否为有效的域名或者IP
|
||||||
func IsActiveUrl(value any) (ok bool) {
|
func IsActiveUrl(value any) (ok bool) {
|
||||||
|
if value == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
return regexp.MustCompile(`^([a-z0-9-]+\.)+[a-z]{2,6}$`).MatchString(cast.ToString(value))
|
return regexp.MustCompile(`^([a-z0-9-]+\.)+[a-z]{2,6}$`).MatchString(cast.ToString(value))
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsIp - 是否为IP
|
// IsIp - 是否为IP
|
||||||
func IsIp(ip any) (ok bool) {
|
func IsIp(ip any) (ok bool) {
|
||||||
|
if ip == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
return regexp.MustCompile(`(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)`).MatchString(cast.ToString(ip))
|
return regexp.MustCompile(`(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)`).MatchString(cast.ToString(ip))
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsUrl - 是否为URL
|
// IsUrl - 是否为URL
|
||||||
func IsUrl(url any) (ok bool) {
|
func IsUrl(url any) (ok bool) {
|
||||||
|
if url == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
return regexp.MustCompile(`^((https|http|ftp|rtsp|mms)?:\/\/)[^\s]+`).MatchString(cast.ToString(url))
|
return regexp.MustCompile(`^((https|http|ftp|rtsp|mms)?:\/\/)[^\s]+`).MatchString(cast.ToString(url))
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsIdCard - 是否为有效的身份证号码
|
// IsIdCard - 是否为有效的身份证号码
|
||||||
func IsIdCard(value any) (ok bool) {
|
func IsIdCard(value any) (ok bool) {
|
||||||
|
if value == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
return regexp.MustCompile(`(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)`).MatchString(cast.ToString(value))
|
return regexp.MustCompile(`(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)`).MatchString(cast.ToString(value))
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsMacAddr - 是否为有效的MAC地址
|
// IsMacAddr - 是否为有效的MAC地址
|
||||||
func IsMacAddr(value any) (ok bool) {
|
func IsMacAddr(value any) (ok bool) {
|
||||||
|
if value == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
return regexp.MustCompile(`^([A-Fa-f0-9]{2}:){5}[A-Fa-f0-9]{2}$`).MatchString(cast.ToString(value))
|
return regexp.MustCompile(`^([A-Fa-f0-9]{2}:){5}[A-Fa-f0-9]{2}$`).MatchString(cast.ToString(value))
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsZip - 是否为有效的邮政编码
|
// IsZip - 是否为有效的邮政编码
|
||||||
func IsZip(value any) (ok bool) {
|
func IsZip(value any) (ok bool) {
|
||||||
|
if value == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
return regexp.MustCompile(`^[1-9]\d{5}$`).MatchString(cast.ToString(value))
|
return regexp.MustCompile(`^[1-9]\d{5}$`).MatchString(cast.ToString(value))
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsString - 是否为字符串
|
// IsString - 是否为字符串
|
||||||
func IsString(value any) (ok bool) {
|
func IsString(value any) (ok bool) {
|
||||||
|
if value == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
return reflect.TypeOf(value).Kind() == reflect.String
|
return reflect.TypeOf(value).Kind() == reflect.String
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsSlice - 是否为切片
|
// IsSlice - 是否为切片
|
||||||
func IsSlice(value any) (ok bool) {
|
func IsSlice(value any) (ok bool) {
|
||||||
|
if value == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
return reflect.TypeOf(value).Kind() == reflect.Slice
|
return reflect.TypeOf(value).Kind() == reflect.Slice
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsArray - 是否为数组
|
// IsArray - 是否为数组
|
||||||
func IsArray(value any) (ok bool) {
|
func IsArray(value any) (ok bool) {
|
||||||
|
if value == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
return reflect.TypeOf(value).Kind() == reflect.Array
|
return reflect.TypeOf(value).Kind() == reflect.Array
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsJsonString - 是否为json字符串
|
// IsJsonString - 是否为json字符串
|
||||||
func IsJsonString(value any) (ok bool) {
|
func IsJsonString(value any) (ok bool) {
|
||||||
|
if value == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
return regexp.MustCompile(`^[\{\[].*[\}\]]$`).MatchString(cast.ToString(value))
|
return regexp.MustCompile(`^[\{\[].*[\}\]]$`).MatchString(cast.ToString(value))
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsMap - 是否为map
|
// IsMap - 是否为map
|
||||||
func IsMap(value any) (ok bool) {
|
func IsMap(value any) (ok bool) {
|
||||||
|
if value == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
return reflect.TypeOf(value).Kind() == reflect.Map
|
return reflect.TypeOf(value).Kind() == reflect.Map
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsSliceSlice - 是否为二维切片
|
// IsSliceSlice - 是否为二维切片
|
||||||
func IsSliceSlice(value any) (ok bool) {
|
func IsSliceSlice(value any) (ok bool) {
|
||||||
|
if value == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
return reflect.TypeOf(value).Kind() == reflect.Slice && reflect.TypeOf(value).Elem().Kind() == reflect.Slice
|
return reflect.TypeOf(value).Kind() == reflect.Slice && reflect.TypeOf(value).Elem().Kind() == reflect.Slice
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsMapAny - 是否为[]map[string]any
|
// IsMapAny - 是否为[]map[string]any
|
||||||
func IsMapAny(value any) (ok bool) {
|
func IsMapAny(value any) (ok bool) {
|
||||||
|
if value == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
return reflect.TypeOf(value).Kind() == reflect.Map && reflect.TypeOf(value).Elem().Kind() == reflect.Interface
|
return reflect.TypeOf(value).Kind() == reflect.Map && reflect.TypeOf(value).Elem().Kind() == reflect.Interface
|
||||||
}
|
}
|
||||||
|
@@ -132,7 +132,7 @@ func ValidateRules(name string, value any, rule string, message map[string]strin
|
|||||||
rules := strings.Split(rule, ",")
|
rules := strings.Split(rule, ",")
|
||||||
|
|
||||||
// 判断 rules 是否包含 required
|
// 判断 rules 是否包含 required
|
||||||
if !InArray("required", cast.ToSlice(rules)) {
|
if !InArray("required", cast.ToStringSlice(rules)) {
|
||||||
// 判断 value 是否为空
|
// 判断 value 是否为空
|
||||||
if IsEmpty(value) {
|
if IsEmpty(value) {
|
||||||
// 结束本次循环,进入下一次循环
|
// 结束本次循环,进入下一次循环
|
||||||
|
Reference in New Issue
Block a user