context.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. PayMchID string
  14. PayNotifyURL string
  15. PayKey string
  16. Cache cache.Cache
  17. Writer http.ResponseWriter
  18. Request *http.Request
  19. //accessTokenLock 读写锁 同一个AppID一个
  20. accessTokenLock *sync.RWMutex
  21. //jsAPITicket 读写锁 同一个AppID一个
  22. jsAPITicketLock *sync.RWMutex
  23. }
  24. // Query returns the keyed url query value if it exists
  25. func (ctx *Context) Query(key string) string {
  26. value, _ := ctx.GetQuery(key)
  27. return value
  28. }
  29. // GetQuery is like Query(), it returns the keyed url query value
  30. func (ctx *Context) GetQuery(key string) (string, bool) {
  31. req := ctx.Request
  32. if values, ok := req.URL.Query()[key]; ok && len(values) > 0 {
  33. return values[0], true
  34. }
  35. return "", false
  36. }
  37. // SetJsAPITicketLock 设置jsAPITicket的lock
  38. func (ctx *Context) SetJsAPITicketLock(lock *sync.RWMutex) {
  39. ctx.jsAPITicketLock = lock
  40. }
  41. // GetJsAPITicketLock 获取jsAPITicket 的lock
  42. func (ctx *Context) GetJsAPITicketLock() *sync.RWMutex {
  43. return ctx.jsAPITicketLock
  44. }