package utils import ( "crypto/md5" "crypto/rand" "crypto/sha256" "encoding/hex" "encoding/json" "errors" "fmt" "github.com/beego/beego/v2/core/logs" "io" "math" "net/url" "os" "regexp" "strconv" "strings" "time" "unicode" ) // 用户输入组合路径安全校验 func CheckPath(param string) error { if count := strings.Count(param, "."); count > 0 { return errors.New("路径中不能包含非法字符“.”") } if count := strings.Count(param, "/"); count > 0 { return errors.New("路径中不能包含非法字符“/”") } if count := strings.Count(param, "\\"); count > 0 { return errors.New("路径中不能包含非法字符“\\”") } return nil } // 用户输入文件名安全校验 func CheckFilename(param string) error { if count := strings.Count(param, "."); count > 1 { return errors.New("文件名中不能超过一个“.”") } if count := strings.Count(param, "/"); count > 0 { return errors.New("文件名中不能包含非法字符“/”") } if count := strings.Count(param, "\\"); count > 0 { return errors.New("文件名中不能包含非法字符“\\”") } return nil } // 用户文件全路径安全校验 func CheckPathFilename(param string) error { if count := strings.Count(param, "."); count > 2 { return errors.New("文件全路径中不能超过两个“.”") } if count := strings.Count(param, "/"); count > 5 { return errors.New("文件全路径中不能包含非法字符“/”") } if count := strings.Count(param, "\\"); count > 0 { return errors.New("文件全路径中不能包含非法字符“\\”") } return nil } // 提取url中的路径 func GetUrlPath(rawURL string) string { parsedURL, err := url.Parse(rawURL) if err != nil { logs.Error("url parse error: %v", err) return "" } return parsedURL.Path } // 字符串替换非法字符 func ReplaceUserInput(s string) string { newStringInput := strings.NewReplacer("\n", " ", "\r", " ") return newStringInput.Replace(s) } // 字符包含非法字符 func ContainsIllegal(target string) bool { var str_array [3]string = [3]string{"/", "./", "\\"} for _, element := range str_array { if strings.Contains(target, element) { return true } } return false } // 文件md5计算 func FileSign(filePath string, sign string) (string, error) { check_err := CheckPathFilename(filePath) if check_err != nil { return "", check_err } file, err := os.Open(filePath) if err != nil { return "", err } if sign == "MD5" { hash := md5.New() _, _ = io.Copy(hash, file) return hex.EncodeToString(hash.Sum(nil)), nil } else { hash := sha256.New() _, _ = io.Copy(hash, file) return hex.EncodeToString(hash.Sum(nil)), nil } } func GetFileSize(filePath string) (int64, error) { check_err := CheckPathFilename(filePath) if check_err != nil { return 0, check_err } fi, err := os.Stat(filePath) if err != nil { return 0, err } return fi.Size(), nil } // GenerateAppKey 生成指定字节长度的随机字符串 func GenerateAppKey(length int) string { b := make([]byte, length) _, err := rand.Read(b) if err != nil { panic(err) } return hex.EncodeToString(b) } // 驼峰转_ func CamelToSnake(s string) string { var result []rune for i, r := range s { if i > 0 && unicode.IsUpper(r) { result = append(result, '_') } result = append(result, unicode.ToLower(r)) } return string(result) } func WordsToSnake(s string) string { return strings.Replace(s, " ", "_", -1) } func ToInt(value interface{}) int { var key int if value == nil { return key } switch value.(type) { case float64: key = int(value.(float64)) case float32: key = int(value.(float32)) case int: key = int(value.(int)) case uint: key = int(value.(uint)) case int8: key = int(value.(int8)) case uint8: key = int(value.(uint8)) case int16: key = int(value.(int16)) case uint16: key = int(value.(uint16)) case int32: key = int(value.(int32)) case uint32: key = int(value.(uint32)) case int64: key = int(value.(int64)) case uint64: key = int(value.(uint64)) case string: key, _ = strconv.Atoi(value.(string)) case []byte: key, _ = strconv.Atoi(string(value.([]byte))) default: newValue, _ := json.Marshal(value) key, _ = strconv.Atoi(string(newValue)) } return key } func ToFloat64(value interface{}) float64 { var key float64 if value == nil { return key } switch value.(type) { case float64: key = value.(float64) case float32: key = float64(value.(float32)) case int: key = float64(value.(int)) case uint: key = float64(value.(uint)) case int8: key = float64(value.(int8)) case uint8: key = float64(value.(uint8)) case int16: key = float64(value.(int16)) case uint16: key = float64(value.(uint16)) case int32: key = float64(value.(int32)) case uint32: key = float64(value.(uint32)) case int64: key = float64(value.(int64)) case uint64: key = float64(value.(uint64)) case string: key_float64, _ := strconv.ParseFloat(value.(string), 32/64) key = key_float64 case []byte: key_float64, _ := strconv.ParseFloat(string(value.([]byte)), 32/64) key = key_float64 default: newValue, _ := json.Marshal(value) key_float64, _ := strconv.ParseFloat(string(newValue), 32/64) key = key_float64 } key_float64, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", key), 32/64) return key_float64 } func ToDate(value interface{}) string { var key string if value == nil { return key } switch value.(type) { case time.Time: key = value.(time.Time).Format("2006-01-02") default: newValue, _ := json.Marshal(value) key = string(newValue) } return key } func ToString(value interface{}) string { if value == nil { return "" } switch v := value.(type) { case string: return v case int: return strconv.FormatInt(int64(v), 10) case int8: return strconv.FormatInt(int64(v), 10) case int16: return strconv.FormatInt(int64(v), 10) case int32: return strconv.FormatInt(int64(v), 10) case int64: return strconv.FormatInt(v, 10) case uint: return strconv.FormatUint(uint64(v), 10) case uint8: return strconv.FormatUint(uint64(v), 10) case uint16: return strconv.FormatUint(uint64(v), 10) case uint32: return strconv.FormatUint(uint64(v), 10) case uint64: return strconv.FormatUint(v, 10) } return "" } // IsChineseOnly 判断字符串是否只包含中文字符 func IsChineseOnly(s string) bool { chineseRegexp := regexp.MustCompile("^[\\p{Han}]+$") return chineseRegexp.MatchString(s) } // 将数字转换为中文大写形式 func AmountConvert(p_money float64, p_round bool) string { var NumberUpper = []string{"壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖", "零"} var Unit = []string{"分", "角", "元", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟"} var regex = [][]string{ {"零拾", "零"}, {"零佰", "零"}, {"零仟", "零"}, {"零零零", "零"}, {"零零", "零"}, {"零角零分", "整"}, {"零分", "整"}, {"零角", "零"}, {"零亿零万零元", "亿元"}, {"亿零万零元", "亿元"}, {"零亿零万", "亿"}, {"零万零元", "万元"}, {"万零元", "万元"}, {"零亿", "亿"}, {"零万", "万"}, {"拾零圆", "拾元"}, {"零圆", "元"}, {"零零", "零"}} str, DigitUpper, Unit_Len, round := "", "", 0, 0 if p_money == 0 { return "零" } if p_money < 0 { str = "负" p_money = math.Abs(p_money) } if p_round { round = 2 } else { round = 1 } Digit_byte := []byte(strconv.FormatFloat(p_money, 'f', round+1, 64)) //注意币种四舍五入 Unit_Len = len(Digit_byte) - round for _, v := range Digit_byte { if Unit_Len >= 1 && v != 46 { s, _ := strconv.ParseInt(string(v), 10, 0) if s != 0 { DigitUpper = NumberUpper[s-1] } else { DigitUpper = "零" } str = str + DigitUpper + Unit[Unit_Len-1] Unit_Len = Unit_Len - 1 } } for i, _ := range regex { reg := regexp.MustCompile(regex[i][0]) str = reg.ReplaceAllString(str, regex[i][1]) } if string(str[0:3]) == "元" { str = str[3:len(str)] } if string(str[0:3]) == "零" { str = str[3:len(str)] } if strings.Contains(str, "角") { str = strings.Replace(str, "整", "", 1) } return str } func IntArrayToStringArray(list []int) (res []string) { for i := 0; i < len(list); i++ { res = append(res, strconv.Itoa(list[i])) } return res }