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