wechat.go 2.6 KB

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