wxGameData.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package v1
  2. import (
  3. "context"
  4. "designs/global"
  5. "designs/service"
  6. "encoding/json"
  7. "github.com/gin-gonic/gin"
  8. "github.com/pkg/errors"
  9. "net/url"
  10. "time"
  11. )
  12. // 获取微信的accessCode
  13. func GetAccessToken(gid string, ttAppid string, ttSecret string) (string, error) {
  14. tokenKey := gid + "||" + "wxToken"
  15. AccessToken := global.App.Redis.Get(context.Background(), tokenKey).Val()
  16. if AccessToken == "" {
  17. params := url.Values{
  18. "appid": {ttAppid},
  19. "secret": {ttSecret},
  20. "grant_type": {"client_credential"},
  21. }
  22. //请求接口获取token
  23. content, err := service.CurlGet("https://api.weixin.qq.com/cgi-bin/token", params, nil)
  24. if err != nil {
  25. return "", err
  26. }
  27. var resp struct {
  28. AccessToken string `json:"access_token"`
  29. ExpiresIn int `json:"expires_in"`
  30. ExpiresAt int `json:"expiresAt"`
  31. Errcode int `json:"errcode"`
  32. Errmsg string `json:"errmsg"`
  33. }
  34. str2oErr := json.Unmarshal([]byte(content), &resp)
  35. if str2oErr != nil {
  36. return "", err
  37. }
  38. if resp.Errcode != 0 {
  39. return "", errors.New(resp.Errmsg)
  40. }
  41. AccessToken = resp.AccessToken
  42. global.App.Redis.Set(context.Background(), tokenKey, AccessToken, time.Second*30)
  43. }
  44. return AccessToken, nil
  45. }
  46. // 获取微信的当日数据
  47. func GetDaily(c *gin.Context) {
  48. //form := request.Check(c, &struct {
  49. //
  50. //}{})
  51. }