helper.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. package tool
  2. import (
  3. "bufio"
  4. "bytes"
  5. "errors"
  6. "fmt"
  7. "io"
  8. "math"
  9. "math/rand"
  10. "net"
  11. "net/http"
  12. "net/url"
  13. "os"
  14. "regexp"
  15. "strconv"
  16. "strings"
  17. "time"
  18. "unicode"
  19. )
  20. // GetOrderNumber 获取订单号
  21. func GetOrderNumber() string {
  22. parse, _ := time.Parse(time.DateTime, "2021-04-27 00:00:00")
  23. hours := time.Now().Sub(parse).Hours()
  24. day := math.Floor(hours / 24)
  25. prefix := fmt.Sprintf("%05d", int64(day))
  26. format := time.Now().Format("") + "%0" + strconv.Itoa(10) + "d"
  27. n := math.Pow10(10)
  28. return prefix + fmt.Sprintf(format, rand.New(rand.NewSource(time.Now().UnixNano())).Int63n(int64(n)))
  29. }
  30. // ByteFmt 格式化显示文件大小
  31. func ByteFmt(size int64) string {
  32. var unitArr = []string{"B", "KB", "MB", "GB", "TB", "EB"}
  33. if size == 0 {
  34. return "unknown"
  35. }
  36. fs := float64(size)
  37. p := int(math.Log(fs) / math.Log(1024))
  38. val := fs / math.Pow(1024, float64(p))
  39. _, frac := math.Modf(val)
  40. if frac > 0 {
  41. return fmt.Sprintf("%.1f%s", math.Floor(val*10)/10, unitArr[p])
  42. } else {
  43. return fmt.Sprintf("%d%s", int(val), unitArr[p])
  44. }
  45. }
  46. // UniqueArray 切片唯一值
  47. func UniqueArray[T comparable](array []T) []T {
  48. result := make([]T, 0, len(array))
  49. temp := map[T]struct{}{}
  50. for _, item := range array {
  51. if _, ok := temp[item]; !ok {
  52. temp[item] = struct{}{}
  53. result = append(result, item)
  54. }
  55. }
  56. return result
  57. }
  58. // InArray 是否在切片
  59. func InArray[T comparable](item T, array []T) bool {
  60. for _, s := range array {
  61. if item == s {
  62. return true
  63. }
  64. }
  65. return false
  66. }
  67. // ChunkArray 分割切片
  68. func ChunkArray[T comparable](array []T, size int) (ss [][]T) {
  69. if size <= 0 || len(array) <= size {
  70. return [][]T{array}
  71. }
  72. mod := len(array) % size
  73. k := len(array) / size
  74. var end int
  75. if mod == 0 {
  76. end = k
  77. } else {
  78. end = k + 1
  79. }
  80. for i := 0; i < end; i++ {
  81. if i != k {
  82. ss = append(ss, array[i*size:(i+1)*size])
  83. } else {
  84. ss = append(ss, array[i*size:])
  85. }
  86. }
  87. return
  88. }
  89. // IsChinese 判断字符串是否包含中文
  90. func IsChinese(str string) bool {
  91. var count int
  92. for _, v := range str {
  93. if unicode.Is(unicode.Han, v) {
  94. count++
  95. break
  96. }
  97. }
  98. return count > 0
  99. }
  100. // RandInt 随机数
  101. func RandInt(max int) int {
  102. if max <= 0 {
  103. return 0
  104. }
  105. return rand.Intn(max)
  106. }
  107. // FindIPv4 字符串中查找IP4
  108. func FindIPv4(input string) string {
  109. partIp := "(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])"
  110. must := partIp + "\\." + partIp + "\\." + partIp + "\\." + partIp
  111. matchMe := regexp.MustCompile(must)
  112. return matchMe.FindString(input)
  113. }
  114. // RandString 随机字符串
  115. func RandString(n int) string {
  116. var letterRunes = []rune("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
  117. b := make([]rune, n)
  118. for i := range b {
  119. b[i] = letterRunes[RandInt(len(letterRunes))]
  120. }
  121. return string(b)
  122. }
  123. // IP4ToInt ip4地址转Int类型
  124. func IP4ToInt(ip net.IP) int {
  125. ipSplit := strings.Split(ip.To4().String(), ".")
  126. var ipInt = 0
  127. var pos uint = 24
  128. for _, ipSeg := range ipSplit {
  129. tempInt, _ := strconv.Atoi(ipSeg)
  130. tempInt = tempInt << pos
  131. ipInt = ipInt | tempInt
  132. pos -= 8
  133. }
  134. return ipInt
  135. }
  136. // IP4IntToString 反序列化Int为IP4地址
  137. func IP4IntToString(ipInt int) string {
  138. ipSplit := make([]string, 4)
  139. var length = len(ipSplit)
  140. buffer := bytes.NewBufferString("")
  141. for i := 0; i < length; i++ {
  142. tempInt := ipInt & 0xFF
  143. ipSplit[length-i-1] = strconv.Itoa(tempInt)
  144. ipInt = ipInt >> 8
  145. }
  146. for i := 0; i < length; i++ {
  147. buffer.WriteString(ipSplit[i])
  148. if i < length-1 {
  149. buffer.WriteString(".")
  150. }
  151. }
  152. return buffer.String()
  153. }
  154. // CompareVersion 比较`x.y.z`类型版本号大小
  155. func CompareVersion(verCurrent, verUpdate string) int {
  156. versionA := strings.Split(verCurrent, ".")
  157. versionB := strings.Split(verUpdate, ".")
  158. for i := len(versionA); i < 4; i++ {
  159. versionA = append(versionA, "0")
  160. }
  161. for i := len(versionB); i < 4; i++ {
  162. versionB = append(versionB, "0")
  163. }
  164. for i := 0; i < 4; i++ {
  165. version1, _ := strconv.Atoi(versionA[i])
  166. version2, _ := strconv.Atoi(versionB[i])
  167. if version1 == version2 {
  168. continue
  169. } else if version1 > version2 {
  170. return 1
  171. } else {
  172. return -1
  173. }
  174. }
  175. return 0
  176. }
  177. // GetLocalIP 获取内网本机ipv4地址
  178. func GetLocalIP() (string, error) {
  179. var address []net.Addr
  180. eth0Interface, err := net.InterfaceByName("eth0")
  181. if err == nil {
  182. address, err = eth0Interface.Addrs()
  183. } else {
  184. address, err = net.InterfaceAddrs()
  185. }
  186. if err != nil {
  187. return "", err
  188. }
  189. for _, addr := range address {
  190. if it, ok := addr.(*net.IPNet); ok && !it.IP.IsLoopback() {
  191. if it.IP.To4() != nil {
  192. return it.IP.String(), nil
  193. }
  194. }
  195. }
  196. return "", err
  197. }
  198. // GetInternetIP 获取外网本机ipv4地址
  199. func GetInternetIP() (string, error) {
  200. resp, err := http.Get("https://api.ipify.org")
  201. if err != nil {
  202. resp, err = http.Get("https://icanhazip.com")
  203. if err != nil {
  204. return "", err
  205. }
  206. }
  207. defer func() {
  208. _ = resp.Body.Close()
  209. }()
  210. s, readErr := io.ReadAll(resp.Body)
  211. if readErr != nil {
  212. return "", readErr
  213. }
  214. return string(s), nil
  215. }
  216. // IsExists 检测指定路径文件或者文件夹是否存在
  217. func IsExists(path string) bool {
  218. _, err := os.Stat(path) //os.Stat获取文件信息
  219. if err != nil {
  220. if os.IsExist(err) {
  221. return true
  222. }
  223. return false
  224. }
  225. return true
  226. }
  227. // CheckWebsiteAddr 检查域名是否正确
  228. func CheckWebsiteAddr(addr string) error {
  229. if 0 == len(addr) {
  230. return errors.New("url is empty")
  231. }
  232. parse, err := url.Parse(addr)
  233. if err != nil {
  234. return err
  235. }
  236. if "http" != parse.Scheme && "https" != parse.Scheme {
  237. return errors.New("url Scheme illegal. err:" + parse.Scheme)
  238. }
  239. 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])$`)
  240. u := strings.Split(parse.Host, ":")
  241. if 2 == len(u) && 0 != len(u[1]) {
  242. result := re.FindAllStringSubmatch(u[0], -1)
  243. if result == nil {
  244. return errors.New("url illegal. err:" + u[0])
  245. }
  246. return nil
  247. }
  248. result := re.FindAllStringSubmatch(parse.Host, -1)
  249. if result == nil {
  250. return errors.New("url illegal. err:" + parse.Host)
  251. }
  252. return nil
  253. }
  254. // LoadKeyValConfig 获取本地`a=c\n`类型文件的键值对
  255. func LoadKeyValConfig(text string) map[string]string {
  256. config := make(map[string]string)
  257. r := bufio.NewReader(bytes.NewBufferString(text))
  258. for {
  259. b, _, err := r.ReadLine()
  260. if err != nil {
  261. if err == io.EOF {
  262. break
  263. }
  264. panic(err)
  265. }
  266. s := strings.TrimSpace(string(b))
  267. index := strings.Index(s, "=")
  268. if index < 0 || strings.HasPrefix(s, "#") {
  269. continue
  270. }
  271. key := strings.TrimSpace(s[:index])
  272. if len(key) == 0 {
  273. continue
  274. }
  275. value := strings.TrimSpace(s[index+1:])
  276. if len(value) == 0 {
  277. continue
  278. }
  279. config[key] = value
  280. }
  281. return config
  282. }
  283. // PortIsUse 判断端口是否占用
  284. func PortIsUse(port int) bool {
  285. _, tcpError := net.DialTimeout("tcp", fmt.Sprintf(":%d", port), time.Millisecond*50)
  286. udpAddr, _ := net.ResolveUDPAddr("udp4", fmt.Sprintf(":%d", port))
  287. udpConn, udpError := net.ListenUDP("udp", udpAddr)
  288. if udpConn != nil {
  289. defer func() {
  290. _ = udpConn.Close()
  291. }()
  292. }
  293. return tcpError == nil || udpError != nil
  294. }