123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- package assist
- import (
- "bufio"
- "bytes"
- "errors"
- "fmt"
- "git.bvbej.com/bvbej/base-golang/pkg/time_parse"
- "io"
- "math"
- "math/rand"
- "net"
- "net/url"
- "regexp"
- "strconv"
- "strings"
- "time"
- "unicode"
- )
- func GetOrderNumber() string {
- parse, _ := time.Parse(time_parse.CSTLayout, "2021-04-27 00:00:00")
- hours := time.Now().Sub(parse).Hours()
- day := math.Floor(hours / 24)
- prefix := fmt.Sprintf("%05d", int64(day))
- format := time.Now().Format("") + "%0" + strconv.Itoa(10) + "d"
- n := math.Pow10(10)
- return prefix + fmt.Sprintf(format, rand.New(rand.NewSource(time.Now().UnixNano())).Int63n(int64(n)))
- }
- func ByteFmt(size int64) string {
- var unitArr = []string{"B", "KB", "MB", "GB", "TB", "EB"}
- if size == 0 {
- return "unknown"
- }
- fs := float64(size)
- p := int(math.Log(fs) / math.Log(1024))
- val := fs / math.Pow(1024, float64(p))
- _, frac := math.Modf(val)
- if frac > 0 {
- return fmt.Sprintf("%.1f%s", math.Floor(val*10)/10, unitArr[p])
- } else {
- return fmt.Sprintf("%d%s", int(val), unitArr[p])
- }
- }
- func UniqueArray[T comparable](array []T) []T {
- result := make([]T, 0, len(array))
- temp := map[T]struct{}{}
- for _, item := range array {
- if _, ok := temp[item]; !ok {
- temp[item] = struct{}{}
- result = append(result, item)
- }
- }
- return result
- }
- func InArray[T comparable](item T, array []T) bool {
- for _, s := range array {
- if item == s {
- return true
- }
- }
- return false
- }
- func CurrencyDecimal(value float64) float64 {
- value, _ = strconv.ParseFloat(fmt.Sprintf("%.8f", value), 64)
- return value
- }
- func IsChinese(str string) bool {
- var count int
- for _, v := range str {
- if unicode.Is(unicode.Han, v) {
- count++
- break
- }
- }
- return count > 0
- }
- func RandInt(max int) int {
- if max <= 0 {
- return 0
- }
- rand.Seed(time.Now().UnixNano())
- <-time.After(time.Nanosecond)
- return rand.Intn(max)
- }
- func FindIPv4(input string) string {
- partIp := "(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])"
- must := partIp + "\\." + partIp + "\\." + partIp + "\\." + partIp
- matchMe := regexp.MustCompile(must)
- return matchMe.FindString(input)
- }
- func RandString(n int) string {
- var letterRunes = []rune("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
- b := make([]rune, n)
- for i := range b {
- b[i] = letterRunes[RandInt(len(letterRunes))]
- }
- return string(b)
- }
- func IP4ToInt(ip net.IP) int {
- ipSplit := strings.Split(ip.To4().String(), ".")
- var ipInt = 0
- var pos uint = 24
- for _, ipSeg := range ipSplit {
- tempInt, _ := strconv.Atoi(ipSeg)
- tempInt = tempInt << pos
- ipInt = ipInt | tempInt
- pos -= 8
- }
- return ipInt
- }
- func IP4IntToString(ipInt int) string {
- ipSplit := make([]string, 4)
- var length = len(ipSplit)
- buffer := bytes.NewBufferString("")
- for i := 0; i < length; i++ {
- tempInt := ipInt & 0xFF
- ipSplit[length-i-1] = strconv.Itoa(tempInt)
- ipInt = ipInt >> 8
- }
- for i := 0; i < length; i++ {
- buffer.WriteString(ipSplit[i])
- if i < length-1 {
- buffer.WriteString(".")
- }
- }
- return buffer.String()
- }
- func CompareVersion(verCurrent, verUpdate string) int {
- versionA := strings.Split(verCurrent, ".")
- versionB := strings.Split(verUpdate, ".")
- for i := len(versionA); i < 4; i++ {
- versionA = append(versionA, "0")
- }
- for i := len(versionB); i < 4; i++ {
- versionB = append(versionB, "0")
- }
- for i := 0; i < 4; i++ {
- version1, _ := strconv.Atoi(versionA[i])
- version2, _ := strconv.Atoi(versionB[i])
- if version1 == version2 {
- continue
- } else if version1 > version2 {
- return 1
- } else {
- return -1
- }
- }
- return 0
- }
- func GetLocalV4IP() (string, error) {
- var address []net.Addr
- eth0Interface, err := net.InterfaceByName("eth0")
- if err == nil {
- address, err = eth0Interface.Addrs()
- } else {
- address, err = net.InterfaceAddrs()
- }
- if err != nil {
- return "", err
- }
- for _, addr := range address {
- if it, ok := addr.(*net.IPNet); ok && !it.IP.IsLoopback() {
- if it.IP.To4() != nil {
- return it.IP.String(), nil
- }
- }
- }
- return "", err
- }
- func CheckWebsiteAddr(addr string) error {
- if 0 == len(addr) {
- return errors.New("url is empty")
- }
- parse, err := url.Parse(addr)
- if err != nil {
- return err
- }
- if "http" != parse.Scheme && "https" != parse.Scheme {
- return errors.New("url Scheme illegal. err:" + parse.Scheme)
- }
- re := regexp.MustCompile(`^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$|^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)+([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])$`)
- u := strings.Split(parse.Host, ":")
- if 2 == len(u) && 0 != len(u[1]) {
- result := re.FindAllStringSubmatch(u[0], -1)
- if result == nil {
- return errors.New("url illegal. err:" + u[0])
- }
- return nil
- }
- result := re.FindAllStringSubmatch(parse.Host, -1)
- if result == nil {
- return errors.New("url illegal. err:" + parse.Host)
- }
- return nil
- }
- func LoadKeyValConfig(text string) map[string]string {
- config := make(map[string]string)
- r := bufio.NewReader(bytes.NewBufferString(text))
- for {
- b, _, err := r.ReadLine()
- if err != nil {
- if err == io.EOF {
- break
- }
- panic(err)
- }
- s := strings.TrimSpace(string(b))
- index := strings.Index(s, "=")
- if index < 0 || strings.HasPrefix(s, "#") {
- continue
- }
- key := strings.TrimSpace(s[:index])
- if len(key) == 0 {
- continue
- }
- value := strings.TrimSpace(s[index+1:])
- if len(value) == 0 {
- continue
- }
- config[key] = value
- }
- return config
- }
|