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

163 lines
3.6 KiB

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