wechat.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package wechat
  2. import (
  3. "github.com/silenceper/wechat/cache"
  4. "github.com/silenceper/wechat/miniprogram"
  5. miniConfig "github.com/silenceper/wechat/miniprogram/config"
  6. "github.com/silenceper/wechat/officialaccount"
  7. offConfig "github.com/silenceper/wechat/officialaccount/config"
  8. "github.com/silenceper/wechat/openplatform"
  9. opConfig "github.com/silenceper/wechat/openplatform/config"
  10. "github.com/silenceper/wechat/pay"
  11. payConfig "github.com/silenceper/wechat/pay/config"
  12. )
  13. // Wechat struct
  14. type Wechat struct {
  15. cache cache.Cache
  16. }
  17. // Config for user
  18. type Config struct {
  19. PayMchID string //支付 - 商户 ID
  20. PayNotifyURL string //支付 - 接受微信支付结果通知的接口地址
  21. PayKey string //支付 - 商户后台设置的支付 key
  22. }
  23. // NewWechat init
  24. func NewWechat() *Wechat {
  25. return &Wechat{}
  26. }
  27. //SetCache 设置cache
  28. func (wc *Wechat) SetCache(cahce cache.Cache) {
  29. wc.cache = cahce
  30. }
  31. //GetOfficialAccount 获取微信公众号实例
  32. func (wc *Wechat) GetOfficialAccount(cfg *offConfig.Config) *officialaccount.OfficialAccount {
  33. if cfg.Cache == nil {
  34. cfg.Cache = wc.cache
  35. }
  36. return officialaccount.NewOfficialAccount(cfg)
  37. }
  38. // GetMiniProgram 获取小程序的实例
  39. func (wc *Wechat) GetMiniProgram(cfg *miniConfig.Config) *miniprogram.MiniProgram {
  40. if cfg.Cache == nil {
  41. cfg.Cache = wc.cache
  42. }
  43. return miniprogram.NewMiniProgram(cfg)
  44. }
  45. // GetPay 获取微信支付的实例
  46. func (wc *Wechat) GetPay(cfg *payConfig.Config) *pay.Pay {
  47. return pay.NewPay(cfg)
  48. }
  49. // GetOpenPlatform 获取微信开放平台的实例
  50. func (wc *Wechat) GetOpenPlatform(cfg *opConfig.Config) *openplatform.OpenPlatform {
  51. return openplatform.NewOpenPlatform(cfg)
  52. }