context.go 1.3 KB

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