This repository has been archived by the owner on Mar 8, 2023. It is now read-only.
forked from silenceper/wechat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wechat.go
135 lines (113 loc) · 3.43 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package wechat
import (
"gitee.com/zhimiao/wechat-sdk/device"
"gitee.com/zhimiao/wechat-sdk/message"
"gitee.com/zhimiao/wechat-sdk/open"
"gitee.com/zhimiao/wechat-sdk/tcb"
"net/http"
"sync"
"gitee.com/zhimiao/wechat-sdk/cache"
"gitee.com/zhimiao/wechat-sdk/context"
"gitee.com/zhimiao/wechat-sdk/js"
"gitee.com/zhimiao/wechat-sdk/material"
"gitee.com/zhimiao/wechat-sdk/menu"
"gitee.com/zhimiao/wechat-sdk/miniprogram"
"gitee.com/zhimiao/wechat-sdk/oauth"
"gitee.com/zhimiao/wechat-sdk/pay"
"gitee.com/zhimiao/wechat-sdk/qr"
"gitee.com/zhimiao/wechat-sdk/server"
"gitee.com/zhimiao/wechat-sdk/user"
)
// Wechat struct
type Wechat struct {
Context *context.Context
}
// Config for user
type Config struct {
AppID string // 小程序/平台 appid
AppSecret string // 小程序/平台secret
Token string // 消息校验token
EncodingAESKey string // 消息加解密key
PayMchID string // 支付 - 商户 ID
PayNotifyURL string // 支付 - 接受微信支付结果通知的接口地址
PayKey string // 支付 - 商户后台设置的支付 key
P12 []byte // 支付 - 商户证书文件
Cache cache.Cache
}
// NewWechat init
func NewWechat(cfg *Config) *Wechat {
context := new(context.Context)
copyConfigToContext(cfg, context)
return &Wechat{context}
}
func copyConfigToContext(cfg *Config, context *context.Context) {
context.AppID = cfg.AppID
context.AppSecret = cfg.AppSecret
context.Token = cfg.Token
context.EncodingAESKey = cfg.EncodingAESKey
context.PayMchID = cfg.PayMchID
context.PayKey = cfg.PayKey
context.PayNotifyURL = cfg.PayNotifyURL
context.Cache = cfg.Cache
context.P12 = cfg.P12
context.SetAccessTokenLock(new(sync.RWMutex))
context.SetJsAPITicketLock(new(sync.RWMutex))
}
// GetServer 消息管理
func (wc *Wechat) GetServer(req *http.Request, writer http.ResponseWriter) *server.Server {
wc.Context.Request = req
wc.Context.Writer = writer
return server.NewServer(wc.Context)
}
// GetAccessToken 获取access_token
func (wc *Wechat) GetAccessToken() (string, error) {
return wc.Context.GetAccessToken()
}
// GetOauth oauth2网页授权
func (wc *Wechat) GetOauth() *oauth.Oauth {
return oauth.NewOauth(wc.Context)
}
// GetOpen 微信开放平台
func (wc *Wechat) GetOpen() *open.Open {
return open.NewOpen(wc.Context)
}
// GetMaterial 素材管理
func (wc *Wechat) GetMaterial() *material.Material {
return material.NewMaterial(wc.Context)
}
// GetJs js-sdk配置
func (wc *Wechat) GetJs() *js.Js {
return js.NewJs(wc.Context)
}
// GetMenu 菜单管理接口
func (wc *Wechat) GetMenu() *menu.Menu {
return menu.NewMenu(wc.Context)
}
// GetUser 用户管理接口
func (wc *Wechat) GetUser() *user.User {
return user.NewUser(wc.Context)
}
// GetTemplate 模板消息接口
func (wc *Wechat) GetTemplate() *message.Template {
return message.NewTemplate(wc.Context)
}
// GetPay 返回支付消息的实例
func (wc *Wechat) GetPay() *pay.Pay {
return pay.NewPay(wc.Context)
}
// GetQR 返回二维码的实例
func (wc *Wechat) GetQR() *qr.QR {
return qr.NewQR(wc.Context)
}
// GetMiniProgram 获取小程序的实例
func (wc *Wechat) GetMiniProgram() *miniprogram.MiniProgram {
return miniprogram.NewMiniProgram(wc.Context)
}
// GetDevice 获取智能设备的实例
func (wc *Wechat) GetDevice() *device.Device {
return device.NewDevice(wc.Context)
}
// GetTcb 获取小程序-云开发的实例
func (wc *Wechat) GetTcb() *tcb.Tcb {
return tcb.NewTcb(wc.Context)
}