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