wechat.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package wechat
  2. import (
  3. "os"
  4. "github.com/silenceper/wechat/v2/cache"
  5. "github.com/silenceper/wechat/v2/miniprogram"
  6. miniConfig "github.com/silenceper/wechat/v2/miniprogram/config"
  7. "github.com/silenceper/wechat/v2/officialaccount"
  8. offConfig "github.com/silenceper/wechat/v2/officialaccount/config"
  9. "github.com/silenceper/wechat/v2/openplatform"
  10. openConfig "github.com/silenceper/wechat/v2/openplatform/config"
  11. "github.com/silenceper/wechat/v2/pay"
  12. payConfig "github.com/silenceper/wechat/v2/pay/config"
  13. log "github.com/sirupsen/logrus"
  14. )
  15. func init() {
  16. // Log as JSON instead of the default ASCII formatter.
  17. log.SetFormatter(&log.TextFormatter{})
  18. // Output to stdout instead of the default stderr
  19. // Can be any io.Writer, see below for File example
  20. log.SetOutput(os.Stdout)
  21. // Only log the warning severity or above.
  22. log.SetLevel(log.DebugLevel)
  23. }
  24. // Wechat struct
  25. type Wechat struct {
  26. cache cache.Cache
  27. }
  28. // NewWechat init
  29. func NewWechat() *Wechat {
  30. return &Wechat{}
  31. }
  32. //SetCache 设置cache
  33. func (wc *Wechat) SetCache(cahce cache.Cache) {
  34. wc.cache = cahce
  35. }
  36. //GetOfficialAccount 获取微信公众号实例
  37. func (wc *Wechat) GetOfficialAccount(cfg *offConfig.Config) *officialaccount.OfficialAccount {
  38. if cfg.Cache == nil {
  39. cfg.Cache = wc.cache
  40. }
  41. return officialaccount.NewOfficialAccount(cfg)
  42. }
  43. // GetMiniProgram 获取小程序的实例
  44. func (wc *Wechat) GetMiniProgram(cfg *miniConfig.Config) *miniprogram.MiniProgram {
  45. if cfg.Cache == nil {
  46. cfg.Cache = wc.cache
  47. }
  48. return miniprogram.NewMiniProgram(cfg)
  49. }
  50. // GetPay 获取微信支付的实例
  51. func (wc *Wechat) GetPay(cfg *payConfig.Config) *pay.Pay {
  52. return pay.NewPay(cfg)
  53. }
  54. // GetOpenPlatform 获取微信开放平台的实例
  55. func (wc *Wechat) GetOpenPlatform(cfg *openConfig.Config) *openplatform.OpenPlatform {
  56. return openplatform.NewOpenPlatform(cfg)
  57. }