wechat.go 2.3 KB

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