utils.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. package common
  2. import (
  3. crand "crypto/rand"
  4. "fmt"
  5. "github.com/google/uuid"
  6. "html/template"
  7. "log"
  8. "math/big"
  9. "math/rand"
  10. "net"
  11. "os/exec"
  12. "runtime"
  13. "strconv"
  14. "strings"
  15. "time"
  16. )
  17. func OpenBrowser(url string) {
  18. var err error
  19. switch runtime.GOOS {
  20. case "linux":
  21. err = exec.Command("xdg-open", url).Start()
  22. case "windows":
  23. err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
  24. case "darwin":
  25. err = exec.Command("open", url).Start()
  26. }
  27. if err != nil {
  28. log.Println(err)
  29. }
  30. }
  31. func GetIp() (ip string) {
  32. ips, err := net.InterfaceAddrs()
  33. if err != nil {
  34. log.Println(err)
  35. return ip
  36. }
  37. for _, a := range ips {
  38. if ipNet, ok := a.(*net.IPNet); ok && !ipNet.IP.IsLoopback() {
  39. if ipNet.IP.To4() != nil {
  40. ip = ipNet.IP.String()
  41. if strings.HasPrefix(ip, "10") {
  42. return
  43. }
  44. if strings.HasPrefix(ip, "172") {
  45. return
  46. }
  47. if strings.HasPrefix(ip, "192.168") {
  48. return
  49. }
  50. ip = ""
  51. }
  52. }
  53. }
  54. return
  55. }
  56. var sizeKB = 1024
  57. var sizeMB = sizeKB * 1024
  58. var sizeGB = sizeMB * 1024
  59. func Bytes2Size(num int64) string {
  60. numStr := ""
  61. unit := "B"
  62. if num/int64(sizeGB) > 1 {
  63. numStr = fmt.Sprintf("%.2f", float64(num)/float64(sizeGB))
  64. unit = "GB"
  65. } else if num/int64(sizeMB) > 1 {
  66. numStr = fmt.Sprintf("%d", int(float64(num)/float64(sizeMB)))
  67. unit = "MB"
  68. } else if num/int64(sizeKB) > 1 {
  69. numStr = fmt.Sprintf("%d", int(float64(num)/float64(sizeKB)))
  70. unit = "KB"
  71. } else {
  72. numStr = fmt.Sprintf("%d", num)
  73. }
  74. return numStr + " " + unit
  75. }
  76. func Seconds2Time(num int) (time string) {
  77. if num/31104000 > 0 {
  78. time += strconv.Itoa(num/31104000) + " 年 "
  79. num %= 31104000
  80. }
  81. if num/2592000 > 0 {
  82. time += strconv.Itoa(num/2592000) + " 个月 "
  83. num %= 2592000
  84. }
  85. if num/86400 > 0 {
  86. time += strconv.Itoa(num/86400) + " 天 "
  87. num %= 86400
  88. }
  89. if num/3600 > 0 {
  90. time += strconv.Itoa(num/3600) + " 小时 "
  91. num %= 3600
  92. }
  93. if num/60 > 0 {
  94. time += strconv.Itoa(num/60) + " 分钟 "
  95. num %= 60
  96. }
  97. time += strconv.Itoa(num) + " 秒"
  98. return
  99. }
  100. func Interface2String(inter interface{}) string {
  101. switch inter.(type) {
  102. case string:
  103. return inter.(string)
  104. case int:
  105. return fmt.Sprintf("%d", inter.(int))
  106. case float64:
  107. return fmt.Sprintf("%f", inter.(float64))
  108. }
  109. return "Not Implemented"
  110. }
  111. func UnescapeHTML(x string) interface{} {
  112. return template.HTML(x)
  113. }
  114. func IntMax(a int, b int) int {
  115. if a >= b {
  116. return a
  117. } else {
  118. return b
  119. }
  120. }
  121. func IsIP(s string) bool {
  122. ip := net.ParseIP(s)
  123. return ip != nil
  124. }
  125. func GetUUID() string {
  126. code := uuid.New().String()
  127. code = strings.Replace(code, "-", "", -1)
  128. return code
  129. }
  130. const keyChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  131. func init() {
  132. rand.Seed(time.Now().UnixNano())
  133. }
  134. func GenerateRandomKey(length int) (string, error) {
  135. b := make([]byte, length)
  136. maxI := big.NewInt(int64(len(keyChars)))
  137. for i := range b {
  138. n, err := crand.Int(crand.Reader, maxI)
  139. if err != nil {
  140. return "", err
  141. }
  142. b[i] = keyChars[n.Int64()]
  143. }
  144. return string(b), nil
  145. }
  146. func GenerateKey() (string, error) {
  147. //rand.Seed(time.Now().UnixNano())
  148. return GenerateRandomKey(48)
  149. }
  150. func GetRandomInt(max int) int {
  151. //rand.Seed(time.Now().UnixNano())
  152. return rand.Intn(max)
  153. }
  154. func GetTimestamp() int64 {
  155. return time.Now().Unix()
  156. }
  157. func GetTimeString() string {
  158. now := time.Now()
  159. return fmt.Sprintf("%s%d", now.Format("20060102150405"), now.UnixNano()%1e9)
  160. }
  161. func Max(a int, b int) int {
  162. if a >= b {
  163. return a
  164. } else {
  165. return b
  166. }
  167. }
  168. func MessageWithRequestId(message string, id string) string {
  169. return fmt.Sprintf("%s (request id: %s)", message, id)
  170. }
  171. func RandomSleep() {
  172. // Sleep for 0-3000 ms
  173. time.Sleep(time.Duration(rand.Intn(3000)) * time.Millisecond)
  174. }