wx.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package utils
  2. import (
  3. "context"
  4. "github.com/flipped-aurora/gin-vue-admin/server/global"
  5. "github.com/medivhzhan/weapp/token"
  6. "go.uber.org/zap"
  7. "math/rand"
  8. )
  9. func GetToken() string {
  10. tok, err := global.GVA_REDIS.Get(context.Background(), "wxtoken").Result()
  11. if err != nil || tok == "" {
  12. tok, exp, err := token.AccessToken(global.GVA_CONFIG.Wxxcx.Appid, global.GVA_CONFIG.Wxxcx.Secret)
  13. if err != nil || tok == "" {
  14. global.GVA_LOG.Error("获取微信token失败!", zap.Any("err", err))
  15. return ""
  16. } else {
  17. global.GVA_REDIS.SetEX(context.Background(), "wxtoken", tok, exp-1000)
  18. return tok
  19. }
  20. }
  21. return tok
  22. }
  23. var defaultLetters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
  24. // RandomString returns a random string with a fixed length
  25. func RandomString(n int, allowedChars ...[]rune) string {
  26. var letters []rune
  27. if len(allowedChars) == 0 {
  28. letters = defaultLetters
  29. } else {
  30. letters = allowedChars[0]
  31. }
  32. b := make([]rune, n)
  33. for i := range b {
  34. b[i] = letters[rand.Intn(len(letters))]
  35. }
  36. return string(b)
  37. }