context.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. // SetJsAPITicketLock 设置jsAPITicket的lock
  35. func (ctx *Context) SetJsAPITicketLock(lock *sync.RWMutex) {
  36. ctx.jsAPITicketLock = lock
  37. }
  38. // GetJsAPITicketLock 获取jsAPITicket 的lock
  39. func (ctx *Context) GetJsAPITicketLock() *sync.RWMutex {
  40. return ctx.jsAPITicketLock
  41. }