wechat.go 2.5 KB

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