forked from mygomod/muses
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wechat.go
executable file
·86 lines (76 loc) · 1.82 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
package wechat
import (
"github.com/BurntSushi/toml"
"github.com/go-resty/resty/v2"
"github.com/pinke/muses/pkg/common"
"github.com/pinke/muses/pkg/wechat/cache"
"github.com/pinke/muses/pkg/wechat/context"
"github.com/pinke/muses/pkg/wechat/miniprogram"
"sync"
)
var defaultCaller = &callerStore{
Name: common.ModWechatName,
}
type callerStore struct {
Name string
cfg Cfg
caller *Client
}
type Client struct {
RestyClient *resty.Client
Cache cache.Cache
Context *context.Context
}
func Register() common.Caller {
return defaultCaller
}
func Caller() *Client {
return defaultCaller.caller
}
func (c *callerStore) InitCfg(cfg []byte) error {
if err := toml.Unmarshal(cfg, &c.cfg); err != nil {
return err
}
return nil
}
func (c *callerStore) InitCaller() error {
db, err := provider(c.cfg.Muses.Wechat)
if err != nil {
return err
}
defaultCaller.caller = db
return nil
}
func provider(cfg CallerCfg) (resp *Client, err error) {
resp = &Client{}
if cfg.Mode == "redis" {
resp.Cache = cache.NewRedis(&cache.RedisOpts{
Debug: cfg.Debug,
Addr: cfg.Addr,
Password: cfg.Password,
Database: cfg.Database,
MaxIdle: cfg.MaxIdle,
MaxActive: cfg.MaxActive,
})
} else {
panic("mode not support")
}
ctx := new(context.Context)
ctx.AppID = cfg.AppID
ctx.AppSecret = cfg.AppSecret
ctx.Token = cfg.Token
ctx.EncodingAESKey = cfg.EncodingAESKey
ctx.PayMchID = cfg.PayMchID
ctx.PayKey = cfg.PayKey
ctx.PayNotifyURL = cfg.PayNotifyURL
ctx.Cache = resp.Cache
ctx.SetAccessTokenLock(new(sync.RWMutex))
ctx.SetJsAPITicketLock(new(sync.RWMutex))
ctx.RestyClient = resty.New().SetDebug(cfg.Debug)
resp.Context = ctx
return
}
// GetMiniProgram 获取小程序的实例
func (wc *Client) GetMiniProgram() *miniprogram.MiniProgram {
return miniprogram.NewMiniProgram(wc.Context)
}