wechat.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. "github.com/silenceper/wechat/v2/work"
  14. workConfig "github.com/silenceper/wechat/v2/work/config"
  15. log "github.com/sirupsen/logrus"
  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(cahce cache.Cache) {
  36. wc.cache = cahce
  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. return openplatform.NewOpenPlatform(cfg)
  59. }
  60. // GetWork 获取企业微信的实例
  61. func (wc *Wechat) GetWork(cfg *workConfig.Config) *work.Work {
  62. return work.NewWork(cfg)
  63. }