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

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