context.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package wxcontext
  2. import (
  3. "net/http"
  4. "sync"
  5. )
  6. // Context struct
  7. type Context struct {
  8. *Config
  9. Writer http.ResponseWriter
  10. Request *http.Request
  11. //accessTokenLock 读写锁 同一个AppID一个
  12. accessTokenLock *sync.RWMutex
  13. //jsAPITicket 读写锁 同一个AppID一个
  14. jsAPITicketLock *sync.RWMutex
  15. HTTPClient *http.Client
  16. SHTTPClient *http.Client //SSL client
  17. }
  18. // Query returns the keyed url query value if it exists
  19. func (ctx *Context) Query(key string) string {
  20. value, _ := ctx.GetQuery(key)
  21. return value
  22. }
  23. // GetQuery is like Query(), it returns the keyed url query value
  24. func (ctx *Context) GetQuery(key string) (string, bool) {
  25. req := ctx.Request
  26. if values, ok := req.URL.Query()[key]; ok && len(values) > 0 {
  27. return values[0], true
  28. }
  29. return "", false
  30. }
  31. // SetJsAPITicketLock 设置jsAPITicket的lock
  32. func (ctx *Context) SetJsAPITicketLock(lock *sync.RWMutex) {
  33. ctx.jsAPITicketLock = lock
  34. }
  35. // GetJsAPITicketLock 获取jsAPITicket 的lock
  36. func (ctx *Context) GetJsAPITicketLock() *sync.RWMutex {
  37. return ctx.jsAPITicketLock
  38. }