/
ma.go
209 lines (175 loc) · 6.32 KB
/
ma.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// 微信小程序接口
package ma
import (
"github.com/cliod/wx-go/common"
"github.com/cliod/wx-go/common/util"
)
type WxMaService interface {
common.WxService
// 获取配置
GetWxMaConfig() WxMaConfig
// 设置配置
SetWxMaConfig(WxMaConfig)
// jsCode换取openid
JsCode2SessionInfo(jsCode string) (*JsCode2SessionResult, error)
// 验证消息的确来自微信服务器
// 详情请见: http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421135319&token=&lang=zh_CN
CheckSignature(timestamp, nonce, signature string) bool
/* 用户支付完成后,获取该用户的UnionId,无需用户授权。本接口支持第三方平台代理查询。
注意:调用前需要用户完成支付,且在支付后的五分钟内有效。
文档地址:https://developers.weixin.qq.com/miniprogram/dev/api/getPaidUnionId.html */
GetPaidUnionId(openid, transactionId, mchId, outTradeNo string) (*WxMaUnionIdResult, error)
// 获取用户接口
GetWxMaUserService() WxMaUserService
// 设置(用户自定义的)用户接口
SetWxMaUserService(WxMaUserService)
// 获取二维码接口
GetWxMaQrcodeService() WxMaQrcodeService
// 获取订阅接口
GetWxMaSubscribeService() WxMaSubscribeService
// 获取分享接口
GetWxMaShareService() WxMaShareService
// 获取消息接口
GetWxMaMessageService() WxMaMsgService
// 获取设置jsapi接口
GetWxMaJsapiService() WxMaJsapiService
// 设置(用户自定义的)二维码接口
SetWxMaQrcodeService(WxMaQrcodeService)
// 设置(用户自定义的)订阅接口
SetWxMaSubscribeService(WxMaSubscribeService)
// 设置(用户自定义的)分享接口
SetWxMaShareService(WxMaShareService)
// 设置(用户自定义的)消息接口
SetWxMaMsgService(WxMaMsgService)
// 设置jsapi接口
SetWxMaJsapiService(WxMaJsapiService)
}
type WxMaServiceImpl struct {
common.WxServiceImpl
config WxMaConfig
userService WxMaUserService
qrCodeService WxMaQrcodeService
subscribeService WxMaSubscribeService
shareService WxMaShareService
msgService WxMaMsgService
liveService WxMaLiveService
jsapiService WxMaJsapiService
}
func newWxMaService(config WxMaConfig) *WxMaServiceImpl {
impl := &WxMaServiceImpl{}
impl.SetHttpService(common.NewService())
impl.SetWxMaConfig(config)
impl.userService = newWxMaUserService(impl)
impl.qrCodeService = newWxMaQrcodeService(impl)
impl.subscribeService = newWxMaSubscribeService(impl)
impl.msgService = newWxMaMsgService(impl)
impl.shareService = newWxMaShareService(impl)
impl.liveService = newWxMaLiveService(impl)
impl.jsapiService = newWxMaJsapiService(impl)
return impl
}
func (s *WxMaServiceImpl) JsCode2SessionInfo(jsCode string) (*JsCode2SessionResult, error) {
var jsr JsCode2SessionResult
err := s.GetFor(&jsr, common.MaSessionInfoUrl, s.config.GetAppID(), s.config.GetSecret(), jsCode)
return &jsr, err
}
func (s *WxMaServiceImpl) CheckSignature(timestamp, nonce, signature string) bool {
return util.CheckSignature(s.GetWxMaConfig().GetToken(), timestamp, nonce, signature)
}
func (s *WxMaServiceImpl) GetPaidUnionId(openid, transactionId, mchId, outTradeNo string) (*WxMaUnionIdResult, error) {
param := map[string]string{}
param["openid"] = openid
if transactionId != "" {
param["transaction_id"] = transactionId
}
if mchId != "" {
param["mch_id"] = mchId
}
if outTradeNo != "" {
param["out_trade_no"] = outTradeNo
}
url := common.MaGetPaidUnionIdUrl
for k, v := range param {
url += "&" + k + "=" + v
}
var res WxMaUnionIdResult
return &res, s.GetFor(&res, url)
}
func (s *WxMaServiceImpl) GetWxMaUserService() WxMaUserService {
return s.userService
}
func (s *WxMaServiceImpl) GetWxMaQrcodeService() WxMaQrcodeService {
return s.qrCodeService
}
func (s *WxMaServiceImpl) GetWxMaSubscribeService() WxMaSubscribeService {
return s.subscribeService
}
func (s *WxMaServiceImpl) GetWxMaShareService() WxMaShareService {
return s.shareService
}
func (s *WxMaServiceImpl) GetWxMaMessageService() WxMaMsgService {
return s.msgService
}
func (s *WxMaServiceImpl) GetWxMaJsapiService() WxMaJsapiService {
return s.jsapiService
}
func (s *WxMaServiceImpl) SetWxMaUserService(userService WxMaUserService) {
s.userService = userService
}
func (s *WxMaServiceImpl) SetWxMaQrcodeService(qrcodeService WxMaQrcodeService) {
s.qrCodeService = qrcodeService
}
func (s *WxMaServiceImpl) SetWxMaSubscribeService(subscribeService WxMaSubscribeService) {
s.subscribeService = subscribeService
}
func (s *WxMaServiceImpl) SetWxMaShareService(shareService WxMaShareService) {
s.shareService = shareService
}
func (s *WxMaServiceImpl) SetWxMaMsgService(msgService WxMaMsgService) {
s.msgService = msgService
}
func (s *WxMaServiceImpl) SetWxMaJsapiService(jsapiService WxMaJsapiService) {
s.jsapiService = jsapiService
}
func (s *WxMaServiceImpl) GetWxMaConfig() WxMaConfig {
return s.config
}
func (s *WxMaServiceImpl) SetWxMaConfig(config WxMaConfig) {
s.SetWxConfig(config)
s.config = config
_, _ = s.ForceGetAccessToken(true)
}
func NewWxMaServiceBy(appId, secret string) WxMaService {
return newWxMaService(newWxMaConfig(appId, secret))
}
func NewWxMaService(config WxMaConfig) WxMaService {
if config == nil {
config = new(WxMaConfigImpl)
}
return newWxMaService(config)
}
// 将方法静态方便使用
// 获取accessToken
func GetAccessToken(appId, secret string) (*common.AccessToken, error) {
return NewWxMaServiceBy(appId, secret).GetAccessToken()
}
// 通过jsCode换取openid和sessionKey
func JsCode2SessionInfo(appId, secret, jsCode string) (*JsCode2SessionResult, error) {
return NewWxMaServiceBy(appId, secret).JsCode2SessionInfo(jsCode)
}
// 创建调用jsapi时所需要的签名.
func CreateJsapiSignatureBy(appId, secret, url string) (*common.WxJsapiSignature, error) {
return NewWxMaServiceBy(appId, secret).GetWxMaJsapiService().CreateJsapiSignature(url)
}
// 获取js_ticket
func GetJsapiTicket(appId, secret string) (*common.Ticket, error) {
return NewWxMaServiceBy(appId, secret).GetWxMaJsapiService().GetJsapiTicket()
}
// 获取ticket
func GetTicket(appId, secret string, t common.TicketType) (*common.Ticket, error) {
return NewWxMaServiceBy(appId, secret).GetWxMaJsapiService().GetTicket(t)
}
// 创建二维码
func CreateWxaCodeUnlimited(appId, secret, scene, page string) ([]byte, error) {
return NewWxMaServiceBy(appId, secret).GetWxMaQrcodeService().CreateWxaCodeUnlimited(scene, page)
}