You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
110 lines
2.3 KiB
110 lines
2.3 KiB
package utils
|
|
|
|
import (
|
|
"math"
|
|
"math/rand"
|
|
"regexp"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
func GetInvitation() string {
|
|
var bs = make([]byte, 6)
|
|
for i := 0; i < 6; i++ {
|
|
rand.Seed(time.Now().UnixNano())
|
|
flag := rand.Intn(2)
|
|
if flag == 1 {
|
|
bs[i] = byte(rand.Float64()*10 + 48)
|
|
} else {
|
|
bs[i] = byte(rand.Float64()*26 + 65)
|
|
}
|
|
}
|
|
return string(bs)
|
|
}
|
|
|
|
func GetInvitationLen(length int) string {
|
|
var bs = make([]byte, length)
|
|
for i := 0; i < length; i++ {
|
|
rand.Seed(time.Now().UnixNano())
|
|
flag := rand.Intn(3)
|
|
if flag == 1 {
|
|
bs[i] = byte(rand.Float64()*10 + 48)
|
|
} else if flag == 0 {
|
|
bs[i] = byte(rand.Float64()*26 + 65)
|
|
} else {
|
|
bs[i] = byte(rand.Float64()*26 + 97)
|
|
}
|
|
}
|
|
return string(bs)
|
|
}
|
|
|
|
func GetInvitationLowerLen(length int) string {
|
|
var bs = make([]byte, length)
|
|
for i := 0; i < length; i++ {
|
|
rand.Seed(time.Now().UnixNano())
|
|
flag := rand.Intn(2)
|
|
if flag == 1 {
|
|
bs[i] = byte(rand.Float64()*10 + 48)
|
|
} else {
|
|
bs[i] = byte(rand.Float64()*26 + 97)
|
|
}
|
|
}
|
|
return string(bs)
|
|
}
|
|
|
|
func StringFloat64ToInt(s string) int {
|
|
f1, err := strconv.ParseFloat(s, 64)
|
|
if err != nil {
|
|
return 0
|
|
}
|
|
s1 := strconv.FormatFloat(math.Ceil(f1), 'f', -1, 64)
|
|
i, _ := strconv.Atoi(s1)
|
|
return i
|
|
}
|
|
|
|
func HideStar(str string) (result string) {
|
|
if str == "" {
|
|
return "***"
|
|
}
|
|
if strings.Contains(str, "@") {
|
|
// 邮箱
|
|
res := strings.Split(str, "@")
|
|
if len(res[0]) < 3 {
|
|
resString := "***"
|
|
result = resString + "@" + res[1]
|
|
} else {
|
|
res2 := Substr2(str, 0, 3)
|
|
resString := res2 + "***"
|
|
result = resString + "@" + res[1]
|
|
}
|
|
return result
|
|
} else {
|
|
reg := `^1[0-9]\d{9}$`
|
|
rgx := regexp.MustCompile(reg)
|
|
mobileMatch := rgx.MatchString(str)
|
|
if mobileMatch {
|
|
// 手机号
|
|
result = Substr2(str, 0, 3) + "****" + Substr2(str, 7, 11)
|
|
} else {
|
|
nameRune := []rune(str)
|
|
lens := len(nameRune)
|
|
if lens <= 1 {
|
|
result = "***"
|
|
} else if lens == 2 {
|
|
result = string(nameRune[:1]) + "*"
|
|
} else if lens == 3 {
|
|
result = string(nameRune[:1]) + "*" + string(nameRune[2:3])
|
|
} else if lens == 4 {
|
|
result = string(nameRune[:1]) + "**" + string(nameRune[lens-1:lens])
|
|
} else if lens > 4 {
|
|
result = string(nameRune[:2]) + "***" + string(nameRune[lens-2:lens])
|
|
}
|
|
}
|
|
return
|
|
}
|
|
}
|
|
func Substr2(str string, start int, end int) string {
|
|
rs := []rune(str)
|
|
return string(rs[start:end])
|
|
}
|
|
|