-
Notifications
You must be signed in to change notification settings - Fork 3
/
wechat.go
84 lines (74 loc) · 2.24 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
package wx
import (
"sync"
"github.com/silenceper/wechat/v2"
"github.com/silenceper/wechat/v2/miniprogram"
programConfig "github.com/silenceper/wechat/v2/miniprogram/config"
"github.com/silenceper/wechat/v2/officialaccount"
officialConfig "github.com/silenceper/wechat/v2/officialaccount/config"
"github.com/silenceper/wechat/v2/openplatform"
openConfig "github.com/silenceper/wechat/v2/openplatform/config"
)
// Client --
type Client struct {
*wechat.Wechat
miniProgram map[string]*miniprogram.MiniProgram
officialAccount map[string]*officialaccount.OfficialAccount
openPlatform map[string]*openplatform.OpenPlatform
mu sync.RWMutex
}
func NewClient() *Client {
wc := wechat.NewWechat()
newClient := &Client{
miniProgram: make(map[string]*miniprogram.MiniProgram),
officialAccount: make(map[string]*officialaccount.OfficialAccount),
openPlatform: make(map[string]*openplatform.OpenPlatform),
}
newClient.Wechat = wc
return newClient
}
// SetMiniProgram 实例化
func (c *Client) SetMiniProgram(group string, cfg *programConfig.Config) *Client {
clientMiniProgram := c.Wechat.GetMiniProgram(cfg)
c.mu.Lock()
c.miniProgram[group] = clientMiniProgram
c.mu.Unlock()
return c
}
// SetOfficialAccount 实例化
func (c *Client) SetOfficialAccount(group string, cfg *officialConfig.Config) *Client {
clientOfficialAccount := c.Wechat.GetOfficialAccount(cfg)
c.mu.Lock()
c.officialAccount[group] = clientOfficialAccount
c.mu.Unlock()
return c
}
// SetOpenPlatform 实例化
func (c *Client) SetOpenPlatform(group string, cfg *openConfig.Config) *Client {
clientOpenPlatform := c.Wechat.GetOpenPlatform(cfg)
c.mu.Lock()
c.openPlatform[group] = clientOpenPlatform
c.mu.Unlock()
return c
}
// MiniProgram 获取实例
func (c *Client) MiniProgram(group string) *miniprogram.MiniProgram {
c.mu.RLock()
res := c.miniProgram[group]
c.mu.RUnlock()
return res
}
// OfficialAccount 获取实例
func (c *Client) OfficialAccount(group string) *officialaccount.OfficialAccount {
c.mu.RLock()
res := c.officialAccount[group]
c.mu.RUnlock()
return res
}
// OfficialAccount 获取实例
func (c *Client) OpenPlatform(group string) *openplatform.OpenPlatform {
c.mu.RLock()
res := c.openPlatform[group]
c.mu.RUnlock()
return res
}