This commit is contained in:
2024-12-17 22:28:48 +08:00
commit 754e1ae43c
50 changed files with 2969 additions and 0 deletions

100
util/convert.go Normal file
View File

@ -0,0 +1,100 @@
package util
import (
"math"
"reflect"
"strconv"
"strings"
"unsafe"
)
// 字符串转Int
// intStr数字的字符串
func String2Int(intStr string) (intNum int) {
intNum, _ = strconv.Atoi(intStr)
return
}
// 字符串转Int64
// intStr数字的字符串
func String2Int64(intStr string) (int64Num int64) {
intNum, _ := strconv.Atoi(intStr)
int64Num = int64(intNum)
return
}
// 字符串转Float64
// floatStr小数点数字的字符串
func String2Float64(floatStr string) (floatNum float64) {
floatNum, _ = strconv.ParseFloat(floatStr, 64)
return
}
// 字符串转Float32
// floatStr小数点数字的字符串
func String2Float32(floatStr string) (floatNum float32) {
floatNum64, _ := strconv.ParseFloat(floatStr, 32)
floatNum = float32(floatNum64)
return
}
// Int转字符串
// intNum数字字符串
func Int2String(intNum int) (intStr string) {
intStr = strconv.Itoa(intNum)
return
}
// Int64转字符串
// intNum数字字符串
func Int642String(intNum int64) (int64Str string) {
//10, 代表10进制
int64Str = strconv.FormatInt(intNum, 10)
return
}
// Float64转字符串
// floatNumfloat64数字
// prec精度位数不传则默认float数字精度
func Float64ToString(floatNum float64, prec ...int) (floatStr string) {
if len(prec) > 0 {
floatStr = strconv.FormatFloat(floatNum, 'f', prec[0], 64)
return
}
floatStr = strconv.FormatFloat(floatNum, 'f', -1, 64)
return
}
// Float32转字符串
// floatNumfloat32数字
// prec精度位数不传则默认float数字精度
func Float32ToString(floatNum float32, prec ...int) (floatStr string) {
if len(prec) > 0 {
floatStr = strconv.FormatFloat(float64(floatNum), 'f', prec[0], 32)
return
}
floatStr = strconv.FormatFloat(float64(floatNum), 'f', -1, 32)
return
}
// 二进制转10进制
func BinaryToDecimal(bit string) (num int) {
fields := strings.Split(bit, "")
lens := len(fields)
var tempF float64 = 0
for i := 0; i < lens; i++ {
floatNum := String2Float64(fields[i])
tempF += floatNum * math.Pow(2, float64(lens-i-1))
}
num = int(tempF)
return
}
// BytesToString 0 拷贝转换 slice byte 为 string
func BytesToString(b []byte) (s string) {
_bptr := (*reflect.SliceHeader)(unsafe.Pointer(&b))
_sptr := (*reflect.StringHeader)(unsafe.Pointer(&s))
_sptr.Data = _bptr.Data
_sptr.Len = _bptr.Len
return s
}

12
util/md5.go Normal file
View File

@ -0,0 +1,12 @@
package util
import (
"crypto/md5"
"encoding/hex"
)
func EncodeMD5(value string) string {
m := md5.New()
m.Write([]byte(value))
return hex.EncodeToString(m.Sum(nil))
}

36
util/password/password.go Normal file
View File

@ -0,0 +1,36 @@
package password
import (
"crypto/md5"
"errors"
"golang.org/x/crypto/bcrypt"
)
func Encode(userPassword string) (string, error) {
md5Password := md5.Sum([]byte(userPassword))
encode, err := generatePassword(string(md5Password[:]))
if err != nil {
return "", errors.New("密码加密失败")
} else {
return string(encode), nil
}
}
func ValidateCode(userPassword string, hashed string) (isOK bool, err error) {
md5Password := md5.Sum([]byte(userPassword))
isOk, err := validatePassword(string(md5Password[:]), hashed)
return isOk, err
}
//generatePassword 给密码就行加密操作
func generatePassword(userPassword string) ([]byte, error) {
return bcrypt.GenerateFromPassword([]byte(userPassword), bcrypt.DefaultCost)
}
//validatePassword 密码比对
func validatePassword(userPassword string, hashed string) (isOK bool, err error) {
if err = bcrypt.CompareHashAndPassword([]byte(hashed), []byte(userPassword)); err != nil {
return false, errors.New("密码错误!")
}
return true, nil
}

