context.go 784 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. package context
  2. import (
  3. "net/http"
  4. "sync"
  5. "github.com/silenceper/wechat/cache"
  6. )
  7. //Context struct
  8. type Context struct {
  9. AppID string
  10. AppSecret string
  11. Token string
  12. EncodingAESKey string
  13. Cache cache.Cache
  14. Writer http.ResponseWriter
  15. Request *http.Request
  16. //accessTokenLock 读写锁 同一个AppID一个
  17. accessTokenLock *sync.RWMutex
  18. }
  19. // Query returns the keyed url query value if it exists
  20. func (ctx *Context) Query(key string) string {
  21. value, _ := ctx.GetQuery(key)
  22. return value
  23. }
  24. // GetQuery is like Query(), it returns the keyed url query value
  25. func (ctx *Context) GetQuery(key string) (string, bool) {
  26. req := ctx.Request
  27. if values, ok := req.URL.Query()[key]; ok && len(values) > 0 {
  28. return values[0], true
  29. }
  30. return "", false
  31. }