-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.go
277 lines (246 loc) · 5.67 KB
/
server.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"strings"
"time"
"github.com/patrickmn/go-cache"
"github.com/dongweiming/geek-pu/errors"
. "github.com/dongweiming/geek-pu/models"
"github.com/gin-gonic/gin"
)
const (
APPID string = "wxf0855fcd9689fd67"
SECRET string = "c9389e5cb38cb2e7e4e4c939367d9479"
ACCESSTOKEN string = "access-token"
)
var (
areaMap = map[string]string{
"au": "澳版",
"us": "美版",
}
c = cache.New(60*time.Minute, 10*time.Minute)
)
func HttpRequest(url string) (body []byte, err error) {
resp, err := http.Get(url)
defer resp.Body.Close()
if err != nil {
return body, err
}
body, err = ioutil.ReadAll(resp.Body)
return body, err
}
func WeChatLogin(code string) ([]byte, error) {
base_url := fmt.Sprintf("https://api.weixin.qq.com/sns/jscode2session?appid=%v"+
"&secret=%v&js_code=%v&grant_type=authorization_code",
APPID, SECRET, code)
body, err := HttpRequest(base_url)
return body, err
}
func GetAccessToken() (string, error) {
if x, found := c.Get(ACCESSTOKEN); found {
return x.(string), nil
}
base_url := fmt.Sprintf("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s",
APPID, SECRET)
body, err := HttpRequest(base_url)
var resp GetAccessTokenResp
err = json.Unmarshal(body, &resp)
if err != nil {
return "", err
}
if resp.Errcode != 0 {
return "", GeekError{resp.Errmsg}
}
c.Set(ACCESSTOKEN, resp.AccessToken, 60*time.Minute)
return resp.AccessToken, nil
}
func SubscribeMessage(openid string) {
token, err := GetAccessToken()
if err != nil {
return
}
url := fmt.Sprintf("https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=%s", token)
values := &SubscribeReq{
AccessToken: token,
Touser: openid,
TemplateId: 624,
Data: SubscribeData{
Thing2: WxValue{Value: "测试"},
Phrase5: WxValue{Value: "成功"},
},
}
jsonValue, _ := json.Marshal(values)
resp, err := http.Post(url, "application/json", bytes.NewBuffer(jsonValue))
defer resp.Body.Close()
}
func ResponseJsonError(c *gin.Context, code int) {
msg := errors.GetMsg(code)
c.JSON(code, gin.H{"message": msg})
}
func PostCode2Session(c *gin.Context, code string) string {
var resp WeChatAuthResp
res, err := WeChatLogin(code)
if err != nil {
ResponseJsonError(c, errors.INVALID_PARAMS)
return ""
}
err = json.Unmarshal(res, &resp)
if err != nil {
ResponseJsonError(c, errors.INVALID_PARAMS)
return ""
}
if resp.ErrCode != 0 {
ResponseJsonError(c, errors.INVALID_PARAMS)
return ""
}
return resp.OpenId
}
func Subscribe(c *gin.Context) {
gid, err := strconv.Atoi(c.Param("id"))
if err != nil {
c.JSON(200, gin.H{
"ok": 0,
})
return
}
openId := c.Request.Header.Get("x-corran-token")
if openId == "" {
c.JSON(200, gin.H{
"ok": 0,
})
return
}
sub := Subscription{Uid: openId, Gid: gid}
db := GetDB()
defer db.Close()
if db.Create(&sub).Error != nil {
c.JSON(200, gin.H{
"ok": 0,
})
} else {
SubscribeMessage(openId)
c.JSON(200, gin.H{
"ok": 1,
})
}
}
func UnSubscribe(c *gin.Context) {
gid, err := strconv.Atoi(c.Param("id"))
if err != nil {
c.JSON(200, gin.H{
"ok": 0,
})
return
}
openId := c.Request.Header.Get("x-corran-token")
if openId == "" {
c.JSON(200, gin.H{
"ok": 0,
})
return
}
db := GetDB()
defer db.Close()
db.Where("id = ? AND uid = ?", gid, openId).Delete(&Subscription{})
c.JSON(200, gin.H{
"ok": 1,
})
}
func GetGameList(c *gin.Context) {
q := c.Query("q")
filter := c.Query("filter")
openId := c.Request.Header.Get("x-corran-token")
if openId == "" {
c.JSON(400, gin.H{"error": "Wrong request"})
return
}
var games []Game
var subs []Subscription
var sub Subscription
db := GetDB()
defer db.Close()
if q != "" {
db = db.Where("title LIKE ?", fmt.Sprintf("%%%s%%", q))
}
if filter != "" {
if strings.HasPrefix(filter, "zone") {
areas := strings.Split(filter, ":")
if area, ok := areaMap[areas[1]]; ok {
db = db.Where("area = ?", area)
}
} else if strings.HasPrefix(filter, "order") {
orders := strings.Split(filter, ":")
order := orders[1]
if strings.Contains(order, "rating") {
db = db.Order("rating desc")
} else if strings.Contains(order, "price") {
sorts := strings.Split(order, "-")
db = db.Order(fmt.Sprintf("price %s", sorts[1]))
}
} else if strings.HasPrefix(filter, "edition") {
if ids := GetEditionIds(); len(ids) != 0 {
db = db.Where("id IN (?)", ids).Order("release_date")
} else {
c.JSON(200, gin.H{
"games": games,
})
return
}
} else if strings.HasPrefix(filter, "mine:subscription") {
db.Select("gid").Where("uid = ?", openId).Find(&subs)
if len(subs) == 0 {
c.JSON(200, gin.H{
"games": games,
})
return
}
var ids []int
for _, sub = range subs {
ids = append(ids, sub.Gid)
}
db = db.Where(ids)
} else if strings.HasPrefix(filter, "in-stock") {
db = db.Where("quantity > 0")
}
}
db.Find(&games)
if len(subs) == 0 {
db.Select("gid").Where("uid = ?", openId).Find(&subs)
}
if len(subs) != 0 {
var ids []int
for _, sub = range subs {
ids = append(ids, sub.Gid)
}
for index := range games {
game := games[index]
if Contains(ids, game.ID) {
game.Subscribed = true
games[index] = game
}
}
}
c.JSON(200, gin.H{
"games": games,
})
}
func main() {
r := gin.Default()
r.POST("/api/login", func(c *gin.Context) {
var code Code
c.BindJSON(&code)
openId := PostCode2Session(c, code.Data)
c.JSON(200, gin.H{
"openid": openId,
})
})
r.GET("/api/games", GetGameList)
r.POST("/api/game/:id/subscribe", Subscribe)
r.POST("/api/game/:id/unsubscribe", UnSubscribe)
r.Run(":80")
}