context.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. //jsapiTicket 读写锁 同一个AppID一个
  19. jsApiTicketLock *sync.RWMutex
  20. }
  21. // Query returns the keyed url query value if it exists
  22. func (ctx *Context) Query(key string) string {
  23. value, _ := ctx.GetQuery(key)
  24. return value
  25. }
  26. // GetQuery is like Query(), it returns the keyed url query value
  27. func (ctx *Context) GetQuery(key string) (string, bool) {
  28. req := ctx.Request
  29. if values, ok := req.URL.Query()[key]; ok && len(values) > 0 {
  30. return values[0], true
  31. }
  32. return "", false
  33. }
  34. //SetJsApiTicket 设置jsApiTicket的lock
  35. func (ctx *Context) SetJsApiTicketLock(lock *sync.RWMutex) {
  36. ctx.jsApiTicketLock = lock
  37. }
  38. func (ctx *Context) GetJsApiTicketLock() *sync.RWMutex {
  39. return ctx.jsApiTicketLock
  40. }