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

121 lines
2.6 KiB

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