context.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. 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. HTTPClient *http.Client
  21. SHTTPClient *http.Client //SSL client
  22. //商户平台APIKey
  23. MchAPIKey string
  24. MchID string
  25. }
  26. // Query returns the keyed url query value if it exists
  27. func (ctx *Context) Query(key string) string {
  28. value, _ := ctx.GetQuery(key)
  29. return value
  30. }
  31. // GetQuery is like Query(), it returns the keyed url query value
  32. func (ctx *Context) GetQuery(key string) (string, bool) {
  33. req := ctx.Request
  34. if values, ok := req.URL.Query()[key]; ok && len(values) > 0 {
  35. return values[0], true
  36. }
  37. return "", false
  38. }
  39. // SetJsAPITicketLock 设置jsAPITicket的lock
  40. func (ctx *Context) SetJsAPITicketLock(lock *sync.RWMutex) {
  41. ctx.jsAPITicketLock = lock
  42. }
  43. // GetJsAPITicketLock 获取jsAPITicket 的lock
  44. func (ctx *Context) GetJsAPITicketLock() *sync.RWMutex {
  45. return ctx.jsAPITicketLock
  46. }