互动
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.

160 lines
3.4 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package utils
  2. import (
  3. "crypto/sha1"
  4. "encoding/base64"
  5. "fmt"
  6. "github.com/skip2/go-qrcode"
  7. "hudongzhuanjia/logger"
  8. "hudongzhuanjia/utils/define"
  9. "io/ioutil"
  10. "math/rand"
  11. "net/http"
  12. "os"
  13. "strconv"
  14. "strings"
  15. "time"
  16. )
  17. func Int(str string) int {
  18. i, _ := strconv.Atoi(str)
  19. return i
  20. }
  21. func Int64(str string) int64 {
  22. i, _ := strconv.ParseInt(str, 10, 64)
  23. return i
  24. }
  25. func Float64(str string) float64 {
  26. f, _ := strconv.ParseFloat(strings.TrimSpace(str), 64)
  27. return f
  28. }
  29. //除法,校验被除数为0时返回接口为0
  30. // return one/two
  31. func Except(one, two float64) float64 {
  32. if two == 0 {
  33. return 0
  34. }
  35. return one / two
  36. }
  37. //自定义小数的位数
  38. func Float64CusDecimal(v float64, decimal int) float64 {
  39. return Float64(fmt.Sprintf("%."+fmt.Sprintf("%d", decimal)+"f", v))
  40. }
  41. func Qrcode2Base64(url string) (string, error) {
  42. qr, err := qrcode.Encode(url, qrcode.Medium, 256)
  43. return fmt.Sprintf("data:image/png;base64,%s", base64.StdEncoding.EncodeToString(qr)), err
  44. }
  45. func GenH5Url(webName string, param map[string]interface{}) string {
  46. path := fmt.Sprintf("%s/%s?", define.H5Host, webName)
  47. for key, value := range param {
  48. if key[len(key)-1] == '=' {
  49. key = key[:len(key)-1]
  50. }
  51. path += fmt.Sprintf("%s=%v&", key, value)
  52. }
  53. lastChar := path[len(path)-1]
  54. if lastChar == '&' || lastChar == '?' {
  55. path = path[:len(path)-1]
  56. }
  57. return path
  58. }
  59. func GenH5Qrcode(WebName string, param map[string]interface{}) (string, error) {
  60. path := GenH5Url(WebName, param)
  61. return Qrcode2Base64(path)
  62. }
  63. func Gif2Base64(url string) (string, error) {
  64. if strings.HasSuffix(url, ".gif") {
  65. resp, err := http.Get(url)
  66. if err != nil {
  67. return "", err
  68. }
  69. bys, err := ioutil.ReadAll(resp.Body)
  70. if err != nil {
  71. return "", err
  72. }
  73. return fmt.Sprintf("data:image/gif;base64,%s", base64.StdEncoding.EncodeToString(bys)), err
  74. }
  75. return url, nil
  76. }
  77. func Sha1(b string) string {
  78. h := sha1.New()
  79. h.Write([]byte(b))
  80. return fmt.Sprintf("%x", h.Sum(nil))
  81. }
  82. func PathExists(path string) (bool, error) {
  83. _, err := os.Stat(path)
  84. if err == nil {
  85. return true, nil
  86. }
  87. if os.IsNotExist(err) {
  88. return false, nil
  89. }
  90. return false, err
  91. }
  92. func CountDownFormat(duration int64) string {
  93. m := duration / 60 // 分钟
  94. s := duration - m*60 // 剩余秒
  95. h := m / 60 // 小时
  96. m = m - h*60 // 剩余分钟
  97. return fmt.Sprintf("%02d:%02d:%02d", h, m, s)
  98. }
  99. func RandomStr(length int) string {
  100. str := "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  101. bytes := []byte(str)
  102. var result []byte
  103. for i := 0; i < length; i++ {
  104. r := rand.New(rand.NewSource(time.Now().UnixNano() + int64(i) + int64(time.Now().Nanosecond())))
  105. result = append(result, bytes[r.Intn(len(bytes))])
  106. }
  107. return string(result)
  108. }
  109. func RandomInt(length int) int {
  110. str := "0123456789"
  111. bytes := []byte(str)
  112. var result []byte
  113. for i := 0; i < length; {
  114. r := rand.New(rand.NewSource(time.Now().UnixNano() + int64(i) + int64(time.Now().Nanosecond())))
  115. b := bytes[r.Intn(len(bytes))]
  116. if i == 0 && b == '0' {
  117. continue
  118. }
  119. result = append(result, b)
  120. i++
  121. }
  122. num, _ := strconv.Atoi(string(result))
  123. return num
  124. }
  125. // 定时任务
  126. func HandleTicker(ticker time.Duration, f func() error) {
  127. defer func() {
  128. if err := recover(); err != nil {
  129. logger.Error("定时任务错误panic原因", err)
  130. }
  131. HandleTicker(ticker, f)
  132. }()
  133. t := time.NewTicker(ticker)
  134. for {
  135. select {
  136. case <-t.C:
  137. err := f()
  138. if err != nil {
  139. logger.Error("定时任务错误原因", err)
  140. }
  141. }
  142. }
  143. }