/
game.go
212 lines (188 loc) · 4.78 KB
/
game.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
210
211
212
package game
import (
"time"
"github.com/strugglerx/wechat/game/module"
"github.com/strugglerx/wechat/utils"
"github.com/tidwall/gjson"
)
/**
* @PROJECT_NAME wechat
* @author Moqi
* @date 2021-06-10 18:41
* @Email:str@li.cm
**/
type App struct {
Appid string
Secret string
Token *utils.Token
Hook utils.Hook
}
//New 新建wechat Hook 处理accesstoken的逻辑
func New(appid, secret string, Hook ...utils.Hook) *App {
app := &App{
Appid: appid,
Secret: secret,
}
if len(Hook) != 0 {
app.Hook = Hook[0]
}
app.init()
return app
}
func (a *App) GetConfig() utils.Config {
return utils.Config{
Appid: a.Appid,
Secret: a.Secret,
}
}
func (a *App) init() {
if a.Token == nil {
a.Token = &utils.Token{
Token: "",
UpdateTime: 0,
}
}
response, _ := utils.Get("/cgi-bin/token", utils.Query{
"appid": a.Appid,
"secret": a.Secret,
"grant_type": "client_credential",
})
a.Token.Token = gjson.Get(string(response), "access_token").String()
if a.Token.Token == "" {
panic("WechatGame Package [" + a.Appid + "] : \n" + string(response))
}
// Hook Logic
if a.Hook != nil {
a.Hook(a.Appid, a.Token.Token)
}
a.Token.UpdateTime = int(time.Now().Unix())
}
//GetAccessTokenSafety 获取公开的accessToken
func (a *App) GetAccessTokenSafety(reflush bool) string {
return a.GetAccessToken(reflush).Token
}
//GetAccessToken 获取accessToken 不建议暴露使用
func (a *App) GetAccessToken(reflush ...bool) *utils.Token {
//hook
if a.Hook != nil {
return a.GetAccessTokenWithHook(reflush...)
}
if a.Token == nil {
a.Token = &utils.Token{
Token: "",
UpdateTime: 0,
}
}
nowTime := int(time.Now().Unix())
doReflush := false
if len(reflush) > 0 {
doReflush = reflush[0]
}
if nowTime-a.Token.UpdateTime >= 7000 || doReflush {
params := utils.Query{
"appid": a.Appid,
"secret": a.Secret,
"grant_type": "client_credential",
}
response, _ := utils.Get("/cgi-bin/token", params)
a.Token.Token = gjson.Get(string(response), "access_token").String()
a.Token.UpdateTime = nowTime
return a.Token
} else {
return a.Token
}
}
//GetAccessTokenWithHook 获取accessToken 不建议暴露使用
func (a *App) GetAccessTokenWithHook(reflush ...bool) *utils.Token {
token := a.Hook()
if token == nil {
token = &utils.Token{
Token: "",
UpdateTime: 0,
}
}
nowTime := int(time.Now().Unix())
doReflush := false
if len(reflush) > 0 {
doReflush = reflush[0]
}
if nowTime-token.UpdateTime >= 7000 || doReflush {
params := utils.Query{
"appid": a.Appid,
"secret": a.Secret,
"grant_type": "client_credential",
}
response, _ := utils.Get("/cgi-bin/token", params)
token.Token = gjson.Get(string(response), "access_token").String()
token.UpdateTime = nowTime
a.Token = a.Hook(a.Appid, token.Token)
return token
} else {
return token
}
}
//Login 用户
func (a *App) Login() *module.Login {
return module.LoginEntity.Init(a)
}
//UrlScheme scheme
func (a *App) UrlScheme() *module.UrlScheme {
return module.UrlSchemeEntity.Init(a)
}
//UrlLink url link
func (a *App) UrlLink() *module.UrlLink {
return module.UrlLinkEntity.Init(a)
}
//Wxacode 小程序码
func (a *App) Wxacode() *module.Wxacode {
return module.WxacodeEntity.Init(a)
}
//UpdatableMessage 动态消息
func (a *App) UpdatableMessage() *module.UpdatableMessage {
return module.UpdatableMessageEntity.Init(a)
}
//SubscribeMessage 订阅消息
func (a *App) SubscribeMessage() *module.SubscribeMessage {
return module.SubscribeMessageEntity.Init(a)
}
//SafetyControlCapability 安全风控
func (a *App) SafetyControlCapability() *module.SafetyControlCapability {
return module.SafetyControlCapabilityEntity.Init(a)
}
//Cloudbase 云开发
func (a *App) Cloudbase() *module.Cloudbase {
return module.CloudbaseEntity.Init(a)
}
//LockStep 帧同步
func (a *App) LockStep() *module.LockStep {
return module.LockStepEntity.Init(a)
}
//DataAnalysis 数据分析
func (a *App) DataAnalysis() *module.DataAnalysis {
return module.DataAnalysisEntity.Init(a)
}
//Storage 开放数据
func (a *App) Storage() *module.Storage {
return module.StorageEntity.Init(a)
}
//Gamematch 对局匹配
func (a *App) Gamematch() *module.Gamematch {
return module.GamematchEntity.Init(a)
}
func (a *App) Get(path string, params utils.Query, withAccessToken ...bool) ([]byte, error) {
if len(withAccessToken) > 0 {
response, err := utils.Get(path, params, utils.ContextApp(a))
return response, err
}
response, err := utils.Get(path, params)
return response, err
}
//Post
func (a *App) PostBody(path string, body []byte, withAccessToken ...bool) ([]byte, error) {
if len(withAccessToken) > 0 {
response, err := utils.PostBody(path, body, utils.ContextApp(a))
return response, err
}
response, err := utils.PostBody(path, body)
return response, err
}