wechat.go 2.1 KB

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