42
util/random.go Normal file
View File

@ -0,0 +1,42 @@
package util
import (
"math/rand"
"time"
)
// 随机生成字符串
func RandomString(l int) string {
str := "0123456789AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz"
bytes := []byte(str)
var result []byte = make([]byte, 0, l)
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < l; i++ {
result = append(result, bytes[r.Intn(len(bytes))])
}
return BytesToString(result)
}
// 随机生成纯字符串
func RandomPureString(l int) string {
str := "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz"
bytes := []byte(str)
var result []byte = make([]byte, 0, l)
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < l; i++ {
result = append(result, bytes[r.Intn(len(bytes))])
}
return BytesToString(result)
}
// 随机生成数字字符串
func RandomNumber(l int) string {
str := "0123456789"
bytes := []byte(str)
var result []byte
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < l; i++ {
result = append(result, bytes[r.Intn(len(bytes))])
}
return BytesToString(result)
}

104
util/string.go Normal file
View File

@ -0,0 +1,104 @@
package util
import (
"strconv"
"strings"
"unicode"
)
//判断是否是数字字符串
func IsDigit(str string) bool {
for _, x := range []rune(str) {
if !unicode.IsDigit(x) {
return false
}
}
return true
}
func Atoi(i string) int {
if i == "" {
return 0
}
j, err := strconv.Atoi(i)
if err != nil {
return 0
} else {
return j
}
}
func StringToInt(s interface{}) int {
switch i := s.(type) {
case string:
return Atoi(i)
case float32:
return int(i)
case float64:
return int(i)
case int:
return i
case uint:
return int(i)
}
return 0
}
/**
* 驼峰转蛇形 snake string
* @description XxYy to xx_yy , XxYY to xx_y_y
* @date 2020/7/30
* @param s 需要转换的字符串
* @return string
**/
func SnakeString(s string) string {
data := make([]byte, 0, len(s)*2)
j := false
num := len(s)
for i := 0; i < num; i++ {
d := s[i]
// or通过ASCII码进行大小写的转化
// 65-90A-Z97-122a-z
//判断如果字母为大写的A-Z就在前面拼接一个_
if i > 0 && d >= 'A' && d <= 'Z' && j {
data = append(data, '_')
}
if d != '_' {
j = true
}
data = append(data, d)
}
//ToLower把大写字母统一转小写
return strings.ToLower(string(data[:]))
}
/**
* 蛇形转驼峰
* @description xx_yy to XxYx xx_y_y to XxYY
* @date 2020/7/30
* @param s要转换的字符串
* @return string
**/
func CamelString(s string) string {
data := make([]byte, 0, len(s))
j := false
k := false
num := len(s) - 1
for i := 0; i <= num; i++ {
d := s[i]
if k == false && d >= 'A' && d <= 'Z' {
k = true
}
if d >= 'a' && d <= 'z' && (j || k == false) {
d = d - 32
j = false
k = true
}
if k && d == '_' && num > i && s[i+1] >= 'a' && s[i+1] <= 'z' {
j = true
continue
}
data = append(data, d)
}
return string(data[:])
}

13
util/validator.go Normal file
View File

@ -0,0 +1,13 @@
package util
import "regexp"
// 识别手机号码
func IsMobile(mobile string) bool {
result, _ := regexp.MatchString(`^(1[0-9][0-9]\d{4,8})$`, mobile)
if result {
return true
} else {
return false
}
}