util.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package utils
  2. import (
  3. "bytes"
  4. "crypto/sha256"
  5. "encoding/hex"
  6. "fmt"
  7. "github.com/skip2/go-qrcode"
  8. "github.com/segmentio/ksuid"
  9. "math/rand"
  10. "os"
  11. "strconv"
  12. "strings"
  13. "time"
  14. )
  15. type Error struct {
  16. Code int `json:"code"`
  17. Desc string `json:"desc"`
  18. }
  19. /**
  20. 生成一个随机的不重复的用户ID
  21. */
  22. func GenUUID() string {
  23. uuid, _ := ksuid.NewRandomWithTime(time.Now())
  24. return uuid.String()
  25. }
  26. /**
  27. 隐藏手机号码中间4位
  28. */
  29. func KeepSecretForMobile(mobile string) string {
  30. if len(mobile) > 0 {
  31. var sqlBuf bytes.Buffer
  32. sqlBuf.WriteString(mobile[0:3])
  33. sqlBuf.WriteString("****")
  34. sqlBuf.WriteString(mobile[7:11])
  35. return sqlBuf.String()
  36. }
  37. return ""
  38. }
  39. /**
  40. 生成一个随机的不重复的数字
  41. */
  42. func GenUserIdentifier() string {
  43. random := rand.New(rand.NewSource(time.Now().UnixNano()))
  44. return fmt.Sprintf("%08v", random.Int31n(100000000))
  45. }
  46. /**
  47. 产生随机验证码
  48. */
  49. func GenRandomCode() string {
  50. random := rand.New(rand.NewSource(time.Now().UnixNano()))
  51. return fmt.Sprintf("%06v", random.Int31n(1000000))
  52. }
  53. /**
  54. 用户头像处理
  55. */
  56. func CompleteAvatar(avatar, defaultAvatar string) string {
  57. if len(avatar) > 0 && (strings.HasPrefix(avatar, "http://") || strings.HasPrefix(avatar, "https://")) {
  58. return avatar
  59. }
  60. return defaultAvatar
  61. }
  62. /**
  63. 生成二维码
  64. */
  65. func main() {
  66. qrcode.WriteFile("http://www.flysnow.org/",qrcode.Medium,256,"./blog_qrcode.png")
  67. }
  68. // 如果不存在指定的目录, 则直接创建一个
  69. func IsNotExistMkDirAll(path string) error {
  70. _, err := os.Stat(path)
  71. if notExist := os.IsNotExist(err); notExist == true {
  72. err := os.MkdirAll(path, os.ModePerm)
  73. if err != nil {
  74. return err
  75. }
  76. }
  77. return nil
  78. }
  79. /**
  80. 随机文件名, 防止文件名重复
  81. */
  82. func RandImageName(len int) string {
  83. b := make([]byte, len)
  84. rand.Read(b)
  85. return fmt.Sprintf("%x", b) + strconv.FormatInt(time.Now().Unix(), 10)
  86. }
  87. /**
  88. 生成签名Sig
  89. */
  90. func CalculateSigForTempl(appkey string, random string, mobiles []string, time int64) string {
  91. cnt := len(mobiles)
  92. if cnt == 0 {
  93. return ""
  94. }
  95. var buffer bytes.Buffer
  96. buffer.WriteString(mobiles[0])
  97. for i := 1; i < cnt; i++ {
  98. buffer.WriteString(",")
  99. buffer.WriteString(mobiles[i])
  100. }
  101. str := "appkey=" + appkey + "&random=" + random + "&time=" + strconv.FormatInt(time, 10) + "&mobile=" + buffer.String()
  102. hash := sha256.New()
  103. hash.Write([]byte(str))
  104. hashInBytes := hash.Sum(nil)
  105. return hex.EncodeToString(hashInBytes)
  106. }
  107. func Interface2String(inter interface{}) string {
  108. switch inter.(type) {
  109. case string:
  110. return inter.(string)
  111. case int:
  112. return strconv.Itoa(inter.(int))
  113. case int64:
  114. return strconv.FormatInt(inter.(int64), 10)
  115. case float64:
  116. return strconv.FormatFloat(inter.(float64), 'f', -1, 64)
  117. case bool:
  118. return strconv.FormatBool(inter.(bool))
  119. }
  120. return ""
  121. }