tools.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. package models
  2. import (
  3. "bytes"
  4. "crypto/md5"
  5. "encoding/hex"
  6. "encoding/json"
  7. "fmt"
  8. "io"
  9. "io/ioutil"
  10. "math/rand"
  11. "net/http"
  12. "strconv"
  13. "strings"
  14. "time"
  15. )
  16. // 10位时间戳时间格式化
  17. func UnixToDate(timestamp int) string {
  18. t := time.Unix(int64(timestamp), 0)
  19. return t.Format("2006-01-02 15:04:05")
  20. }
  21. // 2006-01-02 15:04:05转换成10位时间戳
  22. func DateToUnix(str string) int64 {
  23. template := "2006-01-02 15:04:05"
  24. t, err := time.ParseInLocation(template, str, time.Local)
  25. if err != nil {
  26. fmt.Println(err)
  27. return 0
  28. }
  29. return t.Unix()
  30. }
  31. // 10位时间戳
  32. func GetUnixTimestamp() int64 {
  33. return time.Now().Unix()
  34. }
  35. // 13位时间戳
  36. func GetUnixNano() int64 {
  37. return time.Now().UnixNano() / 1e6
  38. }
  39. // 2006-01-02 15:04:05
  40. func GetDate() string {
  41. template := "2006-01-02 15:04:05"
  42. return time.Now().Format(template)
  43. }
  44. // 一分钟后
  45. func AfterOne() string {
  46. now := time.Now()
  47. t, _ := time.ParseDuration("1m")
  48. t1 := now.Add(t).Format("20060102150405")
  49. return t1
  50. }
  51. // Md5加密
  52. func Md5(str string) string {
  53. m := md5.New()
  54. m.Write([]byte(str))
  55. return string(hex.EncodeToString(m.Sum(nil)))
  56. }
  57. // 邀请码生成
  58. const (
  59. BASE = "E8S2DZX9WYLTN6BQF7CP5IK3MJUAR4HV"
  60. DECIMAL = 32
  61. PAD = "A"
  62. LEN = 8
  63. )
  64. func Encode(uid uint64) string {
  65. id := uid
  66. mod := uint64(0)
  67. res := ""
  68. for id != 0 {
  69. mod = id % DECIMAL
  70. id = id / DECIMAL
  71. res += string(BASE[mod])
  72. }
  73. resLen := len(res)
  74. if resLen < LEN {
  75. res += PAD
  76. for i := 0; i < LEN-resLen-1; i++ {
  77. res += string(BASE[(int(uid)+i)%DECIMAL])
  78. }
  79. }
  80. return res
  81. }
  82. // 封装一个生产随机数的方法
  83. func GetRandomNum() string {
  84. var str string
  85. for i := 0; i < 6; i++ {
  86. current := rand.Intn(10) //0-9 "math/rand"
  87. str += strconv.Itoa(current)
  88. }
  89. return str
  90. }
  91. // 发送GET请求
  92. // url:请求地址
  93. // response:请求返回的内容
  94. func Get(url string) (response string) {
  95. client := http.Client{Timeout: 5 * time.Second}
  96. resp, error := client.Get(url)
  97. defer resp.Body.Close()
  98. if error != nil {
  99. panic(error)
  100. }
  101. var buffer [512]byte
  102. result := bytes.NewBuffer(nil)
  103. for {
  104. n, err := resp.Body.Read(buffer[0:])
  105. result.Write(buffer[0:n])
  106. if err != nil && err == io.EOF {
  107. break
  108. } else if err != nil {
  109. panic(err)
  110. }
  111. }
  112. response = result.String()
  113. return
  114. }
  115. // post请求封装
  116. func Post(url string, data interface{}, contentType string) (content string) {
  117. jsonStr, _ := json.Marshal(data)
  118. req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
  119. req.Header.Add("content-type", contentType)
  120. if err != nil {
  121. panic(err)
  122. }
  123. defer req.Body.Close()
  124. client := &http.Client{Timeout: 5 * time.Second}
  125. resp, error := client.Do(req)
  126. if error != nil {
  127. panic(error)
  128. }
  129. defer resp.Body.Close()
  130. result, _ := ioutil.ReadAll(resp.Body)
  131. content = string(result)
  132. return
  133. }
  134. // 生成随机字符串
  135. func GetRandomString(length int) string {
  136. str := "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  137. bytes := []byte(str)
  138. result := []byte{}
  139. r := rand.New(rand.NewSource(time.Now().UnixNano()))
  140. for i := 0; i < length; i++ {
  141. result = append(result, bytes[r.Intn(len(bytes))])
  142. }
  143. return string(result)
  144. }
  145. // 生成随机6位数
  146. func GenValidateCode(width int) string {
  147. numeric := [10]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
  148. r := len(numeric)
  149. rand.Seed(time.Now().UnixNano())
  150. var sb strings.Builder
  151. for i := 0; i < width; i++ {
  152. fmt.Fprintf(&sb, "%d", numeric[rand.Intn(r)])
  153. }
  154. return sb.String()
  155. }
  156. // 去重
  157. func Unique(s []string) []string {
  158. m := make(map[string]struct{}, 0)
  159. newS := make([]string, 0)
  160. for _, i2 := range s {
  161. if _, ok := m[i2]; !ok {
  162. newS = append(newS, i2)
  163. m[i2] = struct{}{}
  164. }
  165. }
  166. return newS
  167. }