wechat.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. //Package gowechat 一个简单易用的wechat封装.
  2. package gowechat
  3. import (
  4. "fmt"
  5. "net/http"
  6. "sync"
  7. "github.com/astaxie/beego/cache"
  8. "github.com/yaotian/gowechat/util"
  9. "github.com/yaotian/gowechat/wxcontext"
  10. )
  11. //MemCache if wxcontext.Config no cache, this will give a default memory cache.
  12. var MemCache cache.Cache
  13. // Wechat struct
  14. type Wechat struct {
  15. Context *wxcontext.Context
  16. }
  17. // NewWechat init
  18. func NewWechat(cfg wxcontext.Config) *Wechat {
  19. context := new(wxcontext.Context)
  20. initContext(&cfg, context)
  21. return &Wechat{context}
  22. }
  23. func initContext(cfg *wxcontext.Config, context *wxcontext.Context) {
  24. if cfg.Cache == nil {
  25. if MemCache == nil {
  26. MemCache, _ = cache.NewCache("memory", `{"interval":60}`)
  27. }
  28. cfg.Cache = MemCache
  29. }
  30. context.Config = cfg
  31. context.SetAccessTokenLock(new(sync.RWMutex))
  32. context.SetJsAPITicketLock(new(sync.RWMutex))
  33. //create http client
  34. if cfg.SslCertFilePath != "" && cfg.SslKeyFilePath != "" {
  35. if client, err := util.NewTLSHttpClient(cfg.SslCertFilePath, cfg.SslKeyFilePath); err == nil {
  36. context.SHTTPClient = client
  37. } else {
  38. fmt.Print(err)
  39. }
  40. }
  41. if cfg.SslCertContent != "" && cfg.SslKeyContent != "" {
  42. if client, err := util.NewTLSHttpClientFromContent(cfg.SslCertContent, cfg.SslKeyContent); err == nil {
  43. context.SHTTPClient = client
  44. } else {
  45. fmt.Print(err)
  46. }
  47. }
  48. context.HTTPClient = http.DefaultClient
  49. }
  50. //MchMgr 商户平台
  51. func (wc *Wechat) MchMgr() (mch *MchMgr, err error) {
  52. err = wc.checkCfgMch()
  53. if err != nil {
  54. return
  55. }
  56. mch = new(MchMgr)
  57. mch.Wechat = wc
  58. return
  59. }
  60. //MpMgr 公众平台
  61. func (wc *Wechat) MpMgr() (mp *MpMgr, err error) {
  62. err = wc.checkCfgBase()
  63. if err != nil {
  64. return
  65. }
  66. mp = new(MpMgr)
  67. mp.Wechat = wc
  68. return
  69. }
  70. //checkCfgBase 检查配置基本信息
  71. func (wc *Wechat) checkCfgBase() (err error) {
  72. if wc.Context.AppID == "" {
  73. return fmt.Errorf("%s", "配置中没有AppID")
  74. }
  75. if wc.Context.AppSecret == "" {
  76. return fmt.Errorf("%s", "配置中没有AppSecret")
  77. }
  78. if wc.Context.Token == "" {
  79. return fmt.Errorf("%s", "配置中没有Token")
  80. }
  81. return
  82. }
  83. func (wc *Wechat) checkCfgMch() (err error) {
  84. err = wc.checkCfgBase()
  85. if err != nil {
  86. return
  87. }
  88. if wc.Context.MchID == "" {
  89. return fmt.Errorf("%s", "配置中没有MchID")
  90. }
  91. if wc.Context.MchAPIKey == "" {
  92. return fmt.Errorf("%s", "配置中没有MchAPIKey")
  93. }
  94. if wc.Context.SslCertFilePath == "" && wc.Context.SslCertContent == "" {
  95. return fmt.Errorf("%s", "配置中没有SslCert")
  96. }
  97. if wc.Context.SslKeyFilePath == "" && wc.Context.SslKeyContent == "" {
  98. return fmt.Errorf("%s", "配置中没有SslKey")
  99. }
  100. return
  101. }