tool.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. package assist
  2. import (
  3. "bufio"
  4. "bytes"
  5. "errors"
  6. "fmt"
  7. "git.bvbej.com/bvbej/base-golang/pkg/time_parse"
  8. "io"
  9. "math"
  10. "math/rand"
  11. "net"
  12. "net/url"
  13. "regexp"
  14. "strconv"
  15. "strings"
  16. "time"
  17. "unicode"
  18. )
  19. func GetOrderNumber() string {
  20. parse, _ := time.Parse(time_parse.CSTLayout, "2021-04-27 00:00:00")
  21. hours := time.Now().Sub(parse).Hours()
  22. day := math.Floor(hours / 24)
  23. prefix := fmt.Sprintf("%05d", int64(day))
  24. format := time.Now().Format("") + "%0" + strconv.Itoa(10) + "d"
  25. n := math.Pow10(10)
  26. return prefix + fmt.Sprintf(format, rand.New(rand.NewSource(time.Now().UnixNano())).Int63n(int64(n)))
  27. }
  28. func ByteFmt(size int64) string {
  29. var unitArr = []string{"B", "KB", "MB", "GB", "TB", "EB"}
  30. if size == 0 {
  31. return "unknown"
  32. }
  33. fs := float64(size)
  34. p := int(math.Log(fs) / math.Log(1024))
  35. val := fs / math.Pow(1024, float64(p))
  36. _, frac := math.Modf(val)
  37. if frac > 0 {
  38. return fmt.Sprintf("%.1f%s", math.Floor(val*10)/10, unitArr[p])
  39. } else {
  40. return fmt.Sprintf("%d%s", int(val), unitArr[p])
  41. }
  42. }
  43. func UniqueArray[T comparable](array []T) []T {
  44. result := make([]T, 0, len(array))
  45. temp := map[T]struct{}{}
  46. for _, item := range array {
  47. if _, ok := temp[item]; !ok {
  48. temp[item] = struct{}{}
  49. result = append(result, item)
  50. }
  51. }
  52. return result
  53. }
  54. func InArray[T comparable](item T, array []T) bool {
  55. for _, s := range array {
  56. if item == s {
  57. return true
  58. }
  59. }
  60. return false
  61. }
  62. func CurrencyDecimal(value float64) float64 {
  63. value, _ = strconv.ParseFloat(fmt.Sprintf("%.8f", value), 64)
  64. return value
  65. }
  66. func IsChinese(str string) bool {
  67. var count int
  68. for _, v := range str {
  69. if unicode.Is(unicode.Han, v) {
  70. count++
  71. break
  72. }
  73. }
  74. return count > 0
  75. }
  76. func RandInt(max int) int {
  77. if max <= 0 {
  78. return 0
  79. }
  80. rand.Seed(time.Now().UnixNano())
  81. <-time.After(time.Nanosecond)
  82. return rand.Intn(max)
  83. }
  84. func FindIPv4(input string) string {
  85. partIp := "(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])"
  86. must := partIp + "\\." + partIp + "\\." + partIp + "\\." + partIp
  87. matchMe := regexp.MustCompile(must)
  88. return matchMe.FindString(input)
  89. }
  90. func RandString(n int) string {
  91. var letterRunes = []rune("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
  92. b := make([]rune, n)
  93. for i := range b {
  94. b[i] = letterRunes[RandInt(len(letterRunes))]
  95. }
  96. return string(b)
  97. }
  98. func IP4ToInt(ip net.IP) int {
  99. ipSplit := strings.Split(ip.To4().String(), ".")
  100. var ipInt = 0
  101. var pos uint = 24
  102. for _, ipSeg := range ipSplit {
  103. tempInt, _ := strconv.Atoi(ipSeg)
  104. tempInt = tempInt << pos
  105. ipInt = ipInt | tempInt
  106. pos -= 8
  107. }
  108. return ipInt
  109. }
  110. func IP4IntToString(ipInt int) string {
  111. ipSplit := make([]string, 4)
  112. var length = len(ipSplit)
  113. buffer := bytes.NewBufferString("")
  114. for i := 0; i < length; i++ {
  115. tempInt := ipInt & 0xFF
  116. ipSplit[length-i-1] = strconv.Itoa(tempInt)
  117. ipInt = ipInt >> 8
  118. }
  119. for i := 0; i < length; i++ {
  120. buffer.WriteString(ipSplit[i])
  121. if i < length-1 {
  122. buffer.WriteString(".")
  123. }
  124. }
  125. return buffer.String()
  126. }
  127. func CompareVersion(verCurrent, verUpdate string) int {
  128. versionA := strings.Split(verCurrent, ".")
  129. versionB := strings.Split(verUpdate, ".")
  130. for i := len(versionA); i < 4; i++ {
  131. versionA = append(versionA, "0")
  132. }
  133. for i := len(versionB); i < 4; i++ {
  134. versionB = append(versionB, "0")
  135. }
  136. for i := 0; i < 4; i++ {
  137. version1, _ := strconv.Atoi(versionA[i])
  138. version2, _ := strconv.Atoi(versionB[i])
  139. if version1 == version2 {
  140. continue
  141. } else if version1 > version2 {
  142. return 1
  143. } else {
  144. return -1
  145. }
  146. }
  147. return 0
  148. }
  149. func GetLocalV4IP() (string, error) {
  150. var address []net.Addr
  151. eth0Interface, err := net.InterfaceByName("eth0")
  152. if err == nil {
  153. address, err = eth0Interface.Addrs()
  154. } else {
  155. address, err = net.InterfaceAddrs()
  156. }
  157. if err != nil {
  158. return "", err
  159. }
  160. for _, addr := range address {
  161. if it, ok := addr.(*net.IPNet); ok && !it.IP.IsLoopback() {
  162. if it.IP.To4() != nil {
  163. return it.IP.String(), nil
  164. }
  165. }
  166. }
  167. return "", err
  168. }
  169. func CheckWebsiteAddr(addr string) error {
  170. if 0 == len(addr) {
  171. return errors.New("url is empty")
  172. }
  173. parse, err := url.Parse(addr)
  174. if err != nil {
  175. return err
  176. }
  177. if "http" != parse.Scheme && "https" != parse.Scheme {
  178. return errors.New("url Scheme illegal. err:" + parse.Scheme)
  179. }
  180. 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])$`)
  181. u := strings.Split(parse.Host, ":")
  182. if 2 == len(u) && 0 != len(u[1]) {
  183. result := re.FindAllStringSubmatch(u[0], -1)
  184. if result == nil {
  185. return errors.New("url illegal. err:" + u[0])
  186. }
  187. return nil
  188. }
  189. result := re.FindAllStringSubmatch(parse.Host, -1)
  190. if result == nil {
  191. return errors.New("url illegal. err:" + parse.Host)
  192. }
  193. return nil
  194. }
  195. func LoadKeyValConfig(text string) map[string]string {
  196. config := make(map[string]string)
  197. r := bufio.NewReader(bytes.NewBufferString(text))
  198. for {
  199. b, _, err := r.ReadLine()
  200. if err != nil {
  201. if err == io.EOF {
  202. break
  203. }
  204. panic(err)
  205. }
  206. s := strings.TrimSpace(string(b))
  207. index := strings.Index(s, "=")
  208. if index < 0 || strings.HasPrefix(s, "#") {
  209. continue
  210. }
  211. key := strings.TrimSpace(s[:index])
  212. if len(key) == 0 {
  213. continue
  214. }
  215. value := strings.TrimSpace(s[index+1:])
  216. if len(value) == 0 {
  217. continue
  218. }
  219. config[key] = value
  220. }
  221. return config
  222. }